Home | History | Annotate | Download | only in plugin
      1 // Copyright (c) 2012 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 CONTENT_PLUGIN_WEBPLUGIN_PROXY_H_
      6 #define CONTENT_PLUGIN_WEBPLUGIN_PROXY_H_
      7 
      8 #include <string>
      9 
     10 #include "base/containers/hash_tables.h"
     11 #include "base/memory/ref_counted.h"
     12 #if defined(OS_MACOSX)
     13 #include "base/mac/scoped_cftyperef.h"
     14 #endif
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/memory/shared_memory.h"
     17 #include "base/memory/weak_ptr.h"
     18 #include "base/timer/timer.h"
     19 #include "content/child/npapi/webplugin.h"
     20 #include "ipc/ipc_message.h"
     21 #include "ipc/ipc_sender.h"
     22 #include "skia/ext/refptr.h"
     23 #include "third_party/skia/include/core/SkCanvas.h"
     24 #include "url/gurl.h"
     25 #include "ui/gl/gpu_preference.h"
     26 #include "ui/surface/transport_dib.h"
     27 
     28 struct PluginMsg_FetchURL_Params;
     29 
     30 namespace content {
     31 class PluginChannel;
     32 class WebPluginDelegateImpl;
     33 
     34 #if defined(OS_MACOSX)
     35 class WebPluginAcceleratedSurfaceProxy;
     36 #endif
     37 
     38 // This is an implementation of WebPlugin that proxies all calls to the
     39 // renderer.
     40 class WebPluginProxy : public WebPlugin,
     41                        public IPC::Sender {
     42  public:
     43   // Creates a new proxy for WebPlugin, using the given sender to send the
     44   // marshalled WebPlugin calls.
     45   WebPluginProxy(PluginChannel* channel,
     46                  int route_id,
     47                  const GURL& page_url,
     48                  int host_render_view_routing_id);
     49   virtual ~WebPluginProxy();
     50 
     51   void set_delegate(WebPluginDelegateImpl* d) { delegate_ = d; }
     52 
     53   // WebPlugin overrides
     54   virtual void SetWindow(gfx::PluginWindowHandle window) OVERRIDE;
     55   virtual void SetAcceptsInputEvents(bool accepts) OVERRIDE;
     56   virtual void WillDestroyWindow(gfx::PluginWindowHandle window) OVERRIDE;
     57   virtual void CancelResource(unsigned long id) OVERRIDE;
     58   virtual void Invalidate() OVERRIDE;
     59   virtual void InvalidateRect(const gfx::Rect& rect) OVERRIDE;
     60   virtual NPObject* GetWindowScriptNPObject() OVERRIDE;
     61   virtual NPObject* GetPluginElement() OVERRIDE;
     62   virtual bool FindProxyForUrl(const GURL& url,
     63                                std::string* proxy_list) OVERRIDE;
     64   virtual void SetCookie(const GURL& url,
     65                          const GURL& first_party_for_cookies,
     66                          const std::string& cookie) OVERRIDE;
     67   virtual std::string GetCookies(const GURL& url,
     68                                  const GURL& first_party_for_cookies) OVERRIDE;
     69   virtual void HandleURLRequest(const char* url,
     70                                 const char* method,
     71                                 const char* target,
     72                                 const char* buf,
     73                                 unsigned int len,
     74                                 int notify_id,
     75                                 bool popups_allowed,
     76                                 bool notify_redirects) OVERRIDE;
     77   void UpdateGeometry(const gfx::Rect& window_rect,
     78                       const gfx::Rect& clip_rect,
     79                       const TransportDIB::Handle& windowless_buffer0,
     80                       const TransportDIB::Handle& windowless_buffer1,
     81                       int windowless_buffer_index);
     82   virtual void CancelDocumentLoad() OVERRIDE;
     83   virtual void InitiateHTTPRangeRequest(
     84       const char* url, const char* range_info, int range_request_id) OVERRIDE;
     85   virtual void DidStartLoading() OVERRIDE;
     86   virtual void DidStopLoading() OVERRIDE;
     87   virtual void SetDeferResourceLoading(unsigned long resource_id,
     88                                        bool defer) OVERRIDE;
     89   virtual bool IsOffTheRecord() OVERRIDE;
     90   virtual void ResourceClientDeleted(
     91       WebPluginResourceClient* resource_client) OVERRIDE;
     92   virtual void URLRedirectResponse(bool allow, int resource_id) OVERRIDE;
     93   virtual bool CheckIfRunInsecureContent(const GURL& url) OVERRIDE;
     94 #if defined(OS_WIN)
     95   void SetWindowlessData(HANDLE pump_messages_event,
     96                          gfx::NativeViewId dummy_activation_window);
     97 #endif
     98 #if defined(OS_MACOSX)
     99   virtual void FocusChanged(bool focused) OVERRIDE;
    100   virtual void StartIme() OVERRIDE;
    101   virtual WebPluginAcceleratedSurface*
    102       GetAcceleratedSurface(gfx::GpuPreference gpu_preference) OVERRIDE;
    103   virtual void AcceleratedPluginEnabledRendering() OVERRIDE;
    104   virtual void AcceleratedPluginAllocatedIOSurface(int32 width,
    105                                                    int32 height,
    106                                                    uint32 surface_id) OVERRIDE;
    107   virtual void AcceleratedPluginSwappedIOSurface() OVERRIDE;
    108 #endif
    109 
    110   // IPC::Sender implementation.
    111   virtual bool Send(IPC::Message* msg) OVERRIDE;
    112 
    113   // class-specific methods
    114 
    115   // Returns a WebPluginResourceClient object given its id, or NULL if no
    116   // object with that id exists.
    117   WebPluginResourceClient* GetResourceClient(int id);
    118 
    119   // Returns the id of the renderer that contains this plugin.
    120   int GetRendererId();
    121 
    122   // Returns the id of the associated render view.
    123   int host_render_view_routing_id() const {
    124     return host_render_view_routing_id_;
    125   }
    126 
    127   // For windowless plugins, paints the given rectangle into the local buffer.
    128   void Paint(const gfx::Rect& rect);
    129 
    130   // Callback from the renderer to let us know that a paint occurred.
    131   void DidPaint();
    132 
    133   // Notification received on a plugin issued resource request creation.
    134   void OnResourceCreated(int resource_id, WebPluginResourceClient* client);
    135 
    136 #if defined(OS_WIN) && !defined(USE_AURA)
    137   // Retrieves the IME status from a windowless plug-in and sends it to a
    138   // renderer process. A renderer process will convert the coordinates from
    139   // local to the window coordinates and send the converted coordinates to a
    140   // browser process.
    141   void UpdateIMEStatus();
    142 #endif
    143 
    144  private:
    145   class SharedTransportDIB : public base::RefCounted<SharedTransportDIB> {
    146    public:
    147     explicit SharedTransportDIB(TransportDIB* dib);
    148     TransportDIB* dib() { return dib_.get(); }
    149    private:
    150     friend class base::RefCounted<SharedTransportDIB>;
    151     ~SharedTransportDIB();
    152 
    153     scoped_ptr<TransportDIB> dib_;
    154   };
    155 
    156   // Handler for sending over the paint event to the plugin.
    157   void OnPaint(const gfx::Rect& damaged_rect);
    158 
    159 #if defined(OS_WIN)
    160   void CreateCanvasFromHandle(const TransportDIB::Handle& dib_handle,
    161                               const gfx::Rect& window_rect,
    162                               skia::RefPtr<SkCanvas>* canvas);
    163 #elif defined(OS_MACOSX)
    164   static void CreateDIBAndCGContextFromHandle(
    165       const TransportDIB::Handle& dib_handle,
    166       const gfx::Rect& window_rect,
    167       scoped_ptr<TransportDIB>* dib_out,
    168       base::ScopedCFTypeRef<CGContextRef>* cg_context_out);
    169 #endif
    170 
    171   // Updates the shared memory sections where windowless plugins paint.
    172   void SetWindowlessBuffers(const TransportDIB::Handle& windowless_buffer0,
    173                             const TransportDIB::Handle& windowless_buffer1,
    174                             const gfx::Rect& window_rect);
    175 
    176 #if defined(OS_MACOSX)
    177   CGContextRef windowless_context() const {
    178     return windowless_contexts_[windowless_buffer_index_].get();
    179   }
    180 #else
    181   skia::RefPtr<SkCanvas> windowless_canvas() const {
    182     return windowless_canvases_[windowless_buffer_index_];
    183   }
    184 #endif
    185 
    186   typedef base::hash_map<int, WebPluginResourceClient*> ResourceClientMap;
    187   ResourceClientMap resource_clients_;
    188 
    189   scoped_refptr<PluginChannel> channel_;
    190   int route_id_;
    191   NPObject* window_npobject_;
    192   NPObject* plugin_element_;
    193   WebPluginDelegateImpl* delegate_;
    194   gfx::Rect damaged_rect_;
    195   bool waiting_for_paint_;
    196   // The url of the main frame hosting the plugin.
    197   GURL page_url_;
    198 
    199   // Variables used for desynchronized windowless plugin painting. See note in
    200   // webplugin_delegate_proxy.h for how this works. The two sets of windowless_*
    201   // fields are for the front-buffer and back-buffer of a buffer flipping system
    202   // and windowless_buffer_index_ identifies which set we are using as the
    203   // back-buffer at any given time.
    204   int windowless_buffer_index_;
    205 #if defined(OS_MACOSX)
    206   scoped_ptr<TransportDIB> windowless_dibs_[2];
    207   base::ScopedCFTypeRef<CGContextRef> windowless_contexts_[2];
    208   scoped_ptr<WebPluginAcceleratedSurfaceProxy> accelerated_surface_;
    209 #else
    210   skia::RefPtr<SkCanvas> windowless_canvases_[2];
    211 #endif
    212 
    213   // Contains the routing id of the host render view.
    214   int host_render_view_routing_id_;
    215 
    216   base::WeakPtrFactory<WebPluginProxy> weak_factory_;
    217 };
    218 
    219 }  // namespace content
    220 
    221 #endif  // CONTENT_PLUGIN_WEBPLUGIN_PROXY_H_
    222