2015-09-09 16:05:39 +02:00
|
|
|
// Copyright 2015 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/renderer/plugins/plugin_preroller.h"
|
|
|
|
|
|
|
|
#include "libcef/renderer/plugins/cef_plugin_placeholder.h"
|
|
|
|
|
|
|
|
#include "base/base64.h"
|
|
|
|
#include "chrome/grit/generated_resources.h"
|
2016-07-14 03:35:07 +02:00
|
|
|
#include "chrome/grit/renderer_resources.h"
|
2016-01-06 20:20:54 +01:00
|
|
|
#include "chrome/renderer/plugins/power_saver_info.h"
|
2015-09-09 16:05:39 +02:00
|
|
|
#include "third_party/WebKit/public/platform/WebRect.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebElement.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebPlugin.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebPluginContainer.h"
|
|
|
|
#include "ui/gfx/codec/png_codec.h"
|
|
|
|
|
|
|
|
CefPluginPreroller::CefPluginPreroller(
|
|
|
|
content::RenderFrame* render_frame,
|
|
|
|
const blink::WebPluginParams& params,
|
|
|
|
const content::WebPluginInfo& info,
|
|
|
|
const std::string& identifier,
|
|
|
|
const base::string16& name,
|
|
|
|
const base::string16& message,
|
|
|
|
content::PluginInstanceThrottler* throttler)
|
|
|
|
: RenderFrameObserver(render_frame),
|
|
|
|
params_(params),
|
|
|
|
info_(info),
|
|
|
|
identifier_(identifier),
|
|
|
|
name_(name),
|
|
|
|
message_(message),
|
|
|
|
throttler_(throttler) {
|
|
|
|
DCHECK(throttler);
|
|
|
|
throttler_->AddObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefPluginPreroller::~CefPluginPreroller() {
|
|
|
|
if (throttler_)
|
|
|
|
throttler_->RemoveObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPluginPreroller::OnKeyframeExtracted(const SkBitmap* bitmap) {
|
|
|
|
std::vector<unsigned char> png_data;
|
|
|
|
if (!gfx::PNGCodec::EncodeBGRASkBitmap(*bitmap, false, &png_data)) {
|
|
|
|
DLOG(ERROR) << "Provided keyframe could not be encoded as PNG.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::StringPiece png_as_string(reinterpret_cast<char*>(&png_data[0]),
|
|
|
|
png_data.size());
|
|
|
|
|
|
|
|
std::string data_url_header = "data:image/png;base64,";
|
|
|
|
std::string data_url_body;
|
|
|
|
base::Base64Encode(png_as_string, &data_url_body);
|
|
|
|
keyframe_data_url_ = GURL(data_url_header + data_url_body);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPluginPreroller::OnThrottleStateChange() {
|
|
|
|
if (!throttler_->IsThrottled())
|
|
|
|
return;
|
|
|
|
|
2016-01-06 20:20:54 +01:00
|
|
|
PowerSaverInfo power_saver_info;
|
|
|
|
power_saver_info.power_saver_enabled = true;
|
|
|
|
power_saver_info.poster_attribute = keyframe_data_url_.spec();
|
|
|
|
power_saver_info.custom_poster_size = throttler_->GetSize();
|
2015-09-09 16:05:39 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefPluginPlaceholder* placeholder = CefPluginPlaceholder::CreateBlockedPlugin(
|
2017-05-31 17:33:30 +02:00
|
|
|
render_frame(), params_, info_, identifier_, name_,
|
2017-05-17 11:29:28 +02:00
|
|
|
IDR_PLUGIN_POSTER_HTML, message_, power_saver_info);
|
2015-09-09 16:05:39 +02:00
|
|
|
placeholder->SetPremadePlugin(throttler_);
|
|
|
|
placeholder->set_power_saver_enabled(true);
|
|
|
|
placeholder->AllowLoading();
|
|
|
|
|
|
|
|
blink::WebPluginContainer* container =
|
2017-04-20 21:28:17 +02:00
|
|
|
throttler_->GetWebPlugin()->Container();
|
|
|
|
container->SetPlugin(placeholder->plugin());
|
2015-09-09 16:05:39 +02:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
bool success = placeholder->plugin()->Initialize(container);
|
2015-09-09 16:05:39 +02:00
|
|
|
DCHECK(success);
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
container->Invalidate();
|
|
|
|
container->ReportGeometry();
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPluginPreroller::OnThrottlerDestroyed() {
|
|
|
|
throttler_ = nullptr;
|
|
|
|
delete this;
|
|
|
|
}
|
2016-06-21 00:59:23 +02:00
|
|
|
|
|
|
|
void CefPluginPreroller::OnDestruct() {
|
|
|
|
delete this;
|
|
|
|
}
|