b1df67c6f8f7a07e75dff74d76bff82173386099
[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/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"
23
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
44 WebViewPlugin::WebViewPlugin(WebViewPlugin::Delegate* delegate)
45     : delegate_(delegate),
46       container_(NULL),
47       web_view_(WebView::create(this)),
48       web_frame_(WebLocalFrame::create(this)),
49       finished_loading_(false),
50       focused_(false) {
51   web_view_->setMainFrame(web_frame_);
52 }
53
54 // static
55 WebViewPlugin* WebViewPlugin::Create(WebViewPlugin::Delegate* delegate,
56                                      const WebPreferences& preferences,
57                                      const std::string& html_data,
58                                      const GURL& url) {
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);
63   return plugin;
64 }
65
66 WebViewPlugin::~WebViewPlugin() {
67   web_view_->close();
68   web_frame_->close();
69 }
70
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();
76          ++it) {
77       plugin->didReceiveData(
78           it->c_str(), base::checked_cast<int, size_t>(it->length()));
79       total_bytes += it->length();
80     }
81     UMA_HISTOGRAM_MEMORY_KB(
82         "PluginDocument.Memory",
83         (base::checked_cast<int, size_t>(total_bytes / 1024)));
84     UMA_HISTOGRAM_COUNTS(
85         "PluginDocument.NumChunks",
86         (base::checked_cast<int, size_t>(data_.size())));
87   }
88   // We need to transfer the |focused_| to new plugin after it loaded.
89   if (focused_) {
90     plugin->updateFocus(true);
91   }
92   if (finished_loading_) {
93     plugin->didFinishLoading();
94   }
95   if (error_) {
96     plugin->didFailLoading(*error_);
97   }
98 }
99
100 void WebViewPlugin::RestoreTitleText() {
101   if (container_)
102     container_->element().setAttribute("title", old_title_);
103 }
104
105 WebPluginContainer* WebViewPlugin::container() const { return container_; }
106
107 bool WebViewPlugin::initialize(WebPluginContainer* container) {
108   container_ = container;
109   if (container_)
110     old_title_ = container_->element().getAttribute("title");
111   return true;
112 }
113
114 void WebViewPlugin::destroy() {
115   if (delegate_) {
116     delegate_->PluginDestroyed();
117     delegate_ = NULL;
118   }
119   container_ = NULL;
120   base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
121 }
122
123 NPObject* WebViewPlugin::scriptableObject() { return NULL; }
124
125 struct _NPP* WebViewPlugin::pluginNPP() { return NULL; }
126
127 bool WebViewPlugin::getFormValue(WebString& value) { return false; }
128
129 void WebViewPlugin::paint(WebCanvas* canvas, const WebRect& rect) {
130   gfx::Rect paint_rect = gfx::IntersectRects(rect_, rect);
131   if (paint_rect.IsEmpty())
132     return;
133
134   paint_rect.Offset(-rect_.x(), -rect_.y());
135
136   canvas->translate(SkIntToScalar(rect_.x()), SkIntToScalar(rect_.y()));
137   canvas->save();
138
139   web_view_->layout();
140   web_view_->paint(canvas, paint_rect);
141
142   canvas->restore();
143 }
144
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,
149                                    bool is_visible) {
150   if (static_cast<gfx::Rect>(frame_rect) != rect_) {
151     rect_ = frame_rect;
152     WebSize newSize(frame_rect.width, frame_rect.height);
153     web_view_->setFixedLayoutSize(newSize);
154     web_view_->resize(newSize);
155   }
156 }
157
158 void WebViewPlugin::updateFocus(bool focused) {
159   focused_ = focused;
160 }
161
162 bool WebViewPlugin::acceptsInputEvents() { return true; }
163
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)
169     return false;
170
171   if (event.type == WebInputEvent::ContextMenu) {
172     if (delegate_) {
173       const WebMouseEvent& mouse_event =
174           reinterpret_cast<const WebMouseEvent&>(event);
175       delegate_->ShowContextMenu(mouse_event);
176     }
177     return true;
178   }
179   current_cursor_ = cursor;
180   bool handled = web_view_->handleInputEvent(event);
181   cursor = current_cursor_;
182
183   return handled;
184 }
185
186 void WebViewPlugin::didReceiveResponse(const WebURLResponse& response) {
187   DCHECK(response_.isNull());
188   response_ = response;
189 }
190
191 void WebViewPlugin::didReceiveData(const char* data, int data_length) {
192   data_.push_back(std::string(data, data_length));
193 }
194
195 void WebViewPlugin::didFinishLoading() {
196   DCHECK(!finished_loading_);
197   finished_loading_ = true;
198 }
199
200 void WebViewPlugin::didFailLoading(const WebURLError& error) {
201   DCHECK(!error_.get());
202   error_.reset(new WebURLError(error));
203 }
204
205 bool WebViewPlugin::acceptsLoadDrops() { return false; }
206
207 void WebViewPlugin::setToolTipText(const WebString& text,
208                                    blink::WebTextDirection hint) {
209   if (container_)
210     container_->element().setAttribute("title", text);
211 }
212
213 void WebViewPlugin::startDragging(WebLocalFrame*,
214                                   const WebDragData&,
215                                   WebDragOperationsMask,
216                                   const WebImage&,
217                                   const WebPoint&) {
218   // Immediately stop dragging.
219   web_view_->dragSourceSystemDragEnded();
220 }
221
222 bool WebViewPlugin::allowsBrokenNullLayerTreeView() const {
223   return true;
224 }
225
226 void WebViewPlugin::didInvalidateRect(const WebRect& rect) {
227   if (container_)
228     container_->invalidateRect(rect);
229 }
230
231 void WebViewPlugin::didChangeCursor(const WebCursorInfo& cursor) {
232   current_cursor_ = cursor;
233 }
234
235 void WebViewPlugin::didClearWindowObject(WebLocalFrame* frame, int world_id) {
236   if (delegate_)
237     delegate_->BindWebFrame(frame);
238 }
239
240 void WebViewPlugin::didReceiveResponse(WebLocalFrame* frame,
241                                        unsigned identifier,
242                                        const WebURLResponse& response) {
243   WebFrameClient::didReceiveResponse(frame, identifier, response);
244 }