1 // Copyright 2014 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 EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_
6 #define EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_
10 #include "base/memory/weak_ptr.h"
11 #include "base/values.h"
12 #include "content/public/browser/browser_plugin_guest_delegate.h"
13 #include "content/public/browser/render_process_host_observer.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/web_contents_delegate.h"
16 #include "content/public/browser/web_contents_observer.h"
18 struct RendererContentSettingRules
;
20 namespace extensions
{
22 // A GuestViewBase is the base class browser-side API implementation for a
23 // <*view> tag. GuestViewBase maintains an association between a guest
24 // WebContents and an embedder WebContents. It receives events issued from
25 // the guest and relays them to the embedder. GuestViewBase tracks the lifetime
26 // of its embedder render process until it is attached to a particular embedder
27 // WebContents. At that point, its lifetime is restricted in scope to the
28 // lifetime of its embedder WebContents.
29 class GuestViewBase
: public content::BrowserPluginGuestDelegate
,
30 public content::RenderProcessHostObserver
,
31 public content::WebContentsDelegate
,
32 public content::WebContentsObserver
{
36 Event(const std::string
& name
, scoped_ptr
<base::DictionaryValue
> args
);
39 const std::string
& name() const { return name_
; }
41 scoped_ptr
<base::DictionaryValue
> GetArguments();
44 const std::string name_
;
45 scoped_ptr
<base::DictionaryValue
> args_
;
48 // Returns a *ViewGuest if this GuestView is of the given view type.
51 if (IsViewType(T::Type
))
52 return static_cast<T
*>(this);
57 typedef base::Callback
<GuestViewBase
*(
58 content::BrowserContext
*, int)> GuestCreationCallback
;
59 static void RegisterGuestViewType(const std::string
& view_type
,
60 const GuestCreationCallback
& callback
);
62 static GuestViewBase
* Create(content::BrowserContext
* browser_context
,
63 int guest_instance_id
,
64 const std::string
& view_type
);
66 static GuestViewBase
* FromWebContents(content::WebContents
* web_contents
);
68 static GuestViewBase
* From(int embedder_process_id
, int instance_id
);
70 static bool IsGuest(content::WebContents
* web_contents
);
72 virtual const char* GetViewType() const = 0;
74 // This method is called after the guest has been attached to an embedder and
75 // suspended resource loads have been resumed.
77 // This method can be overriden by subclasses. This gives the derived class
78 // an opportunity to perform setup actions after attachment.
79 virtual void DidAttachToEmbedder() {}
81 // This method is called after this GuestViewBase has been initiated.
83 // This gives the derived class an opportunity to perform additional
85 virtual void DidInitialize() {}
87 // This method is called when the initial set of frames within the page have
89 virtual void DidStopLoading() {}
91 // This method is called when the guest's embedder WebContents has been
92 // destroyed and the guest will be destroyed shortly.
94 // This gives the derived class an opportunity to perform some cleanup prior
96 virtual void EmbedderDestroyed() {}
98 // This method is called when the guest WebContents has been destroyed. This
99 // object will be destroyed after this call returns.
101 // This gives the derived class an opportunity to perform some cleanup.
102 virtual void GuestDestroyed() {}
104 // This method is invoked when the guest RenderView is ready, e.g. because we
105 // recreated it after a crash.
107 // This gives the derived class an opportunity to perform some initialization
109 virtual void GuestReady() {}
111 // This method is invoked when the contents auto-resized to give the container
112 // an opportunity to match it if it wishes.
114 // This gives the derived class an opportunity to inform its container element
115 // or perform other actions.
116 virtual void GuestSizeChangedDueToAutoSize(const gfx::Size
& old_size
,
117 const gfx::Size
& new_size
) {}
119 // This method queries whether autosize is supported for this particular view.
120 // By default, autosize is not supported. Derived classes can override this
121 // behavior to support autosize.
122 virtual bool IsAutoSizeSupported() const;
124 // This method queries whether drag-and-drop is enabled for this particular
125 // view. By default, drag-and-drop is disabled. Derived classes can override
126 // this behavior to enable drag-and-drop.
127 virtual bool IsDragAndDropEnabled() const;
129 // This method is called immediately before suspended resource loads have been
130 // resumed on attachment to an embedder.
132 // This method can be overriden by subclasses. This gives the derived class
133 // an opportunity to perform setup actions before attachment.
134 virtual void WillAttachToEmbedder() {}
136 // This method is called when the guest WebContents is about to be destroyed.
138 // This gives the derived class an opportunity to perform some cleanup prior
140 virtual void WillDestroy() {}
142 // This method is to be implemented by the derived class. Access to guest
143 // views are determined by the availability of the internal extension API
144 // used to implement the guest view.
146 // This should be the name of the API as it appears in the _api_features.json
148 virtual const char* GetAPINamespace() const = 0;
150 // This method is to be implemented by the derived class. This method is the
151 // task prefix to show for a task produced by this GuestViewBase's derived
153 virtual int GetTaskPrefix() const = 0;
155 // This method is to be implemented by the derived class. Given a set of
156 // initialization parameters, a concrete subclass of GuestViewBase can
157 // create a specialized WebContents that it returns back to GuestViewBase.
158 typedef base::Callback
<void(content::WebContents
*)>
159 WebContentsCreatedCallback
;
160 virtual void CreateWebContents(
161 const std::string
& embedder_extension_id
,
162 int embedder_render_process_id
,
163 const base::DictionaryValue
& create_params
,
164 const WebContentsCreatedCallback
& callback
) = 0;
166 // This creates a WebContents and initializes |this| GuestViewBase to use the
167 // newly created WebContents.
168 void Init(const std::string
& embedder_extension_id
,
169 content::WebContents
* embedder_web_contents
,
170 const base::DictionaryValue
& create_params
,
171 const WebContentsCreatedCallback
& callback
);
173 void InitWithWebContents(
174 const std::string
& embedder_extension_id
,
175 int embedder_render_process_id
,
176 content::WebContents
* guest_web_contents
);
178 bool IsViewType(const char* const view_type
) const {
179 return !strcmp(GetViewType(), view_type
);
182 // Toggles autosize mode for this GuestView.
183 void SetAutoSize(bool enabled
,
184 const gfx::Size
& min_size
,
185 const gfx::Size
& max_size
);
187 base::WeakPtr
<GuestViewBase
> AsWeakPtr();
189 bool initialized() const { return initialized_
; }
191 content::WebContents
* embedder_web_contents() const {
192 return embedder_web_contents_
;
195 // Returns the guest WebContents.
196 content::WebContents
* guest_web_contents() const {
197 return web_contents();
200 // Returns the parameters associated with the element hosting this GuestView
201 // passed in from JavaScript.
202 base::DictionaryValue
* attach_params() const { return attach_params_
.get(); }
204 // Returns whether this guest has an associated embedder.
205 bool attached() const { return !!embedder_web_contents_
; }
207 // Returns the instance ID of the <*view> element.
208 int view_instance_id() const { return view_instance_id_
; }
210 // Returns the instance ID of this GuestViewBase.
211 int guest_instance_id() const { return guest_instance_id_
; }
213 // Returns the extension ID of the embedder.
214 const std::string
& embedder_extension_id() const {
215 return embedder_extension_id_
;
218 // Returns whether this GuestView is embedded in an extension/app.
219 bool in_extension() const { return !embedder_extension_id_
.empty(); }
221 // Returns the user browser context of the embedder.
222 content::BrowserContext
* browser_context() const { return browser_context_
; }
224 // Returns the embedder's process ID.
225 int embedder_render_process_id() const { return embedder_render_process_id_
; }
227 GuestViewBase
* GetOpener() const {
228 return opener_
.get();
231 // Sets some additional chrome/ initialization parameters.
232 void SetAttachParams(const base::DictionaryValue
& params
);
233 void SetOpener(GuestViewBase
* opener
);
235 // RenderProcessHostObserver implementation
236 virtual void RenderProcessExited(content::RenderProcessHost
* host
,
237 base::ProcessHandle handle
,
238 base::TerminationStatus status
,
239 int exit_code
) OVERRIDE
;
241 // BrowserPluginGuestDelegate implementation.
242 virtual void Destroy() OVERRIDE FINAL
;
243 virtual void DidAttach() OVERRIDE FINAL
;
244 virtual void ElementSizeChanged(const gfx::Size
& old_size
,
245 const gfx::Size
& new_size
) OVERRIDE FINAL
;
246 virtual void GuestSizeChanged(const gfx::Size
& old_size
,
247 const gfx::Size
& new_size
) OVERRIDE FINAL
;
248 virtual void RegisterDestructionCallback(
249 const DestructionCallback
& callback
) OVERRIDE FINAL
;
250 virtual void WillAttach(
251 content::WebContents
* embedder_web_contents
) OVERRIDE FINAL
;
253 // Dispatches an event |event_name| to the embedder with the |event| fields.
254 void DispatchEventToEmbedder(Event
* event
);
257 GuestViewBase(content::BrowserContext
* browser_context
,
258 int guest_instance_id
);
260 virtual ~GuestViewBase();
263 class EmbedderWebContentsObserver
;
265 void SendQueuedEvents();
267 void CompleteInit(const std::string
& embedder_extension_id
,
268 int embedder_render_process_id
,
269 const WebContentsCreatedCallback
& callback
,
270 content::WebContents
* guest_web_contents
);
272 static void RegisterGuestViewTypes();
274 // WebContentsObserver implementation.
275 virtual void DidStopLoading(
276 content::RenderViewHost
* render_view_host
) OVERRIDE FINAL
;
277 virtual void RenderViewReady() OVERRIDE FINAL
;
278 virtual void WebContentsDestroyed() OVERRIDE FINAL
;
280 // WebContentsDelegate implementation.
281 virtual bool ShouldFocusPageAfterCrash() OVERRIDE FINAL
;
282 virtual bool PreHandleGestureEvent(
283 content::WebContents
* source
,
284 const blink::WebGestureEvent
& event
) OVERRIDE FINAL
;
286 content::WebContents
* embedder_web_contents_
;
287 std::string embedder_extension_id_
;
288 int embedder_render_process_id_
;
289 content::BrowserContext
* browser_context_
;
290 // |guest_instance_id_| is a profile-wide unique identifier for a guest
292 const int guest_instance_id_
;
293 // |view_instance_id_| is an identifier that's unique within a particular
294 // embedder RenderViewHost for a particular <*view> instance.
295 int view_instance_id_
;
299 // This is a queue of Events that are destined to be sent to the embedder once
300 // the guest is attached to a particular embedder.
301 std::deque
<linked_ptr
<Event
> > pending_events_
;
303 // The opener guest view.
304 base::WeakPtr
<GuestViewBase
> opener_
;
306 DestructionCallback destruction_callback_
;
308 // The parameters associated with the element hosting this GuestView that
309 // are passed in from JavaScript. This will typically be the view instance ID,
310 // and element-specific parameters. These parameters are passed along to new
311 // guests that are created from this guest.
312 scoped_ptr
<base::DictionaryValue
> attach_params_
;
314 scoped_ptr
<EmbedderWebContentsObserver
> embedder_web_contents_observer_
;
316 // The size of the container element.
317 gfx::Size element_size_
;
319 // The size of the guest content. Note: In autosize mode, the container
320 // element may not match the size of the guest.
321 gfx::Size guest_size_
;
323 // Indicates whether autosize mode is enabled or not.
324 bool auto_size_enabled_
;
326 // The maximum size constraints of the container element in autosize mode.
327 gfx::Size max_auto_size_
;
329 // The minimum size constraints of the container element in autosize mode.
330 gfx::Size min_auto_size_
;
332 // This is used to ensure pending tasks will not fire after this object is
334 base::WeakPtrFactory
<GuestViewBase
> weak_ptr_factory_
;
336 DISALLOW_COPY_AND_ASSIGN(GuestViewBase
);
339 } // namespace extensions
341 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_