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