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/location.h" | 11 #include "base/location.h" |
11 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
12 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
13 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
15 #include "content/public/common/web_preferences.h" | 16 #include "content/public/common/web_preferences.h" |
16 #include "content/public/renderer/render_view.h" | 17 #include "content/public/renderer/render_view.h" |
17 #include "gin/converter.h" | 18 #include "gin/converter.h" |
18 #include "skia/ext/platform_canvas.h" | 19 #include "skia/ext/platform_canvas.h" |
19 #include "third_party/WebKit/public/platform/WebSize.h" | 20 #include "third_party/WebKit/public/platform/WebSize.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 WebViewPlugin::WebViewPlugin(content::RenderView* render_view, | 54 WebViewPlugin::WebViewPlugin(content::RenderView* render_view, |
54 WebViewPlugin::Delegate* delegate, | 55 WebViewPlugin::Delegate* delegate, |
55 const WebPreferences& preferences) | 56 const WebPreferences& preferences) |
56 : content::RenderViewObserver(render_view), | 57 : content::RenderViewObserver(render_view), |
57 delegate_(delegate), | 58 delegate_(delegate), |
58 container_(nullptr), | 59 container_(nullptr), |
59 web_view_(WebView::create(this, blink::WebPageVisibilityStateVisible)), | 60 web_view_(WebView::create(this, blink::WebPageVisibilityStateVisible)), |
60 finished_loading_(false), | 61 finished_loading_(false), |
61 focused_(false), | 62 focused_(false), |
62 is_painting_(false), | 63 is_painting_(false), |
63 is_resizing_(false) { | 64 is_resizing_(false), |
| 65 weak_factory_(this) { |
64 // ApplyWebPreferences before making a WebLocalFrame so that the frame sees a | 66 // ApplyWebPreferences before making a WebLocalFrame so that the frame sees a |
65 // consistent view of our preferences. | 67 // consistent view of our preferences. |
66 content::RenderView::ApplyWebPreferences(preferences, web_view_); | 68 content::RenderView::ApplyWebPreferences(preferences, web_view_); |
67 WebLocalFrame* web_local_frame = | 69 WebLocalFrame* web_local_frame = |
68 WebLocalFrame::create(blink::WebTreeScopeType::Document, this); | 70 WebLocalFrame::create(blink::WebTreeScopeType::Document, this); |
69 web_frame_ = web_local_frame; | 71 web_frame_ = web_local_frame; |
70 web_view_->setMainFrame(web_frame_); | 72 web_view_->setMainFrame(web_frame_); |
71 // TODO(dcheng): The main frame widget currently has a special case. | 73 // TODO(dcheng): The main frame widget currently has a special case. |
72 // Eliminate this once WebView is no longer a WebWidget. | 74 // Eliminate this once WebView is no longer a WebWidget. |
73 web_frame_widget_ = WebFrameWidget::create(this, web_view_, web_local_frame); | 75 web_frame_widget_ = WebFrameWidget::create(this, web_view_, web_local_frame); |
74 } | 76 } |
75 | 77 |
76 // static | 78 // static |
77 WebViewPlugin* WebViewPlugin::Create(content::RenderView* render_view, | 79 WebViewPlugin* WebViewPlugin::Create(content::RenderView* render_view, |
78 WebViewPlugin::Delegate* delegate, | 80 WebViewPlugin::Delegate* delegate, |
79 const WebPreferences& preferences, | 81 const WebPreferences& preferences, |
80 const std::string& html_data, | 82 const std::string& html_data, |
81 const GURL& url) { | 83 const GURL& url) { |
82 DCHECK(url.is_valid()) << "Blink requires the WebView to have a valid URL."; | 84 DCHECK(url.is_valid()) << "Blink requires the WebView to have a valid URL."; |
83 WebViewPlugin* plugin = new WebViewPlugin(render_view, delegate, preferences); | 85 WebViewPlugin* plugin = new WebViewPlugin(render_view, delegate, preferences); |
84 plugin->web_view()->mainFrame()->loadHTMLString(html_data, url); | 86 plugin->web_view()->mainFrame()->loadHTMLString(html_data, url); |
85 return plugin; | 87 return plugin; |
86 } | 88 } |
87 | 89 |
88 WebViewPlugin::~WebViewPlugin() { | 90 WebViewPlugin::~WebViewPlugin() { |
| 91 DCHECK(!weak_factory_.HasWeakPtrs()); |
89 web_frame_widget_->close(); | 92 web_frame_widget_->close(); |
90 web_view_->close(); | 93 web_view_->close(); |
91 web_frame_->close(); | 94 web_frame_->close(); |
92 } | 95 } |
93 | 96 |
94 void WebViewPlugin::ReplayReceivedData(WebPlugin* plugin) { | 97 void WebViewPlugin::ReplayReceivedData(WebPlugin* plugin) { |
95 if (!response_.isNull()) { | 98 if (!response_.isNull()) { |
96 plugin->didReceiveResponse(response_); | 99 plugin->didReceiveResponse(response_); |
97 size_t total_bytes = 0; | 100 size_t total_bytes = 0; |
98 for (std::list<std::string>::iterator it = data_.begin(); it != data_.end(); | 101 for (std::list<std::string>::iterator it = data_.begin(); it != data_.end(); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 | 148 |
146 // Propagate device scale and zoom level to inner webview. | 149 // Propagate device scale and zoom level to inner webview. |
147 web_view_->setDeviceScaleFactor(container_->deviceScaleFactor()); | 150 web_view_->setDeviceScaleFactor(container_->deviceScaleFactor()); |
148 web_view_->setZoomLevel( | 151 web_view_->setZoomLevel( |
149 blink::WebView::zoomFactorToZoomLevel(container_->pageZoomFactor())); | 152 blink::WebView::zoomFactorToZoomLevel(container_->pageZoomFactor())); |
150 | 153 |
151 return true; | 154 return true; |
152 } | 155 } |
153 | 156 |
154 void WebViewPlugin::destroy() { | 157 void WebViewPlugin::destroy() { |
| 158 weak_factory_.InvalidateWeakPtrs(); |
| 159 |
155 if (delegate_) { | 160 if (delegate_) { |
156 delegate_->PluginDestroyed(); | 161 delegate_->PluginDestroyed(); |
157 delegate_ = nullptr; | 162 delegate_ = nullptr; |
158 } | 163 } |
159 container_ = nullptr; | 164 container_ = nullptr; |
160 content::RenderViewObserver::Observe(nullptr); | 165 content::RenderViewObserver::Observe(nullptr); |
161 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | 166 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
162 } | 167 } |
163 | 168 |
164 v8::Local<v8::Object> WebViewPlugin::v8ScriptableObject(v8::Isolate* isolate) { | 169 v8::Local<v8::Object> WebViewPlugin::v8ScriptableObject(v8::Isolate* isolate) { |
(...skipping 30 matching lines...) Expand all Loading... |
195 | 200 |
196 canvas->restore(); | 201 canvas->restore(); |
197 } | 202 } |
198 | 203 |
199 // Coordinates are relative to the containing window. | 204 // Coordinates are relative to the containing window. |
200 void WebViewPlugin::updateGeometry(const WebRect& window_rect, | 205 void WebViewPlugin::updateGeometry(const WebRect& window_rect, |
201 const WebRect& clip_rect, | 206 const WebRect& clip_rect, |
202 const WebRect& unobscured_rect, | 207 const WebRect& unobscured_rect, |
203 const WebVector<WebRect>& cut_outs_rects, | 208 const WebVector<WebRect>& cut_outs_rects, |
204 bool is_visible) { | 209 bool is_visible) { |
205 base::AutoReset<bool> is_resizing( | 210 DCHECK(container_); |
206 &is_resizing_, true); | |
207 | 211 |
208 if (static_cast<gfx::Rect>(window_rect) != rect_) { | 212 // Plugin updates are forbidden during Blink layout. Therefore, |
209 rect_ = window_rect; | 213 // UpdatePluginForNewGeometry must be posted to a task to run asynchronously. |
210 WebSize newSize(window_rect.width, window_rect.height); | 214 base::ThreadTaskRunnerHandle::Get()->PostTask( |
211 web_view_->resize(newSize); | 215 FROM_HERE, |
212 } | 216 base::Bind(&WebViewPlugin::UpdatePluginForNewGeometry, |
213 | 217 weak_factory_.GetWeakPtr(), window_rect, unobscured_rect)); |
214 if (delegate_) { | |
215 delegate_->OnUnobscuredRectUpdate(gfx::Rect(unobscured_rect)); | |
216 // The delegate may have dirtied style and layout of the WebView. | |
217 // See for example the resizePoster function in plugin_poster.html. | |
218 // Run the lifecycle now so that it is clean. | |
219 web_view_->updateAllLifecyclePhases(); | |
220 } | |
221 } | 218 } |
222 | 219 |
223 void WebViewPlugin::updateFocus(bool focused, blink::WebFocusType focus_type) { | 220 void WebViewPlugin::updateFocus(bool focused, blink::WebFocusType focus_type) { |
224 focused_ = focused; | 221 focused_ = focused; |
225 } | 222 } |
226 | 223 |
227 blink::WebInputEventResult WebViewPlugin::handleInputEvent( | 224 blink::WebInputEventResult WebViewPlugin::handleInputEvent( |
228 const WebInputEvent& event, | 225 const WebInputEvent& event, |
229 WebCursorInfo& cursor) { | 226 WebCursorInfo& cursor) { |
230 // For tap events, don't handle them. They will be converted to | 227 // For tap events, don't handle them. They will be converted to |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 } | 336 } |
340 | 337 |
341 void WebViewPlugin::OnDestruct() {} | 338 void WebViewPlugin::OnDestruct() {} |
342 | 339 |
343 void WebViewPlugin::OnZoomLevelChanged() { | 340 void WebViewPlugin::OnZoomLevelChanged() { |
344 if (container_) { | 341 if (container_) { |
345 web_view_->setZoomLevel( | 342 web_view_->setZoomLevel( |
346 blink::WebView::zoomFactorToZoomLevel(container_->pageZoomFactor())); | 343 blink::WebView::zoomFactorToZoomLevel(container_->pageZoomFactor())); |
347 } | 344 } |
348 } | 345 } |
| 346 |
| 347 void WebViewPlugin::UpdatePluginForNewGeometry( |
| 348 const blink::WebRect& window_rect, |
| 349 const blink::WebRect& unobscured_rect) { |
| 350 DCHECK(container_); |
| 351 |
| 352 base::AutoReset<bool> is_resizing(&is_resizing_, true); |
| 353 |
| 354 if (static_cast<gfx::Rect>(window_rect) != rect_) { |
| 355 rect_ = window_rect; |
| 356 web_view_->resize(rect_.size()); |
| 357 } |
| 358 |
| 359 if (delegate_) { |
| 360 delegate_->OnUnobscuredRectUpdate(gfx::Rect(unobscured_rect)); |
| 361 // The delegate may have dirtied style and layout of the WebView. |
| 362 // See for example the resizePoster function in plugin_poster.html. |
| 363 // Run the lifecycle now so that it is clean. |
| 364 web_view_->updateAllLifecyclePhases(); |
| 365 } |
| 366 } |
OLD | NEW |