Update comments of TabObserver#onLoadStarted and rename onContentChanged
[chromium-blink-merge.git] / components / plugins / renderer / webview_plugin.cc
blob96347fd5b22894d909c842c3dc25a6d2309d1aa9
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.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 "skia/ext/platform_canvas.h"
13 #include "third_party/WebKit/public/platform/WebSize.h"
14 #include "third_party/WebKit/public/platform/WebURL.h"
15 #include "third_party/WebKit/public/platform/WebURLRequest.h"
16 #include "third_party/WebKit/public/platform/WebURLResponse.h"
17 #include "third_party/WebKit/public/web/WebDocument.h"
18 #include "third_party/WebKit/public/web/WebElement.h"
19 #include "third_party/WebKit/public/web/WebInputEvent.h"
20 #include "third_party/WebKit/public/web/WebLocalFrame.h"
21 #include "third_party/WebKit/public/web/WebPluginContainer.h"
22 #include "third_party/WebKit/public/web/WebView.h"
24 using blink::WebCanvas;
25 using blink::WebCursorInfo;
26 using blink::WebDragData;
27 using blink::WebDragOperationsMask;
28 using blink::WebImage;
29 using blink::WebInputEvent;
30 using blink::WebLocalFrame;
31 using blink::WebMouseEvent;
32 using blink::WebPlugin;
33 using blink::WebPluginContainer;
34 using blink::WebPoint;
35 using blink::WebRect;
36 using blink::WebSize;
37 using blink::WebString;
38 using blink::WebURLError;
39 using blink::WebURLRequest;
40 using blink::WebURLResponse;
41 using blink::WebVector;
42 using blink::WebView;
43 using content::WebPreferences;
45 WebViewPlugin::WebViewPlugin(WebViewPlugin::Delegate* delegate,
46 const WebPreferences& preferences)
47 : delegate_(delegate),
48 container_(NULL),
49 web_view_(WebView::create(this)),
50 finished_loading_(false),
51 focused_(false) {
52 // ApplyWebPreferences before making a WebLocalFrame so that the frame sees a
53 // consistent view of our preferences.
54 content::RenderView::ApplyWebPreferences(preferences, web_view_);
55 web_frame_ = WebLocalFrame::create(this);
56 web_view_->setMainFrame(web_frame_);
59 // static
60 WebViewPlugin* WebViewPlugin::Create(WebViewPlugin::Delegate* delegate,
61 const WebPreferences& preferences,
62 const std::string& html_data,
63 const GURL& url) {
64 DCHECK(url.is_valid()) << "Blink requires the WebView to have a valid URL.";
65 WebViewPlugin* plugin = new WebViewPlugin(delegate, preferences);
66 plugin->web_view()->mainFrame()->loadHTMLString(html_data, url);
67 return plugin;
70 WebViewPlugin::~WebViewPlugin() {
71 web_view_->close();
72 web_frame_->close();
75 void WebViewPlugin::ReplayReceivedData(WebPlugin* plugin) {
76 if (!response_.isNull()) {
77 plugin->didReceiveResponse(response_);
78 size_t total_bytes = 0;
79 for (std::list<std::string>::iterator it = data_.begin(); it != data_.end();
80 ++it) {
81 plugin->didReceiveData(
82 it->c_str(), base::checked_cast<int, size_t>(it->length()));
83 total_bytes += it->length();
85 UMA_HISTOGRAM_MEMORY_KB(
86 "PluginDocument.Memory",
87 (base::checked_cast<int, size_t>(total_bytes / 1024)));
88 UMA_HISTOGRAM_COUNTS(
89 "PluginDocument.NumChunks",
90 (base::checked_cast<int, size_t>(data_.size())));
92 // We need to transfer the |focused_| to new plugin after it loaded.
93 if (focused_) {
94 plugin->updateFocus(true, blink::WebFocusTypeNone);
96 if (finished_loading_) {
97 plugin->didFinishLoading();
99 if (error_) {
100 plugin->didFailLoading(*error_);
104 void WebViewPlugin::RestoreTitleText() {
105 if (container_)
106 container_->element().setAttribute("title", old_title_);
109 WebPluginContainer* WebViewPlugin::container() const { return container_; }
111 bool WebViewPlugin::initialize(WebPluginContainer* container) {
112 container_ = container;
113 if (container_) {
114 old_title_ = container_->element().getAttribute("title");
116 // Propagate device scale to inner webview to load the correct resource
117 // when images have a "srcset" attribute.
118 web_view_->setDeviceScaleFactor(container_->deviceScaleFactor());
120 return true;
123 void WebViewPlugin::destroy() {
124 if (delegate_) {
125 delegate_->PluginDestroyed();
126 delegate_ = NULL;
128 container_ = NULL;
129 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
132 v8::Local<v8::Object> WebViewPlugin::v8ScriptableObject(v8::Isolate* isolate) {
133 if (!delegate_)
134 return v8::Local<v8::Object>();
136 return delegate_->GetV8ScriptableObject(isolate);
139 void WebViewPlugin::layoutIfNeeded() {
140 web_view_->layout();
143 void WebViewPlugin::paint(WebCanvas* canvas, const WebRect& rect) {
144 gfx::Rect paint_rect = gfx::IntersectRects(rect_, rect);
145 if (paint_rect.IsEmpty())
146 return;
148 paint_rect.Offset(-rect_.x(), -rect_.y());
150 canvas->save();
151 canvas->translate(SkIntToScalar(rect_.x()), SkIntToScalar(rect_.y()));
153 // Apply inverse device scale factor, as the outer webview has already
154 // applied it, and the inner webview will apply it again.
155 SkScalar inverse_scale =
156 SkFloatToScalar(1.0 / container_->deviceScaleFactor());
157 canvas->scale(inverse_scale, inverse_scale);
159 web_view_->paint(canvas, paint_rect);
161 canvas->restore();
164 // Coordinates are relative to the containing window.
165 void WebViewPlugin::updateGeometry(const WebRect& window_rect,
166 const WebRect& clip_rect,
167 const WebRect& unobscured_rect,
168 const WebVector<WebRect>& cut_outs_rects,
169 bool is_visible) {
170 if (static_cast<gfx::Rect>(window_rect) != rect_) {
171 rect_ = window_rect;
172 WebSize newSize(window_rect.width, window_rect.height);
173 web_view_->resize(newSize);
177 void WebViewPlugin::updateFocus(bool focused, blink::WebFocusType focus_type) {
178 focused_ = focused;
181 bool WebViewPlugin::acceptsInputEvents() { return true; }
183 bool WebViewPlugin::handleInputEvent(const WebInputEvent& event,
184 WebCursorInfo& cursor) {
185 // For tap events, don't handle them. They will be converted to
186 // mouse events later and passed to here.
187 if (event.type == WebInputEvent::GestureTap)
188 return false;
190 // For LongPress events we return false, since otherwise the context menu will
191 // be suppressed. https://crbug.com/482842
192 if (event.type == WebInputEvent::GestureLongPress)
193 return false;
195 if (event.type == WebInputEvent::ContextMenu) {
196 if (delegate_) {
197 const WebMouseEvent& mouse_event =
198 reinterpret_cast<const WebMouseEvent&>(event);
199 delegate_->ShowContextMenu(mouse_event);
201 return true;
203 current_cursor_ = cursor;
204 bool handled = web_view_->handleInputEvent(event);
205 cursor = current_cursor_;
207 return handled;
210 void WebViewPlugin::didReceiveResponse(const WebURLResponse& response) {
211 DCHECK(response_.isNull());
212 response_ = response;
215 void WebViewPlugin::didReceiveData(const char* data, int data_length) {
216 data_.push_back(std::string(data, data_length));
219 void WebViewPlugin::didFinishLoading() {
220 DCHECK(!finished_loading_);
221 finished_loading_ = true;
224 void WebViewPlugin::didFailLoading(const WebURLError& error) {
225 DCHECK(!error_.get());
226 error_.reset(new WebURLError(error));
229 bool WebViewPlugin::acceptsLoadDrops() { return false; }
231 void WebViewPlugin::setToolTipText(const WebString& text,
232 blink::WebTextDirection hint) {
233 if (container_)
234 container_->element().setAttribute("title", text);
237 void WebViewPlugin::startDragging(WebLocalFrame*,
238 const WebDragData&,
239 WebDragOperationsMask,
240 const WebImage&,
241 const WebPoint&) {
242 // Immediately stop dragging.
243 web_view_->dragSourceSystemDragEnded();
246 bool WebViewPlugin::allowsBrokenNullLayerTreeView() const {
247 return true;
250 void WebViewPlugin::didInvalidateRect(const WebRect& rect) {
251 if (container_)
252 container_->invalidateRect(rect);
255 void WebViewPlugin::didChangeCursor(const WebCursorInfo& cursor) {
256 current_cursor_ = cursor;
259 void WebViewPlugin::scheduleAnimation() {
260 if (container_)
261 container_->invalidate();
264 void WebViewPlugin::didClearWindowObject(WebLocalFrame* frame) {
265 if (delegate_)
266 delegate_->BindWebFrame(frame);
269 void WebViewPlugin::didReceiveResponse(WebLocalFrame* frame,
270 unsigned identifier,
271 const WebURLResponse& response) {
272 WebFrameClient::didReceiveResponse(frame, identifier, response);