- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / renderer / plugins / chrome_plugin_placeholder.cc
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_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"
27
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;
40
41 namespace {
42 const plugins::PluginPlaceholder* g_last_active_menu = NULL;
43 }  // namespace
44
45 const char ChromePluginPlaceholder::kPluginPlaceholderDataURL[] =
46     "chrome://pluginplaceholderdata/";
47
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,
55                                  frame,
56                                  params,
57                                  html_data,
58                                  GURL(kPluginPlaceholderDataURL)),
59       status_(new ChromeViewHostMsg_GetPluginInfo_Status),
60       title_(title),
61 #if defined(ENABLE_PLUGIN_INSTALLATION)
62       placeholder_routing_id_(MSG_ROUTING_NONE),
63 #endif
64       has_host_(false),
65       context_menu_request_id_(0) {
66   RenderThread::Get()->AddObserver(this);
67 }
68
69 ChromePluginPlaceholder::~ChromePluginPlaceholder() {
70   RenderThread::Get()->RemoveObserver(this);
71   if (context_menu_request_id_)
72     render_view()->CancelContextMenu(context_menu_request_id_);
73
74 #if defined(ENABLE_PLUGIN_INSTALLATION)
75   if (placeholder_routing_id_ == MSG_ROUTING_NONE)
76     return;
77   RenderThread::Get()->RemoveRoute(placeholder_routing_id_);
78   if (has_host_) {
79     RenderThread::Get()->Send(new ChromeViewHostMsg_RemovePluginPlaceholderHost(
80         routing_id(), placeholder_routing_id_));
81   }
82 #endif
83 }
84
85 // static
86 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateMissingPlugin(
87     RenderView* render_view,
88     WebFrame* frame,
89     const WebPluginParams& params) {
90   const base::StringPiece template_html(
91       ResourceBundle::GetSharedInstance().GetRawDataResource(
92           IDR_BLOCKED_PLUGIN_HTML));
93
94   base::DictionaryValue values;
95 #if defined(ENABLE_PLUGIN_INSTALLATION)
96   values.SetString("message", l10n_util::GetStringUTF8(IDS_PLUGIN_SEARCHING));
97 #else
98   values.SetString("message",
99                    l10n_util::GetStringUTF8(IDS_PLUGIN_NOT_SUPPORTED));
100 #endif
101
102   std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
103
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()));
113 #endif
114   return missing_plugin;
115 }
116
117 // static
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));
124
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);
129
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);
134
135   RenderThread::Get()->Send(new ChromeViewHostMsg_CouldNotLoadPlugin(
136       plugin->routing_id(), file_path));
137   return plugin;
138 }
139
140 // static
141 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateBlockedPlugin(
142     RenderView* render_view,
143     WebFrame* frame,
144     const WebPluginParams& params,
145     const content::WebPluginInfo& plugin,
146     const std::string& identifier,
147     const string16& name,
148     int template_id,
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));
154
155   const base::StringPiece template_html(
156       ResourceBundle::GetSharedInstance().GetRawDataResource(template_id));
157
158   DCHECK(!template_html.empty()) << "unable to load template. ID: "
159                                  << template_id;
160   std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
161
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;
168 }
169
170 void ChromePluginPlaceholder::SetStatus(
171     const ChromeViewHostMsg_GetPluginInfo_Status& status) {
172   status_->value = status.value;
173 }
174
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_;
180 }
181 #endif
182
183 bool ChromePluginPlaceholder::OnMessageReceived(const IPC::Message& message) {
184 #if defined(ENABLE_PLUGIN_INSTALLATION)
185   bool handled = true;
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()
200
201   if (handled)
202     return true;
203 #endif
204
205   // We don't swallow these messages because multiple blocked plugins have an
206   // interest in them.
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()
211
212   return false;
213 }
214
215 void ChromePluginPlaceholder::OnLoadBlockedPlugins(
216     const std::string& identifier) {
217   plugins::PluginPlaceholder::OnLoadBlockedPlugins(identifier);
218 }
219
220 void ChromePluginPlaceholder::OpenAboutPluginsCallback(
221     const CppArgumentList& args,
222     CppVariant* result) {
223   RenderThread::Get()->Send(
224       new ChromeViewHostMsg_OpenAboutPlugins(routing_id()));
225 }
226
227 void ChromePluginPlaceholder::OnSetIsPrerendering(bool is_prerendering) {
228   plugins::PluginPlaceholder::OnSetIsPrerendering(is_prerendering);
229 }
230
231 #if defined(ENABLE_PLUGIN_INSTALLATION)
232 void ChromePluginPlaceholder::OnDidNotFindMissingPlugin() {
233   SetMessage(l10n_util::GetStringUTF16(IDS_PLUGIN_NOT_FOUND));
234 }
235
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));
240   has_host_ = true;
241   plugin_name_ = plugin_name;
242 }
243
244 void ChromePluginPlaceholder::OnStartedDownloadingPlugin() {
245   SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING, plugin_name_));
246 }
247
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_));
254 }
255
256 void ChromePluginPlaceholder::OnErrorDownloadingPlugin(
257     const std::string& error) {
258   SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR,
259                                         UTF8ToUTF16(error)));
260 }
261
262 void ChromePluginPlaceholder::OnCancelledDownloadingPlugin() {
263   SetMessage(
264       l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED, plugin_name_));
265 }
266 #endif  // defined(ENABLE_PLUGIN_INSTALLATION)
267
268 void ChromePluginPlaceholder::PluginListChanged() {
269   if (!GetFrame())
270     return;
271   WebDocument document = GetFrame()->top()->document();
272   if (document.isNull())
273     return;
274
275   ChromeViewHostMsg_GetPluginInfo_Output output;
276   std::string mime_type(GetPluginParams().mimeType.utf8());
277   render_view()->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 = chrome::ChromeContentRendererClient::CreatePlugin(
286       render_view(), GetFrame(), GetPluginParams(), output);
287   ReplacePlugin(new_plugin);
288   if (!new_plugin) {
289     PluginUMAReporter::GetInstance()->ReportPluginMissing(
290         GetPluginParams().mimeType.utf8(), GURL(GetPluginParams().url));
291   }
292 }
293
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()->RecordUserMetrics("Plugin_Load_Menu");
301       LoadPlugin();
302       break;
303     }
304     case chrome::MENU_COMMAND_PLUGIN_HIDE: {
305       RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Menu");
306       HidePlugin();
307       break;
308     }
309     default:
310       NOTREACHED();
311   }
312 }
313
314 void ChromePluginPlaceholder::OnMenuClosed(int request_id) {
315   DCHECK_EQ(context_menu_request_id_, request_id);
316   context_menu_request_id_ = 0;
317 }
318
319 void ChromePluginPlaceholder::ShowContextMenu(const WebMouseEvent& event) {
320   if (context_menu_request_id_)
321     return;  // Don't allow nested context menu requests.
322
323   content::ContextMenuParams params;
324
325   content::MenuItem name_item;
326   name_item.label = title_;
327   params.custom_items.push_back(name_item);
328
329   content::MenuItem separator_item;
330   separator_item.type = content::MenuItem::SEPARATOR;
331   params.custom_items.push_back(separator_item);
332
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);
340   }
341
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);
347
348   params.x = event.windowX;
349   params.y = event.windowY;
350
351   context_menu_request_id_ = render_view()->ShowContextMenu(this, params);
352   g_last_active_menu = this;
353 }
354
355 void ChromePluginPlaceholder::BindWebFrame(WebKit::WebFrame* frame) {
356   plugins::PluginPlaceholder::BindWebFrame(frame);
357   BindCallback("openAboutPlugins",
358                base::Bind(&ChromePluginPlaceholder::OpenAboutPluginsCallback,
359                           base::Unretained(this)));
360 }