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/mouse_lock_dispatcher.h"
14 #include "content/renderer/render_view_impl.h"
15 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
16 #include "third_party/WebKit/public/web/WebDragStatus.h"
17 #include "third_party/WebKit/public/web/WebNode.h"
18 #include "third_party/WebKit/public/web/WebWidget.h"
20 struct BrowserPluginHostMsg_ResizeGuest_Params
;
21 struct FrameMsg_BuffersSwapped_Params
;
25 class BrowserPluginDelegate
;
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 static BrowserPlugin
* GetFromNode(blink::WebNode
& node
);
36 RenderViewImpl
* render_view() const { return render_view_
.get(); }
37 int render_view_routing_id() const { return render_view_routing_id_
; }
38 int browser_plugin_instance_id() const { return browser_plugin_instance_id_
; }
39 bool attached() const { return attached_
; }
40 bool ready() const { return attached_
|| attach_pending_
; }
41 BrowserPluginManager
* browser_plugin_manager() const {
42 return browser_plugin_manager_
.get();
45 bool OnMessageReceived(const IPC::Message
& msg
);
47 // Update Browser Plugin's DOM Node attribute |attribute_name| with the value
49 void UpdateDOMAttribute(const std::string
& attribute_name
,
50 const std::string
& attribute_value
);
52 // Returns whether the guest process has crashed.
53 bool guest_crashed() const { return guest_crashed_
; }
55 // Informs the guest of an updated focus state.
56 void UpdateGuestFocusState();
58 // Indicates whether the guest should be focused.
59 bool ShouldGuestBeFocused() const;
61 // Embedder's device scale factor changed, we need to update the guest
63 void UpdateDeviceScaleFactor();
65 // A request to enable hardware compositing.
66 void EnableCompositing(bool enable
);
68 // Provided that a guest instance ID has been allocated, this method attaches
69 // this BrowserPlugin instance to that guest.
72 // Notify the plugin about a compositor commit so that frame ACKs could be
74 void DidCommitCompositorFrame();
76 // Returns whether a message should be forwarded to BrowserPlugin.
77 static bool ShouldForwardToBrowserPlugin(const IPC::Message
& message
);
79 // blink::WebPlugin implementation.
80 virtual blink::WebPluginContainer
* container() const override
;
81 virtual bool initialize(blink::WebPluginContainer
* container
) override
;
82 virtual void destroy() override
;
83 virtual bool supportsKeyboardFocus() const override
;
84 virtual bool supportsEditCommands() const override
;
85 virtual bool supportsInputMethod() const override
;
86 virtual bool canProcessDrag() const override
;
88 blink::WebCanvas
* canvas
,
89 const blink::WebRect
& rect
) override
;
90 virtual void updateGeometry(
91 const blink::WebRect
& frame_rect
,
92 const blink::WebRect
& clip_rect
,
93 const blink::WebVector
<blink::WebRect
>& cut_outs_rects
,
94 bool is_visible
) override
;
95 virtual void updateFocus(bool focused
) override
;
96 virtual void updateVisibility(bool visible
) override
;
97 virtual bool acceptsInputEvents() override
;
98 virtual bool handleInputEvent(
99 const blink::WebInputEvent
& event
,
100 blink::WebCursorInfo
& cursor_info
) override
;
101 virtual bool handleDragStatusUpdate(blink::WebDragStatus drag_status
,
102 const blink::WebDragData
& drag_data
,
103 blink::WebDragOperationsMask mask
,
104 const blink::WebPoint
& position
,
105 const blink::WebPoint
& screen
) override
;
106 virtual void didReceiveResponse(
107 const blink::WebURLResponse
& response
) override
;
108 virtual void didReceiveData(const char* data
, int data_length
) override
;
109 virtual void didFinishLoading() override
;
110 virtual void didFailLoading(const blink::WebURLError
& error
) override
;
111 virtual void didFinishLoadingFrameRequest(
112 const blink::WebURL
& url
,
113 void* notify_data
) override
;
114 virtual void didFailLoadingFrameRequest(
115 const blink::WebURL
& url
,
117 const blink::WebURLError
& error
) override
;
118 virtual bool executeEditCommand(const blink::WebString
& name
) override
;
119 virtual bool executeEditCommand(const blink::WebString
& name
,
120 const blink::WebString
& value
) override
;
121 virtual bool setComposition(
122 const blink::WebString
& text
,
123 const blink::WebVector
<blink::WebCompositionUnderline
>& underlines
,
125 int selectionEnd
) override
;
126 virtual bool confirmComposition(
127 const blink::WebString
& text
,
128 blink::WebWidget::ConfirmCompositionBehavior selectionBehavior
) override
;
129 virtual void extendSelectionAndDelete(int before
, int after
) override
;
131 // MouseLockDispatcher::LockTarget implementation.
132 virtual void OnLockMouseACK(bool succeeded
) override
;
133 virtual void OnMouseLockLost() override
;
134 virtual bool HandleMouseLockedInputEvent(
135 const blink::WebMouseEvent
& event
) override
;
138 friend class base::DeleteHelper
<BrowserPlugin
>;
139 // Only the manager is allowed to create a BrowserPlugin.
140 friend class BrowserPluginManager
;
142 // For unit/integration tests.
143 friend class MockBrowserPlugin
;
145 // A BrowserPlugin object is a controller that represents an instance of a
146 // browser plugin within the embedder renderer process. Once a BrowserPlugin
147 // does an initial navigation or is attached to a newly created guest, it
148 // acquires a browser_plugin_instance_id as well. The guest instance ID
149 // uniquely identifies a guest WebContents that's hosted by this
151 BrowserPlugin(RenderViewImpl
* render_view
,
152 blink::WebFrame
* frame
,
153 scoped_ptr
<BrowserPluginDelegate
> delegate
);
155 virtual ~BrowserPlugin();
157 int width() const { return plugin_rect_
.width(); }
158 int height() const { return plugin_rect_
.height(); }
159 gfx::Size
plugin_size() const { return plugin_rect_
.size(); }
160 gfx::Rect
plugin_rect() const { return plugin_rect_
; }
162 float GetDeviceScaleFactor() const;
164 void ShowSadGraphic();
166 // Populates BrowserPluginHostMsg_ResizeGuest_Params with resize state.
167 void PopulateResizeGuestParameters(
168 const gfx::Size
& view_size
,
169 BrowserPluginHostMsg_ResizeGuest_Params
* params
);
171 // IPC message handlers.
172 // Please keep in alphabetical order.
173 void OnAdvanceFocus(int instance_id
, bool reverse
);
174 void OnAttachACK(int browser_plugin_instance_id
);
175 void OnCompositorFrameSwapped(const IPC::Message
& message
);
176 void OnCopyFromCompositingSurface(int instance_id
,
178 gfx::Rect source_rect
,
179 gfx::Size dest_size
);
180 void OnGuestGone(int instance_id
);
181 void OnSetContentsOpaque(int instance_id
, bool opaque
);
182 void OnSetCursor(int instance_id
, const WebCursor
& cursor
);
183 void OnSetMouseLock(int instance_id
, bool enable
);
184 void OnShouldAcceptTouchEvents(int instance_id
, bool accept
);
186 // This indicates whether this BrowserPlugin has been attached to a
189 bool attach_pending_
;
190 const base::WeakPtr
<RenderViewImpl
> render_view_
;
191 // We cache the |render_view_|'s routing ID because we need it on destruction.
192 // If the |render_view_| is destroyed before the BrowserPlugin is destroyed
193 // then we will attempt to access a NULL pointer.
194 const int render_view_routing_id_
;
195 blink::WebPluginContainer
* container_
;
196 gfx::Rect plugin_rect_
;
197 float last_device_scale_factor_
;
198 // Bitmap for crashed plugin. Lazily initialized, non-owning pointer.
199 SkBitmap
* sad_guest_
;
201 bool plugin_focused_
;
202 // Tracks the visibility of the browser plugin regardless of the whole
203 // embedder RenderView's visibility.
210 // This indicates that the BrowserPlugin has a geometry.
213 // BrowserPlugin outlives RenderViewImpl in Chrome Apps and so we need to
214 // store the BrowserPlugin's BrowserPluginManager in a member variable to
215 // avoid accessing the RenderViewImpl.
216 const scoped_refptr
<BrowserPluginManager
> browser_plugin_manager_
;
218 // Used for HW compositing.
219 scoped_refptr
<ChildFrameCompositingHelper
> compositing_helper_
;
221 // URL for the embedder frame.
222 int browser_plugin_instance_id_
;
224 // Indicates whether the guest content is opaque.
225 bool contents_opaque_
;
227 std::vector
<EditCommand
> edit_commands_
;
229 scoped_ptr
<BrowserPluginDelegate
> delegate_
;
231 // Weak factory used in v8 |MakeWeak| callback, since the v8 callback might
232 // get called after BrowserPlugin has been destroyed.
233 base::WeakPtrFactory
<BrowserPlugin
> weak_ptr_factory_
;
235 DISALLOW_COPY_AND_ASSIGN(BrowserPlugin
);
238 } // namespace content
240 #endif // CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_