1 // Copyright 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_BROWSER_PLUGIN_BROWSER_PLUGIN_H_
6 #define CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_
8 #include "third_party/WebKit/public/web/WebPlugin.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/sequenced_task_runner_helpers.h"
13 #include "content/renderer/browser_plugin/browser_plugin_bindings.h"
14 #include "content/renderer/mouse_lock_dispatcher.h"
15 #include "content/renderer/render_view_impl.h"
16 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
17 #include "third_party/WebKit/public/web/WebDragStatus.h"
18 #include "third_party/WebKit/public/web/WebWidget.h"
20 struct BrowserPluginHostMsg_ResizeGuest_Params;
21 struct BrowserPluginMsg_UpdateRect_Params;
22 struct FrameMsg_BuffersSwapped_Params;
26 class ChildFrameCompositingHelper;
27 class BrowserPluginManager;
28 class MockBrowserPlugin;
30 class CONTENT_EXPORT BrowserPlugin :
31 NON_EXPORTED_BASE(public blink::WebPlugin),
32 public MouseLockDispatcher::LockTarget {
34 RenderViewImpl* render_view() const { return render_view_.get(); }
35 int render_view_routing_id() const { return render_view_routing_id_; }
36 int guest_instance_id() const { return guest_instance_id_; }
37 bool attached() const { return attached_; }
38 BrowserPluginManager* browser_plugin_manager() const {
39 return browser_plugin_manager_.get();
42 bool OnMessageReceived(const IPC::Message& msg);
44 // Update Browser Plugin's DOM Node attribute |attribute_name| with the value
46 void UpdateDOMAttribute(const std::string& attribute_name,
47 const std::string& attribute_value);
48 // Remove the DOM Node attribute with the name |attribute_name|.
49 void RemoveDOMAttribute(const std::string& attribute_name);
50 // Get Browser Plugin's DOM Node attribute |attribute_name|'s value.
51 std::string GetDOMAttributeValue(const std::string& attribute_name) const;
52 // Checks if the attribute |attribute_name| exists in the DOM.
53 bool HasDOMAttribute(const std::string& attribute_name) const;
55 // Get the allowtransparency attribute value.
56 bool GetAllowTransparencyAttribute() const;
57 // Parse the allowtransparency attribute and adjust transparency of
58 // BrowserPlugin accordingly.
59 void ParseAllowTransparencyAttribute();
61 // Get the guest's DOMWindow proxy.
62 NPObject* GetContentWindow() const;
64 // Returns whether the guest process has crashed.
65 bool guest_crashed() const { return guest_crashed_; }
66 // Returns whether this BrowserPlugin has allocated an instance ID.
67 bool HasGuestInstanceID() const;
69 // Informs the guest of an updated focus state.
70 void UpdateGuestFocusState();
71 // Indicates whether the guest should be focused.
72 bool ShouldGuestBeFocused() const;
74 // Embedder's device scale factor changed, we need to update the guest
76 void UpdateDeviceScaleFactor(float device_scale_factor);
78 // A request to enable hardware compositing.
79 void EnableCompositing(bool enable);
81 // Provided that a guest instance ID has been allocated, this method attaches
82 // this BrowserPlugin instance to that guest. |extra_params| are parameters
83 // passed in by the content embedder to the browser process.
84 void Attach(int guest_instance_id,
85 scoped_ptr<base::DictionaryValue> extra_params);
87 // Notify the plugin about a compositor commit so that frame ACKs could be
89 void DidCommitCompositorFrame();
91 // Returns whether a message should be forwarded to BrowserPlugin.
92 static bool ShouldForwardToBrowserPlugin(const IPC::Message& message);
94 // blink::WebPlugin implementation.
95 virtual blink::WebPluginContainer* container() const OVERRIDE;
96 virtual bool initialize(blink::WebPluginContainer* container) OVERRIDE;
97 virtual void destroy() OVERRIDE;
98 virtual NPObject* scriptableObject() OVERRIDE;
99 virtual struct _NPP* pluginNPP() OVERRIDE;
100 virtual bool supportsKeyboardFocus() const OVERRIDE;
101 virtual bool supportsEditCommands() const OVERRIDE;
102 virtual bool supportsInputMethod() const OVERRIDE;
103 virtual bool canProcessDrag() const OVERRIDE;
105 blink::WebCanvas* canvas,
106 const blink::WebRect& rect) OVERRIDE;
107 virtual void updateGeometry(
108 const blink::WebRect& frame_rect,
109 const blink::WebRect& clip_rect,
110 const blink::WebVector<blink::WebRect>& cut_outs_rects,
111 bool is_visible) OVERRIDE;
112 virtual void updateFocus(bool focused) OVERRIDE;
113 virtual void updateVisibility(bool visible) OVERRIDE;
114 virtual bool acceptsInputEvents() OVERRIDE;
115 virtual bool handleInputEvent(
116 const blink::WebInputEvent& event,
117 blink::WebCursorInfo& cursor_info) OVERRIDE;
118 virtual bool handleDragStatusUpdate(blink::WebDragStatus drag_status,
119 const blink::WebDragData& drag_data,
120 blink::WebDragOperationsMask mask,
121 const blink::WebPoint& position,
122 const blink::WebPoint& screen) OVERRIDE;
123 virtual void didReceiveResponse(
124 const blink::WebURLResponse& response) OVERRIDE;
125 virtual void didReceiveData(const char* data, int data_length) OVERRIDE;
126 virtual void didFinishLoading() OVERRIDE;
127 virtual void didFailLoading(const blink::WebURLError& error) OVERRIDE;
128 virtual void didFinishLoadingFrameRequest(
129 const blink::WebURL& url,
130 void* notify_data) OVERRIDE;
131 virtual void didFailLoadingFrameRequest(
132 const blink::WebURL& url,
134 const blink::WebURLError& error) OVERRIDE;
135 virtual bool executeEditCommand(const blink::WebString& name) OVERRIDE;
136 virtual bool executeEditCommand(const blink::WebString& name,
137 const blink::WebString& value) OVERRIDE;
138 virtual bool setComposition(
139 const blink::WebString& text,
140 const blink::WebVector<blink::WebCompositionUnderline>& underlines,
142 int selectionEnd) OVERRIDE;
143 virtual bool confirmComposition(
144 const blink::WebString& text,
145 blink::WebWidget::ConfirmCompositionBehavior selectionBehavior) OVERRIDE;
146 virtual void extendSelectionAndDelete(int before, int after) OVERRIDE;
148 // MouseLockDispatcher::LockTarget implementation.
149 virtual void OnLockMouseACK(bool succeeded) OVERRIDE;
150 virtual void OnMouseLockLost() OVERRIDE;
151 virtual bool HandleMouseLockedInputEvent(
152 const blink::WebMouseEvent& event) OVERRIDE;
155 friend class base::DeleteHelper<BrowserPlugin>;
156 // Only the manager is allowed to create a BrowserPlugin.
157 friend class BrowserPluginManagerImpl;
158 friend class MockBrowserPluginManager;
160 // For unit/integration tests.
161 friend class MockBrowserPlugin;
163 // A BrowserPlugin object is a controller that represents an instance of a
164 // browser plugin within the embedder renderer process. Once a BrowserPlugin
165 // does an initial navigation or is attached to a newly created guest, it
166 // acquires a guest_instance_id as well. The guest instance ID uniquely
167 // identifies a guest WebContents that's hosted by this BrowserPlugin.
168 BrowserPlugin(RenderViewImpl* render_view,
169 blink::WebFrame* frame,
172 virtual ~BrowserPlugin();
174 int width() const { return plugin_rect_.width(); }
175 int height() const { return plugin_rect_.height(); }
176 gfx::Size plugin_size() const { return plugin_rect_.size(); }
177 gfx::Rect plugin_rect() const { return plugin_rect_; }
179 // Virtual to allow for mocking in tests.
180 virtual float GetDeviceScaleFactor() const;
182 void ShowSadGraphic();
184 // Populates BrowserPluginHostMsg_ResizeGuest_Params with resize state.
185 void PopulateResizeGuestParameters(
186 BrowserPluginHostMsg_ResizeGuest_Params* params,
187 const gfx::Size& view_size,
190 // Populates ResizeGuest parameters based on the current
193 BrowserPluginHostMsg_ResizeGuest_Params* resize_guest_params,
196 // IPC message handlers.
197 // Please keep in alphabetical order.
198 void OnAdvanceFocus(int instance_id, bool reverse);
199 void OnAttachACK(int instance_id);
200 void OnBuffersSwapped(int instance_id,
201 const FrameMsg_BuffersSwapped_Params& params);
202 void OnCompositorFrameSwapped(const IPC::Message& message);
203 void OnCopyFromCompositingSurface(int instance_id,
205 gfx::Rect source_rect,
206 gfx::Size dest_size);
207 void OnGuestContentWindowReady(int instance_id,
208 int content_window_routing_id);
209 void OnGuestGone(int instance_id);
210 void OnSetCursor(int instance_id, const WebCursor& cursor);
211 void OnSetMouseLock(int instance_id, bool enable);
212 void OnShouldAcceptTouchEvents(int instance_id, bool accept);
213 void OnUpdateRect(int instance_id,
214 const BrowserPluginMsg_UpdateRect_Params& params);
216 // This is the browser-process-allocated instance ID that uniquely identifies
217 // a guest WebContents.
218 int guest_instance_id_;
219 // This indicates whether this BrowserPlugin has been attached to a
222 const base::WeakPtr<RenderViewImpl> render_view_;
223 // We cache the |render_view_|'s routing ID because we need it on destruction.
224 // If the |render_view_| is destroyed before the BrowserPlugin is destroyed
225 // then we will attempt to access a NULL pointer.
226 const int render_view_routing_id_;
227 blink::WebPluginContainer* container_;
228 scoped_ptr<BrowserPluginBindings> bindings_;
229 bool paint_ack_received_;
230 gfx::Rect plugin_rect_;
231 float last_device_scale_factor_;
232 // Bitmap for crashed plugin. Lazily initialized, non-owning pointer.
233 SkBitmap* sad_guest_;
235 int content_window_routing_id_;
236 bool plugin_focused_;
237 // Tracks the visibility of the browser plugin regardless of the whole
238 // embedder RenderView's visibility.
241 const bool auto_navigate_;
242 std::string html_string_;
246 gfx::Size last_view_size_;
249 // BrowserPlugin outlives RenderViewImpl in Chrome Apps and so we need to
250 // store the BrowserPlugin's BrowserPluginManager in a member variable to
251 // avoid accessing the RenderViewImpl.
252 const scoped_refptr<BrowserPluginManager> browser_plugin_manager_;
254 // Used for HW compositing.
255 scoped_refptr<ChildFrameCompositingHelper> compositing_helper_;
257 // Used to identify the plugin to WebBindings.
258 scoped_ptr<struct _NPP> npp_;
260 std::vector<EditCommand> edit_commands_;
262 // Weak factory used in v8 |MakeWeak| callback, since the v8 callback might
263 // get called after BrowserPlugin has been destroyed.
264 base::WeakPtrFactory<BrowserPlugin> weak_ptr_factory_;
266 DISALLOW_COPY_AND_ASSIGN(BrowserPlugin);
269 } // namespace content
271 #endif // CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_