webplugin_delegate_proxy.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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_RENDERER_NPAPI_WEBPLUGIN_DELEGATE_PROXY_H_
6#define CONTENT_RENDERER_NPAPI_WEBPLUGIN_DELEGATE_PROXY_H_
7
8#include <string>
9#include <vector>
10
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "base/sequenced_task_runner_helpers.h"
15#include "content/child/npapi/webplugin_delegate.h"
16#include "content/public/common/webplugininfo.h"
17#include "ipc/ipc_listener.h"
18#include "ipc/ipc_message.h"
19#include "ipc/ipc_sender.h"
20#include "ui/gfx/native_widget_types.h"
21#include "ui/gfx/rect.h"
22#include "ui/surface/transport_dib.h"
23#include "url/gurl.h"
24
25#if defined(OS_MACOSX)
26#include "base/containers/hash_tables.h"
27#include "base/memory/linked_ptr.h"
28#endif
29
30struct NPObject;
31struct PluginHostMsg_URLRequest_Params;
32class SkBitmap;
33
34namespace base {
35class WaitableEvent;
36}
37
38namespace content {
39class NPObjectStub;
40class PluginChannelHost;
41class RenderViewImpl;
42class WebPluginImpl;
43
44// An implementation of WebPluginDelegate that proxies all calls to
45// the plugin process.
46class WebPluginDelegateProxy
47    : public WebPluginDelegate,
48      public IPC::Listener,
49      public IPC::Sender,
50      public base::SupportsWeakPtr<WebPluginDelegateProxy> {
51 public:
52  WebPluginDelegateProxy(WebPluginImpl* plugin,
53                         const std::string& mime_type,
54                         const base::WeakPtr<RenderViewImpl>& render_view);
55
56  // WebPluginDelegate implementation:
57  virtual void PluginDestroyed() OVERRIDE;
58  virtual bool Initialize(const GURL& url,
59                          const std::vector<std::string>& arg_names,
60                          const std::vector<std::string>& arg_values,
61                          bool load_manually) OVERRIDE;
62  virtual void UpdateGeometry(const gfx::Rect& window_rect,
63                              const gfx::Rect& clip_rect) OVERRIDE;
64  virtual void Paint(SkCanvas* canvas, const gfx::Rect& rect) OVERRIDE;
65  virtual NPObject* GetPluginScriptableObject() OVERRIDE;
66  virtual struct _NPP* GetPluginNPP() OVERRIDE;
67  virtual bool GetFormValue(string16* value) OVERRIDE;
68  virtual void DidFinishLoadWithReason(const GURL& url, NPReason reason,
69                                       int notify_id) OVERRIDE;
70  virtual void SetFocus(bool focused) OVERRIDE;
71  virtual bool HandleInputEvent(const WebKit::WebInputEvent& event,
72                                WebCursor::CursorInfo* cursor) OVERRIDE;
73  virtual int GetProcessId() OVERRIDE;
74
75  // Informs the plugin that its containing content view has gained or lost
76  // first responder status.
77  virtual void SetContentAreaFocus(bool has_focus);
78#if defined(OS_WIN)
79  // Informs the plugin that plugin IME has updated its status.
80  virtual void ImeCompositionUpdated(
81      const string16& text,
82      const std::vector<int>& clauses,
83      const std::vector<int>& target,
84      int cursor_position,
85      int plugin_id);
86  // Informs the plugin that plugin IME has completed.
87  // If |text| is empty, composition was cancelled.
88  virtual void ImeCompositionCompleted(const string16& text, int plugin_id);
89#endif
90#if defined(OS_MACOSX)
91  // Informs the plugin that its enclosing window has gained or lost focus.
92  virtual void SetWindowFocus(bool window_has_focus);
93  // Informs the plugin that its container (window/tab) has changed visibility.
94  virtual void SetContainerVisibility(bool is_visible);
95  // Informs the plugin that its enclosing window's frame has changed.
96  virtual void WindowFrameChanged(gfx::Rect window_frame, gfx::Rect view_frame);
97  // Informs the plugin that plugin IME has completed.
98  // If |text| is empty, composition was cancelled.
99  virtual void ImeCompositionCompleted(const string16& text, int plugin_id);
100#endif
101
102  // IPC::Listener implementation:
103  virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
104  virtual void OnChannelError() OVERRIDE;
105
106  // IPC::Sender implementation:
107  virtual bool Send(IPC::Message* msg) OVERRIDE;
108
109  virtual void SendJavaScriptStream(const GURL& url,
110                                    const std::string& result,
111                                    bool success,
112                                    int notify_id) OVERRIDE;
113
114  virtual void DidReceiveManualResponse(const GURL& url,
115                                        const std::string& mime_type,
116                                        const std::string& headers,
117                                        uint32 expected_length,
118                                        uint32 last_modified) OVERRIDE;
119  virtual void DidReceiveManualData(const char* buffer, int length) OVERRIDE;
120  virtual void DidFinishManualLoading() OVERRIDE;
121  virtual void DidManualLoadFail() OVERRIDE;
122  virtual WebPluginResourceClient* CreateResourceClient(
123      unsigned long resource_id, const GURL& url, int notify_id) OVERRIDE;
124  virtual WebPluginResourceClient* CreateSeekableResourceClient(
125      unsigned long resource_id, int range_request_id) OVERRIDE;
126  virtual void FetchURL(unsigned long resource_id,
127                        int notify_id,
128                        const GURL& url,
129                        const GURL& first_party_for_cookies,
130                        const std::string& method,
131                        const char* buf,
132                        unsigned int len,
133                        const GURL& referrer,
134                        bool notify_redirects,
135                        bool is_plugin_src_load,
136                        int origin_pid,
137                        int render_view_id) OVERRIDE;
138
139  gfx::PluginWindowHandle GetPluginWindowHandle();
140
141 protected:
142  friend class base::DeleteHelper<WebPluginDelegateProxy>;
143  virtual ~WebPluginDelegateProxy();
144
145 private:
146  struct SharedBitmap {
147    SharedBitmap();
148    ~SharedBitmap();
149
150    scoped_ptr<TransportDIB> dib;
151    scoped_ptr<SkCanvas> canvas;
152  };
153
154  // Message handlers for messages that proxy WebPlugin methods, which
155  // we translate into calls to the real WebPlugin.
156  void OnSetWindow(gfx::PluginWindowHandle window);
157  void OnCompleteURL(const std::string& url_in, std::string* url_out,
158                     bool* result);
159  void OnHandleURLRequest(const PluginHostMsg_URLRequest_Params& params);
160  void OnCancelResource(int id);
161  void OnInvalidateRect(const gfx::Rect& rect);
162  void OnGetWindowScriptNPObject(int route_id, bool* success);
163  void OnResolveProxy(const GURL& url, bool* result, std::string* proxy_list);
164  void OnGetPluginElement(int route_id, bool* success);
165  void OnSetCookie(const GURL& url,
166                   const GURL& first_party_for_cookies,
167                   const std::string& cookie);
168  void OnGetCookies(const GURL& url, const GURL& first_party_for_cookies,
169                    std::string* cookies);
170  void OnCancelDocumentLoad();
171  void OnInitiateHTTPRangeRequest(const std::string& url,
172                                  const std::string& range_info,
173                                  int range_request_id);
174  void OnDidStartLoading();
175  void OnDidStopLoading();
176  void OnDeferResourceLoading(unsigned long resource_id, bool defer);
177  void OnURLRedirectResponse(bool allow, int resource_id);
178  void OnCheckIfRunInsecureContent(const GURL& url, bool* result);
179#if defined(OS_MACOSX)
180  void OnFocusChanged(bool focused);
181  void OnStartIme();
182  // Accelerated (Core Animation) plugin implementation.
183  void OnAcceleratedPluginEnabledRendering();
184  void OnAcceleratedPluginAllocatedIOSurface(int32 width,
185                                             int32 height,
186                                             uint32 surface_id);
187  void OnAcceleratedPluginSwappedIOSurface();
188#endif
189#if defined(OS_WIN)
190  void OnSetWindowlessData(HANDLE modal_loop_pump_messages_event,
191                           gfx::NativeViewId dummy_activation_window);
192  void OnNotifyIMEStatus(const int input_mode, const gfx::Rect& caret_rect);
193#endif
194  // Helper function that sends the UpdateGeometry message.
195  void SendUpdateGeometry(bool bitmaps_changed);
196
197  // Copies the given rectangle from the back-buffer transport_stores_ bitmap to
198  // the front-buffer transport_stores_ bitmap.
199  void CopyFromBackBufferToFrontBuffer(const gfx::Rect& rect);
200
201  // Updates the front-buffer with the given rectangle from the back-buffer,
202  // either by copying the rectangle or flipping the buffers.
203  void UpdateFrontBuffer(const gfx::Rect& rect, bool allow_buffer_flipping);
204
205  // Clears the shared memory section and canvases used for windowless plugins.
206  void ResetWindowlessBitmaps();
207
208  int front_buffer_index() const {
209    return front_buffer_index_;
210  }
211
212  int back_buffer_index() const {
213    return 1 - front_buffer_index_;
214  }
215
216  SkCanvas* front_buffer_canvas() const {
217    return transport_stores_[front_buffer_index()].canvas.get();
218  }
219
220  SkCanvas* back_buffer_canvas() const {
221    return transport_stores_[back_buffer_index()].canvas.get();
222  }
223
224  TransportDIB* front_buffer_dib() const {
225    return transport_stores_[front_buffer_index()].dib.get();
226  }
227
228  TransportDIB* back_buffer_dib() const {
229    return transport_stores_[back_buffer_index()].dib.get();
230  }
231
232#if !defined(OS_WIN)
233  // Creates a process-local memory section and canvas. PlatformCanvas on
234  // Windows only works with a DIB, not arbitrary memory.
235  bool CreateLocalBitmap(std::vector<uint8>* memory,
236                         scoped_ptr<SkCanvas>* canvas);
237#endif
238
239  // Creates a shared memory section and canvas.
240  bool CreateSharedBitmap(scoped_ptr<TransportDIB>* memory,
241                          scoped_ptr<SkCanvas>* canvas);
242
243  // Called for cleanup during plugin destruction. Normally right before the
244  // plugin window gets destroyed, or when the plugin has crashed (at which
245  // point the window has already been destroyed).
246  void WillDestroyWindow();
247
248#if defined(OS_WIN)
249  // Returns true if we should update the plugin geometry synchronously.
250  bool UseSynchronousGeometryUpdates();
251#endif
252
253  base::WeakPtr<RenderViewImpl> render_view_;
254  WebPluginImpl* plugin_;
255  bool uses_shared_bitmaps_;
256#if defined(OS_MACOSX)
257  bool uses_compositor_;
258#elif defined(OS_WIN)
259  // Used for windowless plugins so that keyboard activation works.
260  gfx::NativeViewId dummy_activation_window_;
261#endif
262  gfx::PluginWindowHandle window_;
263  scoped_refptr<PluginChannelHost> channel_host_;
264  std::string mime_type_;
265  int instance_id_;
266  WebPluginInfo info_;
267
268  gfx::Rect plugin_rect_;
269  gfx::Rect clip_rect_;
270
271  NPObject* npobject_;
272
273  // Dummy NPP used to uniquely identify this plugin.
274  scoped_ptr<NPP_t> npp_;
275
276  // Event passed in by the plugin process and is used to decide if messages
277  // need to be pumped in the NPP_HandleEvent sync call.
278  scoped_ptr<base::WaitableEvent> modal_loop_pump_messages_event_;
279
280  // Bitmap for crashed plugin
281  SkBitmap* sad_plugin_;
282
283  // True if we got an invalidate from the plugin and are waiting for a paint.
284  bool invalidate_pending_;
285
286  // If the plugin is transparent or not.
287  bool transparent_;
288
289  // The index in the transport_stores_ array of the current front buffer
290  // (i.e., the buffer to display).
291  int front_buffer_index_;
292  SharedBitmap transport_stores_[2];
293  // This lets us know the total portion of the transport store that has been
294  // painted since the buffers were created.
295  gfx::Rect transport_store_painted_;
296  // This is a bounding box on the portion of the front-buffer that was painted
297  // on the last buffer flip and which has not yet been re-painted in the
298  // back-buffer.
299  gfx::Rect front_buffer_diff_;
300
301  // The url of the main frame hosting the plugin.
302  GURL page_url_;
303
304  DISALLOW_COPY_AND_ASSIGN(WebPluginDelegateProxy);
305};
306
307}  // namespace content
308
309#endif  // CONTENT_RENDERER_NPAPI_WEBPLUGIN_DELEGATE_PROXY_H_
310