1 // Copyright 2013 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/chrome_plugin_placeholder.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "chrome/common/prerender_messages.h"
11 #include "chrome/common/render_messages.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "chrome/grit/renderer_resources.h"
14 #include "chrome/renderer/chrome_content_renderer_client.h"
15 #include "chrome/renderer/custom_menu_commands.h"
16 #include "chrome/renderer/plugins/plugin_uma.h"
17 #include "components/content_settings/content/common/content_settings_messages.h"
18 #include "content/app/strings/grit/content_strings.h"
19 #include "content/public/common/context_menu_params.h"
20 #include "content/public/renderer/render_frame.h"
21 #include "content/public/renderer/render_thread.h"
22 #include "gin/handle.h"
23 #include "gin/object_template_builder.h"
24 #include "third_party/WebKit/public/web/WebDocument.h"
25 #include "third_party/WebKit/public/web/WebInputEvent.h"
26 #include "third_party/WebKit/public/web/WebKit.h"
27 #include "third_party/WebKit/public/web/WebLocalFrame.h"
28 #include "third_party/WebKit/public/web/WebScriptSource.h"
29 #include "ui/base/l10n/l10n_util.h"
30 #include "ui/base/resource/resource_bundle.h"
31 #include "ui/base/webui/jstemplate_builder.h"
32 #include "ui/gfx/geometry/size.h"
33 #include "url/url_util.h"
35 using base::UserMetricsAction
;
36 using blink::WebDocument
;
37 using blink::WebElement
;
38 using blink::WebFrame
;
39 using blink::WebLocalFrame
;
40 using blink::WebMouseEvent
;
42 using blink::WebPlugin
;
43 using blink::WebPluginContainer
;
44 using blink::WebPluginParams
;
45 using content::RenderThread
;
46 using content::RenderView
;
49 const plugins::PluginPlaceholder
* g_last_active_menu
= NULL
;
52 // The placeholder is loaded in normal web renderer processes, so it should not
53 // have a chrome:// scheme that might let it be confused with a WebUI page.
54 const char ChromePluginPlaceholder::kPluginPlaceholderDataURL
[] =
55 "data:text/html,pluginplaceholderdata";
57 ChromePluginPlaceholder::ChromePluginPlaceholder(
58 content::RenderFrame
* render_frame
,
59 blink::WebLocalFrame
* frame
,
60 const blink::WebPluginParams
& params
,
61 const std::string
& html_data
,
62 const base::string16
& title
)
63 : plugins::LoadablePluginPlaceholder(render_frame
,
67 GURL(kPluginPlaceholderDataURL
)),
68 status_(ChromeViewHostMsg_GetPluginInfo_Status::kAllowed
),
70 #if defined(ENABLE_PLUGIN_INSTALLATION)
71 placeholder_routing_id_(MSG_ROUTING_NONE
),
74 context_menu_request_id_(0) {
75 RenderThread::Get()->AddObserver(this);
78 ChromePluginPlaceholder::~ChromePluginPlaceholder() {
79 RenderThread::Get()->RemoveObserver(this);
80 if (context_menu_request_id_
&& render_frame())
81 render_frame()->CancelContextMenu(context_menu_request_id_
);
83 #if defined(ENABLE_PLUGIN_INSTALLATION)
84 if (placeholder_routing_id_
== MSG_ROUTING_NONE
)
86 RenderThread::Get()->RemoveRoute(placeholder_routing_id_
);
88 RenderThread::Get()->Send(new ChromeViewHostMsg_RemovePluginPlaceholderHost(
89 routing_id(), placeholder_routing_id_
));
95 ChromePluginPlaceholder
* ChromePluginPlaceholder::CreateMissingPlugin(
96 content::RenderFrame
* render_frame
,
98 const WebPluginParams
& params
) {
99 const base::StringPiece
template_html(
100 ResourceBundle::GetSharedInstance().GetRawDataResource(
101 IDR_BLOCKED_PLUGIN_HTML
));
103 base::DictionaryValue values
;
104 values
.SetString("message",
105 l10n_util::GetStringUTF8(IDS_PLUGIN_NOT_SUPPORTED
));
107 std::string html_data
= webui::GetI18nTemplateHtml(template_html
, &values
);
109 // |missing_plugin| will destroy itself when its WebViewPlugin is going away.
110 ChromePluginPlaceholder
* missing_plugin
= new ChromePluginPlaceholder(
111 render_frame
, frame
, params
, html_data
, params
.mimeType
);
112 missing_plugin
->set_allow_loading(true);
113 return missing_plugin
;
117 ChromePluginPlaceholder
* ChromePluginPlaceholder::CreateErrorPlugin(
118 content::RenderFrame
* render_frame
,
119 const base::FilePath
& file_path
) {
120 base::DictionaryValue values
;
121 values
.SetString("message",
122 l10n_util::GetStringUTF8(IDS_PLUGIN_INITIALIZATION_ERROR
));
124 const base::StringPiece
template_html(
125 ResourceBundle::GetSharedInstance().GetRawDataResource(
126 IDR_BLOCKED_PLUGIN_HTML
));
127 std::string html_data
= webui::GetI18nTemplateHtml(template_html
, &values
);
129 WebPluginParams params
;
130 // |missing_plugin| will destroy itself when its WebViewPlugin is going away.
131 ChromePluginPlaceholder
* plugin
= new ChromePluginPlaceholder(
132 render_frame
, NULL
, params
, html_data
, params
.mimeType
);
134 RenderThread::Get()->Send(new ChromeViewHostMsg_CouldNotLoadPlugin(
135 plugin
->routing_id(), file_path
));
140 ChromePluginPlaceholder
* ChromePluginPlaceholder::CreateBlockedPlugin(
141 content::RenderFrame
* render_frame
,
142 WebLocalFrame
* frame
,
143 const WebPluginParams
& params
,
144 const content::WebPluginInfo
& info
,
145 const std::string
& identifier
,
146 const base::string16
& name
,
148 const base::string16
& message
,
149 const PlaceholderPosterInfo
& poster_info
) {
150 base::DictionaryValue values
;
151 values
.SetString("message", message
);
152 values
.SetString("name", name
);
153 values
.SetString("hide", l10n_util::GetStringUTF8(IDS_PLUGIN_HIDE
));
155 if (!poster_info
.poster_attribute
.empty()) {
156 values
.SetString("poster", poster_info
.poster_attribute
);
157 values
.SetString("baseurl", poster_info
.base_url
.spec());
159 if (!poster_info
.custom_poster_size
.IsEmpty()) {
162 base::IntToString(poster_info
.custom_poster_size
.width()) + "px");
165 base::IntToString(poster_info
.custom_poster_size
.height()) + "px");
169 const base::StringPiece
template_html(
170 ResourceBundle::GetSharedInstance().GetRawDataResource(template_id
));
172 DCHECK(!template_html
.empty()) << "unable to load template. ID: "
174 std::string html_data
= webui::GetI18nTemplateHtml(template_html
, &values
);
176 // |blocked_plugin| will destroy itself when its WebViewPlugin is going away.
177 ChromePluginPlaceholder
* blocked_plugin
= new ChromePluginPlaceholder(
178 render_frame
, frame
, params
, html_data
, name
);
180 #if defined(ENABLE_PLUGINS)
181 if (!poster_info
.poster_attribute
.empty())
182 blocked_plugin
->BlockForPowerSaverPoster();
184 blocked_plugin
->SetPluginInfo(info
);
185 blocked_plugin
->SetIdentifier(identifier
);
186 return blocked_plugin
;
189 void ChromePluginPlaceholder::SetStatus(
190 ChromeViewHostMsg_GetPluginInfo_Status status
) {
194 #if defined(ENABLE_PLUGIN_INSTALLATION)
195 int32
ChromePluginPlaceholder::CreateRoutingId() {
196 placeholder_routing_id_
= RenderThread::Get()->GenerateRoutingID();
197 RenderThread::Get()->AddRoute(placeholder_routing_id_
, this);
198 return placeholder_routing_id_
;
202 bool ChromePluginPlaceholder::OnMessageReceived(const IPC::Message
& message
) {
203 #if defined(ENABLE_PLUGIN_INSTALLATION)
205 IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder
, message
)
206 IPC_MESSAGE_HANDLER(ChromeViewMsg_FoundMissingPlugin
, OnFoundMissingPlugin
)
207 IPC_MESSAGE_HANDLER(ChromeViewMsg_DidNotFindMissingPlugin
,
208 OnDidNotFindMissingPlugin
)
209 IPC_MESSAGE_HANDLER(ChromeViewMsg_StartedDownloadingPlugin
,
210 OnStartedDownloadingPlugin
)
211 IPC_MESSAGE_HANDLER(ChromeViewMsg_FinishedDownloadingPlugin
,
212 OnFinishedDownloadingPlugin
)
213 IPC_MESSAGE_HANDLER(ChromeViewMsg_ErrorDownloadingPlugin
,
214 OnErrorDownloadingPlugin
)
215 IPC_MESSAGE_HANDLER(ChromeViewMsg_CancelledDownloadingPlugin
,
216 OnCancelledDownloadingPlugin
)
217 IPC_MESSAGE_UNHANDLED(handled
= false)
218 IPC_END_MESSAGE_MAP()
224 // We don't swallow these messages because multiple blocked plugins and other
225 // objects have an interest in them.
226 IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder
, message
)
227 IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering
, OnSetIsPrerendering
)
228 IPC_MESSAGE_HANDLER(ChromeViewMsg_LoadBlockedPlugins
, OnLoadBlockedPlugins
)
229 IPC_END_MESSAGE_MAP()
234 void ChromePluginPlaceholder::OpenAboutPluginsCallback() {
235 RenderThread::Get()->Send(
236 new ChromeViewHostMsg_OpenAboutPlugins(routing_id()));
239 #if defined(ENABLE_PLUGIN_INSTALLATION)
240 void ChromePluginPlaceholder::OnDidNotFindMissingPlugin() {
241 SetMessage(l10n_util::GetStringUTF16(IDS_PLUGIN_NOT_FOUND
));
244 void ChromePluginPlaceholder::OnFoundMissingPlugin(
245 const base::string16
& plugin_name
) {
246 if (status_
== ChromeViewHostMsg_GetPluginInfo_Status::kNotFound
)
247 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_FOUND
, plugin_name
));
249 plugin_name_
= plugin_name
;
252 void ChromePluginPlaceholder::OnStartedDownloadingPlugin() {
253 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING
, plugin_name_
));
256 void ChromePluginPlaceholder::OnFinishedDownloadingPlugin() {
258 status_
== ChromeViewHostMsg_GetPluginInfo_Status::kNotFound
;
259 SetMessage(l10n_util::GetStringFUTF16(
260 is_installing
? IDS_PLUGIN_INSTALLING
: IDS_PLUGIN_UPDATING
,
264 void ChromePluginPlaceholder::OnErrorDownloadingPlugin(
265 const std::string
& error
) {
266 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR
,
267 base::UTF8ToUTF16(error
)));
270 void ChromePluginPlaceholder::OnCancelledDownloadingPlugin() {
272 l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED
, plugin_name_
));
274 #endif // defined(ENABLE_PLUGIN_INSTALLATION)
276 void ChromePluginPlaceholder::PluginListChanged() {
277 if (!GetFrame() || !plugin())
279 WebDocument document
= GetFrame()->top()->document();
280 if (document
.isNull())
283 ChromeViewHostMsg_GetPluginInfo_Output output
;
284 std::string
mime_type(GetPluginParams().mimeType
.utf8());
285 render_frame()->Send(
286 new ChromeViewHostMsg_GetPluginInfo(routing_id(),
287 GURL(GetPluginParams().url
),
291 if (output
.status
== status_
)
293 WebPlugin
* new_plugin
= ChromeContentRendererClient::CreatePlugin(
294 render_frame(), GetFrame(), GetPluginParams(), output
);
295 ReplacePlugin(new_plugin
);
297 PluginUMAReporter::GetInstance()->ReportPluginMissing(
298 GetPluginParams().mimeType
.utf8(), GURL(GetPluginParams().url
));
302 void ChromePluginPlaceholder::OnMenuAction(int request_id
, unsigned action
) {
303 DCHECK_EQ(context_menu_request_id_
, request_id
);
304 if (g_last_active_menu
!= this)
307 case chrome::MENU_COMMAND_PLUGIN_RUN
: {
308 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Menu"));
309 #if defined(ENABLE_PLUGINS)
311 content::PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_CLICK
);
316 case chrome::MENU_COMMAND_PLUGIN_HIDE
: {
317 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Menu"));
326 void ChromePluginPlaceholder::OnMenuClosed(int request_id
) {
327 DCHECK_EQ(context_menu_request_id_
, request_id
);
328 context_menu_request_id_
= 0;
331 void ChromePluginPlaceholder::ShowContextMenu(const WebMouseEvent
& event
) {
332 #if !defined(OS_ANDROID) // The context menu is not applicable on Android.
333 if (context_menu_request_id_
)
334 return; // Don't allow nested context menu requests.
336 content::ContextMenuParams params
;
338 if (!title_
.empty()) {
339 content::MenuItem name_item
;
340 name_item
.label
= title_
;
341 params
.custom_items
.push_back(name_item
);
343 content::MenuItem separator_item
;
344 separator_item
.type
= content::MenuItem::SEPARATOR
;
345 params
.custom_items
.push_back(separator_item
);
348 if (!GetPluginInfo().path
.value().empty()) {
349 content::MenuItem run_item
;
350 run_item
.action
= chrome::MENU_COMMAND_PLUGIN_RUN
;
351 // Disable this menu item if the plugin is blocked by policy.
352 run_item
.enabled
= LoadingAllowed();
353 run_item
.label
= l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_PLUGIN_RUN
);
354 params
.custom_items
.push_back(run_item
);
357 content::MenuItem hide_item
;
358 hide_item
.action
= chrome::MENU_COMMAND_PLUGIN_HIDE
;
359 hide_item
.enabled
= true;
360 hide_item
.label
= l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_PLUGIN_HIDE
);
361 params
.custom_items
.push_back(hide_item
);
363 params
.x
= event
.windowX
;
364 params
.y
= event
.windowY
;
366 context_menu_request_id_
= render_frame()->ShowContextMenu(this, params
);
367 g_last_active_menu
= this;
371 void ChromePluginPlaceholder::BindWebFrame(blink::WebFrame
* frame
) {
372 v8::Isolate
* isolate
= blink::mainThreadIsolate();
373 v8::HandleScope
handle_scope(isolate
);
374 v8::Local
<v8::Context
> context
= frame
->mainWorldScriptContext();
375 DCHECK(!context
.IsEmpty());
377 v8::Context::Scope
context_scope(context
);
378 v8::Local
<v8::Object
> global
= context
->Global();
379 global
->Set(gin::StringToV8(isolate
, "plugin"),
380 gin::CreateHandle(isolate
, this).ToV8());
383 gin::ObjectTemplateBuilder
ChromePluginPlaceholder::GetObjectTemplateBuilder(
384 v8::Isolate
* isolate
) {
385 return LoadablePluginPlaceholder::GetObjectTemplateBuilder(isolate
).SetMethod(
386 "openAboutPlugins", &ChromePluginPlaceholder::OpenAboutPluginsCallback
);