• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/renderer/plugins/chrome_plugin_placeholder.h"
6 
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 "content/public/renderer/render_view_observer.h"
18 #include "grit/generated_resources.h"
19 #include "grit/renderer_resources.h"
20 #include "grit/webkit_strings.h"
21 #include "third_party/WebKit/public/web/WebDocument.h"
22 #include "third_party/WebKit/public/web/WebFrame.h"
23 #include "third_party/WebKit/public/web/WebInputEvent.h"
24 #include "third_party/WebKit/public/web/WebScriptSource.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/base/webui/jstemplate_builder.h"
28 
29 using content::RenderThread;
30 using content::RenderView;
31 using content::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 webkit_glue::CppArgumentList;
41 using webkit_glue::CppVariant;
42 
43 namespace {
44 const plugins::PluginPlaceholder* g_last_active_menu = NULL;
45 }  // namespace
46 
47 const char ChromePluginPlaceholder::kPluginPlaceholderDataURL[] =
48     "chrome://pluginplaceholderdata/";
49 
50 class ChromePluginPlaceholder::RenderViewObserver
51     : public content::RenderViewObserver {
52  public:
RenderViewObserver(ChromePluginPlaceholder * placeholder)53   explicit RenderViewObserver(ChromePluginPlaceholder* placeholder)
54       : content::RenderViewObserver(
55             placeholder->render_frame()->GetRenderView()),
56         placeholder_(placeholder) {}
57 
58   // content::RenderViewObserver implementation:
OnMessageReceived(const IPC::Message & message)59   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
60     // We don't swallow these messages because multiple blocked plugins and
61     // other objects have an interest in them.
62     IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder, message)
63       IPC_MESSAGE_FORWARD(ChromeViewMsg_LoadBlockedPlugins, placeholder_,
64                           ChromePluginPlaceholder::OnLoadBlockedPlugins)
65     IPC_END_MESSAGE_MAP()
66 
67     return false;
68   }
69 
70  private:
71   ChromePluginPlaceholder* placeholder_;
72 };
73 
ChromePluginPlaceholder(content::RenderFrame * render_frame,blink::WebFrame * frame,const blink::WebPluginParams & params,const std::string & html_data,const base::string16 & title)74 ChromePluginPlaceholder::ChromePluginPlaceholder(
75     content::RenderFrame* render_frame,
76     blink::WebFrame* frame,
77     const blink::WebPluginParams& params,
78     const std::string& html_data,
79     const base::string16& title)
80     : plugins::PluginPlaceholder(render_frame,
81                                  frame,
82                                  params,
83                                  html_data,
84                                  GURL(kPluginPlaceholderDataURL)),
85       status_(new ChromeViewHostMsg_GetPluginInfo_Status),
86       title_(title),
87 #if defined(ENABLE_PLUGIN_INSTALLATION)
88       placeholder_routing_id_(MSG_ROUTING_NONE),
89 #endif
90       has_host_(false),
91       context_menu_request_id_(0) {
92   RenderThread::Get()->AddObserver(this);
93 
94   view_observer_.reset(new RenderViewObserver(this));
95 }
96 
~ChromePluginPlaceholder()97 ChromePluginPlaceholder::~ChromePluginPlaceholder() {
98   RenderThread::Get()->RemoveObserver(this);
99   if (context_menu_request_id_)
100     render_frame()->CancelContextMenu(context_menu_request_id_);
101 
102 #if defined(ENABLE_PLUGIN_INSTALLATION)
103   if (placeholder_routing_id_ == MSG_ROUTING_NONE)
104     return;
105   RenderThread::Get()->RemoveRoute(placeholder_routing_id_);
106   if (has_host_) {
107     RenderThread::Get()->Send(new ChromeViewHostMsg_RemovePluginPlaceholderHost(
108         routing_id(), placeholder_routing_id_));
109   }
110 #endif
111 }
112 
113 // static
CreateMissingPlugin(content::RenderFrame * render_frame,WebFrame * frame,const WebPluginParams & params)114 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateMissingPlugin(
115     content::RenderFrame* render_frame,
116     WebFrame* frame,
117     const WebPluginParams& params) {
118   const base::StringPiece template_html(
119       ResourceBundle::GetSharedInstance().GetRawDataResource(
120           IDR_BLOCKED_PLUGIN_HTML));
121 
122   base::DictionaryValue values;
123 #if defined(ENABLE_PLUGIN_INSTALLATION)
124   values.SetString("message", l10n_util::GetStringUTF8(IDS_PLUGIN_SEARCHING));
125 #else
126   values.SetString("message",
127                    l10n_util::GetStringUTF8(IDS_PLUGIN_NOT_SUPPORTED));
128 #endif
129 
130   std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
131 
132   // |missing_plugin| will destroy itself when its WebViewPlugin is going away.
133   ChromePluginPlaceholder* missing_plugin = new ChromePluginPlaceholder(
134       render_frame, frame, params, html_data, params.mimeType);
135   missing_plugin->set_allow_loading(true);
136 #if defined(ENABLE_PLUGIN_INSTALLATION)
137   RenderThread::Get()->Send(
138       new ChromeViewHostMsg_FindMissingPlugin(missing_plugin->routing_id(),
139                                               missing_plugin->CreateRoutingId(),
140                                               params.mimeType.utf8()));
141 #endif
142   return missing_plugin;
143 }
144 
145 // static
CreateErrorPlugin(content::RenderFrame * render_frame,const base::FilePath & file_path)146 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateErrorPlugin(
147     content::RenderFrame* render_frame,
148     const base::FilePath& file_path) {
149   base::DictionaryValue values;
150   values.SetString("message",
151                    l10n_util::GetStringUTF8(IDS_PLUGIN_INITIALIZATION_ERROR));
152 
153   const base::StringPiece template_html(
154       ResourceBundle::GetSharedInstance().GetRawDataResource(
155           IDR_BLOCKED_PLUGIN_HTML));
156   std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
157 
158   WebPluginParams params;
159   // |missing_plugin| will destroy itself when its WebViewPlugin is going away.
160   ChromePluginPlaceholder* plugin = new ChromePluginPlaceholder(
161       render_frame, NULL, params, html_data, params.mimeType);
162 
163   RenderThread::Get()->Send(new ChromeViewHostMsg_CouldNotLoadPlugin(
164       plugin->routing_id(), file_path));
165   return plugin;
166 }
167 
168 // static
CreateBlockedPlugin(content::RenderFrame * render_frame,WebFrame * frame,const WebPluginParams & params,const content::WebPluginInfo & plugin,const std::string & identifier,const base::string16 & name,int template_id,const base::string16 & message)169 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateBlockedPlugin(
170     content::RenderFrame* render_frame,
171     WebFrame* frame,
172     const WebPluginParams& params,
173     const content::WebPluginInfo& plugin,
174     const std::string& identifier,
175     const base::string16& name,
176     int template_id,
177     const base::string16& message) {
178   base::DictionaryValue values;
179   values.SetString("message", message);
180   values.SetString("name", name);
181   values.SetString("hide", l10n_util::GetStringUTF8(IDS_PLUGIN_HIDE));
182 
183   const base::StringPiece template_html(
184       ResourceBundle::GetSharedInstance().GetRawDataResource(template_id));
185 
186   DCHECK(!template_html.empty()) << "unable to load template. ID: "
187                                  << template_id;
188   std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
189 
190   // |blocked_plugin| will destroy itself when its WebViewPlugin is going away.
191   ChromePluginPlaceholder* blocked_plugin = new ChromePluginPlaceholder(
192       render_frame, frame, params, html_data, name);
193   blocked_plugin->SetPluginInfo(plugin);
194   blocked_plugin->SetIdentifier(identifier);
195   return blocked_plugin;
196 }
197 
SetStatus(const ChromeViewHostMsg_GetPluginInfo_Status & status)198 void ChromePluginPlaceholder::SetStatus(
199     const ChromeViewHostMsg_GetPluginInfo_Status& status) {
200   status_->value = status.value;
201 }
202 
203 #if defined(ENABLE_PLUGIN_INSTALLATION)
CreateRoutingId()204 int32 ChromePluginPlaceholder::CreateRoutingId() {
205   placeholder_routing_id_ = RenderThread::Get()->GenerateRoutingID();
206   RenderThread::Get()->AddRoute(placeholder_routing_id_, this);
207   return placeholder_routing_id_;
208 }
209 #endif
210 
OnMessageReceived(const IPC::Message & message)211 bool ChromePluginPlaceholder::OnMessageReceived(const IPC::Message& message) {
212 #if defined(ENABLE_PLUGIN_INSTALLATION)
213   bool handled = true;
214   IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder, message)
215   IPC_MESSAGE_HANDLER(ChromeViewMsg_FoundMissingPlugin, OnFoundMissingPlugin)
216   IPC_MESSAGE_HANDLER(ChromeViewMsg_DidNotFindMissingPlugin,
217                       OnDidNotFindMissingPlugin)
218   IPC_MESSAGE_HANDLER(ChromeViewMsg_StartedDownloadingPlugin,
219                       OnStartedDownloadingPlugin)
220   IPC_MESSAGE_HANDLER(ChromeViewMsg_FinishedDownloadingPlugin,
221                       OnFinishedDownloadingPlugin)
222   IPC_MESSAGE_HANDLER(ChromeViewMsg_ErrorDownloadingPlugin,
223                       OnErrorDownloadingPlugin)
224   IPC_MESSAGE_HANDLER(ChromeViewMsg_CancelledDownloadingPlugin,
225                       OnCancelledDownloadingPlugin)
226   IPC_MESSAGE_UNHANDLED(handled = false)
227   IPC_END_MESSAGE_MAP()
228 
229   if (handled)
230     return true;
231 #endif
232 
233   // We don't swallow these messages because multiple blocked plugins and other
234   // objects have an interest in them.
235   IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder, message)
236     IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering, OnSetIsPrerendering)
237   IPC_END_MESSAGE_MAP()
238 
239   return false;
240 }
241 
OnLoadBlockedPlugins(const std::string & identifier)242 void ChromePluginPlaceholder::OnLoadBlockedPlugins(
243     const std::string& identifier) {
244   plugins::PluginPlaceholder::OnLoadBlockedPlugins(identifier);
245 }
246 
OpenAboutPluginsCallback(const CppArgumentList & args,CppVariant * result)247 void ChromePluginPlaceholder::OpenAboutPluginsCallback(
248     const CppArgumentList& args,
249     CppVariant* result) {
250   RenderThread::Get()->Send(
251       new ChromeViewHostMsg_OpenAboutPlugins(routing_id()));
252 }
253 
OnSetIsPrerendering(bool is_prerendering)254 void ChromePluginPlaceholder::OnSetIsPrerendering(bool is_prerendering) {
255   plugins::PluginPlaceholder::OnSetIsPrerendering(is_prerendering);
256 }
257 
258 #if defined(ENABLE_PLUGIN_INSTALLATION)
OnDidNotFindMissingPlugin()259 void ChromePluginPlaceholder::OnDidNotFindMissingPlugin() {
260   SetMessage(l10n_util::GetStringUTF16(IDS_PLUGIN_NOT_FOUND));
261 }
262 
OnFoundMissingPlugin(const base::string16 & plugin_name)263 void ChromePluginPlaceholder::OnFoundMissingPlugin(
264     const base::string16& plugin_name) {
265   if (status_->value == ChromeViewHostMsg_GetPluginInfo_Status::kNotFound)
266     SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_FOUND, plugin_name));
267   has_host_ = true;
268   plugin_name_ = plugin_name;
269 }
270 
OnStartedDownloadingPlugin()271 void ChromePluginPlaceholder::OnStartedDownloadingPlugin() {
272   SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING, plugin_name_));
273 }
274 
OnFinishedDownloadingPlugin()275 void ChromePluginPlaceholder::OnFinishedDownloadingPlugin() {
276   bool is_installing =
277       status_->value == ChromeViewHostMsg_GetPluginInfo_Status::kNotFound;
278   SetMessage(l10n_util::GetStringFUTF16(
279       is_installing ? IDS_PLUGIN_INSTALLING : IDS_PLUGIN_UPDATING,
280       plugin_name_));
281 }
282 
OnErrorDownloadingPlugin(const std::string & error)283 void ChromePluginPlaceholder::OnErrorDownloadingPlugin(
284     const std::string& error) {
285   SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR,
286                                         UTF8ToUTF16(error)));
287 }
288 
OnCancelledDownloadingPlugin()289 void ChromePluginPlaceholder::OnCancelledDownloadingPlugin() {
290   SetMessage(
291       l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED, plugin_name_));
292 }
293 #endif  // defined(ENABLE_PLUGIN_INSTALLATION)
294 
PluginListChanged()295 void ChromePluginPlaceholder::PluginListChanged() {
296   if (!GetFrame())
297     return;
298   WebDocument document = GetFrame()->top()->document();
299   if (document.isNull())
300     return;
301 
302   ChromeViewHostMsg_GetPluginInfo_Output output;
303   std::string mime_type(GetPluginParams().mimeType.utf8());
304   render_frame()->Send(
305       new ChromeViewHostMsg_GetPluginInfo(routing_id(),
306                                           GURL(GetPluginParams().url),
307                                           document.url(),
308                                           mime_type,
309                                           &output));
310   if (output.status.value == status_->value)
311     return;
312   WebPlugin* new_plugin = ChromeContentRendererClient::CreatePlugin(
313       render_frame(),  GetFrame(), GetPluginParams(), output);
314   ReplacePlugin(new_plugin);
315   if (!new_plugin) {
316     PluginUMAReporter::GetInstance()->ReportPluginMissing(
317         GetPluginParams().mimeType.utf8(), GURL(GetPluginParams().url));
318   }
319 }
320 
OnMenuAction(int request_id,unsigned action)321 void ChromePluginPlaceholder::OnMenuAction(int request_id, unsigned action) {
322   DCHECK_EQ(context_menu_request_id_, request_id);
323   if (g_last_active_menu != this)
324     return;
325   switch (action) {
326     case chrome::MENU_COMMAND_PLUGIN_RUN: {
327       RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Menu"));
328       LoadPlugin();
329       break;
330     }
331     case chrome::MENU_COMMAND_PLUGIN_HIDE: {
332       RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Menu"));
333       HidePlugin();
334       break;
335     }
336     default:
337       NOTREACHED();
338   }
339 }
340 
OnMenuClosed(int request_id)341 void ChromePluginPlaceholder::OnMenuClosed(int request_id) {
342   DCHECK_EQ(context_menu_request_id_, request_id);
343   context_menu_request_id_ = 0;
344 }
345 
ShowContextMenu(const WebMouseEvent & event)346 void ChromePluginPlaceholder::ShowContextMenu(const WebMouseEvent& event) {
347   if (context_menu_request_id_)
348     return;  // Don't allow nested context menu requests.
349 
350   content::ContextMenuParams params;
351 
352   content::MenuItem name_item;
353   name_item.label = title_;
354   params.custom_items.push_back(name_item);
355 
356   content::MenuItem separator_item;
357   separator_item.type = content::MenuItem::SEPARATOR;
358   params.custom_items.push_back(separator_item);
359 
360   if (!GetPluginInfo().path.value().empty()) {
361     content::MenuItem run_item;
362     run_item.action = chrome::MENU_COMMAND_PLUGIN_RUN;
363     // Disable this menu item if the plugin is blocked by policy.
364     run_item.enabled = LoadingAllowed();
365     run_item.label = l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_PLUGIN_RUN);
366     params.custom_items.push_back(run_item);
367   }
368 
369   content::MenuItem hide_item;
370   hide_item.action = chrome::MENU_COMMAND_PLUGIN_HIDE;
371   hide_item.enabled = true;
372   hide_item.label = l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_PLUGIN_HIDE);
373   params.custom_items.push_back(hide_item);
374 
375   params.x = event.windowX;
376   params.y = event.windowY;
377 
378   context_menu_request_id_ = render_frame()->ShowContextMenu(this, params);
379   g_last_active_menu = this;
380 }
381 
BindWebFrame(blink::WebFrame * frame)382 void ChromePluginPlaceholder::BindWebFrame(blink::WebFrame* frame) {
383   plugins::PluginPlaceholder::BindWebFrame(frame);
384   BindCallback("openAboutPlugins",
385                base::Bind(&ChromePluginPlaceholder::OpenAboutPluginsCallback,
386                           base::Unretained(this)));
387 }
388