| 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/common/url_constants.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 int g_next_id = 0; | 
|  | 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::WebPlugin; | 
|  | 33 using WebKit::WebPluginContainer; | 
|  | 34 using webkit::WebViewPlugin; | 
|  | 35 | 
|  | 36 void Register(int id, BrowserPlugin* browser_plugin) { | 
|  | 37   g_all_browser_plugins.Get().AddWithID(browser_plugin, id); | 
|  | 38 } | 
|  | 39 | 
|  | 40 void Unregister(int id) { | 
|  | 41   if (g_all_browser_plugins.Get().Lookup(id)) | 
|  | 42     g_all_browser_plugins.Get().Remove(id); | 
|  | 43 } | 
|  | 44 | 
|  | 45 // static | 
|  | 46 WebKit::WebPlugin* BrowserPlugin::Create( | 
|  | 47     RenderViewImpl* render_view, | 
|  | 48     WebKit::WebFrame* frame, | 
|  | 49     const WebKit::WebPluginParams& params) { | 
|  | 50   // TODO(fsamuel): Figure out what the lifetime is of this class. | 
|  | 51   // It seems we need to blow away this object once a WebPluginContainer is | 
|  | 52   // gone. How do we detect it's gone? A WebKit change perhaps? | 
|  | 53   BrowserPlugin* browser_plugin = new BrowserPlugin( | 
|  | 54       render_view, frame, params, ""); | 
|  | 55   return browser_plugin->placeholder(); | 
|  | 56 } | 
|  | 57 | 
|  | 58 // static | 
|  | 59 BrowserPlugin* BrowserPlugin::FromID(int id) { | 
|  | 60   return g_all_browser_plugins.Get().Lookup(id); | 
|  | 61 } | 
|  | 62 | 
|  | 63 BrowserPlugin::BrowserPlugin( | 
|  | 64     RenderViewImpl* render_view, | 
|  | 65     WebKit::WebFrame* frame, | 
|  | 66     const WebKit::WebPluginParams& params, | 
|  | 67     const std::string& html_data) | 
|  | 68     : render_view_(render_view), | 
|  | 69       plugin_params_(params), | 
|  | 70       placeholder_(webkit::WebViewPlugin::Create( | 
|  | 71                        NULL, | 
|  | 72                        render_view->GetWebkitPreferences(), | 
|  | 73                        html_data, | 
|  | 74                        GURL(chrome::kAboutBlankURL))), | 
|  | 75       plugin_(NULL) { | 
|  | 76   id_ = ++g_next_id; | 
|  | 77   Register(id_, this); | 
|  | 78 | 
|  | 79   // By default we do not navigate and simply stay with an | 
|  | 80   // about:blank placeholder. | 
|  | 81   gfx::Size size; | 
|  | 82   std::string src; | 
|  | 83   ParsePluginParameters(0, 0, "", &size, &src); | 
|  | 84 | 
|  | 85   if (!src.empty()) { | 
|  | 86     render_view->Send(new BrowserPluginHostMsg_NavigateFromEmbedder( | 
|  | 87         render_view->GetRoutingID(), | 
|  | 88         id_, | 
|  | 89         frame->identifier(), | 
|  | 90         src, | 
|  | 91         size)); | 
|  | 92   } | 
|  | 93 } | 
|  | 94 | 
|  | 95 BrowserPlugin::~BrowserPlugin() { | 
|  | 96   Unregister(id_); | 
|  | 97 } | 
|  | 98 | 
|  | 99 void BrowserPlugin::ParsePluginParameters( | 
|  | 100     int default_width, | 
|  | 101     int default_height, | 
|  | 102     const std::string& default_src, | 
|  | 103     gfx::Size* size, | 
|  | 104     std::string* 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 | 
|  | 127   size->SetSize(width, height); | 
|  | 128 } | 
|  | 129 | 
|  | 130 void BrowserPlugin::LoadGuest( | 
|  | 131     int guest_process_id, | 
|  | 132     const IPC::ChannelHandle& channel_handle) { | 
|  | 133   webkit::ppapi::WebPluginImpl* new_guest = | 
|  | 134       render_view()->CreateBrowserPlugin(channel_handle, | 
|  | 135                                          guest_process_id, | 
|  | 136                                          plugin_params()); | 
|  | 137   Replace(new_guest); | 
|  | 138 } | 
|  | 139 | 
|  | 140 void BrowserPlugin::Replace( | 
|  | 141     webkit::ppapi::WebPluginImpl* new_plugin) { | 
|  | 142   WebKit::WebPlugin* current_plugin = | 
|  | 143       plugin_ ? static_cast<WebKit::WebPlugin*>(plugin_) : placeholder_; | 
|  | 144   WebKit::WebPluginContainer* container = current_plugin->container(); | 
|  | 145   if (!new_plugin || !new_plugin->initialize(container)) | 
|  | 146     return; | 
|  | 147 | 
|  | 148   // Clear the container's backing texture ID and the script objects. | 
|  | 149   if (plugin_) | 
|  | 150     plugin_->instance()->BindGraphics(plugin_->instance()->pp_instance(), 0); | 
|  | 151 | 
|  | 152   // Inform the browser process of the association between the browser plugin's | 
|  | 153   // instance ID and the pepper channel's PP_Instance identifier. | 
|  | 154   render_view()->Send(new BrowserPluginHostMsg_MapInstance( | 
|  | 155       render_view()->GetRoutingID(), | 
|  | 156       id_, | 
|  | 157       new_plugin->instance()->pp_instance())); | 
|  | 158   // TODO(fsamuel): We should delay the swapping out of the current plugin | 
|  | 159   // until after the guest's WebGraphicsContext3D has been initialized. That | 
|  | 160   // way, we immediately have something to render onto the screen. | 
|  | 161   container->setPlugin(new_plugin); | 
|  | 162   container->invalidate(); | 
|  | 163   container->reportGeometry(); | 
|  | 164   if (plugin_) | 
|  | 165     plugin_->destroy(); | 
|  | 166   plugin_ = new_plugin; | 
|  | 167 } | 
| OLD | NEW | 
|---|