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 // A BrowserPluginEmbedder handles messages coming from a BrowserPlugin's 6 // embedder that are not directed at any particular existing guest process. 7 // In the beginning, when a BrowserPlugin instance in the embedder renderer 8 // process requests an initial navigation, the WebContents for that renderer 9 // renderer creates a BrowserPluginEmbedder for itself. The 10 // BrowserPluginEmbedder, in turn, forwards the requests to a 11 // BrowserPluginGuestManager, which creates and manages the lifetime of the new 12 // guest. 13 14 #ifndef CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_EMBEDDER_H_ 15 #define CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_EMBEDDER_H_ 16 17 #include <map> 18 19 #include "base/memory/weak_ptr.h" 20 #include "base/values.h" 21 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents_observer.h" 23 #include "third_party/WebKit/public/web/WebDragOperation.h" 24 25 struct BrowserPluginHostMsg_Attach_Params; 26 struct BrowserPluginHostMsg_ResizeGuest_Params; 27 28 namespace gfx { 29 class Point; 30 } 31 32 namespace content { 33 34 class BrowserPluginGuest; 35 class BrowserPluginGuestManager; 36 class BrowserPluginHostFactory; 37 class RenderWidgetHostImpl; 38 class WebContentsImpl; 39 struct NativeWebKeyboardEvent; 40 41 class CONTENT_EXPORT BrowserPluginEmbedder : public WebContentsObserver { 42 public: 43 virtual ~BrowserPluginEmbedder(); 44 45 static BrowserPluginEmbedder* Create(WebContentsImpl* web_contents); 46 47 // Returns the RenderViewHost at a point (|x|, |y|) asynchronously via 48 // |callback|. We need a roundtrip to renderer process to get this 49 // information. 50 void GetRenderViewHostAtPosition( 51 int x, 52 int y, 53 const WebContents::GetRenderViewHostCallback& callback); 54 55 // Called when embedder's |rwh| has sent screen rects to renderer. 56 void DidSendScreenRects(); 57 58 // Called when embedder's WebContentsImpl has unhandled keyboard input. 59 // Returns whether the BrowserPlugin has handled the keyboard event. 60 // Currently we are only interested in checking for the escape key to 61 // unlock hte guest's pointer lock. 62 bool HandleKeyboardEvent(const NativeWebKeyboardEvent& event); 63 64 // Overrides factory for testing. Default (NULL) value indicates regular 65 // (non-test) environment. 66 static void set_factory_for_testing(BrowserPluginHostFactory* factory) { 67 factory_ = factory; 68 } 69 70 // WebContentsObserver implementation. 71 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; 72 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 73 74 void DragSourceEndedAt(int client_x, int client_y, int screen_x, 75 int screen_y, WebKit::WebDragOperation operation); 76 77 void DragSourceMovedTo(int client_x, int client_y, 78 int screen_x, int screen_y); 79 80 void OnUpdateDragCursor(bool* handled); 81 82 void DragEnteredGuest(BrowserPluginGuest* guest); 83 84 void DragLeftGuest(BrowserPluginGuest* guest); 85 86 void StartDrag(BrowserPluginGuest* guest); 87 88 void StopDrag(BrowserPluginGuest* guest); 89 90 void SystemDragEnded(); 91 92 private: 93 friend class TestBrowserPluginEmbedder; 94 95 BrowserPluginEmbedder(WebContentsImpl* web_contents); 96 97 void CleanUp(); 98 99 BrowserPluginGuestManager* GetBrowserPluginGuestManager(); 100 101 // Message handlers. 102 103 void OnAllocateInstanceID(int request_id); 104 void OnAttach(int instance_id, 105 const BrowserPluginHostMsg_Attach_Params& params, 106 const base::DictionaryValue& extra_params); 107 void OnPluginAtPositionResponse(int instance_id, 108 int request_id, 109 const gfx::Point& position); 110 111 // Static factory instance (always NULL for non-test). 112 static BrowserPluginHostFactory* factory_; 113 114 // Map that contains outstanding queries to |GetRenderViewHostAtPosition|. 115 // We need a roundtrip to the renderer process to retrieve the answer, 116 // so we store these callbacks until we hear back from the renderer. 117 typedef std::map<int, WebContents::GetRenderViewHostCallback> 118 GetRenderViewHostCallbackMap; 119 GetRenderViewHostCallbackMap pending_get_render_view_callbacks_; 120 // Next request id for BrowserPluginMsg_PluginAtPositionRequest query. 121 int next_get_render_view_request_id_; 122 123 // Used to correctly update the cursor when dragging over a guest, and to 124 // handle a race condition when dropping onto the guest that started the drag 125 // (the race is that the dragend message arrives before the drop message so 126 // the drop never takes place). 127 // crbug.com/233571 128 base::WeakPtr<BrowserPluginGuest> guest_dragging_over_; 129 130 // Pointer to the guest that started the drag, used to forward necessary drag 131 // status messages to the correct guest. 132 base::WeakPtr<BrowserPluginGuest> guest_started_drag_; 133 134 DISALLOW_COPY_AND_ASSIGN(BrowserPluginEmbedder); 135 }; 136 137 } // namespace content 138 139 #endif // CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_EMBEDDER_H_ 140