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/guest_sizer.h"
15 #include "content/public/browser/render_process_host_observer.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/browser/web_contents_delegate.h"
18 #include "content/public/browser/web_contents_observer.h"
19 #include "extensions/common/guest_view/guest_view_constants.h"
21 struct RendererContentSettingRules
;
23 namespace extensions
{
25 // A GuestViewBase is the base class browser-side API implementation for a
26 // <*view> tag. GuestViewBase maintains an association between a guest
27 // WebContents and an owner WebContents. It receives events issued from
28 // the guest and relays them to the owner. GuestViewBase tracks the lifetime
29 // of its owner. A GuestViewBase's owner is referred to as an embedder if
30 // it is attached to a container within the owner's WebContents.
31 class GuestViewBase
: public content::BrowserPluginGuestDelegate
,
32 public content::WebContentsDelegate
,
33 public content::WebContentsObserver
,
34 public ui_zoom::ZoomObserver
{
38 Event(const std::string
& name
, scoped_ptr
<base::DictionaryValue
> args
);
41 const std::string
& name() const { return name_
; }
43 scoped_ptr
<base::DictionaryValue
> GetArguments();
46 const std::string name_
;
47 scoped_ptr
<base::DictionaryValue
> args_
;
50 // Returns a *ViewGuest if this GuestView is of the given view type.
53 if (IsViewType(T::Type
))
54 return static_cast<T
*>(this);
59 using GuestCreationCallback
=
60 base::Callback
<GuestViewBase
*(content::WebContents
*, int)>;
61 static void RegisterGuestViewType(const std::string
& view_type
,
62 const GuestCreationCallback
& callback
);
64 static GuestViewBase
* Create(content::WebContents
* owner_web_contents
,
65 int guest_instance_id
,
66 const std::string
& view_type
);
68 static GuestViewBase
* FromWebContents(content::WebContents
* web_contents
);
70 static GuestViewBase
* From(int owner_process_id
, int instance_id
);
72 static bool IsGuest(content::WebContents
* web_contents
);
74 virtual const char* GetViewType() const = 0;
76 // This method is called after the guest has been attached to an embedder and
77 // suspended resource loads have been resumed.
79 // This method can be overriden by subclasses. This gives the derived class
80 // an opportunity to perform setup actions after attachment.
81 virtual void DidAttachToEmbedder() {}
83 // This method is called after this GuestViewBase has been initiated.
85 // This gives the derived class an opportunity to perform additional
87 virtual void DidInitialize(const base::DictionaryValue
& create_params
) {}
89 // This method is called when the initial set of frames within the page have
91 virtual void DidStopLoading() {}
93 // This method is called before the embedder is destroyed.
94 // |owner_web_contents_| should still be valid during this call. This
95 // allows the derived class to perform some cleanup related to the embedder
97 virtual void EmbedderWillBeDestroyed() {}
99 // This method is called when the guest WebContents has been destroyed. This
100 // object will be destroyed after this call returns.
102 // This gives the derived class an opportunity to perform some cleanup.
103 virtual void GuestDestroyed() {}
105 // This method is invoked when the guest RenderView is ready, e.g. because we
106 // recreated it after a crash or after reattachment.
108 // This gives the derived class an opportunity to perform some initialization
110 virtual void GuestReady() {}
112 // This method is invoked when the contents auto-resized to give the container
113 // an opportunity to match it if it wishes.
115 // This gives the derived class an opportunity to inform its container element
116 // or perform other actions.
117 virtual void GuestSizeChangedDueToAutoSize(const gfx::Size
& old_size
,
118 const gfx::Size
& new_size
) {}
120 // This method queries whether autosize is supported for this particular view.
121 // By default, autosize is not supported. Derived classes can override this
122 // behavior to support autosize.
123 virtual bool IsAutoSizeSupported() const;
125 // This method is invoked when the contents preferred size changes. This will
126 // only ever fire if IsPreferredSizeSupported returns true.
127 virtual void OnPreferredSizeChanged(const gfx::Size
& pref_size
) {}
129 // This method queries whether preferred size events are enabled for this
130 // view. By default, preferred size events are disabled, since they add a
131 // small amount of overhead.
132 virtual bool IsPreferredSizeModeEnabled() const;
134 // This method queries whether drag-and-drop is enabled for this particular
135 // view. By default, drag-and-drop is disabled. Derived classes can override
136 // this behavior to enable drag-and-drop.
137 virtual bool IsDragAndDropEnabled() const;
139 // This method is called immediately before suspended resource loads have been
140 // resumed on attachment to an embedder.
142 // This method can be overriden by subclasses. This gives the derived class
143 // an opportunity to perform setup actions before attachment.
144 virtual void WillAttachToEmbedder() {}
146 // This method is called when the guest WebContents is about to be destroyed.
148 // This gives the derived class an opportunity to perform some cleanup prior
150 virtual void WillDestroy() {}
152 // This method is to be implemented by the derived class. This indicates
153 // whether zoom should propagate from the embedder to the guest content.
154 virtual bool ZoomPropagatesFromEmbedderToGuest() const;
156 // This method is to be implemented by the derived class. Access to guest
157 // views are determined by the availability of the internal extension API
158 // used to implement the guest view.
160 // This should be the name of the API as it appears in the _api_features.json
162 virtual const char* GetAPINamespace() const = 0;
164 // This method is to be implemented by the derived class. This method is the
165 // task prefix to show for a task produced by this GuestViewBase's derived
167 virtual int GetTaskPrefix() const = 0;
169 // This method is to be implemented by the derived class. Given a set of
170 // initialization parameters, a concrete subclass of GuestViewBase can
171 // create a specialized WebContents that it returns back to GuestViewBase.
172 using WebContentsCreatedCallback
=
173 base::Callback
<void(content::WebContents
*)>;
174 virtual void CreateWebContents(
175 const base::DictionaryValue
& create_params
,
176 const WebContentsCreatedCallback
& callback
) = 0;
178 // This creates a WebContents and initializes |this| GuestViewBase to use the
179 // newly created WebContents.
180 void Init(const base::DictionaryValue
& create_params
,
181 const WebContentsCreatedCallback
& callback
);
183 void InitWithWebContents(const base::DictionaryValue
& create_params
,
184 content::WebContents
* guest_web_contents
);
186 bool IsViewType(const char* const view_type
) const {
187 return !strcmp(GetViewType(), view_type
);
190 // Toggles autosize mode for this GuestView.
191 void SetAutoSize(bool enabled
,
192 const gfx::Size
& min_size
,
193 const gfx::Size
& max_size
);
195 bool initialized() const { return initialized_
; }
197 content::WebContents
* embedder_web_contents() const {
198 return attached() ? owner_web_contents_
: NULL
;
201 content::WebContents
* owner_web_contents() const {
202 return owner_web_contents_
;
205 // Returns the parameters associated with the element hosting this GuestView
206 // passed in from JavaScript.
207 base::DictionaryValue
* attach_params() const { return attach_params_
.get(); }
209 // Returns whether this guest has an associated embedder.
210 bool attached() const {
211 return element_instance_id_
!= guestview::kInstanceIDNone
;
214 // Returns the instance ID of the <*view> element.
215 int view_instance_id() const { return view_instance_id_
; }
217 // Returns the instance ID of this GuestViewBase.
218 int guest_instance_id() const { return guest_instance_id_
; }
220 // Returns the instance ID of the GuestViewBase's element.
221 int element_instance_id() const { return element_instance_id_
; }
223 // Returns the extension ID of the embedder.
224 const std::string
& owner_extension_id() const {
225 return owner_extension_id_
;
228 // Returns whether this GuestView is embedded in an extension/app.
229 bool in_extension() const { return !owner_extension_id_
.empty(); }
231 bool can_owner_receive_events() const { return !!view_instance_id_
; }
233 // Returns the user browser context of the embedder.
234 content::BrowserContext
* browser_context() const { return browser_context_
; }
236 GuestViewBase
* GetOpener() const {
237 return opener_
.get();
240 // Returns the URL of the owner WebContents.
241 const GURL
& GetOwnerSiteURL() const;
243 // Whether the guest view is inside a plugin document.
244 bool is_full_page_plugin() { return is_full_page_plugin_
; }
246 // Destroy this guest.
249 // Saves the attach state of the custom element hosting this GuestView.
250 void SetAttachParams(const base::DictionaryValue
& params
);
251 void SetOpener(GuestViewBase
* opener
);
253 // BrowserPluginGuestDelegate implementation.
254 content::WebContents
* CreateNewGuestWindow(
255 const content::WebContents::CreateParams
& create_params
) final
;
256 void DidAttach(int guest_proxy_routing_id
) final
;
257 void DidDetach() final
;
258 void ElementSizeChanged(const gfx::Size
& size
) final
;
259 content::WebContents
* GetOwnerWebContents() const final
;
260 void GuestSizeChanged(const gfx::Size
& old_size
,
261 const gfx::Size
& new_size
) final
;
262 void RegisterDestructionCallback(const DestructionCallback
& callback
) final
;
263 void SetGuestSizer(content::GuestSizer
* guest_sizer
) final
;
264 void WillAttach(content::WebContents
* embedder_web_contents
,
265 int browser_plugin_instance_id
,
266 bool is_full_page_plugin
) final
;
268 // ui_zoom::ZoomObserver implementation.
270 const ui_zoom::ZoomController::ZoomChangedEventData
& data
) override
;
272 // Dispatches an event to the guest proxy.
273 void DispatchEventToGuestProxy(Event
* event
);
275 // Dispatches an event to the view.
276 void DispatchEventToView(Event
* event
);
279 GuestViewBase(content::WebContents
* owner_web_contents
,
280 int guest_instance_id
);
282 ~GuestViewBase() override
;
285 class OwnerLifetimeObserver
;
287 class OpenerLifetimeObserver
;
289 void DispatchEvent(Event
* event
, int instance_id
);
291 void SendQueuedEvents();
293 void CompleteInit(scoped_ptr
<base::DictionaryValue
> create_params
,
294 const WebContentsCreatedCallback
& callback
,
295 content::WebContents
* guest_web_contents
);
297 // Dispatches the onResize event to the embedder.
298 void DispatchOnResizeEvent(const gfx::Size
& old_size
,
299 const gfx::Size
& new_size
);
301 void SetUpAutoSize(const base::DictionaryValue
& params
);
303 void StartTrackingEmbedderZoomLevel();
304 void StopTrackingEmbedderZoomLevel();
306 static void RegisterGuestViewTypes();
308 // WebContentsObserver implementation.
309 void DidStopLoading(content::RenderViewHost
* render_view_host
) final
;
310 void RenderViewReady() final
;
311 void WebContentsDestroyed() final
;
313 // WebContentsDelegate implementation.
314 void ActivateContents(content::WebContents
* contents
) final
;
315 void DeactivateContents(content::WebContents
* contents
) final
;
316 void ContentsZoomChange(bool zoom_in
) override
;
317 void HandleKeyboardEvent(
318 content::WebContents
* source
,
319 const content::NativeWebKeyboardEvent
& event
) override
;
320 void RunFileChooser(content::WebContents
* web_contents
,
321 const content::FileChooserParams
& params
) override
;
322 bool ShouldFocusPageAfterCrash() final
;
323 bool PreHandleGestureEvent(content::WebContents
* source
,
324 const blink::WebGestureEvent
& event
) final
;
325 void UpdatePreferredSize(content::WebContents
* web_contents
,
326 const gfx::Size
& pref_size
) final
;
328 // This guest tracks the lifetime of the WebContents specified by
329 // |owner_web_contents_|. If |owner_web_contents_| is destroyed then this
330 // guest will also self-destruct.
331 content::WebContents
* owner_web_contents_
;
332 std::string owner_extension_id_
;
333 content::BrowserContext
* const browser_context_
;
335 // |guest_instance_id_| is a profile-wide unique identifier for a guest
337 const int guest_instance_id_
;
339 // |view_instance_id_| is an identifier that's unique within a particular
340 // embedder RenderViewHost for a particular <*view> instance.
341 int view_instance_id_
;
343 // |element_instance_id_| is an identifer that's unique to a particular
344 // GuestViewContainer element.
345 int element_instance_id_
;
347 // |initialized_| indicates whether GuestViewBase::Init has been called for
351 // Indicates that this guest is in the process of being destroyed.
352 bool is_being_destroyed_
;
354 // This is a queue of Events that are destined to be sent to the embedder once
355 // the guest is attached to a particular embedder.
356 std::deque
<linked_ptr
<Event
> > pending_events_
;
358 // The opener guest view.
359 base::WeakPtr
<GuestViewBase
> opener_
;
361 DestructionCallback destruction_callback_
;
363 // The parameters associated with the element hosting this GuestView that
364 // are passed in from JavaScript. This will typically be the view instance ID,
365 // and element-specific parameters. These parameters are passed along to new
366 // guests that are created from this guest.
367 scoped_ptr
<base::DictionaryValue
> attach_params_
;
369 // This observer ensures that this guest self-destructs if the embedder goes
371 scoped_ptr
<OwnerLifetimeObserver
> owner_lifetime_observer_
;
373 // This observer ensures that if the guest is unattached and its opener goes
374 // away then this guest also self-destructs.
375 scoped_ptr
<OpenerLifetimeObserver
> opener_lifetime_observer_
;
377 // The size of the container element.
378 gfx::Size element_size_
;
380 // The size of the guest content. Note: In autosize mode, the container
381 // element may not match the size of the guest.
382 gfx::Size guest_size_
;
384 // A pointer to the guest_sizer.
385 content::GuestSizer
* guest_sizer_
;
387 // Indicates whether autosize mode is enabled or not.
388 bool auto_size_enabled_
;
390 // The maximum size constraints of the container element in autosize mode.
391 gfx::Size max_auto_size_
;
393 // The minimum size constraints of the container element in autosize mode.
394 gfx::Size min_auto_size_
;
396 // Whether the guest view is inside a plugin document.
397 bool is_full_page_plugin_
;
399 // This is used to ensure pending tasks will not fire after this object is
401 base::WeakPtrFactory
<GuestViewBase
> weak_ptr_factory_
;
403 DISALLOW_COPY_AND_ASSIGN(GuestViewBase
);
406 } // namespace extensions
408 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_