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/utf_string_conversions.h"
8 #include "base/values.h"
9 #include "chrome/common/prerender_messages.h"
10 #include "chrome/common/render_messages.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "chrome/grit/renderer_resources.h"
13 #include "chrome/renderer/chrome_content_renderer_client.h"
14 #include "chrome/renderer/custom_menu_commands.h"
15 #include "chrome/renderer/plugins/plugin_uma.h"
16 #include "content/app/strings/grit/content_strings.h"
17 #include "content/public/common/context_menu_params.h"
18 #include "content/public/renderer/render_frame.h"
19 #include "content/public/renderer/render_thread.h"
20 #include "gin/handle.h"
21 #include "gin/object_template_builder.h"
22 #include "third_party/WebKit/public/web/WebDocument.h"
23 #include "third_party/WebKit/public/web/WebInputEvent.h"
24 #include "third_party/WebKit/public/web/WebKit.h"
25 #include "third_party/WebKit/public/web/WebLocalFrame.h"
26 #include "third_party/WebKit/public/web/WebScriptSource.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/base/webui/jstemplate_builder.h"
30 #include "url/url_util.h"
32 using base::UserMetricsAction
;
33 using blink::WebDocument
;
34 using blink::WebElement
;
35 using blink::WebFrame
;
36 using blink::WebLocalFrame
;
37 using blink::WebMouseEvent
;
39 using blink::WebPlugin
;
40 using blink::WebPluginContainer
;
41 using blink::WebPluginParams
;
42 using content::RenderThread
;
43 using content::RenderView
;
46 const plugins::PluginPlaceholder
* g_last_active_menu
= NULL
;
49 // The placeholder is loaded in normal web renderer processes, so it should not
50 // have a chrome:// scheme that might let it be confused with a WebUI page.
51 const char ChromePluginPlaceholder::kPluginPlaceholderDataURL
[] =
52 "data:text/html,pluginplaceholderdata";
54 ChromePluginPlaceholder::ChromePluginPlaceholder(
55 content::RenderFrame
* render_frame
,
56 blink::WebLocalFrame
* frame
,
57 const blink::WebPluginParams
& params
,
58 const std::string
& html_data
,
59 const base::string16
& title
)
60 : plugins::LoadablePluginPlaceholder(render_frame
,
64 GURL(kPluginPlaceholderDataURL
)),
65 status_(new ChromeViewHostMsg_GetPluginInfo_Status
),
67 #if defined(ENABLE_PLUGIN_INSTALLATION)
68 placeholder_routing_id_(MSG_ROUTING_NONE
),
71 context_menu_request_id_(0) {
72 RenderThread::Get()->AddObserver(this);
75 ChromePluginPlaceholder::~ChromePluginPlaceholder() {
76 RenderThread::Get()->RemoveObserver(this);
77 if (context_menu_request_id_
&& render_frame())
78 render_frame()->CancelContextMenu(context_menu_request_id_
);
80 #if defined(ENABLE_PLUGIN_INSTALLATION)
81 if (placeholder_routing_id_
== MSG_ROUTING_NONE
)
83 RenderThread::Get()->RemoveRoute(placeholder_routing_id_
);
85 RenderThread::Get()->Send(new ChromeViewHostMsg_RemovePluginPlaceholderHost(
86 routing_id(), placeholder_routing_id_
));
92 ChromePluginPlaceholder
* ChromePluginPlaceholder::CreateMissingPlugin(
93 content::RenderFrame
* render_frame
,
95 const WebPluginParams
& params
) {
96 const base::StringPiece
template_html(
97 ResourceBundle::GetSharedInstance().GetRawDataResource(
98 IDR_BLOCKED_PLUGIN_HTML
));
100 base::DictionaryValue values
;
101 #if defined(ENABLE_PLUGIN_INSTALLATION)
102 values
.SetString("message", l10n_util::GetStringUTF8(IDS_PLUGIN_SEARCHING
));
104 values
.SetString("message",
105 l10n_util::GetStringUTF8(IDS_PLUGIN_NOT_SUPPORTED
));
108 std::string html_data
= webui::GetI18nTemplateHtml(template_html
, &values
);
110 // |missing_plugin| will destroy itself when its WebViewPlugin is going away.
111 ChromePluginPlaceholder
* missing_plugin
= new ChromePluginPlaceholder(
112 render_frame
, frame
, params
, html_data
, params
.mimeType
);
113 missing_plugin
->set_allow_loading(true);
114 #if defined(ENABLE_PLUGIN_INSTALLATION)
115 RenderThread::Get()->Send(
116 new ChromeViewHostMsg_FindMissingPlugin(missing_plugin
->routing_id(),
117 missing_plugin
->CreateRoutingId(),
118 params
.mimeType
.utf8()));
120 return missing_plugin
;
124 ChromePluginPlaceholder
* ChromePluginPlaceholder::CreateErrorPlugin(
125 content::RenderFrame
* render_frame
,
126 const base::FilePath
& file_path
) {
127 base::DictionaryValue values
;
128 values
.SetString("message",
129 l10n_util::GetStringUTF8(IDS_PLUGIN_INITIALIZATION_ERROR
));
131 const base::StringPiece
template_html(
132 ResourceBundle::GetSharedInstance().GetRawDataResource(
133 IDR_BLOCKED_PLUGIN_HTML
));
134 std::string html_data
= webui::GetI18nTemplateHtml(template_html
, &values
);
136 WebPluginParams params
;
137 // |missing_plugin| will destroy itself when its WebViewPlugin is going away.
138 ChromePluginPlaceholder
* plugin
= new ChromePluginPlaceholder(
139 render_frame
, NULL
, params
, html_data
, params
.mimeType
);
141 RenderThread::Get()->Send(new ChromeViewHostMsg_CouldNotLoadPlugin(
142 plugin
->routing_id(), file_path
));
147 ChromePluginPlaceholder
* ChromePluginPlaceholder::CreateBlockedPlugin(
148 content::RenderFrame
* render_frame
,
149 WebLocalFrame
* frame
,
150 const WebPluginParams
& params
,
151 const content::WebPluginInfo
& plugin
,
152 const std::string
& identifier
,
153 const base::string16
& name
,
155 const base::string16
& message
,
156 const GURL
& poster_url
) {
157 base::DictionaryValue values
;
158 values
.SetString("message", message
);
159 values
.SetString("name", name
);
160 values
.SetString("hide", l10n_util::GetStringUTF8(IDS_PLUGIN_HIDE
));
162 if (poster_url
.is_valid())
163 values
.SetString("background", "url('" + poster_url
.spec() + "')");
165 const base::StringPiece
template_html(
166 ResourceBundle::GetSharedInstance().GetRawDataResource(template_id
));
168 DCHECK(!template_html
.empty()) << "unable to load template. ID: "
170 std::string html_data
= webui::GetI18nTemplateHtml(template_html
, &values
);
172 // |blocked_plugin| will destroy itself when its WebViewPlugin is going away.
173 ChromePluginPlaceholder
* blocked_plugin
= new ChromePluginPlaceholder(
174 render_frame
, frame
, params
, html_data
, name
);
176 #if defined(ENABLE_PLUGINS)
177 if (poster_url
.is_valid())
178 blocked_plugin
->BlockForPowerSaverPoster();
180 blocked_plugin
->SetPluginInfo(plugin
);
181 blocked_plugin
->SetIdentifier(identifier
);
182 return blocked_plugin
;
185 void ChromePluginPlaceholder::SetStatus(
186 const ChromeViewHostMsg_GetPluginInfo_Status
& status
) {
187 status_
->value
= status
.value
;
190 #if defined(ENABLE_PLUGIN_INSTALLATION)
191 int32
ChromePluginPlaceholder::CreateRoutingId() {
192 placeholder_routing_id_
= RenderThread::Get()->GenerateRoutingID();
193 RenderThread::Get()->AddRoute(placeholder_routing_id_
, this);
194 return placeholder_routing_id_
;
198 bool ChromePluginPlaceholder::OnMessageReceived(const IPC::Message
& message
) {
199 #if defined(ENABLE_PLUGIN_INSTALLATION)
201 IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder
, message
)
202 IPC_MESSAGE_HANDLER(ChromeViewMsg_FoundMissingPlugin
, OnFoundMissingPlugin
)
203 IPC_MESSAGE_HANDLER(ChromeViewMsg_DidNotFindMissingPlugin
,
204 OnDidNotFindMissingPlugin
)
205 IPC_MESSAGE_HANDLER(ChromeViewMsg_StartedDownloadingPlugin
,
206 OnStartedDownloadingPlugin
)
207 IPC_MESSAGE_HANDLER(ChromeViewMsg_FinishedDownloadingPlugin
,
208 OnFinishedDownloadingPlugin
)
209 IPC_MESSAGE_HANDLER(ChromeViewMsg_ErrorDownloadingPlugin
,
210 OnErrorDownloadingPlugin
)
211 IPC_MESSAGE_HANDLER(ChromeViewMsg_CancelledDownloadingPlugin
,
212 OnCancelledDownloadingPlugin
)
213 IPC_MESSAGE_UNHANDLED(handled
= false)
214 IPC_END_MESSAGE_MAP()
220 // We don't swallow these messages because multiple blocked plugins and other
221 // objects have an interest in them.
222 IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder
, message
)
223 IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering
, OnSetIsPrerendering
)
224 IPC_MESSAGE_HANDLER(ChromeViewMsg_LoadBlockedPlugins
, OnLoadBlockedPlugins
)
225 IPC_END_MESSAGE_MAP()
230 void ChromePluginPlaceholder::OpenAboutPluginsCallback() {
231 RenderThread::Get()->Send(
232 new ChromeViewHostMsg_OpenAboutPlugins(routing_id()));
235 #if defined(ENABLE_PLUGIN_INSTALLATION)
236 void ChromePluginPlaceholder::OnDidNotFindMissingPlugin() {
237 SetMessage(l10n_util::GetStringUTF16(IDS_PLUGIN_NOT_FOUND
));
240 void ChromePluginPlaceholder::OnFoundMissingPlugin(
241 const base::string16
& plugin_name
) {
242 if (status_
->value
== ChromeViewHostMsg_GetPluginInfo_Status::kNotFound
)
243 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_FOUND
, plugin_name
));
245 plugin_name_
= plugin_name
;
248 void ChromePluginPlaceholder::OnStartedDownloadingPlugin() {
249 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING
, plugin_name_
));
252 void ChromePluginPlaceholder::OnFinishedDownloadingPlugin() {
254 status_
->value
== ChromeViewHostMsg_GetPluginInfo_Status::kNotFound
;
255 SetMessage(l10n_util::GetStringFUTF16(
256 is_installing
? IDS_PLUGIN_INSTALLING
: IDS_PLUGIN_UPDATING
,
260 void ChromePluginPlaceholder::OnErrorDownloadingPlugin(
261 const std::string
& error
) {
262 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR
,
263 base::UTF8ToUTF16(error
)));
266 void ChromePluginPlaceholder::OnCancelledDownloadingPlugin() {
268 l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED
, plugin_name_
));
270 #endif // defined(ENABLE_PLUGIN_INSTALLATION)
272 void ChromePluginPlaceholder::PluginListChanged() {
273 if (!GetFrame() || !plugin())
275 WebDocument document
= GetFrame()->top()->document();
276 if (document
.isNull())
279 ChromeViewHostMsg_GetPluginInfo_Output output
;
280 std::string
mime_type(GetPluginParams().mimeType
.utf8());
281 render_frame()->Send(
282 new ChromeViewHostMsg_GetPluginInfo(routing_id(),
283 GURL(GetPluginParams().url
),
287 if (output
.status
.value
== status_
->value
)
289 WebPlugin
* new_plugin
= ChromeContentRendererClient::CreatePlugin(
290 render_frame(), GetFrame(), GetPluginParams(), output
);
291 ReplacePlugin(new_plugin
);
293 PluginUMAReporter::GetInstance()->ReportPluginMissing(
294 GetPluginParams().mimeType
.utf8(), GURL(GetPluginParams().url
));
298 void ChromePluginPlaceholder::OnMenuAction(int request_id
, unsigned action
) {
299 DCHECK_EQ(context_menu_request_id_
, request_id
);
300 if (g_last_active_menu
!= this)
303 case chrome::MENU_COMMAND_PLUGIN_RUN
: {
304 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Menu"));
305 #if defined(ENABLE_PLUGINS)
306 DisablePowerSaverForInstance();
311 case chrome::MENU_COMMAND_PLUGIN_HIDE
: {
312 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Menu"));
321 void ChromePluginPlaceholder::OnMenuClosed(int request_id
) {
322 DCHECK_EQ(context_menu_request_id_
, request_id
);
323 context_menu_request_id_
= 0;
326 void ChromePluginPlaceholder::ShowContextMenu(const WebMouseEvent
& event
) {
327 #if !defined(OS_ANDROID) // The context menu is not applicable on Android.
328 if (context_menu_request_id_
)
329 return; // Don't allow nested context menu requests.
331 content::ContextMenuParams params
;
333 content::MenuItem name_item
;
334 name_item
.label
= title_
;
335 params
.custom_items
.push_back(name_item
);
337 content::MenuItem separator_item
;
338 separator_item
.type
= content::MenuItem::SEPARATOR
;
339 params
.custom_items
.push_back(separator_item
);
341 if (!GetPluginInfo().path
.value().empty()) {
342 content::MenuItem run_item
;
343 run_item
.action
= chrome::MENU_COMMAND_PLUGIN_RUN
;
344 // Disable this menu item if the plugin is blocked by policy.
345 run_item
.enabled
= LoadingAllowed();
346 run_item
.label
= l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_PLUGIN_RUN
);
347 params
.custom_items
.push_back(run_item
);
350 content::MenuItem hide_item
;
351 hide_item
.action
= chrome::MENU_COMMAND_PLUGIN_HIDE
;
352 hide_item
.enabled
= true;
353 hide_item
.label
= l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_PLUGIN_HIDE
);
354 params
.custom_items
.push_back(hide_item
);
356 params
.x
= event
.windowX
;
357 params
.y
= event
.windowY
;
359 context_menu_request_id_
= render_frame()->ShowContextMenu(this, params
);
360 g_last_active_menu
= this;
364 void ChromePluginPlaceholder::BindWebFrame(blink::WebFrame
* frame
) {
365 v8::Isolate
* isolate
= blink::mainThreadIsolate();
366 v8::HandleScope
handle_scope(isolate
);
367 v8::Handle
<v8::Context
> context
= frame
->mainWorldScriptContext();
368 DCHECK(!context
.IsEmpty());
370 v8::Context::Scope
context_scope(context
);
371 v8::Handle
<v8::Object
> global
= context
->Global();
372 global
->Set(gin::StringToV8(isolate
, "plugin"),
373 gin::CreateHandle(isolate
, this).ToV8());
376 gin::ObjectTemplateBuilder
ChromePluginPlaceholder::GetObjectTemplateBuilder(
377 v8::Isolate
* isolate
) {
378 return LoadablePluginPlaceholder::GetObjectTemplateBuilder(isolate
).SetMethod(
379 "openAboutPlugins", &ChromePluginPlaceholder::OpenAboutPluginsCallback
);