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/plugins/browser_plugin_placeholder.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/view_messages.h" |
| 16 #include "content/public/renderer/render_view.h" |
| 17 #include "ipc/ipc_channel_handle.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPlugin.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| 21 #include "webkit/plugins/webview_plugin.h" |
| 22 |
| 23 static base::AtomicSequenceNumber g_next_id(base::LINKER_INITIALIZED); |
| 24 // the global list of all renderer processes |
| 25 base::LazyInstance<IDMap<BrowserPluginPlaceholder> >::Leaky |
| 26 g_all_placeholders = LAZY_INSTANCE_INITIALIZER; |
| 27 |
| 28 |
| 29 using webkit::WebViewPlugin; |
| 30 using WebKit::WebPlugin; |
| 31 using WebKit::WebPluginContainer; |
| 32 |
| 33 const char* const kPluginPlaceholderDataURL = |
| 34 "about:blank"; |
| 35 |
| 36 // static |
| 37 webkit::WebViewPlugin* BrowserPluginPlaceholder::Create( |
| 38 content::RenderView* render_view, |
| 39 WebKit::WebFrame* frame, |
| 40 const WebKit::WebPluginParams& params) { |
| 41 // TODO(fsamuel): Need a better loading screen at some point. Maybe this can |
| 42 // be a part of the <browser> API? |
| 43 // |browser_plugin| will destroy itself when its WebViewPlugin is going away. |
| 44 BrowserPluginPlaceholder* browser_plugin = new BrowserPluginPlaceholder( |
| 45 render_view, frame, params, ""); |
| 46 return browser_plugin->plugin(); |
| 47 } |
| 48 |
| 49 // static |
| 50 BrowserPluginPlaceholder* BrowserPluginPlaceholder::FromID(int id) { |
| 51 return g_all_placeholders.Get().Lookup(id); |
| 52 } |
| 53 |
| 54 BrowserPluginPlaceholder::BrowserPluginPlaceholder( |
| 55 content::RenderView* render_view, |
| 56 WebKit::WebFrame* frame, |
| 57 const WebKit::WebPluginParams& params, |
| 58 const std::string& html_data) |
| 59 : render_view_(render_view), |
| 60 plugin_params_(params), |
| 61 plugin_(webkit::WebViewPlugin::Create( |
| 62 this, render_view->GetWebkitPreferences(), html_data, |
| 63 GURL(kPluginPlaceholderDataURL))), |
| 64 width_(0), |
| 65 height_(0) { |
| 66 id_ = g_next_id.GetNext(); |
| 67 RegisterPlaceholder(GetID(), this); |
| 68 |
| 69 // By default we navigate to google.com |
| 70 GetPluginParameters(0, 0, "http://www.google.com/"); |
| 71 |
| 72 render_view->Send(new ViewHostMsg_OpenChannelToBrowserPlugin( |
| 73 render_view->GetRoutingID(), GetID(), src_)); |
| 74 } |
| 75 |
| 76 BrowserPluginPlaceholder::~BrowserPluginPlaceholder() { |
| 77 UnregisterPlaceholder(GetID()); |
| 78 } |
| 79 |
| 80 void BrowserPluginPlaceholder::RegisterPlaceholder( |
| 81 int id, |
| 82 BrowserPluginPlaceholder* placeholder) { |
| 83 g_all_placeholders.Get().AddWithID(placeholder, id); |
| 84 } |
| 85 |
| 86 void BrowserPluginPlaceholder::UnregisterPlaceholder(int id) { |
| 87 if (g_all_placeholders.Get().Lookup(id)) |
| 88 g_all_placeholders.Get().Remove(id); |
| 89 } |
| 90 |
| 91 const WebKit::WebPluginParams& BrowserPluginPlaceholder::plugin_params() const { |
| 92 return plugin_params_; |
| 93 } |
| 94 |
| 95 void BrowserPluginPlaceholder::GetPluginParameters( |
| 96 int default_width, int default_height, |
| 97 const std::string& default_src) { |
| 98 |
| 99 // Get the plugin parameters from the attributes vector |
| 100 for (unsigned i = 0; i < plugin_params_.attributeNames.size(); ++i) { |
| 101 std::string attributeName = plugin_params_.attributeNames[i].utf8(); |
| 102 if (LowerCaseEqualsASCII(attributeName, "width")) { |
| 103 std::string attributeValue = plugin_params_.attributeValues[i].utf8(); |
| 104 CHECK(base::StringToInt(attributeValue, &width_)); |
| 105 } else if (LowerCaseEqualsASCII(attributeName, "height")) { |
| 106 std::string attributeValue = plugin_params_.attributeValues[i].utf8(); |
| 107 CHECK(base::StringToInt(attributeValue, &height_)); |
| 108 } else if (LowerCaseEqualsASCII(attributeName, "src")) { |
| 109 src_ = plugin_params_.attributeValues[i].utf8(); |
| 110 } |
| 111 } |
| 112 // If we didn't find the attributes set or they're not sensible, |
| 113 // we reset our attributes to the default. |
| 114 if (!width_) |
| 115 width_ = default_width; |
| 116 |
| 117 if (!height_) |
| 118 height_ = default_height; |
| 119 |
| 120 if (src_.empty()) |
| 121 src_ = default_src; |
| 122 } |
| 123 |
| 124 void BrowserPluginPlaceholder::GuestReady( |
| 125 base::ProcessHandle process_handle, |
| 126 const IPC::ChannelHandle& channel_handle) { |
| 127 WebKit::WebPlugin* new_plugin = |
| 128 render_view()->CreateBrowserPlugin(process_handle, |
| 129 channel_handle, |
| 130 plugin_params()); |
| 131 LoadGuest(new_plugin); |
| 132 } |
| 133 |
| 134 void BrowserPluginPlaceholder::LoadGuest(WebKit::WebPlugin* new_plugin) { |
| 135 CHECK(plugin_); |
| 136 WebKit::WebPluginContainer* container = plugin_->container(); |
| 137 if (!new_plugin || !new_plugin->initialize(container)) |
| 138 return; |
| 139 plugin_->RestoreTitleText(); |
| 140 container->setPlugin(new_plugin); |
| 141 container->invalidate(); |
| 142 container->reportGeometry(); |
| 143 plugin_->ReplayReceivedData(new_plugin); |
| 144 plugin_->destroy(); |
| 145 } |
| 146 |
| 147 void BrowserPluginPlaceholder::WillDestroyPlugin() { |
| 148 delete this; |
| 149 } |
OLD | NEW |