NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / renderer / plugins / chrome_plugin_placeholder.cc
blob6a453f94fcddba3339c3d1b8c339748f7a806811
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/renderer/chrome_content_renderer_client.h"
12 #include "chrome/renderer/custom_menu_commands.h"
13 #include "chrome/renderer/plugins/plugin_uma.h"
14 #include "content/public/common/context_menu_params.h"
15 #include "content/public/renderer/render_frame.h"
16 #include "content/public/renderer/render_thread.h"
17 #include "gin/handle.h"
18 #include "gin/object_template_builder.h"
19 #include "grit/generated_resources.h"
20 #include "grit/renderer_resources.h"
21 #include "grit/webkit_strings.h"
22 #include "third_party/WebKit/public/web/WebDocument.h"
23 #include "third_party/WebKit/public/web/WebFrame.h"
24 #include "third_party/WebKit/public/web/WebInputEvent.h"
25 #include "third_party/WebKit/public/web/WebKit.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"
31 using base::UserMetricsAction;
32 using blink::WebDocument;
33 using blink::WebElement;
34 using blink::WebFrame;
35 using blink::WebMouseEvent;
36 using blink::WebNode;
37 using blink::WebPlugin;
38 using blink::WebPluginContainer;
39 using blink::WebPluginParams;
40 using content::RenderThread;
41 using content::RenderView;
43 namespace {
44 const plugins::PluginPlaceholder* g_last_active_menu = NULL;
45 } // namespace
47 const char ChromePluginPlaceholder::kPluginPlaceholderDataURL[] =
48 "chrome://pluginplaceholderdata/";
50 ChromePluginPlaceholder::ChromePluginPlaceholder(
51 content::RenderFrame* render_frame,
52 blink::WebFrame* frame,
53 const blink::WebPluginParams& params,
54 const std::string& html_data,
55 const base::string16& title)
56 : plugins::PluginPlaceholder(render_frame,
57 frame,
58 params,
59 html_data,
60 GURL(kPluginPlaceholderDataURL)),
61 status_(new ChromeViewHostMsg_GetPluginInfo_Status),
62 title_(title),
63 #if defined(ENABLE_PLUGIN_INSTALLATION)
64 placeholder_routing_id_(MSG_ROUTING_NONE),
65 #endif
66 has_host_(false),
67 context_menu_request_id_(0) {
68 RenderThread::Get()->AddObserver(this);
71 ChromePluginPlaceholder::~ChromePluginPlaceholder() {
72 RenderThread::Get()->RemoveObserver(this);
73 if (context_menu_request_id_)
74 render_frame()->CancelContextMenu(context_menu_request_id_);
76 #if defined(ENABLE_PLUGIN_INSTALLATION)
77 if (placeholder_routing_id_ == MSG_ROUTING_NONE)
78 return;
79 RenderThread::Get()->RemoveRoute(placeholder_routing_id_);
80 if (has_host_) {
81 RenderThread::Get()->Send(new ChromeViewHostMsg_RemovePluginPlaceholderHost(
82 routing_id(), placeholder_routing_id_));
84 #endif
87 // static
88 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateMissingPlugin(
89 content::RenderFrame* render_frame,
90 WebFrame* frame,
91 const WebPluginParams& params) {
92 const base::StringPiece template_html(
93 ResourceBundle::GetSharedInstance().GetRawDataResource(
94 IDR_BLOCKED_PLUGIN_HTML));
96 base::DictionaryValue values;
97 #if defined(ENABLE_PLUGIN_INSTALLATION)
98 values.SetString("message", l10n_util::GetStringUTF8(IDS_PLUGIN_SEARCHING));
99 #else
100 values.SetString("message",
101 l10n_util::GetStringUTF8(IDS_PLUGIN_NOT_SUPPORTED));
102 #endif
104 std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
106 // |missing_plugin| will destroy itself when its WebViewPlugin is going away.
107 ChromePluginPlaceholder* missing_plugin = new ChromePluginPlaceholder(
108 render_frame, frame, params, html_data, params.mimeType);
109 missing_plugin->set_allow_loading(true);
110 #if defined(ENABLE_PLUGIN_INSTALLATION)
111 RenderThread::Get()->Send(
112 new ChromeViewHostMsg_FindMissingPlugin(missing_plugin->routing_id(),
113 missing_plugin->CreateRoutingId(),
114 params.mimeType.utf8()));
115 #endif
116 return missing_plugin;
119 // static
120 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateErrorPlugin(
121 content::RenderFrame* render_frame,
122 const base::FilePath& file_path) {
123 base::DictionaryValue values;
124 values.SetString("message",
125 l10n_util::GetStringUTF8(IDS_PLUGIN_INITIALIZATION_ERROR));
127 const base::StringPiece template_html(
128 ResourceBundle::GetSharedInstance().GetRawDataResource(
129 IDR_BLOCKED_PLUGIN_HTML));
130 std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
132 WebPluginParams params;
133 // |missing_plugin| will destroy itself when its WebViewPlugin is going away.
134 ChromePluginPlaceholder* plugin = new ChromePluginPlaceholder(
135 render_frame, NULL, params, html_data, params.mimeType);
137 RenderThread::Get()->Send(new ChromeViewHostMsg_CouldNotLoadPlugin(
138 plugin->routing_id(), file_path));
139 return plugin;
142 // static
143 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateBlockedPlugin(
144 content::RenderFrame* render_frame,
145 WebFrame* frame,
146 const WebPluginParams& params,
147 const content::WebPluginInfo& plugin,
148 const std::string& identifier,
149 const base::string16& name,
150 int template_id,
151 const base::string16& message) {
152 base::DictionaryValue values;
153 values.SetString("message", message);
154 values.SetString("name", name);
155 values.SetString("hide", l10n_util::GetStringUTF8(IDS_PLUGIN_HIDE));
157 const base::StringPiece template_html(
158 ResourceBundle::GetSharedInstance().GetRawDataResource(template_id));
160 DCHECK(!template_html.empty()) << "unable to load template. ID: "
161 << template_id;
162 std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
164 // |blocked_plugin| will destroy itself when its WebViewPlugin is going away.
165 ChromePluginPlaceholder* blocked_plugin = new ChromePluginPlaceholder(
166 render_frame, frame, params, html_data, name);
167 blocked_plugin->SetPluginInfo(plugin);
168 blocked_plugin->SetIdentifier(identifier);
169 return blocked_plugin;
172 void ChromePluginPlaceholder::SetStatus(
173 const ChromeViewHostMsg_GetPluginInfo_Status& status) {
174 status_->value = status.value;
177 #if defined(ENABLE_PLUGIN_INSTALLATION)
178 int32 ChromePluginPlaceholder::CreateRoutingId() {
179 placeholder_routing_id_ = RenderThread::Get()->GenerateRoutingID();
180 RenderThread::Get()->AddRoute(placeholder_routing_id_, this);
181 return placeholder_routing_id_;
183 #endif
185 bool ChromePluginPlaceholder::OnMessageReceived(const IPC::Message& message) {
186 #if defined(ENABLE_PLUGIN_INSTALLATION)
187 bool handled = true;
188 IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder, message)
189 IPC_MESSAGE_HANDLER(ChromeViewMsg_FoundMissingPlugin, OnFoundMissingPlugin)
190 IPC_MESSAGE_HANDLER(ChromeViewMsg_DidNotFindMissingPlugin,
191 OnDidNotFindMissingPlugin)
192 IPC_MESSAGE_HANDLER(ChromeViewMsg_StartedDownloadingPlugin,
193 OnStartedDownloadingPlugin)
194 IPC_MESSAGE_HANDLER(ChromeViewMsg_FinishedDownloadingPlugin,
195 OnFinishedDownloadingPlugin)
196 IPC_MESSAGE_HANDLER(ChromeViewMsg_ErrorDownloadingPlugin,
197 OnErrorDownloadingPlugin)
198 IPC_MESSAGE_HANDLER(ChromeViewMsg_CancelledDownloadingPlugin,
199 OnCancelledDownloadingPlugin)
200 IPC_MESSAGE_UNHANDLED(handled = false)
201 IPC_END_MESSAGE_MAP()
203 if (handled)
204 return true;
205 #endif
207 // We don't swallow these messages because multiple blocked plugins and other
208 // objects have an interest in them.
209 IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder, message)
210 IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering, OnSetIsPrerendering)
211 IPC_MESSAGE_HANDLER(ChromeViewMsg_LoadBlockedPlugins, OnLoadBlockedPlugins)
212 IPC_END_MESSAGE_MAP()
214 return false;
217 void ChromePluginPlaceholder::OnLoadBlockedPlugins(
218 const std::string& identifier) {
219 plugins::PluginPlaceholder::OnLoadBlockedPlugins(identifier);
222 void ChromePluginPlaceholder::OpenAboutPluginsCallback() {
223 RenderThread::Get()->Send(
224 new ChromeViewHostMsg_OpenAboutPlugins(routing_id()));
227 void ChromePluginPlaceholder::OnSetIsPrerendering(bool is_prerendering) {
228 plugins::PluginPlaceholder::OnSetIsPrerendering(is_prerendering);
231 #if defined(ENABLE_PLUGIN_INSTALLATION)
232 void ChromePluginPlaceholder::OnDidNotFindMissingPlugin() {
233 SetMessage(l10n_util::GetStringUTF16(IDS_PLUGIN_NOT_FOUND));
236 void ChromePluginPlaceholder::OnFoundMissingPlugin(
237 const base::string16& plugin_name) {
238 if (status_->value == ChromeViewHostMsg_GetPluginInfo_Status::kNotFound)
239 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_FOUND, plugin_name));
240 has_host_ = true;
241 plugin_name_ = plugin_name;
244 void ChromePluginPlaceholder::OnStartedDownloadingPlugin() {
245 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING, plugin_name_));
248 void ChromePluginPlaceholder::OnFinishedDownloadingPlugin() {
249 bool is_installing =
250 status_->value == ChromeViewHostMsg_GetPluginInfo_Status::kNotFound;
251 SetMessage(l10n_util::GetStringFUTF16(
252 is_installing ? IDS_PLUGIN_INSTALLING : IDS_PLUGIN_UPDATING,
253 plugin_name_));
256 void ChromePluginPlaceholder::OnErrorDownloadingPlugin(
257 const std::string& error) {
258 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR,
259 base::UTF8ToUTF16(error)));
262 void ChromePluginPlaceholder::OnCancelledDownloadingPlugin() {
263 SetMessage(
264 l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED, plugin_name_));
266 #endif // defined(ENABLE_PLUGIN_INSTALLATION)
268 void ChromePluginPlaceholder::PluginListChanged() {
269 if (!GetFrame() || !plugin())
270 return;
271 WebDocument document = GetFrame()->top()->document();
272 if (document.isNull())
273 return;
275 ChromeViewHostMsg_GetPluginInfo_Output output;
276 std::string mime_type(GetPluginParams().mimeType.utf8());
277 render_frame()->Send(
278 new ChromeViewHostMsg_GetPluginInfo(routing_id(),
279 GURL(GetPluginParams().url),
280 document.url(),
281 mime_type,
282 &output));
283 if (output.status.value == status_->value)
284 return;
285 WebPlugin* new_plugin = ChromeContentRendererClient::CreatePlugin(
286 render_frame(), GetFrame(), GetPluginParams(), output);
287 ReplacePlugin(new_plugin);
288 if (!new_plugin) {
289 PluginUMAReporter::GetInstance()->ReportPluginMissing(
290 GetPluginParams().mimeType.utf8(), GURL(GetPluginParams().url));
294 void ChromePluginPlaceholder::OnMenuAction(int request_id, unsigned action) {
295 DCHECK_EQ(context_menu_request_id_, request_id);
296 if (g_last_active_menu != this)
297 return;
298 switch (action) {
299 case chrome::MENU_COMMAND_PLUGIN_RUN: {
300 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Menu"));
301 LoadPlugin();
302 break;
304 case chrome::MENU_COMMAND_PLUGIN_HIDE: {
305 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Menu"));
306 HidePlugin();
307 break;
309 default:
310 NOTREACHED();
314 void ChromePluginPlaceholder::OnMenuClosed(int request_id) {
315 DCHECK_EQ(context_menu_request_id_, request_id);
316 context_menu_request_id_ = 0;
319 void ChromePluginPlaceholder::ShowContextMenu(const WebMouseEvent& event) {
320 if (context_menu_request_id_)
321 return; // Don't allow nested context menu requests.
323 content::ContextMenuParams params;
325 content::MenuItem name_item;
326 name_item.label = title_;
327 params.custom_items.push_back(name_item);
329 content::MenuItem separator_item;
330 separator_item.type = content::MenuItem::SEPARATOR;
331 params.custom_items.push_back(separator_item);
333 if (!GetPluginInfo().path.value().empty()) {
334 content::MenuItem run_item;
335 run_item.action = chrome::MENU_COMMAND_PLUGIN_RUN;
336 // Disable this menu item if the plugin is blocked by policy.
337 run_item.enabled = LoadingAllowed();
338 run_item.label = l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_PLUGIN_RUN);
339 params.custom_items.push_back(run_item);
342 content::MenuItem hide_item;
343 hide_item.action = chrome::MENU_COMMAND_PLUGIN_HIDE;
344 hide_item.enabled = true;
345 hide_item.label = l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_PLUGIN_HIDE);
346 params.custom_items.push_back(hide_item);
348 params.x = event.windowX;
349 params.y = event.windowY;
351 context_menu_request_id_ = render_frame()->ShowContextMenu(this, params);
352 g_last_active_menu = this;
355 void ChromePluginPlaceholder::BindWebFrame(blink::WebFrame* frame) {
356 v8::Isolate* isolate = blink::mainThreadIsolate();
357 v8::HandleScope handle_scope(isolate);
358 v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
359 DCHECK(!context.IsEmpty());
361 v8::Context::Scope context_scope(context);
362 v8::Handle<v8::Object> global = context->Global();
363 global->Set(gin::StringToV8(isolate, "plugin"),
364 gin::CreateHandle(isolate, this).ToV8());
367 gin::ObjectTemplateBuilder ChromePluginPlaceholder::GetObjectTemplateBuilder(
368 v8::Isolate* isolate) {
369 return PluginPlaceholder::GetObjectTemplateBuilder(isolate).SetMethod(
370 "openAboutPlugins", &ChromePluginPlaceholder::OpenAboutPluginsCallback);