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
3 // found in the LICENSE file.
5 #include "components/plugins/renderer/webview_plugin.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "content/public/common/web_preferences.h"
11 #include "content/public/renderer/render_view.h"
12 #include "gin/converter.h"
13 #include "skia/ext/platform_canvas.h"
14 #include "third_party/WebKit/public/platform/WebSize.h"
15 #include "third_party/WebKit/public/platform/WebURL.h"
16 #include "third_party/WebKit/public/platform/WebURLRequest.h"
17 #include "third_party/WebKit/public/platform/WebURLResponse.h"
18 #include "third_party/WebKit/public/web/WebDocument.h"
19 #include "third_party/WebKit/public/web/WebElement.h"
20 #include "third_party/WebKit/public/web/WebInputEvent.h"
21 #include "third_party/WebKit/public/web/WebLocalFrame.h"
22 #include "third_party/WebKit/public/web/WebPluginContainer.h"
23 #include "third_party/WebKit/public/web/WebView.h"
25 using blink::WebCanvas
;
26 using blink::WebCursorInfo
;
27 using blink::WebDragData
;
28 using blink::WebDragOperationsMask
;
29 using blink::WebImage
;
30 using blink::WebInputEvent
;
31 using blink::WebLocalFrame
;
32 using blink::WebMouseEvent
;
33 using blink::WebPlugin
;
34 using blink::WebPluginContainer
;
35 using blink::WebPoint
;
38 using blink::WebString
;
39 using blink::WebURLError
;
40 using blink::WebURLRequest
;
41 using blink::WebURLResponse
;
42 using blink::WebVector
;
44 using content::WebPreferences
;
46 WebViewPlugin::WebViewPlugin(content::RenderView
* render_view
,
47 WebViewPlugin::Delegate
* delegate
,
48 const WebPreferences
& preferences
)
49 : content::RenderViewObserver(render_view
),
52 web_view_(WebView::create(this)),
53 finished_loading_(false),
55 // ApplyWebPreferences before making a WebLocalFrame so that the frame sees a
56 // consistent view of our preferences.
57 content::RenderView::ApplyWebPreferences(preferences
, web_view_
);
58 web_frame_
= WebLocalFrame::create(blink::WebTreeScopeType::Document
, this);
59 web_view_
->setMainFrame(web_frame_
);
63 WebViewPlugin
* WebViewPlugin::Create(content::RenderView
* render_view
,
64 WebViewPlugin::Delegate
* delegate
,
65 const WebPreferences
& preferences
,
66 const std::string
& html_data
,
68 DCHECK(url
.is_valid()) << "Blink requires the WebView to have a valid URL.";
69 WebViewPlugin
* plugin
= new WebViewPlugin(render_view
, delegate
, preferences
);
70 plugin
->web_view()->mainFrame()->loadHTMLString(html_data
, url
);
74 WebViewPlugin::~WebViewPlugin() {
79 void WebViewPlugin::ReplayReceivedData(WebPlugin
* plugin
) {
80 if (!response_
.isNull()) {
81 plugin
->didReceiveResponse(response_
);
82 size_t total_bytes
= 0;
83 for (std::list
<std::string
>::iterator it
= data_
.begin(); it
!= data_
.end();
85 plugin
->didReceiveData(
86 it
->c_str(), base::checked_cast
<int, size_t>(it
->length()));
87 total_bytes
+= it
->length();
89 UMA_HISTOGRAM_MEMORY_KB(
90 "PluginDocument.Memory",
91 (base::checked_cast
<int, size_t>(total_bytes
/ 1024)));
93 "PluginDocument.NumChunks",
94 (base::checked_cast
<int, size_t>(data_
.size())));
96 // We need to transfer the |focused_| to new plugin after it loaded.
98 plugin
->updateFocus(true, blink::WebFocusTypeNone
);
100 if (finished_loading_
) {
101 plugin
->didFinishLoading();
104 plugin
->didFailLoading(*error_
);
108 void WebViewPlugin::RestoreTitleText() {
110 container_
->element().setAttribute("title", old_title_
);
113 WebPluginContainer
* WebViewPlugin::container() const { return container_
; }
115 bool WebViewPlugin::initialize(WebPluginContainer
* container
) {
116 container_
= container
;
118 // We must call layout again here to ensure that the container is laid
119 // out before we next try to paint it, which is a requirement of the
120 // document life cycle in Blink. In most cases, needsLayout is set by
121 // scheduleAnimation, but due to timers controlling widget update,
122 // scheduleAnimation may be invoked before this initialize call (which
123 // comes through the widget update process). It doesn't hurt to mark
124 // for layout again, and it does help us in the race-condition situation.
125 container_
->setNeedsLayout();
127 old_title_
= container_
->element().getAttribute("title");
129 // Propagate device scale and zoom level to inner webview.
130 web_view_
->setDeviceScaleFactor(container_
->deviceScaleFactor());
131 web_view_
->setZoomLevel(
132 blink::WebView::zoomFactorToZoomLevel(container_
->pageZoomFactor()));
137 void WebViewPlugin::destroy() {
139 delegate_
->PluginDestroyed();
142 container_
= nullptr;
143 content::RenderViewObserver::Observe(nullptr);
144 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
147 v8::Local
<v8::Object
> WebViewPlugin::v8ScriptableObject(v8::Isolate
* isolate
) {
149 return v8::Local
<v8::Object
>();
151 return delegate_
->GetV8ScriptableObject(isolate
);
154 void WebViewPlugin::layoutIfNeeded() {
158 void WebViewPlugin::paint(WebCanvas
* canvas
, const WebRect
& rect
) {
159 gfx::Rect paint_rect
= gfx::IntersectRects(rect_
, rect
);
160 if (paint_rect
.IsEmpty())
163 paint_rect
.Offset(-rect_
.x(), -rect_
.y());
166 canvas
->translate(SkIntToScalar(rect_
.x()), SkIntToScalar(rect_
.y()));
168 // Apply inverse device scale factor, as the outer webview has already
169 // applied it, and the inner webview will apply it again.
170 SkScalar inverse_scale
=
171 SkFloatToScalar(1.0 / container_
->deviceScaleFactor());
172 canvas
->scale(inverse_scale
, inverse_scale
);
174 web_view_
->paint(canvas
, paint_rect
);
179 // Coordinates are relative to the containing window.
180 void WebViewPlugin::updateGeometry(const WebRect
& window_rect
,
181 const WebRect
& clip_rect
,
182 const WebRect
& unobscured_rect
,
183 const WebVector
<WebRect
>& cut_outs_rects
,
185 if (static_cast<gfx::Rect
>(window_rect
) != rect_
) {
187 WebSize
newSize(window_rect
.width
, window_rect
.height
);
188 web_view_
->resize(newSize
);
192 delegate_
->OnUnobscuredRectUpdate(gfx::Rect(unobscured_rect
));
195 void WebViewPlugin::updateFocus(bool focused
, blink::WebFocusType focus_type
) {
199 bool WebViewPlugin::acceptsInputEvents() { return true; }
201 bool WebViewPlugin::handleInputEvent(const WebInputEvent
& event
,
202 WebCursorInfo
& cursor
) {
203 // For tap events, don't handle them. They will be converted to
204 // mouse events later and passed to here.
205 if (event
.type
== WebInputEvent::GestureTap
)
208 // For LongPress events we return false, since otherwise the context menu will
209 // be suppressed. https://crbug.com/482842
210 if (event
.type
== WebInputEvent::GestureLongPress
)
213 if (event
.type
== WebInputEvent::ContextMenu
) {
215 const WebMouseEvent
& mouse_event
=
216 reinterpret_cast<const WebMouseEvent
&>(event
);
217 delegate_
->ShowContextMenu(mouse_event
);
221 current_cursor_
= cursor
;
222 bool handled
= web_view_
->handleInputEvent(event
);
223 cursor
= current_cursor_
;
228 void WebViewPlugin::didReceiveResponse(const WebURLResponse
& response
) {
229 DCHECK(response_
.isNull());
230 response_
= response
;
233 void WebViewPlugin::didReceiveData(const char* data
, int data_length
) {
234 data_
.push_back(std::string(data
, data_length
));
237 void WebViewPlugin::didFinishLoading() {
238 DCHECK(!finished_loading_
);
239 finished_loading_
= true;
242 void WebViewPlugin::didFailLoading(const WebURLError
& error
) {
243 DCHECK(!error_
.get());
244 error_
.reset(new WebURLError(error
));
247 bool WebViewPlugin::acceptsLoadDrops() { return false; }
249 void WebViewPlugin::setToolTipText(const WebString
& text
,
250 blink::WebTextDirection hint
) {
252 container_
->element().setAttribute("title", text
);
255 void WebViewPlugin::startDragging(WebLocalFrame
*,
257 WebDragOperationsMask
,
260 // Immediately stop dragging.
261 web_view_
->dragSourceSystemDragEnded();
264 bool WebViewPlugin::allowsBrokenNullLayerTreeView() const {
268 void WebViewPlugin::didInvalidateRect(const WebRect
& rect
) {
270 container_
->invalidateRect(rect
);
273 void WebViewPlugin::didUpdateLayoutSize(const WebSize
&) {
275 container_
->setNeedsLayout();
278 void WebViewPlugin::didChangeCursor(const WebCursorInfo
& cursor
) {
279 current_cursor_
= cursor
;
282 void WebViewPlugin::scheduleAnimation() {
284 container_
->setNeedsLayout();
287 void WebViewPlugin::didClearWindowObject(WebLocalFrame
* frame
) {
291 v8::Isolate
* isolate
= blink::mainThreadIsolate();
292 v8::HandleScope
handle_scope(isolate
);
293 v8::Local
<v8::Context
> context
= frame
->mainWorldScriptContext();
294 DCHECK(!context
.IsEmpty());
296 v8::Context::Scope
context_scope(context
);
297 v8::Local
<v8::Object
> global
= context
->Global();
299 global
->Set(gin::StringToV8(isolate
, "plugin"),
300 delegate_
->GetV8Handle(isolate
));
303 void WebViewPlugin::didReceiveResponse(WebLocalFrame
* frame
,
305 const WebURLResponse
& response
) {
306 WebFrameClient::didReceiveResponse(frame
, identifier
, response
);
309 void WebViewPlugin::OnDestruct() {
310 // By default RenderViewObservers are destroyed along with the RenderView.
311 // WebViewPlugin has a custom destruction mechanism, so we disable this.
314 void WebViewPlugin::OnZoomLevelChanged() {
316 web_view_
->setZoomLevel(
317 blink::WebView::zoomFactorToZoomLevel(container_
->pageZoomFactor()));