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/old/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/old_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 namespace content { | |
28 namespace old { | |
29 | |
30 static int g_next_id = 0; | |
31 | |
32 // The global list of all Browser Plugin Placeholders within a process. | |
33 base::LazyInstance<IDMap<BrowserPlugin> >::Leaky | |
34 g_all_browser_plugins = LAZY_INSTANCE_INITIALIZER; | |
35 | |
36 using WebKit::WebPlugin; | |
37 using WebKit::WebPluginContainer; | |
38 using webkit::WebViewPlugin; | |
39 | |
40 void Register(int id, BrowserPlugin* browser_plugin) { | |
41 g_all_browser_plugins.Get().AddWithID(browser_plugin, id); | |
42 } | |
43 | |
44 void Unregister(int id) { | |
45 if (g_all_browser_plugins.Get().Lookup(id)) | |
46 g_all_browser_plugins.Get().Remove(id); | |
47 } | |
48 | |
49 // static | |
50 WebKit::WebPlugin* BrowserPlugin::Create( | |
51 RenderViewImpl* render_view, | |
52 WebKit::WebFrame* frame, | |
53 const WebKit::WebPluginParams& params) { | |
54 // TODO(fsamuel): Figure out what the lifetime is of this class. | |
55 // It seems we need to blow away this object once a WebPluginContainer is | |
56 // gone. How do we detect it's gone? A WebKit change perhaps? | |
57 BrowserPlugin* browser_plugin = new BrowserPlugin( | |
58 render_view, frame, params, ""); | |
59 return browser_plugin->placeholder(); | |
60 } | |
61 | |
62 // static | |
63 BrowserPlugin* BrowserPlugin::FromID(int id) { | |
64 return g_all_browser_plugins.Get().Lookup(id); | |
65 } | |
66 | |
67 BrowserPlugin::BrowserPlugin( | |
68 RenderViewImpl* render_view, | |
69 WebKit::WebFrame* frame, | |
70 const WebKit::WebPluginParams& params, | |
71 const std::string& html_data) | |
72 : render_view_(render_view), | |
73 plugin_params_(params), | |
74 placeholder_(webkit::WebViewPlugin::Create( | |
75 NULL, | |
76 render_view->GetWebkitPreferences(), | |
77 html_data, | |
78 GURL(chrome::kAboutBlankURL))), | |
79 plugin_(NULL) { | |
80 id_ = ++g_next_id; | |
81 Register(id_, this); | |
82 | |
83 // By default we do not navigate and simply stay with an | |
84 // about:blank placeholder. | |
85 std::string src; | |
86 ParseSrcAttribute("", &src); | |
87 | |
88 if (!src.empty()) { | |
89 render_view->Send(new OldBrowserPluginHostMsg_NavigateFromEmbedder( | |
90 render_view->GetRoutingID(), | |
91 id_, | |
92 frame->identifier(), | |
93 src)); | |
94 } | |
95 } | |
96 | |
97 BrowserPlugin::~BrowserPlugin() { | |
98 Unregister(id_); | |
99 } | |
100 | |
101 void BrowserPlugin::ParseSrcAttribute( | |
102 const std::string& default_src, | |
103 std::string* src) { | |
104 // Get the src attribute from the attributes vector | |
105 for (unsigned i = 0; i < plugin_params_.attributeNames.size(); ++i) { | |
106 std::string attributeName = plugin_params_.attributeNames[i].utf8(); | |
107 if (LowerCaseEqualsASCII(attributeName, "src")) { | |
108 *src = plugin_params_.attributeValues[i].utf8(); | |
109 break; | |
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 (src->empty()) { | |
115 *src = default_src; | |
116 } | |
117 } | |
118 | |
119 void BrowserPlugin::LoadGuest( | |
120 int guest_process_id, | |
121 const IPC::ChannelHandle& channel_handle) { | |
122 webkit::ppapi::WebPluginImpl* new_guest = | |
123 render_view()->CreateBrowserPlugin(channel_handle, | |
124 guest_process_id, | |
125 plugin_params()); | |
126 Replace(new_guest); | |
127 } | |
128 | |
129 void BrowserPlugin::AdvanceFocus(bool reverse) { | |
130 render_view()->GetWebView()->advanceFocus(reverse); | |
131 } | |
132 | |
133 void BrowserPlugin::Replace( | |
134 webkit::ppapi::WebPluginImpl* new_plugin) { | |
135 WebKit::WebPlugin* current_plugin = | |
136 plugin_ ? static_cast<WebKit::WebPlugin*>(plugin_) : placeholder_; | |
137 WebKit::WebPluginContainer* container = current_plugin->container(); | |
138 if (!new_plugin || !new_plugin->initialize(container)) | |
139 return; | |
140 | |
141 // Clear the container's backing texture ID. | |
142 if (plugin_) | |
143 plugin_->instance()->BindGraphics(plugin_->instance()->pp_instance(), 0); | |
144 | |
145 PP_Instance instance = new_plugin->instance()->pp_instance(); | |
146 ppapi::proxy::HostDispatcher* dispatcher = | |
147 ppapi::proxy::HostDispatcher::GetForInstance(instance); | |
148 dispatcher->Send(new OldBrowserPluginMsg_GuestReady(instance, id_)); | |
149 | |
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 } | |
160 | |
161 } // namespace old | |
162 } // namespace content | |
OLD | NEW |