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 "ppapi/proxy/host_dispatcher.h" | |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPlugin.h" | |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | |
23 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
24 #include "webkit/plugins/ppapi/ppapi_webplugin_impl.h" | |
25 #include "webkit/plugins/webview_plugin.h" | |
26 | |
27 static int g_next_id = 0; | |
28 | |
29 // The global list of all Browser Plugin Placeholders within a process. | |
30 base::LazyInstance<IDMap<BrowserPlugin> >::Leaky | |
31 g_all_browser_plugins = LAZY_INSTANCE_INITIALIZER; | |
32 | |
33 using WebKit::WebPlugin; | |
34 using WebKit::WebPluginContainer; | |
35 using webkit::WebViewPlugin; | |
36 | |
37 void Register(int id, BrowserPlugin* browser_plugin) { | |
38 g_all_browser_plugins.Get().AddWithID(browser_plugin, id); | |
39 } | |
40 | |
41 void Unregister(int id) { | |
42 if (g_all_browser_plugins.Get().Lookup(id)) | |
43 g_all_browser_plugins.Get().Remove(id); | |
44 } | |
45 | |
46 // static | |
47 WebKit::WebPlugin* BrowserPlugin::Create( | |
48 RenderViewImpl* render_view, | |
49 WebKit::WebFrame* frame, | |
50 const WebKit::WebPluginParams& params) { | |
51 // TODO(fsamuel): Figure out what the lifetime is of this class. | |
52 // It seems we need to blow away this object once a WebPluginContainer is | |
53 // gone. How do we detect it's gone? A WebKit change perhaps? | |
54 BrowserPlugin* browser_plugin = new BrowserPlugin( | |
55 render_view, frame, params, ""); | |
56 return browser_plugin->placeholder(); | |
57 } | |
58 | |
59 // static | |
60 BrowserPlugin* BrowserPlugin::FromID(int id) { | |
61 return g_all_browser_plugins.Get().Lookup(id); | |
62 } | |
63 | |
64 BrowserPlugin::BrowserPlugin( | |
65 RenderViewImpl* render_view, | |
66 WebKit::WebFrame* frame, | |
67 const WebKit::WebPluginParams& params, | |
68 const std::string& html_data) | |
69 : render_view_(render_view), | |
70 plugin_params_(params), | |
71 placeholder_(webkit::WebViewPlugin::Create( | |
72 NULL, | |
73 render_view->GetWebkitPreferences(), | |
74 html_data, | |
75 GURL(chrome::kAboutBlankURL))), | |
76 plugin_(NULL) { | |
77 id_ = ++g_next_id; | |
78 Register(id_, this); | |
79 | |
80 // By default we do not navigate and simply stay with an | |
81 // about:blank placeholder. | |
82 std::string src; | |
83 ParseSrcAttribute("", &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 } | |
92 } | |
93 | |
94 BrowserPlugin::~BrowserPlugin() { | |
95 Unregister(id_); | |
96 } | |
97 | |
98 void BrowserPlugin::ParseSrcAttribute( | |
99 const std::string& default_src, | |
100 std::string* src) { | |
101 // Get the src attribute 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, "src")) { | |
105 *src = plugin_params_.attributeValues[i].utf8(); | |
106 break; | |
107 } | |
108 } | |
109 // If we didn't find the attributes set or they're not sensible, | |
110 // we reset our attributes to the default. | |
111 if (src->empty()) { | |
112 *src = default_src; | |
113 } | |
114 } | |
115 | |
116 void BrowserPlugin::LoadGuest( | |
117 int guest_process_id, | |
118 const IPC::ChannelHandle& channel_handle) { | |
119 webkit::ppapi::WebPluginImpl* new_guest = | |
120 render_view()->CreateBrowserPlugin(channel_handle, | |
121 guest_process_id, | |
122 plugin_params()); | |
123 Replace(new_guest); | |
124 } | |
125 | |
126 void BrowserPlugin::AdvanceFocus(bool reverse) { | |
127 // TODO(fsamuel): Uncomment this once http://wkbug.com/88827 lands. | |
128 // render_view()->GetWebView()->advanceFocus(reverse); | |
129 } | |
130 | |
131 void BrowserPlugin::Replace( | |
132 webkit::ppapi::WebPluginImpl* new_plugin) { | |
133 WebKit::WebPlugin* current_plugin = | |
134 plugin_ ? static_cast<WebKit::WebPlugin*>(plugin_) : placeholder_; | |
135 WebKit::WebPluginContainer* container = current_plugin->container(); | |
136 if (!new_plugin || !new_plugin->initialize(container)) | |
137 return; | |
138 | |
139 // Clear the container's backing texture ID. | |
140 if (plugin_) | |
141 plugin_->instance()->BindGraphics(plugin_->instance()->pp_instance(), 0); | |
142 | |
143 PP_Instance instance = new_plugin->instance()->pp_instance(); | |
144 ppapi::proxy::HostDispatcher* dispatcher = | |
145 ppapi::proxy::HostDispatcher::GetForInstance(instance); | |
146 dispatcher->Send(new BrowserPluginMsg_GuestReady(instance, id_)); | |
147 | |
148 // TODO(fsamuel): We should delay the swapping out of the current plugin | |
149 // until after the guest's WebGraphicsContext3D has been initialized. That | |
150 // way, we immediately have something to render onto the screen. | |
151 container->setPlugin(new_plugin); | |
152 container->invalidate(); | |
153 container->reportGeometry(); | |
154 if (plugin_) | |
155 plugin_->destroy(); | |
156 plugin_ = new_plugin; | |
157 } | |
OLD | NEW |