1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/renderer/plugins/plugin_preroller.h"
7 #include "base/base64.h"
8 #include "chrome/grit/renderer_resources.h"
9 #include "chrome/renderer/plugins/chrome_plugin_placeholder.h"
10 #include "third_party/WebKit/public/platform/WebRect.h"
11 #include "third_party/WebKit/public/web/WebElement.h"
12 #include "third_party/WebKit/public/web/WebPlugin.h"
13 #include "third_party/WebKit/public/web/WebPluginContainer.h"
14 #include "ui/gfx/codec/png_codec.h"
16 PluginPreroller::PluginPreroller(content::RenderFrame
* render_frame
,
17 blink::WebLocalFrame
* frame
,
18 const blink::WebPluginParams
& params
,
19 const content::WebPluginInfo
& info
,
20 const std::string
& identifier
,
21 const base::string16
& name
,
22 const base::string16
& message
,
23 content::PluginInstanceThrottler
* throttler
)
24 : RenderFrameObserver(render_frame
),
28 identifier_(identifier
),
31 throttler_(throttler
) {
33 throttler_
->AddObserver(this);
36 PluginPreroller::~PluginPreroller() {
38 throttler_
->RemoveObserver(this);
41 void PluginPreroller::OnKeyframeExtracted(const SkBitmap
* bitmap
) {
42 std::vector
<unsigned char> png_data
;
43 if (!gfx::PNGCodec::EncodeBGRASkBitmap(*bitmap
, false, &png_data
)) {
44 DLOG(ERROR
) << "Provided keyframe could not be encoded as PNG.";
48 base::StringPiece
png_as_string(reinterpret_cast<char*>(&png_data
[0]),
51 std::string data_url_header
= "data:image/png;base64,";
52 std::string data_url_body
;
53 base::Base64Encode(png_as_string
, &data_url_body
);
54 keyframe_data_url_
= GURL(data_url_header
+ data_url_body
);
57 void PluginPreroller::OnThrottleStateChange() {
58 if (!throttler_
->IsThrottled())
61 PlaceholderPosterInfo poster_info
;
62 poster_info
.poster_attribute
= keyframe_data_url_
.spec();
63 poster_info
.custom_poster_size
= throttler_
->GetSize();
65 ChromePluginPlaceholder
* placeholder
=
66 ChromePluginPlaceholder::CreateBlockedPlugin(
67 render_frame(), frame_
, params_
, info_
, identifier_
, name_
,
68 IDR_PLUGIN_POSTER_HTML
, message_
, poster_info
);
69 placeholder
->SetPremadePlugin(throttler_
);
70 placeholder
->set_power_saver_enabled(true);
71 placeholder
->AllowLoading();
73 blink::WebPluginContainer
* container
=
74 throttler_
->GetWebPlugin()->container();
75 container
->setPlugin(placeholder
->plugin());
77 bool success
= placeholder
->plugin()->initialize(container
);
80 container
->invalidate();
81 container
->reportGeometry();
86 void PluginPreroller::OnThrottlerDestroyed() {