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 "components/ui/zoom/zoom_observer.h"
13 #include "content/public/browser/browser_plugin_guest_delegate.h"
14 #include "content/public/browser/render_process_host_observer.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_delegate.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "extensions/common/guest_view/guest_view_constants.h"
20 struct RendererContentSettingRules
;
22 namespace extensions
{
24 // A GuestViewBase is the base class browser-side API implementation for a
25 // <*view> tag. GuestViewBase maintains an association between a guest
26 // WebContents and an owner WebContents. It receives events issued from
27 // the guest and relays them to the owner. GuestViewBase tracks the lifetime
28 // of its owner. A GuestViewBase's owner is referred to as an embedder if
29 // it is attached to a container within the owner's WebContents.
30 class GuestViewBase
: public content::BrowserPluginGuestDelegate
,
31 public content::WebContentsDelegate
,
32 public content::WebContentsObserver
,
33 public ui_zoom::ZoomObserver
{
37 Event(const std::string
& name
, scoped_ptr
<base::DictionaryValue
> args
);
40 const std::string
& name() const { return name_
; }
42 scoped_ptr
<base::DictionaryValue
> GetArguments();
45 const std::string name_
;
46 scoped_ptr
<base::DictionaryValue
> args_
;
49 // Returns a *ViewGuest if this GuestView is of the given view type.
52 if (IsViewType(T::Type
))
53 return static_cast<T
*>(this);
58 typedef base::Callback
<GuestViewBase
*(
59 content::BrowserContext
*,
60 content::WebContents
*,
61 int)> GuestCreationCallback
;
62 static void RegisterGuestViewType(const std::string
& view_type
,
63 const GuestCreationCallback
& callback
);
65 static GuestViewBase
* Create(content::BrowserContext
* browser_context
,
66 content::WebContents
* owner_web_contents
,
67 int guest_instance_id
,
68 const std::string
& view_type
);
70 static GuestViewBase
* FromWebContents(content::WebContents
* web_contents
);
72 static GuestViewBase
* From(int embedder_process_id
, int instance_id
);
74 static bool IsGuest(content::WebContents
* web_contents
);
76 virtual const char* GetViewType() const = 0;
78 // This method is called after the guest has been attached to an embedder and
79 // suspended resource loads have been resumed.
81 // This method can be overriden by subclasses. This gives the derived class
82 // an opportunity to perform setup actions after attachment.
83 virtual void DidAttachToEmbedder() {}
85 // This method is called after this GuestViewBase has been initiated.
87 // This gives the derived class an opportunity to perform additional
89 virtual void DidInitialize() {}
91 // This method is called when the initial set of frames within the page have
93 virtual void DidStopLoading() {}
95 // This method is called before the embedder is destroyed.
96 // |owner_web_contents_| should still be valid during this call. This
97 // allows the derived class to perform some cleanup related to the embedder
99 virtual void EmbedderWillBeDestroyed() {}
101 // This method is called when the guest WebContents has been destroyed. This
102 // object will be destroyed after this call returns.
104 // This gives the derived class an opportunity to perform some cleanup.
105 virtual void GuestDestroyed() {}
107 // This method is invoked when the guest RenderView is ready, e.g. because we
108 // recreated it after a crash or after reattachment.
110 // This gives the derived class an opportunity to perform some initialization
112 virtual void GuestReady() {}
114 // This method is invoked when the contents auto-resized to give the container
115 // an opportunity to match it if it wishes.
117 // This gives the derived class an opportunity to inform its container element
118 // or perform other actions.
119 virtual void GuestSizeChangedDueToAutoSize(const gfx::Size
& old_size
,
120 const gfx::Size
& new_size
) {}
122 // This method queries whether autosize is supported for this particular view.
123 // By default, autosize is not supported. Derived classes can override this
124 // behavior to support autosize.
125 virtual bool IsAutoSizeSupported() const;
127 // This method queries whether drag-and-drop is enabled for this particular
128 // view. By default, drag-and-drop is disabled. Derived classes can override
129 // this behavior to enable drag-and-drop.
130 virtual bool IsDragAndDropEnabled() const;
132 // This method is called immediately before suspended resource loads have been
133 // resumed on attachment to an embedder.
135 // This method can be overriden by subclasses. This gives the derived class
136 // an opportunity to perform setup actions before attachment.
137 virtual void WillAttachToEmbedder() {}
139 // This method is called when the guest WebContents is about to be destroyed.
141 // This gives the derived class an opportunity to perform some cleanup prior
143 virtual void WillDestroy() {}
145 // This method is to be implemented by the derived class. This indicates
146 // whether zoom should propagate from the embedder to the guest content.
147 virtual bool ZoomPropagatesFromEmbedderToGuest() const;
149 // This method is to be implemented by the derived class. Access to guest
150 // views are determined by the availability of the internal extension API
151 // used to implement the guest view.
153 // This should be the name of the API as it appears in the _api_features.json
155 virtual const char* GetAPINamespace() const = 0;
157 // This method is to be implemented by the derived class. This method is the
158 // task prefix to show for a task produced by this GuestViewBase's derived
160 virtual int GetTaskPrefix() const = 0;
162 // This method is to be implemented by the derived class. Given a set of
163 // initialization parameters, a concrete subclass of GuestViewBase can
164 // create a specialized WebContents that it returns back to GuestViewBase.
165 typedef base::Callback
<void(content::WebContents
*)>
166 WebContentsCreatedCallback
;
167 virtual void CreateWebContents(
168 const base::DictionaryValue
& create_params
,
169 const WebContentsCreatedCallback
& callback
) = 0;
171 // This creates a WebContents and initializes |this| GuestViewBase to use the
172 // newly created WebContents.
173 void Init(const std::string
& owner_extension_id
,
174 const base::DictionaryValue
& create_params
,
175 const WebContentsCreatedCallback
& callback
);
177 void InitWithWebContents(const std::string
& owner_extension_id
,
178 content::WebContents
* guest_web_contents
);
180 bool IsViewType(const char* const view_type
) const {
181 return !strcmp(GetViewType(), view_type
);
184 // Toggles autosize mode for this GuestView.
185 void SetAutoSize(bool enabled
,
186 const gfx::Size
& min_size
,
187 const gfx::Size
& max_size
);
189 bool initialized() const { return initialized_
; }
191 content::WebContents
* embedder_web_contents() const {
192 return attached() ? owner_web_contents_
: NULL
;
195 content::WebContents
* owner_web_contents() const {
196 return owner_web_contents_
;
199 // Returns the parameters associated with the element hosting this GuestView
200 // passed in from JavaScript.
201 base::DictionaryValue
* attach_params() const { return attach_params_
.get(); }
203 // Returns whether this guest has an associated embedder.
204 bool attached() const {
205 return element_instance_id_
!= guestview::kInstanceIDNone
;
208 // Returns the instance ID of the <*view> element.
209 int view_instance_id() const { return view_instance_id_
; }
211 // Returns the instance ID of this GuestViewBase.
212 int guest_instance_id() const { return guest_instance_id_
; }
214 // Returns the instance ID of the GuestViewBase's element.
215 int element_instance_id() const { return element_instance_id_
; }
217 // Returns the extension ID of the embedder.
218 const std::string
& owner_extension_id() const {
219 return owner_extension_id_
;
222 // Returns whether this GuestView is embedded in an extension/app.
223 bool in_extension() const { return !owner_extension_id_
.empty(); }
225 // Returns the user browser context of the embedder.
226 content::BrowserContext
* browser_context() const { return browser_context_
; }
228 GuestViewBase
* GetOpener() const {
229 return opener_
.get();
232 // Returns the URL of the owner WebContents.
233 const GURL
& GetOwnerSiteURL() const;
235 // Whether the guest view is inside a plugin document.
236 bool is_full_page_plugin() { return is_full_page_plugin_
; }
238 // Destroy this guest.
241 // Saves the attach state of the custom element hosting this GuestView.
242 void SetAttachParams(const base::DictionaryValue
& params
);
243 void SetOpener(GuestViewBase
* opener
);
245 // BrowserPluginGuestDelegate implementation.
246 void DidAttach(int guest_proxy_routing_id
) final
;
247 void DidDetach() final
;
248 void ElementSizeChanged(const gfx::Size
& old_size
,
249 const gfx::Size
& new_size
) final
;
250 content::WebContents
* GetOwnerWebContents() const final
;
251 void GuestSizeChanged(const gfx::Size
& old_size
,
252 const gfx::Size
& new_size
) final
;
253 void RegisterDestructionCallback(const DestructionCallback
& callback
) final
;
254 void WillAttach(content::WebContents
* embedder_web_contents
,
255 int browser_plugin_instance_id
,
256 bool is_full_page_plugin
) final
;
258 // ui_zoom::ZoomObserver implementation.
260 const ui_zoom::ZoomController::ZoomChangedEventData
& data
) override
;
262 // Dispatches an event |event_name| to the embedder with the |event| fields.
263 void DispatchEventToEmbedder(Event
* event
);
266 GuestViewBase(content::BrowserContext
* browser_context
,
267 content::WebContents
* owner_web_contents
,
268 int guest_instance_id
);
270 ~GuestViewBase() override
;
273 class OwnerLifetimeObserver
;
275 class OpenerLifetimeObserver
;
277 void SendQueuedEvents();
279 void CompleteInit(const std::string
& owner_extension_id
,
280 const WebContentsCreatedCallback
& callback
,
281 content::WebContents
* guest_web_contents
);
283 void StartTrackingEmbedderZoomLevel();
284 void StopTrackingEmbedderZoomLevel();
286 static void RegisterGuestViewTypes();
288 // WebContentsObserver implementation.
289 void DidStopLoading(content::RenderViewHost
* render_view_host
) final
;
290 void RenderViewReady() final
;
291 void WebContentsDestroyed() final
;
293 // WebContentsDelegate implementation.
294 void ActivateContents(content::WebContents
* contents
) final
;
295 void DeactivateContents(content::WebContents
* contents
) final
;
296 void RunFileChooser(content::WebContents
* web_contents
,
297 const content::FileChooserParams
& params
) override
;
298 bool ShouldFocusPageAfterCrash() final
;
299 bool PreHandleGestureEvent(content::WebContents
* source
,
300 const blink::WebGestureEvent
& event
) final
;
303 // This guest tracks the lifetime of the WebContents specified by
304 // |owner_web_contents_|. If |owner_web_contents_| is destroyed then this
305 // guest will also self-destruct.
306 content::WebContents
* owner_web_contents_
;
307 std::string owner_extension_id_
;
308 content::BrowserContext
* browser_context_
;
310 // |guest_instance_id_| is a profile-wide unique identifier for a guest
312 const int guest_instance_id_
;
314 // |view_instance_id_| is an identifier that's unique within a particular
315 // embedder RenderViewHost for a particular <*view> instance.
316 int view_instance_id_
;
318 // |element_instance_id_| is an identifer that's unique to a particular
319 // GuestViewContainer element.
320 int element_instance_id_
;
322 // |initialized_| indicates whether GuestViewBase::Init has been called for
326 // Indicates that this guest is in the process of being destroyed.
327 bool is_being_destroyed_
;
329 // This is a queue of Events that are destined to be sent to the embedder once
330 // the guest is attached to a particular embedder.
331 std::deque
<linked_ptr
<Event
> > pending_events_
;
333 // The opener guest view.
334 base::WeakPtr
<GuestViewBase
> opener_
;
336 DestructionCallback destruction_callback_
;
338 // The parameters associated with the element hosting this GuestView that
339 // are passed in from JavaScript. This will typically be the view instance ID,
340 // and element-specific parameters. These parameters are passed along to new
341 // guests that are created from this guest.
342 scoped_ptr
<base::DictionaryValue
> attach_params_
;
344 // This observer ensures that this guest self-destructs if the embedder goes
346 scoped_ptr
<OwnerLifetimeObserver
> owner_lifetime_observer_
;
348 // This observer ensures that if the guest is unattached and its opener goes
349 // away then this guest also self-destructs.
350 scoped_ptr
<OpenerLifetimeObserver
> opener_lifetime_observer_
;
352 // The size of the container element.
353 gfx::Size element_size_
;
355 // The size of the guest content. Note: In autosize mode, the container
356 // element may not match the size of the guest.
357 gfx::Size guest_size_
;
359 // Indicates whether autosize mode is enabled or not.
360 bool auto_size_enabled_
;
362 // The maximum size constraints of the container element in autosize mode.
363 gfx::Size max_auto_size_
;
365 // The minimum size constraints of the container element in autosize mode.
366 gfx::Size min_auto_size_
;
368 // Whether the guest view is inside a plugin document.
369 bool is_full_page_plugin_
;
371 // This is used to ensure pending tasks will not fire after this object is
373 base::WeakPtrFactory
<GuestViewBase
> weak_ptr_factory_
;
375 DISALLOW_COPY_AND_ASSIGN(GuestViewBase
);
378 } // namespace extensions
380 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_