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_thread.h"
16 #include "content/public/renderer/render_view.h"
17 #include "grit/generated_resources.h"
18 #include "grit/renderer_resources.h"
19 #include "grit/webkit_strings.h"
20 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebFrame.h"
22 #include "third_party/WebKit/public/web/WebInputEvent.h"
23 #include "third_party/WebKit/public/web/WebScriptSource.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/base/resource/resource_bundle.h"
26 #include "ui/base/webui/jstemplate_builder.h"
28 using content::RenderThread;
29 using content::RenderView;
30 using WebKit::WebDocument;
31 using WebKit::WebElement;
32 using WebKit::WebFrame;
33 using WebKit::WebMouseEvent;
34 using WebKit::WebNode;
35 using WebKit::WebPlugin;
36 using WebKit::WebPluginContainer;
37 using WebKit::WebPluginParams;
38 using webkit_glue::CppArgumentList;
39 using webkit_glue::CppVariant;
42 const plugins::PluginPlaceholder* g_last_active_menu = NULL;
45 const char ChromePluginPlaceholder::kPluginPlaceholderDataURL[] =
46 "chrome://pluginplaceholderdata/";
48 ChromePluginPlaceholder::ChromePluginPlaceholder(
49 content::RenderView* render_view,
50 WebKit::WebFrame* frame,
51 const WebKit::WebPluginParams& params,
52 const std::string& html_data,
53 const string16& title)
54 : plugins::PluginPlaceholder(render_view,
58 GURL(kPluginPlaceholderDataURL)),
59 status_(new ChromeViewHostMsg_GetPluginInfo_Status),
61 #if defined(ENABLE_PLUGIN_INSTALLATION)
62 placeholder_routing_id_(MSG_ROUTING_NONE),
65 context_menu_request_id_(0) {
66 RenderThread::Get()->AddObserver(this);
69 ChromePluginPlaceholder::~ChromePluginPlaceholder() {
70 RenderThread::Get()->RemoveObserver(this);
71 if (context_menu_request_id_)
72 render_view()->CancelContextMenu(context_menu_request_id_);
74 #if defined(ENABLE_PLUGIN_INSTALLATION)
75 if (placeholder_routing_id_ == MSG_ROUTING_NONE)
77 RenderThread::Get()->RemoveRoute(placeholder_routing_id_);
79 RenderThread::Get()->Send(new ChromeViewHostMsg_RemovePluginPlaceholderHost(
80 routing_id(), placeholder_routing_id_));
86 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateMissingPlugin(
87 RenderView* render_view,
89 const WebPluginParams& params) {
90 const base::StringPiece template_html(
91 ResourceBundle::GetSharedInstance().GetRawDataResource(
92 IDR_BLOCKED_PLUGIN_HTML));
94 base::DictionaryValue values;
95 #if defined(ENABLE_PLUGIN_INSTALLATION)
96 values.SetString("message", l10n_util::GetStringUTF8(IDS_PLUGIN_SEARCHING));
98 values.SetString("message",
99 l10n_util::GetStringUTF8(IDS_PLUGIN_NOT_SUPPORTED));
102 std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
104 // |missing_plugin| will destroy itself when its WebViewPlugin is going away.
105 ChromePluginPlaceholder* missing_plugin = new ChromePluginPlaceholder(
106 render_view, frame, params, html_data, params.mimeType);
107 missing_plugin->set_allow_loading(true);
108 #if defined(ENABLE_PLUGIN_INSTALLATION)
109 RenderThread::Get()->Send(
110 new ChromeViewHostMsg_FindMissingPlugin(missing_plugin->routing_id(),
111 missing_plugin->CreateRoutingId(),
112 params.mimeType.utf8()));
114 return missing_plugin;
118 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateErrorPlugin(
119 RenderView* render_view,
120 const base::FilePath& file_path) {
121 base::DictionaryValue values;
122 values.SetString("message",
123 l10n_util::GetStringUTF8(IDS_PLUGIN_INITIALIZATION_ERROR));
125 const base::StringPiece template_html(
126 ResourceBundle::GetSharedInstance().GetRawDataResource(
127 IDR_BLOCKED_PLUGIN_HTML));
128 std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
130 WebPluginParams params;
131 // |missing_plugin| will destroy itself when its WebViewPlugin is going away.
132 ChromePluginPlaceholder* plugin = new ChromePluginPlaceholder(
133 render_view, NULL, params, html_data, params.mimeType);
135 RenderThread::Get()->Send(new ChromeViewHostMsg_CouldNotLoadPlugin(
136 plugin->routing_id(), file_path));
141 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateBlockedPlugin(
142 RenderView* render_view,
144 const WebPluginParams& params,
145 const content::WebPluginInfo& plugin,
146 const std::string& identifier,
147 const string16& name,
149 const string16& message) {
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 const base::StringPiece template_html(
156 ResourceBundle::GetSharedInstance().GetRawDataResource(template_id));
158 DCHECK(!template_html.empty()) << "unable to load template. ID: "
160 std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
162 // |blocked_plugin| will destroy itself when its WebViewPlugin is going away.
163 ChromePluginPlaceholder* blocked_plugin =
164 new ChromePluginPlaceholder(render_view, frame, params, html_data, name);
165 blocked_plugin->SetPluginInfo(plugin);
166 blocked_plugin->SetIdentifier(identifier);
167 return blocked_plugin;
170 void ChromePluginPlaceholder::SetStatus(
171 const ChromeViewHostMsg_GetPluginInfo_Status& status) {
172 status_->value = status.value;
175 #if defined(ENABLE_PLUGIN_INSTALLATION)
176 int32 ChromePluginPlaceholder::CreateRoutingId() {
177 placeholder_routing_id_ = RenderThread::Get()->GenerateRoutingID();
178 RenderThread::Get()->AddRoute(placeholder_routing_id_, this);
179 return placeholder_routing_id_;
183 bool ChromePluginPlaceholder::OnMessageReceived(const IPC::Message& message) {
184 #if defined(ENABLE_PLUGIN_INSTALLATION)
186 IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder, message)
187 IPC_MESSAGE_HANDLER(ChromeViewMsg_FoundMissingPlugin, OnFoundMissingPlugin)
188 IPC_MESSAGE_HANDLER(ChromeViewMsg_DidNotFindMissingPlugin,
189 OnDidNotFindMissingPlugin)
190 IPC_MESSAGE_HANDLER(ChromeViewMsg_StartedDownloadingPlugin,
191 OnStartedDownloadingPlugin)
192 IPC_MESSAGE_HANDLER(ChromeViewMsg_FinishedDownloadingPlugin,
193 OnFinishedDownloadingPlugin)
194 IPC_MESSAGE_HANDLER(ChromeViewMsg_ErrorDownloadingPlugin,
195 OnErrorDownloadingPlugin)
196 IPC_MESSAGE_HANDLER(ChromeViewMsg_CancelledDownloadingPlugin,
197 OnCancelledDownloadingPlugin)
198 IPC_MESSAGE_UNHANDLED(handled = false)
199 IPC_END_MESSAGE_MAP()
205 // We don't swallow these messages because multiple blocked plugins have an
207 IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder, message)
208 IPC_MESSAGE_HANDLER(ChromeViewMsg_LoadBlockedPlugins, OnLoadBlockedPlugins)
209 IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering, OnSetIsPrerendering)
210 IPC_END_MESSAGE_MAP()
215 void ChromePluginPlaceholder::OnLoadBlockedPlugins(
216 const std::string& identifier) {
217 plugins::PluginPlaceholder::OnLoadBlockedPlugins(identifier);
220 void ChromePluginPlaceholder::OpenAboutPluginsCallback(
221 const CppArgumentList& args,
222 CppVariant* result) {
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 string16& plugin_name) {
238 if (status_->value == ChromeViewHostMsg_GetPluginInfo_Status::kNotFound)
239 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_FOUND, plugin_name));
241 plugin_name_ = plugin_name;
244 void ChromePluginPlaceholder::OnStartedDownloadingPlugin() {
245 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING, plugin_name_));
248 void ChromePluginPlaceholder::OnFinishedDownloadingPlugin() {
250 status_->value == ChromeViewHostMsg_GetPluginInfo_Status::kNotFound;
251 SetMessage(l10n_util::GetStringFUTF16(
252 is_installing ? IDS_PLUGIN_INSTALLING : IDS_PLUGIN_UPDATING,
256 void ChromePluginPlaceholder::OnErrorDownloadingPlugin(
257 const std::string& error) {
258 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR,
259 UTF8ToUTF16(error)));
262 void ChromePluginPlaceholder::OnCancelledDownloadingPlugin() {
264 l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED, plugin_name_));
266 #endif // defined(ENABLE_PLUGIN_INSTALLATION)
268 void ChromePluginPlaceholder::PluginListChanged() {
271 WebDocument document = GetFrame()->top()->document();
272 if (document.isNull())
275 ChromeViewHostMsg_GetPluginInfo_Output output;
276 std::string mime_type(GetPluginParams().mimeType.utf8());
278 new ChromeViewHostMsg_GetPluginInfo(routing_id(),
279 GURL(GetPluginParams().url),
283 if (output.status.value == status_->value)
285 WebPlugin* new_plugin = chrome::ChromeContentRendererClient::CreatePlugin(
286 render_view(), GetFrame(), GetPluginParams(), output);
287 ReplacePlugin(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)
299 case chrome::MENU_COMMAND_PLUGIN_RUN: {
300 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Menu");
304 case chrome::MENU_COMMAND_PLUGIN_HIDE: {
305 RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Menu");
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_view()->ShowContextMenu(this, params);
352 g_last_active_menu = this;
355 void ChromePluginPlaceholder::BindWebFrame(WebKit::WebFrame* frame) {
356 plugins::PluginPlaceholder::BindWebFrame(frame);
357 BindCallback("openAboutPlugins",
358 base::Bind(&ChromePluginPlaceholder::OpenAboutPluginsCallback,
359 base::Unretained(this)));