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/safe_numerics.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/WebCursorInfo.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/WebFrame.h"
20 #include "third_party/WebKit/public/web/WebInputEvent.h"
21 #include "third_party/WebKit/public/web/WebPluginContainer.h"
22 #include "third_party/WebKit/public/web/WebView.h"
23 #include "webkit/common/webpreferences.h"
25 using WebKit::WebCanvas;
26 using WebKit::WebCursorInfo;
27 using WebKit::WebDragData;
28 using WebKit::WebDragOperationsMask;
29 using WebKit::WebFrame;
30 using WebKit::WebImage;
31 using WebKit::WebInputEvent;
32 using WebKit::WebMouseEvent;
33 using WebKit::WebPlugin;
34 using WebKit::WebPluginContainer;
35 using WebKit::WebPoint;
36 using WebKit::WebRect;
37 using WebKit::WebSize;
38 using WebKit::WebString;
39 using WebKit::WebURLError;
40 using WebKit::WebURLRequest;
41 using WebKit::WebURLResponse;
42 using WebKit::WebVector;
43 using WebKit::WebView;
45 WebViewPlugin::WebViewPlugin(WebViewPlugin::Delegate* delegate)
46 : delegate_(delegate), container_(NULL), finished_loading_(false) {
47 web_view_ = WebView::create(this);
48 web_view_->initializeMainFrame(this);
52 WebViewPlugin* WebViewPlugin::Create(WebViewPlugin::Delegate* delegate,
53 const WebPreferences& preferences,
54 const std::string& html_data,
56 WebViewPlugin* plugin = new WebViewPlugin(delegate);
57 WebView* web_view = plugin->web_view();
58 content::ApplyWebPreferences(preferences, web_view);
59 web_view->mainFrame()->loadHTMLString(html_data, url);
63 WebViewPlugin::~WebViewPlugin() { web_view_->close(); }
65 void WebViewPlugin::ReplayReceivedData(WebPlugin* plugin) {
66 if (!response_.isNull()) {
67 plugin->didReceiveResponse(response_);
68 size_t total_bytes = 0;
69 for (std::list<std::string>::iterator it = data_.begin(); it != data_.end();
71 plugin->didReceiveData(
72 it->c_str(), base::checked_numeric_cast<int, size_t>(it->length()));
73 total_bytes += it->length();
75 UMA_HISTOGRAM_MEMORY_KB(
76 "PluginDocument.Memory",
77 (base::checked_numeric_cast<int, size_t>(total_bytes / 1024)));
79 "PluginDocument.NumChunks",
80 (base::checked_numeric_cast<int, size_t>(data_.size())));
82 if (finished_loading_) {
83 plugin->didFinishLoading();
86 plugin->didFailLoading(*error_);
90 void WebViewPlugin::RestoreTitleText() {
92 container_->element().setAttribute("title", old_title_);
95 WebPluginContainer* WebViewPlugin::container() const { return container_; }
97 bool WebViewPlugin::initialize(WebPluginContainer* container) {
98 container_ = container;
100 old_title_ = container_->element().getAttribute("title");
104 void WebViewPlugin::destroy() {
106 delegate_->WillDestroyPlugin();
110 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
113 NPObject* WebViewPlugin::scriptableObject() { return NULL; }
115 struct _NPP* WebViewPlugin::pluginNPP() { return NULL; }
117 bool WebViewPlugin::getFormValue(WebString& value) { return false; }
119 void WebViewPlugin::paint(WebCanvas* canvas, const WebRect& rect) {
120 gfx::Rect paint_rect = gfx::IntersectRects(rect_, rect);
121 if (paint_rect.IsEmpty())
124 paint_rect.Offset(-rect_.x(), -rect_.y());
126 canvas->translate(SkIntToScalar(rect_.x()), SkIntToScalar(rect_.y()));
130 web_view_->paint(canvas, paint_rect);
135 // Coordinates are relative to the containing window.
136 void WebViewPlugin::updateGeometry(const WebRect& frame_rect,
137 const WebRect& clip_rect,
138 const WebVector<WebRect>& cut_out_rects,
140 if (static_cast<gfx::Rect>(frame_rect) != rect_) {
142 web_view_->resize(WebSize(frame_rect.width, frame_rect.height));
146 bool WebViewPlugin::acceptsInputEvents() { return true; }
148 bool WebViewPlugin::handleInputEvent(const WebInputEvent& event,
149 WebCursorInfo& cursor) {
150 // For tap events, don't handle them. They will be converted to
151 // mouse events later and passed to here.
152 if (event.type == WebInputEvent::GestureTap)
155 if (event.type == WebInputEvent::ContextMenu) {
157 const WebMouseEvent& mouse_event =
158 reinterpret_cast<const WebMouseEvent&>(event);
159 delegate_->ShowContextMenu(mouse_event);
163 current_cursor_ = cursor;
164 bool handled = web_view_->handleInputEvent(event);
165 cursor = current_cursor_;
170 void WebViewPlugin::didReceiveResponse(const WebURLResponse& response) {
171 DCHECK(response_.isNull());
172 response_ = response;
175 void WebViewPlugin::didReceiveData(const char* data, int data_length) {
176 data_.push_back(std::string(data, data_length));
179 void WebViewPlugin::didFinishLoading() {
180 DCHECK(!finished_loading_);
181 finished_loading_ = true;
184 void WebViewPlugin::didFailLoading(const WebURLError& error) {
185 DCHECK(!error_.get());
186 error_.reset(new WebURLError(error));
189 bool WebViewPlugin::acceptsLoadDrops() { return false; }
191 void WebViewPlugin::setToolTipText(const WebString& text,
192 WebKit::WebTextDirection hint) {
194 container_->element().setAttribute("title", text);
197 void WebViewPlugin::startDragging(WebFrame*,
199 WebDragOperationsMask,
202 // Immediately stop dragging.
203 web_view_->dragSourceSystemDragEnded();
206 void WebViewPlugin::didInvalidateRect(const WebRect& rect) {
208 container_->invalidateRect(rect);
211 void WebViewPlugin::didChangeCursor(const WebCursorInfo& cursor) {
212 current_cursor_ = cursor;
215 void WebViewPlugin::didClearWindowObject(WebFrame* frame) {
217 delegate_->BindWebFrame(frame);
220 void WebViewPlugin::didReceiveResponse(WebFrame* frame,
222 const WebURLResponse& response) {
223 WebFrameClient::didReceiveResponse(frame, identifier, response);