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 navigate to google.com | |
80 GetPluginParameters(0, 0, ""); | |
81 | |
82 if (!src_.empty()) { | |
83 render_view->Send(new BrowserPluginHostMsg_NavigateFromEmbedder( | |
84 render_view->GetRoutingID(), | |
85 id_, | |
86 frame->identifier(), | |
87 src_, | |
88 size_)); | |
89 } | |
90 } | |
91 | |
92 BrowserPlugin::~BrowserPlugin() { | |
93 Unregister(id_); | |
94 } | |
95 | |
96 void BrowserPlugin::GetPluginParameters( | |
97 int default_width, int default_height, const std::string& default_src) { | |
Charlie Reis
2012/05/17 20:36:47
These parameters need to each be on their own line
Fady Samuel
2012/05/17 22:18:03
Done.
| |
98 int width = default_width; | |
99 int height = default_height; | |
100 | |
101 // Get the plugin parameters from the attributes vector | |
102 for (unsigned i = 0; i < plugin_params_.attributeNames.size(); ++i) { | |
103 std::string attributeName = plugin_params_.attributeNames[i].utf8(); | |
104 if (LowerCaseEqualsASCII(attributeName, "width")) { | |
105 std::string attributeValue = plugin_params_.attributeValues[i].utf8(); | |
106 CHECK(base::StringToInt(attributeValue, &width)); | |
107 } else if (LowerCaseEqualsASCII(attributeName, "height")) { | |
108 std::string attributeValue = plugin_params_.attributeValues[i].utf8(); | |
109 CHECK(base::StringToInt(attributeValue, &height)); | |
110 } else if (LowerCaseEqualsASCII(attributeName, "src")) { | |
111 src_ = plugin_params_.attributeValues[i].utf8(); | |
112 } | |
113 } | |
114 // If we didn't find the attributes set or they're not sensible, | |
115 // we reset our attributes to the default. | |
116 if (src_.empty()) | |
117 src_ = default_src; | |
118 | |
119 size_.SetSize(width, height); | |
120 } | |
121 | |
122 void BrowserPlugin::LoadGuest( | |
123 int guest_process_id, | |
124 const IPC::ChannelHandle& channel_handle) { | |
125 webkit::ppapi::WebPluginImpl* new_guest = | |
126 render_view()->CreateBrowserPlugin(channel_handle, | |
127 guest_process_id, | |
128 plugin_params()); | |
129 Replace(new_guest); | |
130 } | |
131 | |
132 void BrowserPlugin::Replace( | |
133 webkit::ppapi::WebPluginImpl* new_plugin) { | |
134 WebKit::WebPlugin* current_plugin = | |
135 plugin_ ? static_cast<WebKit::WebPlugin*>(plugin_) : placeholder_; | |
136 WebKit::WebPluginContainer* container = current_plugin->container(); | |
137 if (!new_plugin || !new_plugin->initialize(container)) | |
138 return; | |
139 | |
140 // Clear the container's backing texture ID and the script objects. | |
141 if (plugin_) | |
142 plugin_->instance()->BindGraphics(plugin_->instance()->pp_instance(), 0); | |
143 | |
144 // Inform the browser process of the association between the browser plugin's | |
145 // instance ID and the pepper channel's PP_Instance identifier. | |
146 render_view()->Send(new BrowserPluginHostMsg_MapInstance( | |
147 render_view()->GetRoutingID(), | |
148 id_, | |
149 new_plugin->instance()->pp_instance())); | |
150 // TODO(fsamuel): We should delay the swapping out of the current plugin | |
151 // until after the guest's WebGraphicsContext3D has been initialized. That | |
152 // way, we immediately have something to render onto the screen. | |
153 container->setPlugin(new_plugin); | |
154 container->invalidate(); | |
155 container->reportGeometry(); | |
156 if (plugin_) | |
157 plugin_->destroy(); | |
158 plugin_ = new_plugin; | |
159 } | |
OLD | NEW |