Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/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_); | |
|
Bernhard Bauer
2013/10/15 19:43:21
Nit: newline afterwards for better readability?
aberent
2013/10/16 09:47:53
Done.
| |
| 73 #if defined(ENABLE_PLUGIN_INSTALLATION) | |
| 74 if (placeholder_routing_id_ == MSG_ROUTING_NONE) | |
| 75 return; | |
| 76 RenderThread::Get()->RemoveRoute(placeholder_routing_id_); | |
| 77 if (has_host_) { | |
| 78 RenderThread::Get()->Send(new ChromeViewHostMsg_RemovePluginPlaceholderHost( | |
| 79 routing_id(), placeholder_routing_id_)); | |
| 80 } | |
| 81 #endif | |
| 82 } | |
| 83 | |
| 84 // static | |
| 85 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateMissingPlugin( | |
| 86 RenderView* render_view, | |
| 87 WebFrame* frame, | |
| 88 const WebPluginParams& params) { | |
| 89 const base::StringPiece template_html( | |
| 90 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 91 IDR_BLOCKED_PLUGIN_HTML)); | |
| 92 | |
| 93 base::DictionaryValue values; | |
| 94 #if defined(ENABLE_PLUGIN_INSTALLATION) | |
| 95 values.SetString("message", l10n_util::GetStringUTF8(IDS_PLUGIN_SEARCHING)); | |
| 96 #else | |
| 97 values.SetString("message", | |
| 98 l10n_util::GetStringUTF8(IDS_PLUGIN_NOT_SUPPORTED)); | |
| 99 #endif | |
| 100 | |
| 101 std::string html_data = webui::GetI18nTemplateHtml(template_html, &values); | |
| 102 | |
| 103 // |missing_plugin| will destroy itself when its WebViewPlugin is going away. | |
| 104 ChromePluginPlaceholder* missing_plugin = new ChromePluginPlaceholder( | |
| 105 render_view, frame, params, html_data, params.mimeType); | |
| 106 missing_plugin->set_allow_loading(true); | |
| 107 #if defined(ENABLE_PLUGIN_INSTALLATION) | |
| 108 RenderThread::Get()->Send( | |
| 109 new ChromeViewHostMsg_FindMissingPlugin(missing_plugin->routing_id(), | |
| 110 missing_plugin->CreateRoutingId(), | |
| 111 params.mimeType.utf8())); | |
| 112 #endif | |
| 113 return missing_plugin; | |
| 114 } | |
| 115 | |
| 116 // static | |
| 117 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateErrorPlugin( | |
| 118 RenderView* render_view, | |
| 119 const base::FilePath& file_path) { | |
| 120 base::DictionaryValue values; | |
| 121 values.SetString("message", | |
| 122 l10n_util::GetStringUTF8(IDS_PLUGIN_INITIALIZATION_ERROR)); | |
| 123 | |
| 124 const base::StringPiece template_html( | |
| 125 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 126 IDR_BLOCKED_PLUGIN_HTML)); | |
| 127 std::string html_data = webui::GetI18nTemplateHtml(template_html, &values); | |
| 128 | |
| 129 WebPluginParams params; | |
| 130 // |missing_plugin| will destroy itself when its WebViewPlugin is going away. | |
| 131 ChromePluginPlaceholder* plugin = new ChromePluginPlaceholder( | |
| 132 render_view, NULL, params, html_data, params.mimeType); | |
| 133 | |
| 134 RenderThread::Get()->Send(new ChromeViewHostMsg_CouldNotLoadPlugin( | |
| 135 plugin->routing_id(), file_path)); | |
| 136 return plugin; | |
| 137 } | |
| 138 | |
| 139 // static | |
| 140 ChromePluginPlaceholder* ChromePluginPlaceholder::CreateBlockedPlugin( | |
| 141 RenderView* render_view, | |
| 142 WebFrame* frame, | |
| 143 const WebPluginParams& params, | |
| 144 const content::WebPluginInfo& plugin, | |
| 145 const std::string& identifier, | |
| 146 const string16& name, | |
| 147 int template_id, | |
| 148 const string16& message) { | |
| 149 base::DictionaryValue values; | |
| 150 values.SetString("message", message); | |
| 151 values.SetString("name", name); | |
| 152 values.SetString("hide", l10n_util::GetStringUTF8(IDS_PLUGIN_HIDE)); | |
| 153 | |
| 154 const base::StringPiece template_html( | |
| 155 ResourceBundle::GetSharedInstance().GetRawDataResource(template_id)); | |
| 156 | |
| 157 DCHECK(!template_html.empty()) << "unable to load template. ID: " | |
| 158 << template_id; | |
| 159 std::string html_data = webui::GetI18nTemplateHtml(template_html, &values); | |
| 160 | |
| 161 // |blocked_plugin| will destroy itself when its WebViewPlugin is going away. | |
| 162 ChromePluginPlaceholder* blocked_plugin = | |
| 163 new ChromePluginPlaceholder(render_view, frame, params, html_data, name); | |
| 164 blocked_plugin->SetPluginInfo(plugin); | |
| 165 blocked_plugin->SetIdentifier(identifier); | |
| 166 return blocked_plugin; | |
| 167 } | |
| 168 | |
| 169 void ChromePluginPlaceholder::SetStatus( | |
| 170 const ChromeViewHostMsg_GetPluginInfo_Status& status) { | |
| 171 status_->value = status.value; | |
| 172 } | |
| 173 | |
| 174 #if defined(ENABLE_PLUGIN_INSTALLATION) | |
| 175 int32 ChromePluginPlaceholder::CreateRoutingId() { | |
| 176 placeholder_routing_id_ = RenderThread::Get()->GenerateRoutingID(); | |
| 177 RenderThread::Get()->AddRoute(placeholder_routing_id_, this); | |
| 178 return placeholder_routing_id_; | |
| 179 } | |
| 180 #endif | |
| 181 | |
| 182 bool ChromePluginPlaceholder::OnMessageReceived(const IPC::Message& message) { | |
| 183 #if defined(ENABLE_PLUGIN_INSTALLATION) | |
| 184 bool handled = true; | |
| 185 IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder, message) | |
| 186 IPC_MESSAGE_HANDLER(ChromeViewMsg_FoundMissingPlugin, OnFoundMissingPlugin) | |
| 187 IPC_MESSAGE_HANDLER(ChromeViewMsg_DidNotFindMissingPlugin, | |
| 188 OnDidNotFindMissingPlugin) | |
| 189 IPC_MESSAGE_HANDLER(ChromeViewMsg_StartedDownloadingPlugin, | |
| 190 OnStartedDownloadingPlugin) | |
| 191 IPC_MESSAGE_HANDLER(ChromeViewMsg_FinishedDownloadingPlugin, | |
| 192 OnFinishedDownloadingPlugin) | |
| 193 IPC_MESSAGE_HANDLER(ChromeViewMsg_ErrorDownloadingPlugin, | |
| 194 OnErrorDownloadingPlugin) | |
| 195 IPC_MESSAGE_HANDLER(ChromeViewMsg_CancelledDownloadingPlugin, | |
| 196 OnCancelledDownloadingPlugin) | |
| 197 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 198 IPC_END_MESSAGE_MAP() | |
| 199 | |
| 200 if (handled) | |
| 201 return true; | |
| 202 #endif | |
| 203 | |
| 204 // We don't swallow these messages because multiple blocked plugins have an | |
| 205 // interest in them. | |
| 206 IPC_BEGIN_MESSAGE_MAP(ChromePluginPlaceholder, message) | |
| 207 IPC_MESSAGE_HANDLER(ChromeViewMsg_LoadBlockedPlugins, OnLoadBlockedPlugins) | |
| 208 IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering, OnSetIsPrerendering) | |
| 209 IPC_END_MESSAGE_MAP() | |
| 210 | |
| 211 return false; | |
| 212 } | |
| 213 | |
| 214 void ChromePluginPlaceholder::OnLoadBlockedPlugins( | |
| 215 const std::string& identifier) { | |
| 216 plugins::PluginPlaceholder::OnLoadBlockedPlugins(identifier); | |
| 217 } | |
| 218 | |
| 219 void ChromePluginPlaceholder::OpenAboutPluginsCallback( | |
| 220 const CppArgumentList& args, | |
| 221 CppVariant* result) { | |
| 222 RenderThread::Get()->Send( | |
| 223 new ChromeViewHostMsg_OpenAboutPlugins(routing_id())); | |
| 224 } | |
| 225 | |
| 226 void ChromePluginPlaceholder::OnSetIsPrerendering(bool is_prerendering) { | |
| 227 plugins::PluginPlaceholder::OnSetIsPrerendering(is_prerendering); | |
| 228 } | |
| 229 | |
| 230 #if defined(ENABLE_PLUGIN_INSTALLATION) | |
| 231 void ChromePluginPlaceholder::OnDidNotFindMissingPlugin() { | |
| 232 SetMessage(l10n_util::GetStringUTF16(IDS_PLUGIN_NOT_FOUND)); | |
| 233 } | |
| 234 | |
| 235 void ChromePluginPlaceholder::OnFoundMissingPlugin( | |
| 236 const string16& plugin_name) { | |
| 237 if (status_->value == ChromeViewHostMsg_GetPluginInfo_Status::kNotFound) | |
| 238 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_FOUND, plugin_name)); | |
| 239 has_host_ = true; | |
| 240 plugin_name_ = plugin_name; | |
| 241 } | |
| 242 | |
| 243 void ChromePluginPlaceholder::OnStartedDownloadingPlugin() { | |
| 244 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING, plugin_name_)); | |
| 245 } | |
| 246 | |
| 247 void ChromePluginPlaceholder::OnFinishedDownloadingPlugin() { | |
| 248 bool is_installing = | |
| 249 status_->value == ChromeViewHostMsg_GetPluginInfo_Status::kNotFound; | |
| 250 SetMessage(l10n_util::GetStringFUTF16( | |
| 251 is_installing ? IDS_PLUGIN_INSTALLING : IDS_PLUGIN_UPDATING, | |
| 252 plugin_name_)); | |
| 253 } | |
| 254 | |
| 255 void ChromePluginPlaceholder::OnErrorDownloadingPlugin( | |
| 256 const std::string& error) { | |
| 257 SetMessage(l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_ERROR, | |
| 258 UTF8ToUTF16(error))); | |
| 259 } | |
| 260 | |
| 261 void ChromePluginPlaceholder::OnCancelledDownloadingPlugin() { | |
| 262 SetMessage( | |
| 263 l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOAD_CANCELLED, plugin_name_)); | |
| 264 } | |
| 265 #endif // defined(ENABLE_PLUGIN_INSTALLATION) | |
| 266 | |
| 267 void ChromePluginPlaceholder::PluginListChanged() { | |
| 268 if (!GetFrame()) | |
| 269 return; | |
| 270 WebDocument document = GetFrame()->top()->document(); | |
| 271 if (document.isNull()) | |
| 272 return; | |
| 273 | |
| 274 ChromeViewHostMsg_GetPluginInfo_Output output; | |
| 275 std::string mime_type(GetPluginParams().mimeType.utf8()); | |
| 276 render_view()->Send( | |
| 277 new ChromeViewHostMsg_GetPluginInfo(routing_id(), | |
| 278 GURL(GetPluginParams().url), | |
| 279 document.url(), | |
| 280 mime_type, | |
| 281 &output)); | |
| 282 if (output.status.value == status_->value) | |
| 283 return; | |
| 284 WebPlugin* new_plugin = chrome::ChromeContentRendererClient::CreatePlugin( | |
| 285 render_view(), GetFrame(), GetPluginParams(), output); | |
| 286 ReplacePlugin(new_plugin); | |
| 287 if (!new_plugin) { | |
| 288 PluginUMAReporter::GetInstance()->ReportPluginMissing( | |
| 289 GetPluginParams().mimeType.utf8(), GURL(GetPluginParams().url)); | |
| 290 } | |
| 291 } | |
| 292 | |
| 293 void ChromePluginPlaceholder::OnMenuAction(int request_id, unsigned action) { | |
| 294 DCHECK_EQ(context_menu_request_id_, request_id); | |
| 295 if (g_last_active_menu != this) | |
| 296 return; | |
| 297 switch (action) { | |
| 298 case chrome::MENU_COMMAND_PLUGIN_RUN: { | |
| 299 RenderThread::Get()->RecordUserMetrics("Plugin_Load_Menu"); | |
| 300 LoadPlugin(); | |
| 301 break; | |
| 302 } | |
| 303 case chrome::MENU_COMMAND_PLUGIN_HIDE: { | |
| 304 RenderThread::Get()->RecordUserMetrics("Plugin_Hide_Menu"); | |
| 305 HidePlugin(); | |
| 306 break; | |
| 307 } | |
| 308 default: | |
| 309 NOTREACHED(); | |
| 310 } | |
| 311 } | |
| 312 | |
| 313 void ChromePluginPlaceholder::OnMenuClosed(int request_id) { | |
| 314 DCHECK_EQ(context_menu_request_id_, request_id); | |
| 315 context_menu_request_id_ = 0; | |
| 316 } | |
| 317 | |
| 318 void ChromePluginPlaceholder::ShowContextMenu(const WebMouseEvent& event) { | |
| 319 if (context_menu_request_id_) | |
| 320 return; // Don't allow nested context menu requests. | |
| 321 | |
| 322 content::ContextMenuParams params; | |
| 323 | |
| 324 content::MenuItem name_item; | |
| 325 name_item.label = title_; | |
| 326 params.custom_items.push_back(name_item); | |
| 327 | |
| 328 content::MenuItem separator_item; | |
| 329 separator_item.type = content::MenuItem::SEPARATOR; | |
| 330 params.custom_items.push_back(separator_item); | |
| 331 | |
| 332 if (!GetPluginInfo().path.value().empty()) { | |
| 333 content::MenuItem run_item; | |
| 334 run_item.action = chrome::MENU_COMMAND_PLUGIN_RUN; | |
| 335 // Disable this menu item if the plugin is blocked by policy. | |
| 336 run_item.enabled = LoadingAllowed(); | |
| 337 run_item.label = l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_PLUGIN_RUN); | |
| 338 params.custom_items.push_back(run_item); | |
| 339 } | |
| 340 | |
| 341 content::MenuItem hide_item; | |
| 342 hide_item.action = chrome::MENU_COMMAND_PLUGIN_HIDE; | |
| 343 hide_item.enabled = true; | |
| 344 hide_item.label = l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_PLUGIN_HIDE); | |
| 345 params.custom_items.push_back(hide_item); | |
| 346 | |
| 347 params.x = event.windowX; | |
| 348 params.y = event.windowY; | |
| 349 | |
| 350 context_menu_request_id_ = render_view()->ShowContextMenu(this, params); | |
| 351 g_last_active_menu = this; | |
| 352 } | |
| 353 | |
| 354 void ChromePluginPlaceholder::BindWebFrame(WebKit::WebFrame* frame) { | |
| 355 plugins::PluginPlaceholder::BindWebFrame(frame); | |
| 356 BindCallback("openAboutPlugins", | |
| 357 base::Bind(&ChromePluginPlaceholder::OpenAboutPluginsCallback, | |
| 358 base::Unretained(this))); | |
| 359 } | |
| OLD | NEW |