OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/plugins/renderer/plugin_placeholder.h" | 5 #include "components/plugins/renderer/plugin_placeholder.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/json/string_escape.h" | 9 #include "base/json/string_escape.h" |
10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "content/public/common/content_constants.h" | 14 #include "content/public/common/content_constants.h" |
15 #include "content/public/common/context_menu_params.h" | 15 #include "content/public/common/context_menu_params.h" |
16 #include "content/public/renderer/render_frame.h" | 16 #include "content/public/renderer/render_frame.h" |
17 #include "content/public/renderer/render_thread.h" | 17 #include "content/public/renderer/render_thread.h" |
18 #include "gin/object_template_builder.h" | |
18 #include "third_party/WebKit/public/web/WebDocument.h" | 19 #include "third_party/WebKit/public/web/WebDocument.h" |
19 #include "third_party/WebKit/public/web/WebElement.h" | 20 #include "third_party/WebKit/public/web/WebElement.h" |
20 #include "third_party/WebKit/public/web/WebFrame.h" | 21 #include "third_party/WebKit/public/web/WebFrame.h" |
21 #include "third_party/WebKit/public/web/WebInputEvent.h" | 22 #include "third_party/WebKit/public/web/WebInputEvent.h" |
22 #include "third_party/WebKit/public/web/WebPluginContainer.h" | 23 #include "third_party/WebKit/public/web/WebPluginContainer.h" |
23 #include "third_party/WebKit/public/web/WebScriptSource.h" | 24 #include "third_party/WebKit/public/web/WebScriptSource.h" |
24 #include "third_party/WebKit/public/web/WebView.h" | 25 #include "third_party/WebKit/public/web/WebView.h" |
25 #include "third_party/re2/re2/re2.h" | 26 #include "third_party/re2/re2/re2.h" |
26 | 27 |
27 using content::RenderThread; | 28 using content::RenderThread; |
28 using content::UserMetricsAction; | 29 using content::UserMetricsAction; |
29 using blink::WebElement; | 30 using blink::WebElement; |
30 using blink::WebFrame; | 31 using blink::WebFrame; |
31 using blink::WebMouseEvent; | 32 using blink::WebMouseEvent; |
32 using blink::WebNode; | 33 using blink::WebNode; |
33 using blink::WebPlugin; | 34 using blink::WebPlugin; |
34 using blink::WebPluginContainer; | 35 using blink::WebPluginContainer; |
35 using blink::WebPluginParams; | 36 using blink::WebPluginParams; |
36 using blink::WebScriptSource; | 37 using blink::WebScriptSource; |
37 using blink::WebURLRequest; | 38 using blink::WebURLRequest; |
38 using webkit_glue::CppArgumentList; | |
39 using webkit_glue::CppVariant; | |
40 | 39 |
41 namespace plugins { | 40 namespace plugins { |
42 | 41 |
42 gin::WrapperInfo PluginPlaceholder::kWrapperInfo = {gin::kEmbedderNativeGin}; | |
43 | |
43 PluginPlaceholder::PluginPlaceholder(content::RenderFrame* render_frame, | 44 PluginPlaceholder::PluginPlaceholder(content::RenderFrame* render_frame, |
44 WebFrame* frame, | 45 WebFrame* frame, |
45 const WebPluginParams& params, | 46 const WebPluginParams& params, |
46 const std::string& html_data, | 47 const std::string& html_data, |
47 GURL placeholderDataUrl) | 48 GURL placeholderDataUrl) |
48 : content::RenderFrameObserver(render_frame), | 49 : content::RenderFrameObserver(render_frame), |
49 frame_(frame), | 50 frame_(frame), |
50 plugin_params_(params), | 51 plugin_params_(params), |
51 plugin_(WebViewPlugin::Create(this, | 52 plugin_(WebViewPlugin::Create(this, |
52 render_frame->GetWebkitPreferences(), | 53 render_frame->GetWebkitPreferences(), |
53 html_data, | 54 html_data, |
54 placeholderDataUrl)), | 55 placeholderDataUrl)), |
55 is_blocked_for_prerendering_(false), | 56 is_blocked_for_prerendering_(false), |
56 allow_loading_(false), | 57 allow_loading_(false), |
57 hidden_(false), | 58 hidden_(false), |
58 finished_loading_(false) {} | 59 finished_loading_(false) {} |
59 | 60 |
60 PluginPlaceholder::~PluginPlaceholder() {} | 61 PluginPlaceholder::~PluginPlaceholder() {} |
61 | 62 |
62 void PluginPlaceholder::BindWebFrame(WebFrame* frame) { | 63 gin::ObjectTemplateBuilder PluginPlaceholder::GetObjectTemplateBuilder( |
63 BindToJavascript(frame, "plugin"); | 64 v8::Isolate* isolate) { |
64 BindCallback( | 65 return gin::Wrappable<PluginPlaceholder>::GetObjectTemplateBuilder(isolate) |
65 "load", | 66 .SetMethod("load", &PluginPlaceholder::LoadCallback) |
66 base::Bind(&PluginPlaceholder::LoadCallback, base::Unretained(this))); | 67 .SetMethod("hide", &PluginPlaceholder::HideCallback) |
67 BindCallback( | 68 .SetMethod("didFinishLoading", |
68 "hide", | 69 &PluginPlaceholder::DidFinishLoadingCallback); |
69 base::Bind(&PluginPlaceholder::HideCallback, base::Unretained(this))); | |
70 BindCallback("didFinishLoading", | |
71 base::Bind(&PluginPlaceholder::DidFinishLoadingCallback, | |
72 base::Unretained(this))); | |
73 } | 70 } |
74 | 71 |
75 void PluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) { | 72 void PluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) { |
76 CHECK(plugin_); | 73 CHECK(plugin_); |
77 if (!new_plugin) return; | 74 if (!new_plugin) return; |
78 WebPluginContainer* container = plugin_->container(); | 75 WebPluginContainer* container = plugin_->container(); |
79 // Set the new plug-in on the container before initializing it. | 76 // Set the new plug-in on the container before initializing it. |
80 container->setPlugin(new_plugin); | 77 container->setPlugin(new_plugin); |
81 // Save the element in case the plug-in is removed from the page during | 78 // Save the element in case the plug-in is removed from the page during |
82 // initialization. | 79 // initialization. |
83 WebElement element = container->element(); | 80 WebElement element = container->element(); |
84 if (!new_plugin->initialize(container)) { | 81 if (!new_plugin->initialize(container)) { |
85 // We couldn't initialize the new plug-in. Restore the old one and abort. | 82 // We couldn't initialize the new plug-in. Restore the old one and abort. |
86 container->setPlugin(plugin_); | 83 container->setPlugin(plugin_); |
87 return; | 84 return; |
88 } | 85 } |
89 | 86 |
90 // The plug-in has been removed from the page. Destroy the old plug-in | 87 // The plug-in has been removed from the page. Destroy the old plug-in |
91 // (which will destroy us). | 88 // (which will destroy us). |
Bernhard Bauer
2014/01/07 09:35:42
I think this comment needs updating now.
jochen (gone - plz use gerrit)
2014/01/08 19:18:23
Done.
| |
92 if (!element.pluginContainer()) { | 89 if (!element.pluginContainer()) { |
93 plugin_->destroy(); | 90 plugin_->destroy(); |
91 plugin_ = NULL; | |
94 return; | 92 return; |
95 } | 93 } |
96 | 94 |
97 // During initialization, the new plug-in might have replaced itself in turn | 95 // During initialization, the new plug-in might have replaced itself in turn |
98 // with another plug-in. Make sure not to use the passed in |new_plugin| after | 96 // with another plug-in. Make sure not to use the passed in |new_plugin| after |
99 // this point. | 97 // this point. |
100 new_plugin = container->plugin(); | 98 new_plugin = container->plugin(); |
101 | 99 |
102 plugin_->RestoreTitleText(); | 100 plugin_->RestoreTitleText(); |
103 container->invalidate(); | 101 container->invalidate(); |
104 container->reportGeometry(); | 102 container->reportGeometry(); |
105 plugin_->ReplayReceivedData(new_plugin); | 103 plugin_->ReplayReceivedData(new_plugin); |
106 plugin_->destroy(); | 104 plugin_->destroy(); |
105 plugin_ = NULL; | |
107 } | 106 } |
108 | 107 |
109 void PluginPlaceholder::HidePlugin() { | 108 void PluginPlaceholder::HidePlugin() { |
110 hidden_ = true; | 109 hidden_ = true; |
111 WebPluginContainer* container = plugin_->container(); | 110 WebPluginContainer* container = plugin_->container(); |
112 WebElement element = container->element(); | 111 WebElement element = container->element(); |
113 element.setAttribute("style", "display: none;"); | 112 element.setAttribute("style", "display: none;"); |
114 // If we have a width and height, search for a parent (often <div>) with the | 113 // If we have a width and height, search for a parent (often <div>) with the |
115 // same dimensions. If we find such a parent, hide that as well. | 114 // same dimensions. If we find such a parent, hide that as well. |
116 // This makes much more uncovered page content usable (including clickable) | 115 // This makes much more uncovered page content usable (including clickable) |
(...skipping 28 matching lines...) Expand all Loading... | |
145 if (element.hasAttribute("style")) { | 144 if (element.hasAttribute("style")) { |
146 std::string style_str = element.getAttribute("style").utf8(); | 145 std::string style_str = element.getAttribute("style").utf8(); |
147 if (RE2::PartialMatch(style_str, width_str) && | 146 if (RE2::PartialMatch(style_str, width_str) && |
148 RE2::PartialMatch(style_str, height_str)) | 147 RE2::PartialMatch(style_str, height_str)) |
149 element.setAttribute("style", "display: none;"); | 148 element.setAttribute("style", "display: none;"); |
150 } | 149 } |
151 } | 150 } |
152 } | 151 } |
153 } | 152 } |
154 | 153 |
155 void PluginPlaceholder::WillDestroyPlugin() { delete this; } | |
156 | |
157 void PluginPlaceholder::SetMessage(const base::string16& message) { | 154 void PluginPlaceholder::SetMessage(const base::string16& message) { |
158 message_ = message; | 155 message_ = message; |
159 if (finished_loading_) | 156 if (finished_loading_) |
160 UpdateMessage(); | 157 UpdateMessage(); |
161 } | 158 } |
162 | 159 |
163 void PluginPlaceholder::UpdateMessage() { | 160 void PluginPlaceholder::UpdateMessage() { |
164 std::string script = | 161 std::string script = |
165 "window.setMessage(" + base::GetQuotedJSONString(message_) + ")"; | 162 "window.setMessage(" + base::GetQuotedJSONString(message_) + ")"; |
166 plugin_->web_view()->mainFrame()->executeScript( | 163 plugin_->web_view()->mainFrame()->executeScript( |
167 WebScriptSource(ASCIIToUTF16(script))); | 164 WebScriptSource(ASCIIToUTF16(script))); |
168 } | 165 } |
169 | 166 |
170 void PluginPlaceholder::ShowContextMenu(const WebMouseEvent& event) { | 167 void PluginPlaceholder::ShowContextMenu(const WebMouseEvent& event) { |
171 // Does nothing by default. Will be overridden if a specific browser wants | 168 // Does nothing by default. Will be overridden if a specific browser wants |
172 // a context menu. | 169 // a context menu. |
173 return; | 170 return; |
174 } | 171 } |
175 | 172 |
173 void PluginPlaceholder::OnDestruct() { | |
174 frame_ = NULL; | |
175 } | |
176 | |
176 void PluginPlaceholder::OnLoadBlockedPlugins(const std::string& identifier) { | 177 void PluginPlaceholder::OnLoadBlockedPlugins(const std::string& identifier) { |
177 if (!identifier.empty() && identifier != identifier_) | 178 if (!identifier.empty() && identifier != identifier_) |
178 return; | 179 return; |
179 | 180 |
180 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_UI")); | 181 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_UI")); |
181 LoadPlugin(); | 182 LoadPlugin(); |
182 } | 183 } |
183 | 184 |
184 void PluginPlaceholder::OnSetIsPrerendering(bool is_prerendering) { | 185 void PluginPlaceholder::OnSetIsPrerendering(bool is_prerendering) { |
185 // Prerendering can only be enabled prior to a RenderView's first navigation, | 186 // Prerendering can only be enabled prior to a RenderView's first navigation, |
(...skipping 14 matching lines...) Expand all Loading... | |
200 } | 201 } |
201 | 202 |
202 // TODO(mmenke): In the case of prerendering, feed into | 203 // TODO(mmenke): In the case of prerendering, feed into |
203 // ChromeContentRendererClient::CreatePlugin instead, to | 204 // ChromeContentRendererClient::CreatePlugin instead, to |
204 // reduce the chance of future regressions. | 205 // reduce the chance of future regressions. |
205 WebPlugin* plugin = | 206 WebPlugin* plugin = |
206 render_frame()->CreatePlugin(frame_, plugin_info_, plugin_params_); | 207 render_frame()->CreatePlugin(frame_, plugin_info_, plugin_params_); |
207 ReplacePlugin(plugin); | 208 ReplacePlugin(plugin); |
208 } | 209 } |
209 | 210 |
210 void PluginPlaceholder::LoadCallback(const CppArgumentList& args, | 211 void PluginPlaceholder::LoadCallback() { |
211 CppVariant* result) { | |
212 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Click")); | 212 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Click")); |
213 LoadPlugin(); | 213 LoadPlugin(); |
214 } | 214 } |
215 | 215 |
216 void PluginPlaceholder::HideCallback(const CppArgumentList& args, | 216 void PluginPlaceholder::HideCallback() { |
217 CppVariant* result) { | |
218 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Click")); | 217 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Click")); |
219 HidePlugin(); | 218 HidePlugin(); |
220 } | 219 } |
221 | 220 |
222 void PluginPlaceholder::DidFinishLoadingCallback(const CppArgumentList& args, | 221 void PluginPlaceholder::DidFinishLoadingCallback() { |
223 CppVariant* result) { | |
224 finished_loading_ = true; | 222 finished_loading_ = true; |
225 if (message_.length() > 0) | 223 if (message_.length() > 0) |
226 UpdateMessage(); | 224 UpdateMessage(); |
227 } | 225 } |
228 | 226 |
229 void PluginPlaceholder::SetPluginInfo( | 227 void PluginPlaceholder::SetPluginInfo( |
230 const content::WebPluginInfo& plugin_info) { | 228 const content::WebPluginInfo& plugin_info) { |
231 plugin_info_ = plugin_info; | 229 plugin_info_ = plugin_info; |
232 } | 230 } |
233 | 231 |
234 const content::WebPluginInfo& PluginPlaceholder::GetPluginInfo() const { | 232 const content::WebPluginInfo& PluginPlaceholder::GetPluginInfo() const { |
235 return plugin_info_; | 233 return plugin_info_; |
236 } | 234 } |
237 | 235 |
238 void PluginPlaceholder::SetIdentifier(const std::string& identifier) { | 236 void PluginPlaceholder::SetIdentifier(const std::string& identifier) { |
239 identifier_ = identifier; | 237 identifier_ = identifier; |
240 } | 238 } |
241 | 239 |
242 blink::WebFrame* PluginPlaceholder::GetFrame() { return frame_; } | 240 blink::WebFrame* PluginPlaceholder::GetFrame() { return frame_; } |
243 | 241 |
244 const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const { | 242 const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const { |
245 return plugin_params_; | 243 return plugin_params_; |
246 } | 244 } |
247 | 245 |
248 } // namespace plugins | 246 } // namespace plugins |
OLD | NEW |