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