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