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_
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"
25 #if defined(OS_MACOSX)
26 #include "base/containers/hash_tables.h"
27 #include "base/memory/linked_ptr.h"
31 struct PluginHostMsg_URLRequest_Params
;
40 class PluginChannelHost
;
41 class RenderFrameImpl
;
45 // An implementation of WebPluginDelegate that proxies all calls to
46 // the plugin process.
47 class WebPluginDelegateProxy
48 : public WebPluginDelegate
,
51 public base::SupportsWeakPtr
<WebPluginDelegateProxy
> {
53 WebPluginDelegateProxy(WebPluginImpl
* plugin
,
54 const std::string
& mime_type
,
55 const base::WeakPtr
<RenderViewImpl
>& render_view
,
56 RenderFrameImpl
* render_frame
);
58 // WebPluginDelegate implementation:
59 void PluginDestroyed() override
;
60 bool Initialize(const GURL
& url
,
61 const std::vector
<std::string
>& arg_names
,
62 const std::vector
<std::string
>& arg_values
,
63 bool load_manually
) override
;
64 void UpdateGeometry(const gfx::Rect
& window_rect
,
65 const gfx::Rect
& clip_rect
) override
;
66 void Paint(SkCanvas
* canvas
, const gfx::Rect
& rect
) override
;
67 NPObject
* GetPluginScriptableObject() override
;
68 struct _NPP
* GetPluginNPP() override
;
69 bool GetFormValue(base::string16
* value
) override
;
70 void DidFinishLoadWithReason(const GURL
& url
,
72 int notify_id
) override
;
73 void SetFocus(bool focused
) override
;
74 bool HandleInputEvent(const blink::WebInputEvent
& event
,
75 WebCursor::CursorInfo
* cursor
) override
;
76 int GetProcessId() override
;
78 // Informs the plugin that its containing content view has gained or lost
79 // first responder status.
80 virtual void SetContentAreaFocus(bool has_focus
);
82 // Informs the plugin that plugin IME has updated its status.
83 virtual void ImeCompositionUpdated(
84 const base::string16
& text
,
85 const std::vector
<int>& clauses
,
86 const std::vector
<int>& target
,
89 // Informs the plugin that plugin IME has completed.
90 // If |text| is empty, composition was cancelled.
91 virtual void ImeCompositionCompleted(const base::string16
& text
,
94 #if defined(OS_MACOSX)
95 // Informs the plugin that its enclosing window has gained or lost focus.
96 virtual void SetWindowFocus(bool window_has_focus
);
97 // Informs the plugin that its container (window/tab) has changed visibility.
98 virtual void SetContainerVisibility(bool is_visible
);
99 // Informs the plugin that its enclosing window's frame has changed.
100 virtual void WindowFrameChanged(gfx::Rect window_frame
, gfx::Rect view_frame
);
101 // Informs the plugin that plugin IME has completed.
102 // If |text| is empty, composition was cancelled.
103 virtual void ImeCompositionCompleted(const base::string16
& text
,
107 // IPC::Listener implementation:
108 bool OnMessageReceived(const IPC::Message
& msg
) override
;
109 void OnChannelError() override
;
111 // IPC::Sender implementation:
112 bool Send(IPC::Message
* msg
) override
;
114 void SendJavaScriptStream(const GURL
& url
,
115 const std::string
& result
,
117 int notify_id
) override
;
119 void DidReceiveManualResponse(const GURL
& url
,
120 const std::string
& mime_type
,
121 const std::string
& headers
,
122 uint32 expected_length
,
123 uint32 last_modified
) override
;
124 void DidReceiveManualData(const char* buffer
, int length
) override
;
125 void DidFinishManualLoading() override
;
126 void DidManualLoadFail() override
;
127 WebPluginResourceClient
* CreateResourceClient(unsigned long resource_id
,
129 int notify_id
) override
;
130 WebPluginResourceClient
* CreateSeekableResourceClient(
131 unsigned long resource_id
,
132 int range_request_id
) override
;
133 void FetchURL(unsigned long resource_id
,
136 const GURL
& first_party_for_cookies
,
137 const std::string
& method
,
140 const GURL
& referrer
,
141 bool notify_redirects
,
142 bool is_plugin_src_load
,
145 int render_view_id
) override
;
147 gfx::PluginWindowHandle
GetPluginWindowHandle();
150 friend class base::DeleteHelper
<WebPluginDelegateProxy
>;
151 ~WebPluginDelegateProxy() override
;
154 struct SharedBitmap
{
158 scoped_ptr
<TransportDIB
> dib
;
159 scoped_ptr
<SkCanvas
> canvas
;
162 // Message handlers for messages that proxy WebPlugin methods, which
163 // we translate into calls to the real WebPlugin.
164 void OnSetWindow(gfx::PluginWindowHandle window
);
165 void OnCompleteURL(const std::string
& url_in
, std::string
* url_out
,
167 void OnHandleURLRequest(const PluginHostMsg_URLRequest_Params
& params
);
168 void OnCancelResource(int id
);
169 void OnInvalidateRect(const gfx::Rect
& rect
);
170 void OnGetWindowScriptNPObject(int route_id
, bool* success
);
171 void OnResolveProxy(const GURL
& url
, bool* result
, std::string
* proxy_list
);
172 void OnGetPluginElement(int route_id
, bool* success
);
173 void OnSetCookie(const GURL
& url
,
174 const GURL
& first_party_for_cookies
,
175 const std::string
& cookie
);
176 void OnGetCookies(const GURL
& url
, const GURL
& first_party_for_cookies
,
177 std::string
* cookies
);
178 void OnCancelDocumentLoad();
179 void OnInitiateHTTPRangeRequest(const std::string
& url
,
180 const std::string
& range_info
,
181 int range_request_id
);
182 void OnDidStartLoading();
183 void OnDidStopLoading();
184 void OnDeferResourceLoading(unsigned long resource_id
, bool defer
);
185 void OnURLRedirectResponse(bool allow
, int resource_id
);
186 void OnCheckIfRunInsecureContent(const GURL
& url
, bool* result
);
187 #if defined(OS_MACOSX)
188 void OnFocusChanged(bool focused
);
190 // Accelerated (Core Animation) plugin implementation.
191 void OnAcceleratedPluginEnabledRendering();
192 void OnAcceleratedPluginAllocatedIOSurface(int32 width
,
195 void OnAcceleratedPluginSwappedIOSurface();
198 void OnSetWindowlessData(HANDLE modal_loop_pump_messages_event
,
199 gfx::NativeViewId dummy_activation_window
);
200 void OnNotifyIMEStatus(const int input_mode
, const gfx::Rect
& caret_rect
);
202 // Helper function that sends the UpdateGeometry message.
203 void SendUpdateGeometry(bool bitmaps_changed
);
205 // Copies the given rectangle from the back-buffer transport_stores_ bitmap to
206 // the front-buffer transport_stores_ bitmap.
207 void CopyFromBackBufferToFrontBuffer(const gfx::Rect
& rect
);
209 // Updates the front-buffer with the given rectangle from the back-buffer,
210 // either by copying the rectangle or flipping the buffers.
211 void UpdateFrontBuffer(const gfx::Rect
& rect
, bool allow_buffer_flipping
);
213 // Clears the shared memory section and canvases used for windowless plugins.
214 void ResetWindowlessBitmaps();
216 int front_buffer_index() const {
217 return front_buffer_index_
;
220 int back_buffer_index() const {
221 return 1 - front_buffer_index_
;
224 SkCanvas
* front_buffer_canvas() const {
225 return transport_stores_
[front_buffer_index()].canvas
.get();
228 SkCanvas
* back_buffer_canvas() const {
229 return transport_stores_
[back_buffer_index()].canvas
.get();
232 TransportDIB
* front_buffer_dib() const {
233 return transport_stores_
[front_buffer_index()].dib
.get();
236 TransportDIB
* back_buffer_dib() const {
237 return transport_stores_
[back_buffer_index()].dib
.get();
241 // Creates a process-local memory section and canvas. PlatformCanvas on
242 // Windows only works with a DIB, not arbitrary memory.
243 bool CreateLocalBitmap(std::vector
<uint8
>* memory
,
244 scoped_ptr
<SkCanvas
>* canvas
);
247 // Creates a shared memory section and canvas.
248 bool CreateSharedBitmap(scoped_ptr
<TransportDIB
>* memory
,
249 scoped_ptr
<SkCanvas
>* canvas
);
251 // Called for cleanup during plugin destruction. Normally right before the
252 // plugin window gets destroyed, or when the plugin has crashed (at which
253 // point the window has already been destroyed).
254 void WillDestroyWindow();
257 // Returns true if we should update the plugin geometry synchronously.
258 bool UseSynchronousGeometryUpdates();
261 base::WeakPtr
<RenderViewImpl
> render_view_
;
262 RenderFrameImpl
* render_frame_
;
263 WebPluginImpl
* plugin_
;
264 bool uses_shared_bitmaps_
;
265 #if defined(OS_MACOSX)
266 bool uses_compositor_
;
267 #elif defined(OS_WIN)
268 // Used for windowless plugins so that keyboard activation works.
269 gfx::NativeViewId dummy_activation_window_
;
271 gfx::PluginWindowHandle window_
;
272 scoped_refptr
<PluginChannelHost
> channel_host_
;
273 std::string mime_type_
;
277 gfx::Rect plugin_rect_
;
278 gfx::Rect clip_rect_
;
282 // Dummy NPP used to uniquely identify this plugin.
283 scoped_ptr
<NPP_t
> npp_
;
285 // Event passed in by the plugin process and is used to decide if messages
286 // need to be pumped in the NPP_HandleEvent sync call.
287 scoped_ptr
<base::WaitableEvent
> modal_loop_pump_messages_event_
;
289 // Bitmap for crashed plugin
290 SkBitmap
* sad_plugin_
;
292 // True if we got an invalidate from the plugin and are waiting for a paint.
293 bool invalidate_pending_
;
295 // If the plugin is transparent or not.
298 // The index in the transport_stores_ array of the current front buffer
299 // (i.e., the buffer to display).
300 int front_buffer_index_
;
301 SharedBitmap transport_stores_
[2];
302 // This lets us know the total portion of the transport store that has been
303 // painted since the buffers were created.
304 gfx::Rect transport_store_painted_
;
305 // This is a bounding box on the portion of the front-buffer that was painted
306 // on the last buffer flip and which has not yet been re-painted in the
308 gfx::Rect front_buffer_diff_
;
310 // The url of the main frame hosting the plugin.
313 DISALLOW_COPY_AND_ASSIGN(WebPluginDelegateProxy
);
316 } // namespace content
318 #endif // CONTENT_RENDERER_NPAPI_WEBPLUGIN_DELEGATE_PROXY_H_