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