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 struct of parameters for SetSize(). The parameters are all declared as
26 // scoped pointers since they are all optional. Null pointers indicate that the
27 // parameter has not been provided, and the last used value should be used. Note
28 // that when |enable_auto_size| is true, providing |normal_size| is not
29 // meaningful. This is because the normal size of the guestview is overridden
30 // whenever autosizing occurs.
31 struct SetSizeParams
{
35 scoped_ptr
<bool> enable_auto_size
;
36 scoped_ptr
<gfx::Size
> min_size
;
37 scoped_ptr
<gfx::Size
> max_size
;
38 scoped_ptr
<gfx::Size
> normal_size
;
41 // A GuestViewBase is the base class browser-side API implementation for a
42 // <*view> tag. GuestViewBase maintains an association between a guest
43 // WebContents and an owner WebContents. It receives events issued from
44 // the guest and relays them to the owner. GuestViewBase tracks the lifetime
45 // of its owner. A GuestViewBase's owner is referred to as an embedder if
46 // it is attached to a container within the owner's WebContents.
47 class GuestViewBase
: public content::BrowserPluginGuestDelegate
,
48 public content::WebContentsDelegate
,
49 public content::WebContentsObserver
,
50 public ui_zoom::ZoomObserver
{
54 Event(const std::string
& name
, scoped_ptr
<base::DictionaryValue
> args
);
57 const std::string
& name() const { return name_
; }
59 scoped_ptr
<base::DictionaryValue
> GetArguments();
62 const std::string name_
;
63 scoped_ptr
<base::DictionaryValue
> args_
;
66 // Returns a *ViewGuest if this GuestView is of the given view type.
69 if (IsViewType(T::Type
))
70 return static_cast<T
*>(this);
75 using GuestCreationCallback
=
76 base::Callback
<GuestViewBase
*(content::WebContents
*)>;
77 static void RegisterGuestViewType(const std::string
& view_type
,
78 const GuestCreationCallback
& callback
);
80 static GuestViewBase
* Create(content::WebContents
* owner_web_contents
,
81 const std::string
& view_type
);
83 static GuestViewBase
* FromWebContents(content::WebContents
* web_contents
);
85 static GuestViewBase
* From(int owner_process_id
, int instance_id
);
87 static bool IsGuest(content::WebContents
* web_contents
);
89 virtual const char* GetViewType() const = 0;
91 // This method is called after the guest has been attached to an embedder and
92 // suspended resource loads have been resumed.
94 // This method can be overriden by subclasses. This gives the derived class
95 // an opportunity to perform setup actions after attachment.
96 virtual void DidAttachToEmbedder() {}
98 // This method is called after this GuestViewBase has been initiated.
100 // This gives the derived class an opportunity to perform additional
102 virtual void DidInitialize(const base::DictionaryValue
& create_params
) {}
104 // This method is called when the initial set of frames within the page have
105 // completed loading.
106 virtual void DidStopLoading() {}
108 // This method is called before the embedder is destroyed.
109 // |owner_web_contents_| should still be valid during this call. This
110 // allows the derived class to perform some cleanup related to the embedder
112 virtual void EmbedderWillBeDestroyed() {}
114 // This method is called when the guest WebContents has been destroyed. This
115 // object will be destroyed after this call returns.
117 // This gives the derived class an opportunity to perform some cleanup.
118 virtual void GuestDestroyed() {}
120 // This method is invoked when the guest RenderView is ready, e.g. because we
121 // recreated it after a crash or after reattachment.
123 // This gives the derived class an opportunity to perform some initialization
125 virtual void GuestReady() {}
127 // This method is invoked when the contents auto-resized to give the container
128 // an opportunity to match it if it wishes.
130 // This gives the derived class an opportunity to inform its container element
131 // or perform other actions.
132 virtual void GuestSizeChangedDueToAutoSize(const gfx::Size
& old_size
,
133 const gfx::Size
& new_size
) {}
135 // This method queries whether autosize is supported for this particular view.
136 // By default, autosize is not supported. Derived classes can override this
137 // behavior to support autosize.
138 virtual bool IsAutoSizeSupported() const;
140 // This method is invoked when the contents preferred size changes. This will
141 // only ever fire if IsPreferredSizeSupported returns true.
142 virtual void OnPreferredSizeChanged(const gfx::Size
& pref_size
) {}
144 // This method queries whether preferred size events are enabled for this
145 // view. By default, preferred size events are disabled, since they add a
146 // small amount of overhead.
147 virtual bool IsPreferredSizeModeEnabled() const;
149 // This method queries whether drag-and-drop is enabled for this particular
150 // view. By default, drag-and-drop is disabled. Derived classes can override
151 // this behavior to enable drag-and-drop.
152 virtual bool IsDragAndDropEnabled() const;
154 // This method is called immediately before suspended resource loads have been
155 // resumed on attachment to an embedder.
157 // This method can be overriden by subclasses. This gives the derived class
158 // an opportunity to perform setup actions before attachment.
159 virtual void WillAttachToEmbedder() {}
161 // This method is called when the guest WebContents is about to be destroyed.
163 // This gives the derived class an opportunity to perform some cleanup prior
165 virtual void WillDestroy() {}
167 // This method is to be implemented by the derived class. This indicates
168 // whether zoom should propagate from the embedder to the guest content.
169 virtual bool ZoomPropagatesFromEmbedderToGuest() const;
171 // This method is to be implemented by the derived class. Access to guest
172 // views are determined by the availability of the internal extension API
173 // used to implement the guest view.
175 // This should be the name of the API as it appears in the _api_features.json
177 virtual const char* GetAPINamespace() const = 0;
179 // This method is to be implemented by the derived class. This method is the
180 // task prefix to show for a task produced by this GuestViewBase's derived
182 virtual int GetTaskPrefix() const = 0;
184 // This method is to be implemented by the derived class. Given a set of
185 // initialization parameters, a concrete subclass of GuestViewBase can
186 // create a specialized WebContents that it returns back to GuestViewBase.
187 using WebContentsCreatedCallback
=
188 base::Callback
<void(content::WebContents
*)>;
189 virtual void CreateWebContents(
190 const base::DictionaryValue
& create_params
,
191 const WebContentsCreatedCallback
& callback
) = 0;
193 // This creates a WebContents and initializes |this| GuestViewBase to use the
194 // newly created WebContents.
195 void Init(const base::DictionaryValue
& create_params
,
196 const WebContentsCreatedCallback
& callback
);
198 void InitWithWebContents(const base::DictionaryValue
& create_params
,
199 content::WebContents
* guest_web_contents
);
201 bool IsViewType(const char* const view_type
) const {
202 return !strcmp(GetViewType(), view_type
);
205 // Used to toggle autosize mode for this GuestView, and set both the automatic
207 void SetSize(const SetSizeParams
& params
);
209 bool initialized() const { return initialized_
; }
211 content::WebContents
* embedder_web_contents() const {
212 return attached() ? owner_web_contents_
: nullptr;
215 content::WebContents
* owner_web_contents() const {
216 return owner_web_contents_
;
219 // Returns the parameters associated with the element hosting this GuestView
220 // passed in from JavaScript.
221 base::DictionaryValue
* attach_params() const { return attach_params_
.get(); }
223 // Returns whether this guest has an associated embedder.
224 bool attached() const {
225 return element_instance_id_
!= guestview::kInstanceIDNone
;
228 // Returns the instance ID of the <*view> element.
229 int view_instance_id() const { return view_instance_id_
; }
231 // Returns the instance ID of this GuestViewBase.
232 int guest_instance_id() const { return guest_instance_id_
; }
234 // Returns the instance ID of the GuestViewBase's element.
235 int element_instance_id() const { return element_instance_id_
; }
237 // Returns the extension ID of the embedder.
238 const std::string
& owner_extension_id() const {
239 return owner_extension_id_
;
242 // Returns whether this GuestView is embedded in an extension/app.
243 bool in_extension() const { return !owner_extension_id_
.empty(); }
245 bool can_owner_receive_events() const { return !!view_instance_id_
; }
247 // Returns the user browser context of the embedder.
248 content::BrowserContext
* browser_context() const { return browser_context_
; }
250 GuestViewBase
* GetOpener() const {
251 return opener_
.get();
254 // Returns the URL of the owner WebContents.
255 const GURL
& GetOwnerSiteURL() const;
257 // Whether the guest view is inside a plugin document.
258 bool is_full_page_plugin() { return is_full_page_plugin_
; }
260 // Destroy this guest.
263 // Saves the attach state of the custom element hosting this GuestView.
264 void SetAttachParams(const base::DictionaryValue
& params
);
265 void SetOpener(GuestViewBase
* opener
);
267 // BrowserPluginGuestDelegate implementation.
268 content::WebContents
* CreateNewGuestWindow(
269 const content::WebContents::CreateParams
& create_params
) final
;
270 void DidAttach(int guest_proxy_routing_id
) final
;
271 void DidDetach() final
;
272 void ElementSizeChanged(const gfx::Size
& size
) final
;
273 content::WebContents
* GetOwnerWebContents() const final
;
274 void GuestSizeChanged(const gfx::Size
& new_size
) final
;
275 void RegisterDestructionCallback(const DestructionCallback
& callback
) final
;
276 void SetGuestSizer(content::GuestSizer
* guest_sizer
) final
;
277 void WillAttach(content::WebContents
* embedder_web_contents
,
278 int browser_plugin_instance_id
,
279 bool is_full_page_plugin
) final
;
281 // ui_zoom::ZoomObserver implementation.
283 const ui_zoom::ZoomController::ZoomChangedEventData
& data
) override
;
285 // Dispatches an event to the guest proxy.
286 void DispatchEventToGuestProxy(Event
* event
);
288 // Dispatches an event to the view.
289 void DispatchEventToView(Event
* event
);
292 explicit GuestViewBase(content::WebContents
* owner_web_contents
);
294 ~GuestViewBase() override
;
296 // WebContentsObserver implementation.
297 void DidStopLoading(content::RenderViewHost
* render_view_host
) final
;
298 void RenderViewReady() final
;
299 void WebContentsDestroyed() final
;
301 // WebContentsDelegate implementation.
302 void ActivateContents(content::WebContents
* contents
) final
;
303 void DeactivateContents(content::WebContents
* contents
) final
;
304 void ContentsMouseEvent(content::WebContents
* source
,
305 const gfx::Point
& location
,
306 bool motion
) override
;
307 void ContentsZoomChange(bool zoom_in
) override
;
308 void HandleKeyboardEvent(
309 content::WebContents
* source
,
310 const content::NativeWebKeyboardEvent
& event
) override
;
311 void LoadingStateChanged(content::WebContents
* source
,
312 bool to_different_document
) final
;
313 content::ColorChooser
* OpenColorChooser(
314 content::WebContents
* web_contents
,
316 const std::vector
<content::ColorSuggestion
>& suggestions
) override
;
317 void RunFileChooser(content::WebContents
* web_contents
,
318 const content::FileChooserParams
& params
) override
;
319 bool ShouldFocusPageAfterCrash() final
;
320 bool PreHandleGestureEvent(content::WebContents
* source
,
321 const blink::WebGestureEvent
& event
) final
;
322 void UpdatePreferredSize(content::WebContents
* web_contents
,
323 const gfx::Size
& pref_size
) final
;
324 void UpdateTargetURL(content::WebContents
* source
, const GURL
& url
) override
;
327 class OwnerLifetimeObserver
;
329 class OpenerLifetimeObserver
;
331 void DispatchEvent(Event
* event
, int instance_id
);
333 void SendQueuedEvents();
335 void CompleteInit(scoped_ptr
<base::DictionaryValue
> create_params
,
336 const WebContentsCreatedCallback
& callback
,
337 content::WebContents
* guest_web_contents
);
339 // Dispatches the onResize event to the embedder.
340 void DispatchOnResizeEvent(const gfx::Size
& old_size
,
341 const gfx::Size
& new_size
);
343 void SetUpSizing(const base::DictionaryValue
& params
);
345 void StartTrackingEmbedderZoomLevel();
346 void StopTrackingEmbedderZoomLevel();
348 static void RegisterGuestViewTypes();
350 // This guest tracks the lifetime of the WebContents specified by
351 // |owner_web_contents_|. If |owner_web_contents_| is destroyed then this
352 // guest will also self-destruct.
353 content::WebContents
* owner_web_contents_
;
354 std::string owner_extension_id_
;
355 content::BrowserContext
* const browser_context_
;
357 // |guest_instance_id_| is a profile-wide unique identifier for a guest
359 const int guest_instance_id_
;
361 // |view_instance_id_| is an identifier that's unique within a particular
362 // embedder RenderViewHost for a particular <*view> instance.
363 int view_instance_id_
;
365 // |element_instance_id_| is an identifer that's unique to a particular
366 // GuestViewContainer element.
367 int element_instance_id_
;
369 // |initialized_| indicates whether GuestViewBase::Init has been called for
373 // Indicates that this guest is in the process of being destroyed.
374 bool is_being_destroyed_
;
376 // This is a queue of Events that are destined to be sent to the embedder once
377 // the guest is attached to a particular embedder.
378 std::deque
<linked_ptr
<Event
> > pending_events_
;
380 // The opener guest view.
381 base::WeakPtr
<GuestViewBase
> opener_
;
383 DestructionCallback destruction_callback_
;
385 // The parameters associated with the element hosting this GuestView that
386 // are passed in from JavaScript. This will typically be the view instance ID,
387 // and element-specific parameters. These parameters are passed along to new
388 // guests that are created from this guest.
389 scoped_ptr
<base::DictionaryValue
> attach_params_
;
391 // This observer ensures that this guest self-destructs if the embedder goes
393 scoped_ptr
<OwnerLifetimeObserver
> owner_lifetime_observer_
;
395 // This observer ensures that if the guest is unattached and its opener goes
396 // away then this guest also self-destructs.
397 scoped_ptr
<OpenerLifetimeObserver
> opener_lifetime_observer_
;
399 // The size of the guest content. Note: In autosize mode, the container
400 // element may not match the size of the guest.
401 gfx::Size guest_size_
;
403 // A pointer to the guest_sizer.
404 content::GuestSizer
* guest_sizer_
;
406 // Indicates whether autosize mode is enabled or not.
407 bool auto_size_enabled_
;
409 // The maximum size constraints of the container element in autosize mode.
410 gfx::Size max_auto_size_
;
412 // The minimum size constraints of the container element in autosize mode.
413 gfx::Size min_auto_size_
;
415 // The size that will be used when autosize mode is disabled.
416 gfx::Size normal_size_
;
418 // Whether the guest view is inside a plugin document.
419 bool is_full_page_plugin_
;
421 // This is used to ensure pending tasks will not fire after this object is
423 base::WeakPtrFactory
<GuestViewBase
> weak_ptr_factory_
;
425 DISALLOW_COPY_AND_ASSIGN(GuestViewBase
);
428 } // namespace extensions
430 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_