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 #ifndef COMPONENTS_PLUGINS_RENDERER_WEBVIEW_PLUGIN_H_
      6 #define COMPONENTS_PLUGINS_RENDERER_WEBVIEW_PLUGIN_H_
      7 
      8 #include <list>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/sequenced_task_runner_helpers.h"
     12 #include "third_party/WebKit/public/platform/WebCursorInfo.h"
     13 #include "third_party/WebKit/public/platform/WebString.h"
     14 #include "third_party/WebKit/public/platform/WebURLResponse.h"
     15 #include "third_party/WebKit/public/web/WebFrameClient.h"
     16 #include "third_party/WebKit/public/web/WebPlugin.h"
     17 #include "third_party/WebKit/public/web/WebViewClient.h"
     18 
     19 struct WebPreferences;
     20 
     21 namespace blink {
     22 class WebMouseEvent;
     23 }
     24 
     25 // This class implements the WebPlugin interface by forwarding drawing and
     26 // handling input events to a WebView.
     27 // It can be used as a placeholder for an actual plugin, using HTML for the UI.
     28 // To show HTML data inside the WebViewPlugin,
     29 // call web_view->mainFrame()->loadHTMLString() with the HTML data and a fake
     30 // chrome:// URL as origin.
     31 
     32 class WebViewPlugin : public blink::WebPlugin,
     33                       public blink::WebViewClient,
     34                       public blink::WebFrameClient {
     35  public:
     36   class Delegate {
     37    public:
     38     // Bind |frame| to a Javascript object, enabling the delegate to receive
     39     // callback methods from Javascript inside the WebFrame.
     40     // This method is called from WebFrameClient::didClearWindowObject.
     41     virtual void BindWebFrame(blink::WebFrame* frame) = 0;
     42 
     43     // Called upon a context menu event.
     44     virtual void ShowContextMenu(const blink::WebMouseEvent&) = 0;
     45 
     46     // Called when the WebViewPlugin is destroyed.
     47     virtual void PluginDestroyed() = 0;
     48   };
     49 
     50   // Convenience method to set up a new WebViewPlugin using |preferences|
     51   // and displaying |html_data|. |url| should be a (fake) chrome:// URL; it is
     52   // only used for navigation and never actually resolved.
     53   static WebViewPlugin* Create(Delegate* delegate,
     54                                const WebPreferences& preferences,
     55                                const std::string& html_data,
     56                                const GURL& url);
     57 
     58   blink::WebView* web_view() { return web_view_; }
     59 
     60   // When loading a plug-in document (i.e. a full page plug-in not embedded in
     61   // another page), we save all data that has been received, and replay it with
     62   // this method on the actual plug-in.
     63   void ReplayReceivedData(blink::WebPlugin* plugin);
     64 
     65   void RestoreTitleText();
     66 
     67   // WebPlugin methods:
     68   virtual blink::WebPluginContainer* container() const;
     69   virtual bool initialize(blink::WebPluginContainer*);
     70   virtual void destroy();
     71 
     72   virtual NPObject* scriptableObject();
     73   virtual struct _NPP* pluginNPP();
     74 
     75   virtual bool getFormValue(blink::WebString& value);
     76 
     77   virtual void paint(blink::WebCanvas* canvas, const blink::WebRect& rect);
     78 
     79   // Coordinates are relative to the containing window.
     80   virtual void updateGeometry(
     81       const blink::WebRect& frame_rect,
     82       const blink::WebRect& clip_rect,
     83       const blink::WebVector<blink::WebRect>& cut_out_rects,
     84       bool is_visible);
     85 
     86   virtual void updateFocus(bool);
     87   virtual void updateVisibility(bool) {}
     88 
     89   virtual bool acceptsInputEvents();
     90   virtual bool handleInputEvent(const blink::WebInputEvent& event,
     91                                 blink::WebCursorInfo& cursor_info);
     92 
     93   virtual void didReceiveResponse(const blink::WebURLResponse& response);
     94   virtual void didReceiveData(const char* data, int data_length);
     95   virtual void didFinishLoading();
     96   virtual void didFailLoading(const blink::WebURLError& error);
     97 
     98   // Called in response to WebPluginContainer::loadFrameRequest
     99   virtual void didFinishLoadingFrameRequest(const blink::WebURL& url,
    100                                             void* notifyData) {}
    101   virtual void didFailLoadingFrameRequest(const blink::WebURL& url,
    102                                           void* notify_data,
    103                                           const blink::WebURLError& error) {}
    104 
    105   // WebViewClient methods:
    106   virtual bool acceptsLoadDrops();
    107 
    108   virtual void setToolTipText(const blink::WebString&,
    109                               blink::WebTextDirection);
    110 
    111   virtual void startDragging(blink::WebLocalFrame* frame,
    112                              const blink::WebDragData& drag_data,
    113                              blink::WebDragOperationsMask mask,
    114                              const blink::WebImage& image,
    115                              const blink::WebPoint& point);
    116 
    117   // TODO(ojan): Remove this override and have this class use a non-null
    118   // layerTreeView.
    119   virtual bool allowsBrokenNullLayerTreeView() const;
    120 
    121   // WebWidgetClient methods:
    122   virtual void didInvalidateRect(const blink::WebRect&);
    123   virtual void didChangeCursor(const blink::WebCursorInfo& cursor);
    124 
    125   // WebFrameClient methods:
    126   virtual void didClearWindowObject(blink::WebLocalFrame* frame);
    127 
    128   // This method is defined in WebPlugin as well as in WebFrameClient, but with
    129   // different parameters. We only care about implementing the WebPlugin
    130   // version, so we implement this method and call the default in WebFrameClient
    131   // (which does nothing) to correctly overload it.
    132   virtual void didReceiveResponse(blink::WebLocalFrame* frame,
    133                                   unsigned identifier,
    134                                   const blink::WebURLResponse& response);
    135 
    136  private:
    137   friend class base::DeleteHelper<WebViewPlugin>;
    138   WebViewPlugin(Delegate* delegate, const WebPreferences& preferences);
    139   virtual ~WebViewPlugin();
    140 
    141   // Manages its own lifetime.
    142   Delegate* delegate_;
    143 
    144   blink::WebCursorInfo current_cursor_;
    145 
    146   // Owns us.
    147   blink::WebPluginContainer* container_;
    148 
    149   // Owned by us, deleted via |close()|.
    150   blink::WebView* web_view_;
    151 
    152   // Owned by us, deleted via |close()|.
    153   blink::WebFrame* web_frame_;
    154   gfx::Rect rect_;
    155 
    156   blink::WebURLResponse response_;
    157   std::list<std::string> data_;
    158   bool finished_loading_;
    159   scoped_ptr<blink::WebURLError> error_;
    160   blink::WebString old_title_;
    161   bool focused_;
    162 };
    163 
    164 #endif  // COMPONENTS_PLUGINS_RENDERER_WEBVIEW_PLUGIN_H_
    165