Upstream version 10.38.220.0
[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/common/web_preferences.h"
11 #include "content/public/renderer/render_view.h"
12 #include "skia/ext/platform_canvas.h"
13 #include "third_party/WebKit/public/platform/WebSize.h"
14 #include "third_party/WebKit/public/platform/WebURL.h"
15 #include "third_party/WebKit/public/platform/WebURLRequest.h"
16 #include "third_party/WebKit/public/platform/WebURLResponse.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/WebInputEvent.h"
20 #include "third_party/WebKit/public/web/WebLocalFrame.h"
21 #include "third_party/WebKit/public/web/WebPluginContainer.h"
22 #include "third_party/WebKit/public/web/WebView.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 using content::WebPreferences;
44
45 WebViewPlugin::WebViewPlugin(WebViewPlugin::Delegate* delegate,
46                              const WebPreferences& preferences)
47     : delegate_(delegate),
48       container_(NULL),
49       web_view_(WebView::create(this)),
50       finished_loading_(false),
51       focused_(false) {
52   // ApplyWebPreferences before making a WebLocalFrame so that the frame sees a
53   // consistent view of our preferences.
54   content::RenderView::ApplyWebPreferences(preferences, web_view_);
55   web_frame_ = WebLocalFrame::create(this);
56   web_view_->setMainFrame(web_frame_);
57 }
58
59 // static
60 WebViewPlugin* WebViewPlugin::Create(WebViewPlugin::Delegate* delegate,
61                                      const WebPreferences& preferences,
62                                      const std::string& html_data,
63                                      const GURL& url) {
64   WebViewPlugin* plugin = new WebViewPlugin(delegate, preferences);
65   plugin->web_view()->mainFrame()->loadHTMLString(html_data, url);
66   return plugin;
67 }
68
69 WebViewPlugin::~WebViewPlugin() {
70   web_view_->close();
71   web_frame_->close();
72 }
73
74 void WebViewPlugin::ReplayReceivedData(WebPlugin* plugin) {
75   if (!response_.isNull()) {
76     plugin->didReceiveResponse(response_);
77     size_t total_bytes = 0;
78     for (std::list<std::string>::iterator it = data_.begin(); it != data_.end();
79          ++it) {
80       plugin->didReceiveData(
81           it->c_str(), base::checked_cast<int, size_t>(it->length()));
82       total_bytes += it->length();
83     }
84     UMA_HISTOGRAM_MEMORY_KB(
85         "PluginDocument.Memory",
86         (base::checked_cast<int, size_t>(total_bytes / 1024)));
87     UMA_HISTOGRAM_COUNTS(
88         "PluginDocument.NumChunks",
89         (base::checked_cast<int, size_t>(data_.size())));
90   }
91   // We need to transfer the |focused_| to new plugin after it loaded.
92   if (focused_) {
93     plugin->updateFocus(true);
94   }
95   if (finished_loading_) {
96     plugin->didFinishLoading();
97   }
98   if (error_) {
99     plugin->didFailLoading(*error_);
100   }
101 }
102
103 void WebViewPlugin::RestoreTitleText() {
104   if (container_)
105     container_->element().setAttribute("title", old_title_);
106 }
107
108 WebPluginContainer* WebViewPlugin::container() const { return container_; }
109
110 bool WebViewPlugin::initialize(WebPluginContainer* container) {
111   container_ = container;
112   if (container_)
113     old_title_ = container_->element().getAttribute("title");
114   return true;
115 }
116
117 void WebViewPlugin::destroy() {
118   if (delegate_) {
119     delegate_->PluginDestroyed();
120     delegate_ = NULL;
121   }
122   container_ = NULL;
123   base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
124 }
125
126 NPObject* WebViewPlugin::scriptableObject() { return NULL; }
127
128 struct _NPP* WebViewPlugin::pluginNPP() { return NULL; }
129
130 bool WebViewPlugin::getFormValue(WebString& value) { return false; }
131
132 void WebViewPlugin::paint(WebCanvas* canvas, const WebRect& rect) {
133   gfx::Rect paint_rect = gfx::IntersectRects(rect_, rect);
134   if (paint_rect.IsEmpty())
135     return;
136
137   paint_rect.Offset(-rect_.x(), -rect_.y());
138
139   canvas->translate(SkIntToScalar(rect_.x()), SkIntToScalar(rect_.y()));
140   canvas->save();
141
142   web_view_->layout();
143   web_view_->paint(canvas, paint_rect);
144
145   canvas->restore();
146 }
147
148 // Coordinates are relative to the containing window.
149 void WebViewPlugin::updateGeometry(const WebRect& frame_rect,
150                                    const WebRect& clip_rect,
151                                    const WebVector<WebRect>& cut_out_rects,
152                                    bool is_visible) {
153   if (static_cast<gfx::Rect>(frame_rect) != rect_) {
154     rect_ = frame_rect;
155     WebSize newSize(frame_rect.width, frame_rect.height);
156     web_view_->setFixedLayoutSize(newSize);
157     web_view_->resize(newSize);
158   }
159 }
160
161 void WebViewPlugin::updateFocus(bool focused) {
162   focused_ = focused;
163 }
164
165 bool WebViewPlugin::acceptsInputEvents() { return true; }
166
167 bool WebViewPlugin::handleInputEvent(const WebInputEvent& event,
168                                      WebCursorInfo& cursor) {
169   // For tap events, don't handle them. They will be converted to
170   // mouse events later and passed to here.
171   if (event.type == WebInputEvent::GestureTap)
172     return false;
173
174   if (event.type == WebInputEvent::ContextMenu) {
175     if (delegate_) {
176       const WebMouseEvent& mouse_event =
177           reinterpret_cast<const WebMouseEvent&>(event);
178       delegate_->ShowContextMenu(mouse_event);
179     }
180     return true;
181   }
182   current_cursor_ = cursor;
183   bool handled = web_view_->handleInputEvent(event);
184   cursor = current_cursor_;
185
186   return handled;
187 }
188
189 void WebViewPlugin::didReceiveResponse(const WebURLResponse& response) {
190   DCHECK(response_.isNull());
191   response_ = response;
192 }
193
194 void WebViewPlugin::didReceiveData(const char* data, int data_length) {
195   data_.push_back(std::string(data, data_length));
196 }
197
198 void WebViewPlugin::didFinishLoading() {
199   DCHECK(!finished_loading_);
200   finished_loading_ = true;
201 }
202
203 void WebViewPlugin::didFailLoading(const WebURLError& error) {
204   DCHECK(!error_.get());
205   error_.reset(new WebURLError(error));
206 }
207
208 bool WebViewPlugin::acceptsLoadDrops() { return false; }
209
210 void WebViewPlugin::setToolTipText(const WebString& text,
211                                    blink::WebTextDirection hint) {
212   if (container_)
213     container_->element().setAttribute("title", text);
214 }
215
216 void WebViewPlugin::startDragging(WebLocalFrame*,
217                                   const WebDragData&,
218                                   WebDragOperationsMask,
219                                   const WebImage&,
220                                   const WebPoint&) {
221   // Immediately stop dragging.
222   web_view_->dragSourceSystemDragEnded();
223 }
224
225 bool WebViewPlugin::allowsBrokenNullLayerTreeView() const {
226   return true;
227 }
228
229 void WebViewPlugin::didInvalidateRect(const WebRect& rect) {
230   if (container_)
231     container_->invalidateRect(rect);
232 }
233
234 void WebViewPlugin::didChangeCursor(const WebCursorInfo& cursor) {
235   current_cursor_ = cursor;
236 }
237
238 void WebViewPlugin::scheduleAnimation() {
239   if (container_)
240     container_->invalidate();
241 }
242
243 void WebViewPlugin::didClearWindowObject(WebLocalFrame* frame) {
244   if (delegate_)
245     delegate_->BindWebFrame(frame);
246 }
247
248 void WebViewPlugin::didReceiveResponse(WebLocalFrame* frame,
249                                        unsigned identifier,
250                                        const WebURLResponse& response) {
251   WebFrameClient::didReceiveResponse(frame, identifier, response);
252 }