- add sources.
[platform/framework/web/crosswalk.git] / src / components / plugins / renderer / webview_plugin.cc
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.
4
5 #include "components/plugins/renderer/webview_plugin.h"
6
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"
24
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;
44
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);
49 }
50
51 // static
52 WebViewPlugin* WebViewPlugin::Create(WebViewPlugin::Delegate* delegate,
53                                      const WebPreferences& preferences,
54                                      const std::string& html_data,
55                                      const GURL& url) {
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);
60   return plugin;
61 }
62
63 WebViewPlugin::~WebViewPlugin() { web_view_->close(); }
64
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();
70          ++it) {
71       plugin->didReceiveData(
72           it->c_str(), base::checked_numeric_cast<int, size_t>(it->length()));
73       total_bytes += it->length();
74     }
75     UMA_HISTOGRAM_MEMORY_KB(
76         "PluginDocument.Memory",
77         (base::checked_numeric_cast<int, size_t>(total_bytes / 1024)));
78     UMA_HISTOGRAM_COUNTS(
79         "PluginDocument.NumChunks",
80         (base::checked_numeric_cast<int, size_t>(data_.size())));
81   }
82   if (finished_loading_) {
83     plugin->didFinishLoading();
84   }
85   if (error_) {
86     plugin->didFailLoading(*error_);
87   }
88 }
89
90 void WebViewPlugin::RestoreTitleText() {
91   if (container_)
92     container_->element().setAttribute("title", old_title_);
93 }
94
95 WebPluginContainer* WebViewPlugin::container() const { return container_; }
96
97 bool WebViewPlugin::initialize(WebPluginContainer* container) {
98   container_ = container;
99   if (container_)
100     old_title_ = container_->element().getAttribute("title");
101   return true;
102 }
103
104 void WebViewPlugin::destroy() {
105   if (delegate_) {
106     delegate_->WillDestroyPlugin();
107     delegate_ = NULL;
108   }
109   container_ = NULL;
110   base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
111 }
112
113 NPObject* WebViewPlugin::scriptableObject() { return NULL; }
114
115 struct _NPP* WebViewPlugin::pluginNPP() { return NULL; }
116
117 bool WebViewPlugin::getFormValue(WebString& value) { return false; }
118
119 void WebViewPlugin::paint(WebCanvas* canvas, const WebRect& rect) {
120   gfx::Rect paint_rect = gfx::IntersectRects(rect_, rect);
121   if (paint_rect.IsEmpty())
122     return;
123
124   paint_rect.Offset(-rect_.x(), -rect_.y());
125
126   canvas->translate(SkIntToScalar(rect_.x()), SkIntToScalar(rect_.y()));
127   canvas->save();
128
129   web_view_->layout();
130   web_view_->paint(canvas, paint_rect);
131
132   canvas->restore();
133 }
134
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,
139                                    bool is_visible) {
140   if (static_cast<gfx::Rect>(frame_rect) != rect_) {
141     rect_ = frame_rect;
142     web_view_->resize(WebSize(frame_rect.width, frame_rect.height));
143   }
144 }
145
146 bool WebViewPlugin::acceptsInputEvents() { return true; }
147
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)
153     return false;
154
155   if (event.type == WebInputEvent::ContextMenu) {
156     if (delegate_) {
157       const WebMouseEvent& mouse_event =
158           reinterpret_cast<const WebMouseEvent&>(event);
159       delegate_->ShowContextMenu(mouse_event);
160     }
161     return true;
162   }
163   current_cursor_ = cursor;
164   bool handled = web_view_->handleInputEvent(event);
165   cursor = current_cursor_;
166
167   return handled;
168 }
169
170 void WebViewPlugin::didReceiveResponse(const WebURLResponse& response) {
171   DCHECK(response_.isNull());
172   response_ = response;
173 }
174
175 void WebViewPlugin::didReceiveData(const char* data, int data_length) {
176   data_.push_back(std::string(data, data_length));
177 }
178
179 void WebViewPlugin::didFinishLoading() {
180   DCHECK(!finished_loading_);
181   finished_loading_ = true;
182 }
183
184 void WebViewPlugin::didFailLoading(const WebURLError& error) {
185   DCHECK(!error_.get());
186   error_.reset(new WebURLError(error));
187 }
188
189 bool WebViewPlugin::acceptsLoadDrops() { return false; }
190
191 void WebViewPlugin::setToolTipText(const WebString& text,
192                                    WebKit::WebTextDirection hint) {
193   if (container_)
194     container_->element().setAttribute("title", text);
195 }
196
197 void WebViewPlugin::startDragging(WebFrame*,
198                                   const WebDragData&,
199                                   WebDragOperationsMask,
200                                   const WebImage&,
201                                   const WebPoint&) {
202   // Immediately stop dragging.
203   web_view_->dragSourceSystemDragEnded();
204 }
205
206 void WebViewPlugin::didInvalidateRect(const WebRect& rect) {
207   if (container_)
208     container_->invalidateRect(rect);
209 }
210
211 void WebViewPlugin::didChangeCursor(const WebCursorInfo& cursor) {
212   current_cursor_ = cursor;
213 }
214
215 void WebViewPlugin::didClearWindowObject(WebFrame* frame) {
216   if (delegate_)
217     delegate_->BindWebFrame(frame);
218 }
219
220 void WebViewPlugin::didReceiveResponse(WebFrame* frame,
221                                        unsigned identifier,
222                                        const WebURLResponse& response) {
223   WebFrameClient::didReceiveResponse(frame, identifier, response);
224 }