[Android] Mark testNotificationDeleteIntentClosesNotification instrumentation test...
[chromium-blink-merge.git] / chrome / renderer / plugins / chrome_plugin_placeholder.cc
blobc1788a3d2695e4085cdc40514894aea64a148b2c
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;
38 using blink::WebNode;
39 using blink::WebPlugin;
40 using blink::WebPluginContainer;
41 using blink::WebPluginParams;
42 using content::RenderThread;
43 using content::RenderView;
45 namespace {
46 const plugins::PluginPlaceholder* g_last_active_menu = NULL;
47 } // namespace
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,
61 frame,
62 params,
63 html_data,
64 GURL(kPluginPlaceholderDataURL)),
65 status_(new ChromeViewHostMsg_GetPluginInfo_Status),
66 title_(title),
67 #if defined(ENABLE_PLUGIN_INSTALLATION)
68 placeholder_routing_id_(MSG_ROUTING_NONE),
69 #endif
70 has_host_(false),
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)
82 return;
83 RenderThread::Get()->RemoveRoute(placeholder_routing_id_);
84 if (has_host_) {
85 RenderThread::Get()->Send(new ChromeViewHostMsg_RemovePluginPlaceholderHost(
86 routing_id(), placeholder_routing_id_));
88 #endif
91 // static
92 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateMissingPlugin(
93 content::RenderFrame* render_frame,
94 WebLocalFrame* 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 values.SetString("message",
102 l10n_util::GetStringUTF8(IDS_PLUGIN_NOT_SUPPORTED));
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 return missing_plugin;
113 // static
114 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateErrorPlugin(
115 content::RenderFrame* render_frame,
116 const base::FilePath& file_path) {
117 base::DictionaryValue values;
118 values.SetString("message",
119 l10n_util::GetStringUTF8(IDS_PLUGIN_INITIALIZATION_ERROR));
121 const base::StringPiece template_html(
122 ResourceBundle::GetSharedInstance().GetRawDataResource(
123 IDR_BLOCKED_PLUGIN_HTML));
124 std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
126 WebPluginParams params;
127 // |missing_plugin| will destroy itself when its WebViewPlugin is going away.
128 ChromePluginPlaceholder* plugin = new ChromePluginPlaceholder(
129 render_frame, NULL, params, html_data, params.mimeType);
131 RenderThread::Get()->Send(new ChromeViewHostMsg_CouldNotLoadPlugin(
132 plugin->routing_id(), file_path));
133 return plugin;
136 // static
137 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateBlockedPlugin(
138 content::RenderFrame* render_frame,
139 WebLocalFrame* frame,
140 const WebPluginParams& params,
141 const content::WebPluginInfo& info,
142 const std::string& identifier,
143 const base::string16& name,
144 int template_id,
145 const base::string16& message,
146 const std::string& poster_attribute,
147 const GURL& base_url) {
148 base::DictionaryValue values;
149 values.SetString("message", message);
150 values.SetString("name", name);
151 values.SetString("hide", l10n_util::GetStringUTF8(IDS_PLUGIN_HIDE));
153 if (!poster_attribute.empty()) {
154 values.SetString("poster", poster_attribute);
155 values.SetString("baseurl", base_url.spec());
158 const base::StringPiece template_html(
159 ResourceBundle::GetSharedInstance().GetRawDataResource(template_id));
161 DCHECK(!template_html.empty()) << "unable to load template. ID: "
162 << template_id;
163 std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
165 // |blocked_plugin| will destroy itself when its WebViewPlugin is going away.
166 ChromePluginPlaceholder* blocked_plugin = new ChromePluginPlaceholder(
167 render_frame, frame, params, html_data, name);
169 #if defined(ENABLE_PLUGINS)
170 if (!poster_attribute.empty())
171 blocked_plugin->BlockForPowerSaverPoster();
172 #endif
173 blocked_plugin->SetPluginInfo(info);
174 blocked_plugin->SetIdentifier(identifier);
175 return blocked_plugin;
178 void ChromePluginPlaceholder::SetStatus(
179 const ChromeViewHostMsg_GetPluginInfo_Status& status) {
180 status_->value = status.value;
183 #if defined(ENABLE_PLUGIN_INSTALLATION)
184 int32 ChromePluginPlaceholder::CreateRoutingId() {
185 placeholder_routing_id_ = RenderThread::Get()->GenerateRoutingID();
186 RenderThread::Get()->AddRoute(placeholder_routing_id_, this);
187 return placeholder_routing_id_;
189 #endif
191 bool ChromePluginPlaceholder::OnMessageReceived(const IPC::Message& message) {
192 #if defined(ENABLE_PLUGIN_INSTALLATION)
193 bool handled = true;
194 IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder, message)
195 IPC_MESSAGE_HANDLER(ChromeViewMsg_FoundMissingPlugin, OnFoundMissingPlugin)
196 IPC_MESSAGE_HANDLER(ChromeViewMsg_DidNotFindMissingPlugin,
197 OnDidNotFindMissingPlugin)
198 IPC_MESSAGE_HANDLER(ChromeViewMsg_StartedDownloadingPlugin,
199 OnStartedDownloadingPlugin)
200 IPC_MESSAGE_HANDLER(ChromeViewMsg_FinishedDownloadingPlugin,
201 OnFinishedDownloadingPlugin)
202 IPC_MESSAGE_HANDLER(ChromeViewMsg_ErrorDownloadingPlugin,
203 OnErrorDownloadingPlugin)
204 IPC_MESSAGE_HANDLER(ChromeViewMsg_CancelledDownloadingPlugin,
205 OnCancelledDownloadingPlugin)
206 IPC_MESSAGE_UNHANDLED(handled = false)
207 IPC_END_MESSAGE_MAP()
209 if (handled)
210 return true;
211 #endif
213 // We don't swallow these messages because multiple blocked plugins and other
214 // objects have an interest in them.
215 IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder, message)
216 IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering, OnSetIsPrerendering)
217 IPC_MESSAGE_HANDLER(ChromeViewMsg_LoadBlockedPlugins, OnLoadBlockedPlugins)
218 IPC_END_MESSAGE_MAP()
220 return false;
223 void ChromePluginPlaceholder::OpenAboutPluginsCallback() {
224 RenderThread::Get()->Send(
225 new ChromeViewHostMsg_OpenAboutPlugins(routing_id()));
228 #if defined(ENABLE_PLUGIN_INSTALLATION)
229 void ChromePluginPlaceholder::OnDidNotFindMissingPlugin() {
230 SetMessage(l10n_util::GetStringUTF16(IDS_PLUGIN_NOT_FOUND));
233 void ChromePluginPlaceholder::OnFoundMissingPlugin(
234 const base::string16& plugin_name) {
235 if (status_->value == ChromeViewHostMsg_GetPluginInfo_Status::kNotFound)
236 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_FOUND, plugin_name));
237 has_host_ = true;
238 plugin_name_ = plugin_name;
241 void ChromePluginPlaceholder::OnStartedDownloadingPlugin() {
242 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING, plugin_name_));
245 void ChromePluginPlaceholder::OnFinishedDownloadingPlugin() {
246 bool is_installing =
247 status_->value == ChromeViewHostMsg_GetPluginInfo_Status::kNotFound;
248 SetMessage(l10n_util::GetStringFUTF16(
249 is_installing ? IDS_PLUGIN_INSTALLING : IDS_PLUGIN_UPDATING,
250 plugin_name_));
253 void ChromePluginPlaceholder::OnErrorDownloadingPlugin(
254 const std::string& error) {
255 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR,
256 base::UTF8ToUTF16(error)));
259 void ChromePluginPlaceholder::OnCancelledDownloadingPlugin() {
260 SetMessage(
261 l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED, plugin_name_));
263 #endif // defined(ENABLE_PLUGIN_INSTALLATION)
265 void ChromePluginPlaceholder::PluginListChanged() {
266 if (!GetFrame() || !plugin())
267 return;
268 WebDocument document = GetFrame()->top()->document();
269 if (document.isNull())
270 return;
272 ChromeViewHostMsg_GetPluginInfo_Output output;
273 std::string mime_type(GetPluginParams().mimeType.utf8());
274 render_frame()->Send(
275 new ChromeViewHostMsg_GetPluginInfo(routing_id(),
276 GURL(GetPluginParams().url),
277 document.url(),
278 mime_type,
279 &output));
280 if (output.status.value == status_->value)
281 return;
282 WebPlugin* new_plugin = ChromeContentRendererClient::CreatePlugin(
283 render_frame(), GetFrame(), GetPluginParams(), output);
284 ReplacePlugin(new_plugin);
285 if (!new_plugin) {
286 PluginUMAReporter::GetInstance()->ReportPluginMissing(
287 GetPluginParams().mimeType.utf8(), GURL(GetPluginParams().url));
291 void ChromePluginPlaceholder::OnMenuAction(int request_id, unsigned action) {
292 DCHECK_EQ(context_menu_request_id_, request_id);
293 if (g_last_active_menu != this)
294 return;
295 switch (action) {
296 case chrome::MENU_COMMAND_PLUGIN_RUN: {
297 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Menu"));
298 #if defined(ENABLE_PLUGINS)
299 MarkPluginEssential(
300 content::PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_CLICK);
301 #endif
302 LoadPlugin();
303 break;
305 case chrome::MENU_COMMAND_PLUGIN_HIDE: {
306 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Menu"));
307 HidePlugin();
308 break;
310 default:
311 NOTREACHED();
315 void ChromePluginPlaceholder::OnMenuClosed(int request_id) {
316 DCHECK_EQ(context_menu_request_id_, request_id);
317 context_menu_request_id_ = 0;
320 void ChromePluginPlaceholder::ShowContextMenu(const WebMouseEvent& event) {
321 #if !defined(OS_ANDROID) // The context menu is not applicable on Android.
322 if (context_menu_request_id_)
323 return; // Don't allow nested context menu requests.
325 content::ContextMenuParams params;
327 if (!title_.empty()) {
328 content::MenuItem name_item;
329 name_item.label = title_;
330 params.custom_items.push_back(name_item);
332 content::MenuItem separator_item;
333 separator_item.type = content::MenuItem::SEPARATOR;
334 params.custom_items.push_back(separator_item);
337 if (!GetPluginInfo().path.value().empty()) {
338 content::MenuItem run_item;
339 run_item.action = chrome::MENU_COMMAND_PLUGIN_RUN;
340 // Disable this menu item if the plugin is blocked by policy.
341 run_item.enabled = LoadingAllowed();
342 run_item.label = l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_PLUGIN_RUN);
343 params.custom_items.push_back(run_item);
346 content::MenuItem hide_item;
347 hide_item.action = chrome::MENU_COMMAND_PLUGIN_HIDE;
348 hide_item.enabled = true;
349 hide_item.label = l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_PLUGIN_HIDE);
350 params.custom_items.push_back(hide_item);
352 params.x = event.windowX;
353 params.y = event.windowY;
355 context_menu_request_id_ = render_frame()->ShowContextMenu(this, params);
356 g_last_active_menu = this;
357 #endif // OS_ANDROID
360 void ChromePluginPlaceholder::BindWebFrame(blink::WebFrame* frame) {
361 v8::Isolate* isolate = blink::mainThreadIsolate();
362 v8::HandleScope handle_scope(isolate);
363 v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
364 DCHECK(!context.IsEmpty());
366 v8::Context::Scope context_scope(context);
367 v8::Handle<v8::Object> global = context->Global();
368 global->Set(gin::StringToV8(isolate, "plugin"),
369 gin::CreateHandle(isolate, this).ToV8());
372 gin::ObjectTemplateBuilder ChromePluginPlaceholder::GetObjectTemplateBuilder(
373 v8::Isolate* isolate) {
374 return LoadablePluginPlaceholder::GetObjectTemplateBuilder(isolate).SetMethod(
375 "openAboutPlugins", &ChromePluginPlaceholder::OpenAboutPluginsCallback);