Chromium Code Reviews| Index: chrome/renderer/plugins/plugin_preroller.cc |
| diff --git a/chrome/renderer/plugins/plugin_preroller.cc b/chrome/renderer/plugins/plugin_preroller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..85ca08a7b4fada0eba47f1aa437705d652fceb38 |
| --- /dev/null |
| +++ b/chrome/renderer/plugins/plugin_preroller.cc |
| @@ -0,0 +1,93 @@ |
| +// 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 "chrome/renderer/plugins/plugin_preroller.h" |
| + |
| +#include "base/base64.h" |
| +#include "chrome/grit/renderer_resources.h" |
| +#include "chrome/renderer/plugins/chrome_plugin_placeholder.h" |
| +#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" |
| + |
| +PluginPreroller::PluginPreroller(content::RenderFrame* render_frame, |
| + blink::WebLocalFrame* frame, |
| + const blink::WebPluginParams& params, |
| + const content::WebPluginInfo& info, |
| + const std::string& identifier, |
| + const base::string16& name, |
| + const base::string16& message, |
| + blink::WebPlugin* plugin, |
| + content::PluginInstanceThrottler* throttler) |
| + : render_frame_(render_frame), |
| + frame_(frame), |
| + params_(params), |
| + info_(info), |
| + identifier_(identifier), |
| + name_(name), |
| + message_(message), |
| + plugin_(plugin), |
| + throttler_(throttler) { |
| + DCHECK(plugin); |
| + DCHECK(throttler); |
| + throttler_->AddObserver(this); |
| +} |
| + |
| +PluginPreroller::~PluginPreroller() { |
| + if (throttler_) |
| + throttler_->RemoveObserver(this); |
| +} |
| + |
| +void PluginPreroller::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."; |
| + } |
| + |
| + std::string png_as_string(reinterpret_cast<char*>(&png_data[0]), |
|
Bernhard Bauer
2015/01/30 14:12:56
If encoding the keyframe failed, the vector will b
tommycli
2015/01/30 17:02:40
Done.
|
| + 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); |
|
Bernhard Bauer
2015/01/30 14:12:56
base::Base64Encode() takes a base::StringPiece, so
tommycli
2015/01/30 17:02:40
Done.
|
| + keyframe_data_url_ = data_url_header + data_url_body; |
| +} |
| + |
| +void PluginPreroller::OnThrottleStateChange() { |
| + if (throttler_->IsThrottled()) { |
|
Bernhard Bauer
2015/01/30 14:12:56
Early-return if the throttler is not throttled? Th
tommycli
2015/01/30 17:02:40
Done. But I think I only want to destroy the prero
|
| + ChromePluginPlaceholder* placeholder = |
| + ChromePluginPlaceholder::CreateBlockedPlugin( |
| + render_frame_, frame_, params_, info_, identifier_, name_, |
| + IDR_PLUGIN_POSTER_HTML, message_, GURL(keyframe_data_url_), plugin_, |
| + throttler_); |
| + placeholder->set_power_saver_mode( |
| + content::PluginPowerSaverMode::POWER_SAVER_MODE_PERIPHERAL_THROTTLED); |
| + placeholder->set_allow_loading(true); |
| + |
| + // Set the new plug-in on the container before initializing it. |
|
Bernhard Bauer
2015/01/30 14:12:56
I don't think the order of calls is important anym
tommycli
2015/01/30 17:02:40
Done.
|
| + blink::WebPluginContainer* container = plugin_->container(); |
| + container->setPlugin(placeholder->plugin()); |
| + |
| + if (!placeholder->plugin()->initialize(container)) { |
|
Bernhard Bauer
2015/01/30 14:12:56
WebViewPlugin::initialize() always returns true, s
tommycli
2015/01/30 17:02:40
Done.
|
| + // We couldn't initialize the new plug-in. Restore the old one and abort. |
| + container->setPlugin(plugin_); |
| + placeholder->plugin()->destroy(); |
| + delete this; |
| + return; |
| + } |
| + |
| + placeholder->plugin()->RestoreTitleText(); |
|
Bernhard Bauer
2015/01/30 14:12:56
This is probably also not necessary, because the p
tommycli
2015/01/30 17:02:40
Done.
|
| + container->invalidate(); |
| + container->reportGeometry(); |
| + |
| + delete this; |
| + } |
| +} |
| + |
| +void PluginPreroller::OnThrottlerDestroyed() { |
| + throttler_ = nullptr; |
| + delete this; |
| +} |