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/renderer/web_preferences.h"
11 #include "skia/ext/platform_canvas.h"
12 #include "third_party/WebKit/public/platform/WebSize.h"
13 #include "third_party/WebKit/public/platform/WebURL.h"
14 #include "third_party/WebKit/public/platform/WebURLRequest.h"
15 #include "third_party/WebKit/public/platform/WebURLResponse.h"
16 #include "third_party/WebKit/public/web/WebDocument.h"
17 #include "third_party/WebKit/public/web/WebElement.h"
18 #include "third_party/WebKit/public/web/WebInputEvent.h"
19 #include "third_party/WebKit/public/web/WebLocalFrame.h"
20 #include "third_party/WebKit/public/web/WebPluginContainer.h"
21 #include "third_party/WebKit/public/web/WebView.h"
22 #include "webkit/common/webpreferences.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;
37 using blink::WebString;
38 using blink::WebURLError;
39 using blink::WebURLRequest;
40 using blink::WebURLResponse;
41 using blink::WebVector;
44 WebViewPlugin::WebViewPlugin(WebViewPlugin::Delegate* delegate)
45 : delegate_(delegate),
47 web_view_(WebView::create(this)),
48 web_frame_(WebLocalFrame::create(this)),
49 finished_loading_(false),
51 web_view_->setMainFrame(web_frame_);
55 WebViewPlugin* WebViewPlugin::Create(WebViewPlugin::Delegate* delegate,
56 const WebPreferences& preferences,
57 const std::string& html_data,
59 WebViewPlugin* plugin = new WebViewPlugin(delegate);
60 WebView* web_view = plugin->web_view();
61 content::ApplyWebPreferences(preferences, web_view);
62 web_view->mainFrame()->loadHTMLString(html_data, url);
66 WebViewPlugin::~WebViewPlugin() {
71 void WebViewPlugin::ReplayReceivedData(WebPlugin* plugin) {
72 if (!response_.isNull()) {
73 plugin->didReceiveResponse(response_);
74 size_t total_bytes = 0;
75 for (std::list<std::string>::iterator it = data_.begin(); it != data_.end();
77 plugin->didReceiveData(
78 it->c_str(), base::checked_cast<int, size_t>(it->length()));
79 total_bytes += it->length();
81 UMA_HISTOGRAM_MEMORY_KB(
82 "PluginDocument.Memory",
83 (base::checked_cast<int, size_t>(total_bytes / 1024)));
85 "PluginDocument.NumChunks",
86 (base::checked_cast<int, size_t>(data_.size())));
88 // We need to transfer the |focused_| to new plugin after it loaded.
90 plugin->updateFocus(true);
92 if (finished_loading_) {
93 plugin->didFinishLoading();
96 plugin->didFailLoading(*error_);
100 void WebViewPlugin::RestoreTitleText() {
102 container_->element().setAttribute("title", old_title_);
105 WebPluginContainer* WebViewPlugin::container() const { return container_; }
107 bool WebViewPlugin::initialize(WebPluginContainer* container) {
108 container_ = container;
110 old_title_ = container_->element().getAttribute("title");
114 void WebViewPlugin::destroy() {
116 delegate_->PluginDestroyed();
120 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
123 NPObject* WebViewPlugin::scriptableObject() { return NULL; }
125 struct _NPP* WebViewPlugin::pluginNPP() { return NULL; }
127 bool WebViewPlugin::getFormValue(WebString& value) { return false; }
129 void WebViewPlugin::paint(WebCanvas* canvas, const WebRect& rect) {
130 gfx::Rect paint_rect = gfx::IntersectRects(rect_, rect);
131 if (paint_rect.IsEmpty())
134 paint_rect.Offset(-rect_.x(), -rect_.y());
136 canvas->translate(SkIntToScalar(rect_.x()), SkIntToScalar(rect_.y()));
140 web_view_->paint(canvas, paint_rect);
145 // Coordinates are relative to the containing window.
146 void WebViewPlugin::updateGeometry(const WebRect& frame_rect,
147 const WebRect& clip_rect,
148 const WebVector<WebRect>& cut_out_rects,
150 if (static_cast<gfx::Rect>(frame_rect) != rect_) {
152 WebSize newSize(frame_rect.width, frame_rect.height);
153 web_view_->setFixedLayoutSize(newSize);
154 web_view_->resize(newSize);
158 void WebViewPlugin::updateFocus(bool focused) {
162 bool WebViewPlugin::acceptsInputEvents() { return true; }
164 bool WebViewPlugin::handleInputEvent(const WebInputEvent& event,
165 WebCursorInfo& cursor) {
166 // For tap events, don't handle them. They will be converted to
167 // mouse events later and passed to here.
168 if (event.type == WebInputEvent::GestureTap)
171 if (event.type == WebInputEvent::ContextMenu) {
173 const WebMouseEvent& mouse_event =
174 reinterpret_cast<const WebMouseEvent&>(event);
175 delegate_->ShowContextMenu(mouse_event);
179 current_cursor_ = cursor;
180 bool handled = web_view_->handleInputEvent(event);
181 cursor = current_cursor_;
186 void WebViewPlugin::didReceiveResponse(const WebURLResponse& response) {
187 DCHECK(response_.isNull());
188 response_ = response;
191 void WebViewPlugin::didReceiveData(const char* data, int data_length) {
192 data_.push_back(std::string(data, data_length));
195 void WebViewPlugin::didFinishLoading() {
196 DCHECK(!finished_loading_);
197 finished_loading_ = true;
200 void WebViewPlugin::didFailLoading(const WebURLError& error) {
201 DCHECK(!error_.get());
202 error_.reset(new WebURLError(error));
205 bool WebViewPlugin::acceptsLoadDrops() { return false; }
207 void WebViewPlugin::setToolTipText(const WebString& text,
208 blink::WebTextDirection hint) {
210 container_->element().setAttribute("title", text);
213 void WebViewPlugin::startDragging(WebLocalFrame*,
215 WebDragOperationsMask,
218 // Immediately stop dragging.
219 web_view_->dragSourceSystemDragEnded();
222 bool WebViewPlugin::allowsBrokenNullLayerTreeView() const {
226 void WebViewPlugin::didInvalidateRect(const WebRect& rect) {
228 container_->invalidateRect(rect);
231 void WebViewPlugin::didChangeCursor(const WebCursorInfo& cursor) {
232 current_cursor_ = cursor;
235 void WebViewPlugin::didClearWindowObject(WebLocalFrame* frame, int world_id) {
237 delegate_->BindWebFrame(frame);
240 void WebViewPlugin::didReceiveResponse(WebLocalFrame* frame,
242 const WebURLResponse& response) {
243 WebFrameClient::didReceiveResponse(frame, identifier, response);