OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "content/renderer/browser_plugin/browser_plugin.h" |
| 6 |
| 7 #include "base/atomic_sequence_num.h" |
| 8 #include "base/id_map.h" |
| 9 #include "base/lazy_instance.h" |
| 10 #include "base/process.h" |
| 11 #include "base/string_number_conversions.h" |
| 12 #include "base/string_piece.h" |
| 13 #include "base/string_util.h" |
| 14 #include "base/values.h" |
| 15 #include "content/common/browser_plugin_messages.h" |
| 16 #include "content/public/renderer/render_view.h" |
| 17 #include "content/renderer/render_view_impl.h" |
| 18 #include "ipc/ipc_channel_handle.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPlugin.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| 22 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 23 #include "webkit/plugins/ppapi/ppapi_webplugin_impl.h" |
| 24 #include "webkit/plugins/webview_plugin.h" |
| 25 |
| 26 static base::StaticAtomicSequenceNumber g_next_id; |
| 27 |
| 28 // The global list of all Browser Plugin Placeholders within a process. |
| 29 base::LazyInstance<IDMap<BrowserPlugin> >::Leaky |
| 30 g_all_browser_plugins = LAZY_INSTANCE_INITIALIZER; |
| 31 |
| 32 using webkit::WebViewPlugin; |
| 33 using WebKit::WebPlugin; |
| 34 using WebKit::WebPluginContainer; |
| 35 |
| 36 const char* const kPluginPlaceholderDataURL = |
| 37 "about:blank"; |
| 38 |
| 39 // static |
| 40 WebKit::WebPlugin* BrowserPlugin::Create( |
| 41 RenderViewImpl* render_view, |
| 42 WebKit::WebFrame* frame, |
| 43 const WebKit::WebPluginParams& params) { |
| 44 // TODO(fsamuel): Figure out what the lifetime is of this class. |
| 45 // It seems we need to blow away this object once a WebPluginContainer is |
| 46 // gone. How do we detect it's gone? A WebKit change perhaps? |
| 47 BrowserPlugin* browser_plugin = new BrowserPlugin( |
| 48 render_view, frame, params, ""); |
| 49 return browser_plugin->plugin_placeholder(); |
| 50 } |
| 51 |
| 52 // static |
| 53 BrowserPlugin* BrowserPlugin::FromID(int id) { |
| 54 return g_all_browser_plugins.Get().Lookup(id); |
| 55 } |
| 56 |
| 57 BrowserPlugin::BrowserPlugin( |
| 58 RenderViewImpl* render_view, |
| 59 WebKit::WebFrame* frame, |
| 60 const WebKit::WebPluginParams& params, |
| 61 const std::string& html_data) |
| 62 : render_view_(render_view), |
| 63 plugin_params_(params), |
| 64 placeholder_(webkit::WebViewPlugin::Create( |
| 65 NULL, render_view->GetWebkitPreferences(), html_data, |
| 66 GURL(kPluginPlaceholderDataURL))), |
| 67 plugin_(NULL) { |
| 68 id_ = g_next_id.GetNext(); |
| 69 Register(GetID(), this); |
| 70 |
| 71 // By default we navigate to google.com |
| 72 GetPluginParameters(0, 0, ""); |
| 73 |
| 74 if (!src_.empty()) |
| 75 render_view->Send(new BrowserPluginHostMsg_NavigateFromEmbedder( |
| 76 render_view->GetRoutingID(), |
| 77 GetID(), |
| 78 frame->identifier(), |
| 79 src_, |
| 80 size_)); |
| 81 } |
| 82 |
| 83 BrowserPlugin::~BrowserPlugin() { |
| 84 Unregister(GetID()); |
| 85 } |
| 86 |
| 87 // static |
| 88 void BrowserPlugin::Register(int id, BrowserPlugin* browser_plugin) { |
| 89 g_all_browser_plugins.Get().AddWithID(browser_plugin, id); |
| 90 } |
| 91 |
| 92 // static |
| 93 void BrowserPlugin::Unregister(int id) { |
| 94 if (g_all_browser_plugins.Get().Lookup(id)) |
| 95 g_all_browser_plugins.Get().Remove(id); |
| 96 } |
| 97 |
| 98 const WebKit::WebPluginParams& BrowserPlugin::plugin_params() const { |
| 99 return plugin_params_; |
| 100 } |
| 101 |
| 102 void BrowserPlugin::GetPluginParameters( |
| 103 int default_width, int default_height, |
| 104 const std::string& default_src) { |
| 105 int width = default_width; |
| 106 int height = default_height; |
| 107 |
| 108 // Get the plugin parameters from the attributes vector |
| 109 for (unsigned i = 0; i < plugin_params_.attributeNames.size(); ++i) { |
| 110 std::string attributeName = plugin_params_.attributeNames[i].utf8(); |
| 111 if (LowerCaseEqualsASCII(attributeName, "width")) { |
| 112 std::string attributeValue = plugin_params_.attributeValues[i].utf8(); |
| 113 CHECK(base::StringToInt(attributeValue, &width)); |
| 114 } else if (LowerCaseEqualsASCII(attributeName, "height")) { |
| 115 std::string attributeValue = plugin_params_.attributeValues[i].utf8(); |
| 116 CHECK(base::StringToInt(attributeValue, &height)); |
| 117 } else if (LowerCaseEqualsASCII(attributeName, "src")) { |
| 118 src_ = plugin_params_.attributeValues[i].utf8(); |
| 119 } |
| 120 } |
| 121 // If we didn't find the attributes set or they're not sensible, |
| 122 // we reset our attributes to the default. |
| 123 if (src_.empty()) |
| 124 src_ = default_src; |
| 125 |
| 126 size_.SetSize(width, height); |
| 127 } |
| 128 |
| 129 void BrowserPlugin::LoadGuest( |
| 130 int guest_process_id, |
| 131 const IPC::ChannelHandle& channel_handle) { |
| 132 webkit::ppapi::WebPluginImpl* new_guest = |
| 133 render_view()->CreateBrowserPlugin(channel_handle, |
| 134 guest_process_id, |
| 135 plugin_params()); |
| 136 Replace(new_guest); |
| 137 } |
| 138 |
| 139 void BrowserPlugin::Replace( |
| 140 webkit::ppapi::WebPluginImpl* new_plugin) { |
| 141 WebKit::WebPlugin* current_plugin = |
| 142 plugin_ ? static_cast<WebKit::WebPlugin*>(plugin_) : placeholder_; |
| 143 WebKit::WebPluginContainer* container = current_plugin->container(); |
| 144 if (!new_plugin || !new_plugin->initialize(container)) |
| 145 return; |
| 146 |
| 147 // Clear the container's backing texture ID. |
| 148 if (plugin_) |
| 149 plugin_->instance()->BindGraphics(plugin_->instance()->pp_instance(), 0); |
| 150 |
| 151 // Inform the browser process of the association between the browser plugin's |
| 152 // instance ID and the pepper channel's PP_Instance identifier. |
| 153 render_view()->Send(new BrowserPluginHostMsg_MapInstance( |
| 154 render_view()->GetRoutingID(), |
| 155 GetID(), |
| 156 new_plugin->instance()->pp_instance())); |
| 157 // TODO(fsamuel): We should delay the swapping out of the current plugin |
| 158 // until after the guest's WebGraphicsContext3D has been initialized. That |
| 159 // way, we immediately have something to render onto the screen. |
| 160 container->setPlugin(new_plugin); |
| 161 container->invalidate(); |
| 162 container->reportGeometry(); |
| 163 if (plugin_) |
| 164 plugin_->destroy(); |
| 165 plugin_ = new_plugin; |
| 166 } |
OLD | NEW |