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/webview_plugin.h" | 5 #include "components/plugins/renderer/webview_plugin.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
13 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
16 #include "content/public/common/web_preferences.h" | 16 #include "content/public/common/web_preferences.h" |
17 #include "content/public/renderer/render_view.h" | 17 #include "content/public/renderer/render_view.h" |
18 #include "gin/converter.h" | 18 #include "gin/converter.h" |
19 #include "skia/ext/platform_canvas.h" | 19 #include "skia/ext/platform_canvas.h" |
20 #include "third_party/WebKit/public/platform/WebSize.h" | |
21 #include "third_party/WebKit/public/platform/WebURL.h" | 20 #include "third_party/WebKit/public/platform/WebURL.h" |
22 #include "third_party/WebKit/public/platform/WebURLRequest.h" | |
23 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 21 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
24 #include "third_party/WebKit/public/web/WebDocument.h" | 22 #include "third_party/WebKit/public/web/WebDocument.h" |
25 #include "third_party/WebKit/public/web/WebElement.h" | 23 #include "third_party/WebKit/public/web/WebElement.h" |
26 #include "third_party/WebKit/public/web/WebFrameWidget.h" | 24 #include "third_party/WebKit/public/web/WebFrameWidget.h" |
27 #include "third_party/WebKit/public/web/WebInputEvent.h" | 25 #include "third_party/WebKit/public/web/WebInputEvent.h" |
28 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 26 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
29 #include "third_party/WebKit/public/web/WebPluginContainer.h" | 27 #include "third_party/WebKit/public/web/WebPluginContainer.h" |
30 #include "third_party/WebKit/public/web/WebView.h" | 28 #include "third_party/WebKit/public/web/WebView.h" |
31 | 29 |
32 using blink::WebCanvas; | 30 using blink::WebCanvas; |
33 using blink::WebCursorInfo; | 31 using blink::WebCursorInfo; |
34 using blink::WebDragData; | 32 using blink::WebDragData; |
35 using blink::WebDragOperationsMask; | 33 using blink::WebDragOperationsMask; |
36 using blink::WebFrameWidget; | 34 using blink::WebFrameWidget; |
37 using blink::WebImage; | 35 using blink::WebImage; |
38 using blink::WebInputEvent; | 36 using blink::WebInputEvent; |
39 using blink::WebLocalFrame; | 37 using blink::WebLocalFrame; |
40 using blink::WebMouseEvent; | 38 using blink::WebMouseEvent; |
41 using blink::WebPlugin; | 39 using blink::WebPlugin; |
42 using blink::WebPluginContainer; | 40 using blink::WebPluginContainer; |
43 using blink::WebPoint; | 41 using blink::WebPoint; |
44 using blink::WebRect; | 42 using blink::WebRect; |
45 using blink::WebSize; | |
46 using blink::WebString; | 43 using blink::WebString; |
47 using blink::WebURLError; | 44 using blink::WebURLError; |
48 using blink::WebURLRequest; | |
49 using blink::WebURLResponse; | 45 using blink::WebURLResponse; |
50 using blink::WebVector; | 46 using blink::WebVector; |
51 using blink::WebView; | 47 using blink::WebView; |
52 using content::WebPreferences; | 48 using content::WebPreferences; |
53 | 49 |
54 WebViewPlugin::WebViewPlugin(content::RenderView* render_view, | 50 WebViewPlugin::WebViewPlugin(content::RenderView* render_view, |
55 WebViewPlugin::Delegate* delegate, | 51 WebViewPlugin::Delegate* delegate, |
56 const WebPreferences& preferences) | 52 const WebPreferences& preferences) |
57 : content::RenderViewObserver(render_view), | 53 : content::RenderViewObserver(render_view), |
58 delegate_(delegate), | 54 delegate_(delegate), |
59 container_(nullptr), | 55 container_(nullptr), |
60 web_view_(WebView::create(this, blink::WebPageVisibilityStateVisible)), | 56 web_view_(WebView::create(this, blink::WebPageVisibilityStateVisible)), |
61 finished_loading_(false), | |
62 focused_(false), | 57 focused_(false), |
63 is_painting_(false), | 58 is_painting_(false), |
64 is_resizing_(false), | 59 is_resizing_(false), |
| 60 web_frame_client_(this), |
65 weak_factory_(this) { | 61 weak_factory_(this) { |
66 // ApplyWebPreferences before making a WebLocalFrame so that the frame sees a | 62 // ApplyWebPreferences before making a WebLocalFrame so that the frame sees a |
67 // consistent view of our preferences. | 63 // consistent view of our preferences. |
68 content::RenderView::ApplyWebPreferences(preferences, web_view_); | 64 content::RenderView::ApplyWebPreferences(preferences, web_view_); |
69 WebLocalFrame* web_local_frame = | 65 WebLocalFrame* web_local_frame = WebLocalFrame::create( |
70 WebLocalFrame::create(blink::WebTreeScopeType::Document, this); | 66 blink::WebTreeScopeType::Document, &web_frame_client_); |
71 web_frame_ = web_local_frame; | 67 web_frame_ = web_local_frame; |
72 web_view_->setMainFrame(web_frame_); | 68 web_view_->setMainFrame(web_frame_); |
73 // TODO(dcheng): The main frame widget currently has a special case. | 69 // TODO(dcheng): The main frame widget currently has a special case. |
74 // Eliminate this once WebView is no longer a WebWidget. | 70 // Eliminate this once WebView is no longer a WebWidget. |
75 web_frame_widget_ = WebFrameWidget::create(this, web_view_, web_local_frame); | 71 web_frame_widget_ = WebFrameWidget::create(this, web_view_, web_local_frame); |
76 } | 72 } |
77 | 73 |
78 // static | 74 // static |
79 WebViewPlugin* WebViewPlugin::Create(content::RenderView* render_view, | 75 WebViewPlugin* WebViewPlugin::Create(content::RenderView* render_view, |
80 WebViewPlugin::Delegate* delegate, | 76 WebViewPlugin::Delegate* delegate, |
81 const WebPreferences& preferences, | 77 const WebPreferences& preferences, |
82 const std::string& html_data, | 78 const std::string& html_data, |
83 const GURL& url) { | 79 const GURL& url) { |
84 DCHECK(url.is_valid()) << "Blink requires the WebView to have a valid URL."; | 80 DCHECK(url.is_valid()) << "Blink requires the WebView to have a valid URL."; |
85 WebViewPlugin* plugin = new WebViewPlugin(render_view, delegate, preferences); | 81 WebViewPlugin* plugin = new WebViewPlugin(render_view, delegate, preferences); |
86 plugin->web_view()->mainFrame()->loadHTMLString(html_data, url); | 82 plugin->web_view()->mainFrame()->loadHTMLString(html_data, url); |
87 return plugin; | 83 return plugin; |
88 } | 84 } |
89 | 85 |
90 WebViewPlugin::~WebViewPlugin() { | 86 WebViewPlugin::~WebViewPlugin() { |
91 DCHECK(!weak_factory_.HasWeakPtrs()); | 87 DCHECK(!weak_factory_.HasWeakPtrs()); |
92 web_frame_widget_->close(); | 88 web_frame_widget_->close(); |
93 web_view_->close(); | 89 web_view_->close(); |
94 web_frame_->close(); | 90 web_frame_->close(); |
95 } | 91 } |
96 | 92 |
97 void WebViewPlugin::ReplayReceivedData(WebPlugin* plugin) { | |
98 // We need to transfer the |focused_| to new plugin after it loaded. | |
99 if (focused_) { | |
100 plugin->updateFocus(true, blink::WebFocusTypeNone); | |
101 } | |
102 if (finished_loading_) { | |
103 plugin->didFinishLoading(); | |
104 } | |
105 if (error_) { | |
106 plugin->didFailLoading(*error_); | |
107 } | |
108 } | |
109 | |
110 void WebViewPlugin::RestoreTitleText() { | |
111 if (container_) | |
112 container_->element().setAttribute("title", old_title_); | |
113 } | |
114 | |
115 WebPluginContainer* WebViewPlugin::container() const { return container_; } | 93 WebPluginContainer* WebViewPlugin::container() const { return container_; } |
116 | 94 |
117 bool WebViewPlugin::initialize(WebPluginContainer* container) { | 95 bool WebViewPlugin::initialize(WebPluginContainer* container) { |
118 DCHECK(container); | 96 DCHECK(container); |
119 DCHECK_EQ(this, container->plugin()); | 97 DCHECK_EQ(this, container->plugin()); |
120 container_ = container; | 98 container_ = container; |
121 | 99 |
122 // We must call layout again here to ensure that the container is laid | 100 // We must call layout again here to ensure that the container is laid |
123 // out before we next try to paint it, which is a requirement of the | 101 // out before we next try to paint it, which is a requirement of the |
124 // document life cycle in Blink. In most cases, needsLayout is set by | 102 // document life cycle in Blink. In most cases, needsLayout is set by |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 } | 211 } |
234 return blink::WebInputEventResult::HandledSuppressed; | 212 return blink::WebInputEventResult::HandledSuppressed; |
235 } | 213 } |
236 current_cursor_ = cursor; | 214 current_cursor_ = cursor; |
237 blink::WebInputEventResult handled = web_view_->handleInputEvent(event); | 215 blink::WebInputEventResult handled = web_view_->handleInputEvent(event); |
238 cursor = current_cursor_; | 216 cursor = current_cursor_; |
239 | 217 |
240 return handled; | 218 return handled; |
241 } | 219 } |
242 | 220 |
| 221 void WebViewPlugin::didReceiveResponse(const WebURLResponse& response) { |
| 222 NOTREACHED(); |
| 223 } |
| 224 |
243 void WebViewPlugin::didReceiveData(const char* data, int data_length) { | 225 void WebViewPlugin::didReceiveData(const char* data, int data_length) { |
244 data_.push_back(std::string(data, data_length)); | 226 NOTREACHED(); |
245 } | 227 } |
246 | 228 |
247 void WebViewPlugin::didFinishLoading() { | 229 void WebViewPlugin::didFinishLoading() { |
248 DCHECK(!finished_loading_); | 230 NOTREACHED(); |
249 finished_loading_ = true; | |
250 } | 231 } |
251 | 232 |
252 void WebViewPlugin::didFailLoading(const WebURLError& error) { | 233 void WebViewPlugin::didFailLoading(const WebURLError& error) { |
253 DCHECK(!error_.get()); | 234 NOTREACHED(); |
254 error_.reset(new WebURLError(error)); | |
255 } | 235 } |
256 | 236 |
257 bool WebViewPlugin::acceptsLoadDrops() { return false; } | 237 bool WebViewPlugin::acceptsLoadDrops() { return false; } |
258 | 238 |
259 void WebViewPlugin::setToolTipText(const WebString& text, | 239 void WebViewPlugin::setToolTipText(const WebString& text, |
260 blink::WebTextDirection hint) { | 240 blink::WebTextDirection hint) { |
261 if (container_) | 241 if (container_) |
262 container_->element().setAttribute("title", text); | 242 container_->element().setAttribute("title", text); |
263 } | 243 } |
264 | 244 |
(...skipping 28 matching lines...) Expand all Loading... |
293 // lifecycle stages during later ones. | 273 // lifecycle stages during later ones. |
294 if (is_resizing_) | 274 if (is_resizing_) |
295 return; | 275 return; |
296 if (container_) { | 276 if (container_) { |
297 // This should never happen; see also crbug.com/545039 for context. | 277 // This should never happen; see also crbug.com/545039 for context. |
298 CHECK(!is_painting_); | 278 CHECK(!is_painting_); |
299 container_->scheduleAnimation(); | 279 container_->scheduleAnimation(); |
300 } | 280 } |
301 } | 281 } |
302 | 282 |
303 void WebViewPlugin::didClearWindowObject(WebLocalFrame* frame) { | 283 void WebViewPlugin::PluginWebFrameClient::didClearWindowObject( |
304 if (!delegate_) | 284 WebLocalFrame* frame) { |
| 285 if (!plugin_->delegate_) |
305 return; | 286 return; |
306 | 287 |
307 v8::Isolate* isolate = blink::mainThreadIsolate(); | 288 v8::Isolate* isolate = blink::mainThreadIsolate(); |
308 v8::HandleScope handle_scope(isolate); | 289 v8::HandleScope handle_scope(isolate); |
309 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); | 290 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
310 DCHECK(!context.IsEmpty()); | 291 DCHECK(!context.IsEmpty()); |
311 | 292 |
312 v8::Context::Scope context_scope(context); | 293 v8::Context::Scope context_scope(context); |
313 v8::Local<v8::Object> global = context->Global(); | 294 v8::Local<v8::Object> global = context->Global(); |
314 | 295 |
315 global->Set(gin::StringToV8(isolate, "plugin"), | 296 global->Set(gin::StringToV8(isolate, "plugin"), |
316 delegate_->GetV8Handle(isolate)); | 297 plugin_->delegate_->GetV8Handle(isolate)); |
317 } | 298 } |
318 | 299 |
319 void WebViewPlugin::OnDestruct() {} | 300 void WebViewPlugin::OnDestruct() {} |
320 | 301 |
321 void WebViewPlugin::OnZoomLevelChanged() { | 302 void WebViewPlugin::OnZoomLevelChanged() { |
322 if (container_) { | 303 if (container_) { |
323 web_view_->setZoomLevel( | 304 web_view_->setZoomLevel( |
324 blink::WebView::zoomFactorToZoomLevel(container_->pageZoomFactor())); | 305 blink::WebView::zoomFactorToZoomLevel(container_->pageZoomFactor())); |
325 } | 306 } |
326 } | 307 } |
327 | 308 |
328 void WebViewPlugin::UpdatePluginForNewGeometry( | 309 void WebViewPlugin::UpdatePluginForNewGeometry( |
329 const blink::WebRect& window_rect, | 310 const blink::WebRect& window_rect, |
330 const blink::WebRect& unobscured_rect) { | 311 const blink::WebRect& unobscured_rect) { |
331 DCHECK(container_); | 312 DCHECK(container_); |
332 if (!delegate_) | 313 if (!delegate_) |
333 return; | 314 return; |
334 | 315 |
335 // The delegate may instantiate a new plugin. | 316 // The delegate may instantiate a new plugin. |
336 delegate_->OnUnobscuredRectUpdate(gfx::Rect(unobscured_rect)); | 317 delegate_->OnUnobscuredRectUpdate(gfx::Rect(unobscured_rect)); |
337 // The delegate may have dirtied style and layout of the WebView. | 318 // The delegate may have dirtied style and layout of the WebView. |
338 // See for example the resizePoster function in plugin_poster.html. | 319 // See for example the resizePoster function in plugin_poster.html. |
339 // Run the lifecycle now so that it is clean. | 320 // Run the lifecycle now so that it is clean. |
340 web_view_->updateAllLifecyclePhases(); | 321 web_view_->updateAllLifecyclePhases(); |
341 } | 322 } |
OLD | NEW |