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.
4
5#ifndef  CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_
6#define  CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_
7
8#include "third_party/WebKit/public/web/WebPlugin.h"
9
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"
19
20struct BrowserPluginHostMsg_ResizeGuest_Params;
21struct FrameMsg_BuffersSwapped_Params;
22
23namespace content {
24
25class BrowserPluginDelegate;
26class ChildFrameCompositingHelper;
27class BrowserPluginManager;
28class MockBrowserPlugin;
29
30class CONTENT_EXPORT BrowserPlugin :
31    NON_EXPORTED_BASE(public blink::WebPlugin),
32    public MouseLockDispatcher::LockTarget {
33 public:
34  static BrowserPlugin* GetFromNode(blink::WebNode& node);
35
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();
43  }
44
45  bool OnMessageReceived(const IPC::Message& msg);
46
47  // Update Browser Plugin's DOM Node attribute |attribute_name| with the value
48  // |attribute_value|.
49  void UpdateDOMAttribute(const std::string& attribute_name,
50                          const std::string& attribute_value);
51
52  // Returns whether the guest process has crashed.
53  bool guest_crashed() const { return guest_crashed_; }
54
55  // Informs the guest of an updated focus state.
56  void UpdateGuestFocusState();
57
58  // Indicates whether the guest should be focused.
59  bool ShouldGuestBeFocused() const;
60
61  // Embedder's device scale factor changed, we need to update the guest
62  // renderer.
63  void UpdateDeviceScaleFactor();
64
65  // A request to enable hardware compositing.
66  void EnableCompositing(bool enable);
67
68  // Provided that a guest instance ID has been allocated, this method attaches
69  // this BrowserPlugin instance to that guest.
70  void Attach();
71
72  // Notify the plugin about a compositor commit so that frame ACKs could be
73  // sent, if needed.
74  void DidCommitCompositorFrame();
75
76  // Returns whether a message should be forwarded to BrowserPlugin.
77  static bool ShouldForwardToBrowserPlugin(const IPC::Message& message);
78
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;
87  virtual void paint(
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,
116      void* notify_data,
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,
124      int selectionStart,
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;
130
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;
136
137 private:
138  friend class base::DeleteHelper<BrowserPlugin>;
139  // Only the manager is allowed to create a BrowserPlugin.
140  friend class BrowserPluginManagerImpl;
141  friend class MockBrowserPluginManager;
142
143  // For unit/integration tests.
144  friend class MockBrowserPlugin;
145
146  // A BrowserPlugin object is a controller that represents an instance of a
147  // browser plugin within the embedder renderer process. Once a BrowserPlugin
148  // does an initial navigation or is attached to a newly created guest, it
149  // acquires a browser_plugin_instance_id as well. The guest instance ID
150  // uniquely identifies a guest WebContents that's hosted by this
151  // BrowserPlugin.
152  BrowserPlugin(RenderViewImpl* render_view,
153                blink::WebFrame* frame,
154                scoped_ptr<BrowserPluginDelegate> delegate);
155
156  virtual ~BrowserPlugin();
157
158  int width() const { return plugin_rect_.width(); }
159  int height() const { return plugin_rect_.height(); }
160  gfx::Size plugin_size() const { return plugin_rect_.size(); }
161  gfx::Rect plugin_rect() const { return plugin_rect_; }
162
163  float GetDeviceScaleFactor() const;
164
165  void ShowSadGraphic();
166
167  // Populates BrowserPluginHostMsg_ResizeGuest_Params with resize state.
168  void PopulateResizeGuestParameters(
169      const gfx::Size& view_size,
170      BrowserPluginHostMsg_ResizeGuest_Params* params);
171
172  // IPC message handlers.
173  // Please keep in alphabetical order.
174  void OnAdvanceFocus(int instance_id, bool reverse);
175  void OnAttachACK(int browser_plugin_instance_id);
176  void OnCompositorFrameSwapped(const IPC::Message& message);
177  void OnCopyFromCompositingSurface(int instance_id,
178                                    int request_id,
179                                    gfx::Rect source_rect,
180                                    gfx::Size dest_size);
181  void OnGuestGone(int instance_id);
182  void OnSetContentsOpaque(int instance_id, bool opaque);
183  void OnSetCursor(int instance_id, const WebCursor& cursor);
184  void OnSetMouseLock(int instance_id, bool enable);
185  void OnShouldAcceptTouchEvents(int instance_id, bool accept);
186
187  // This indicates whether this BrowserPlugin has been attached to a
188  // WebContents.
189  bool attached_;
190  bool attach_pending_;
191  const base::WeakPtr<RenderViewImpl> render_view_;
192  // We cache the |render_view_|'s routing ID because we need it on destruction.
193  // If the |render_view_| is destroyed before the BrowserPlugin is destroyed
194  // then we will attempt to access a NULL pointer.
195  const int render_view_routing_id_;
196  blink::WebPluginContainer* container_;
197  gfx::Rect plugin_rect_;
198  float last_device_scale_factor_;
199  // Bitmap for crashed plugin. Lazily initialized, non-owning pointer.
200  SkBitmap* sad_guest_;
201  bool guest_crashed_;
202  bool plugin_focused_;
203  // Tracks the visibility of the browser plugin regardless of the whole
204  // embedder RenderView's visibility.
205  bool visible_;
206
207  WebCursor cursor_;
208
209  bool mouse_locked_;
210
211  // BrowserPlugin outlives RenderViewImpl in Chrome Apps and so we need to
212  // store the BrowserPlugin's BrowserPluginManager in a member variable to
213  // avoid accessing the RenderViewImpl.
214  const scoped_refptr<BrowserPluginManager> browser_plugin_manager_;
215
216  // Used for HW compositing.
217  scoped_refptr<ChildFrameCompositingHelper> compositing_helper_;
218
219  // URL for the embedder frame.
220  int browser_plugin_instance_id_;
221
222  // Indicates whether the guest content is opaque.
223  bool contents_opaque_;
224
225  std::vector<EditCommand> edit_commands_;
226
227  scoped_ptr<BrowserPluginDelegate> delegate_;
228
229  // Weak factory used in v8 |MakeWeak| callback, since the v8 callback might
230  // get called after BrowserPlugin has been destroyed.
231  base::WeakPtrFactory<BrowserPlugin> weak_ptr_factory_;
232
233  DISALLOW_COPY_AND_ASSIGN(BrowserPlugin);
234};
235
236}  // namespace content
237
238#endif  // CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_
239