Rename GetIconID to GetIconId
[chromium-blink-merge.git] / components / plugins / renderer / webview_plugin.cc
blob20446c700f0899ce16b73a8a03421de2f12e73aa
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;
36 using blink::WebRect;
37 using blink::WebSize;
38 using blink::WebString;
39 using blink::WebURLError;
40 using blink::WebURLRequest;
41 using blink::WebURLResponse;
42 using blink::WebVector;
43 using blink::WebView;
44 using content::WebPreferences;
46 WebViewPlugin::WebViewPlugin(content::RenderView* render_view,
47 WebViewPlugin::Delegate* delegate,
48 const WebPreferences& preferences)
49 : content::RenderViewObserver(render_view),
50 delegate_(delegate),
51 container_(NULL),
52 web_view_(WebView::create(this)),
53 finished_loading_(false),
54 focused_(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_);
62 // static
63 WebViewPlugin* WebViewPlugin::Create(content::RenderView* render_view,
64 WebViewPlugin::Delegate* delegate,
65 const WebPreferences& preferences,
66 const std::string& html_data,
67 const GURL& url) {
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);
71 return plugin;
74 WebViewPlugin::~WebViewPlugin() {
75 web_view_->close();
76 web_frame_->close();
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();
84 ++it) {
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)));
92 UMA_HISTOGRAM_COUNTS(
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.
97 if (focused_) {
98 plugin->updateFocus(true, blink::WebFocusTypeNone);
100 if (finished_loading_) {
101 plugin->didFinishLoading();
103 if (error_) {
104 plugin->didFailLoading(*error_);
108 void WebViewPlugin::RestoreTitleText() {
109 if (container_)
110 container_->element().setAttribute("title", old_title_);
113 WebPluginContainer* WebViewPlugin::container() const { return container_; }
115 bool WebViewPlugin::initialize(WebPluginContainer* container) {
116 container_ = container;
117 if (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()));
134 return true;
137 void WebViewPlugin::destroy() {
138 if (delegate_) {
139 delegate_->PluginDestroyed();
140 delegate_ = NULL;
142 container_ = NULL;
143 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
146 v8::Local<v8::Object> WebViewPlugin::v8ScriptableObject(v8::Isolate* isolate) {
147 if (!delegate_)
148 return v8::Local<v8::Object>();
150 return delegate_->GetV8ScriptableObject(isolate);
153 void WebViewPlugin::layoutIfNeeded() {
154 web_view_->layout();
157 void WebViewPlugin::paint(WebCanvas* canvas, const WebRect& rect) {
158 gfx::Rect paint_rect = gfx::IntersectRects(rect_, rect);
159 if (paint_rect.IsEmpty())
160 return;
162 paint_rect.Offset(-rect_.x(), -rect_.y());
164 canvas->save();
165 canvas->translate(SkIntToScalar(rect_.x()), SkIntToScalar(rect_.y()));
167 // Apply inverse device scale factor, as the outer webview has already
168 // applied it, and the inner webview will apply it again.
169 SkScalar inverse_scale =
170 SkFloatToScalar(1.0 / container_->deviceScaleFactor());
171 canvas->scale(inverse_scale, inverse_scale);
173 web_view_->paint(canvas, paint_rect);
175 canvas->restore();
178 // Coordinates are relative to the containing window.
179 void WebViewPlugin::updateGeometry(const WebRect& window_rect,
180 const WebRect& clip_rect,
181 const WebRect& unobscured_rect,
182 const WebVector<WebRect>& cut_outs_rects,
183 bool is_visible) {
184 if (static_cast<gfx::Rect>(window_rect) != rect_) {
185 rect_ = window_rect;
186 WebSize newSize(window_rect.width, window_rect.height);
187 web_view_->resize(newSize);
190 if (delegate_)
191 delegate_->OnUnobscuredRectUpdate(gfx::Rect(unobscured_rect));
194 void WebViewPlugin::updateFocus(bool focused, blink::WebFocusType focus_type) {
195 focused_ = focused;
198 bool WebViewPlugin::acceptsInputEvents() { return true; }
200 bool WebViewPlugin::handleInputEvent(const WebInputEvent& event,
201 WebCursorInfo& cursor) {
202 // For tap events, don't handle them. They will be converted to
203 // mouse events later and passed to here.
204 if (event.type == WebInputEvent::GestureTap)
205 return false;
207 // For LongPress events we return false, since otherwise the context menu will
208 // be suppressed. https://crbug.com/482842
209 if (event.type == WebInputEvent::GestureLongPress)
210 return false;
212 if (event.type == WebInputEvent::ContextMenu) {
213 if (delegate_) {
214 const WebMouseEvent& mouse_event =
215 reinterpret_cast<const WebMouseEvent&>(event);
216 delegate_->ShowContextMenu(mouse_event);
218 return true;
220 current_cursor_ = cursor;
221 bool handled = web_view_->handleInputEvent(event);
222 cursor = current_cursor_;
224 return handled;
227 void WebViewPlugin::didReceiveResponse(const WebURLResponse& response) {
228 DCHECK(response_.isNull());
229 response_ = response;
232 void WebViewPlugin::didReceiveData(const char* data, int data_length) {
233 data_.push_back(std::string(data, data_length));
236 void WebViewPlugin::didFinishLoading() {
237 DCHECK(!finished_loading_);
238 finished_loading_ = true;
241 void WebViewPlugin::didFailLoading(const WebURLError& error) {
242 DCHECK(!error_.get());
243 error_.reset(new WebURLError(error));
246 bool WebViewPlugin::acceptsLoadDrops() { return false; }
248 void WebViewPlugin::setToolTipText(const WebString& text,
249 blink::WebTextDirection hint) {
250 if (container_)
251 container_->element().setAttribute("title", text);
254 void WebViewPlugin::startDragging(WebLocalFrame*,
255 const WebDragData&,
256 WebDragOperationsMask,
257 const WebImage&,
258 const WebPoint&) {
259 // Immediately stop dragging.
260 web_view_->dragSourceSystemDragEnded();
263 bool WebViewPlugin::allowsBrokenNullLayerTreeView() const {
264 return true;
267 void WebViewPlugin::didInvalidateRect(const WebRect& rect) {
268 if (container_)
269 container_->invalidateRect(rect);
272 void WebViewPlugin::didUpdateLayoutSize(const WebSize&) {
273 if (container_)
274 container_->setNeedsLayout();
277 void WebViewPlugin::didChangeCursor(const WebCursorInfo& cursor) {
278 current_cursor_ = cursor;
281 void WebViewPlugin::scheduleAnimation() {
282 if (container_)
283 container_->setNeedsLayout();
286 void WebViewPlugin::didClearWindowObject(WebLocalFrame* frame) {
287 if (!delegate_)
288 return;
290 v8::Isolate* isolate = blink::mainThreadIsolate();
291 v8::HandleScope handle_scope(isolate);
292 v8::Local<v8::Context> context = frame->mainWorldScriptContext();
293 DCHECK(!context.IsEmpty());
295 v8::Context::Scope context_scope(context);
296 v8::Local<v8::Object> global = context->Global();
298 global->Set(gin::StringToV8(isolate, "plugin"),
299 delegate_->GetV8Handle(isolate));
302 void WebViewPlugin::didReceiveResponse(WebLocalFrame* frame,
303 unsigned identifier,
304 const WebURLResponse& response) {
305 WebFrameClient::didReceiveResponse(frame, identifier, response);
308 void WebViewPlugin::OnDestruct() {
309 // By default RenderViewObservers are destroyed along with the RenderView.
310 // WebViewPlugin has a custom destruction mechanism, so we disable this.
313 void WebViewPlugin::OnZoomLevelChanged() {
314 if (container_) {
315 web_view_->setZoomLevel(
316 blink::WebView::zoomFactorToZoomLevel(container_->pageZoomFactor()));