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_host.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(
84 const content::WebContents
* web_contents
);
86 static GuestViewBase
* From(int owner_process_id
, int instance_id
);
88 static bool IsGuest(content::WebContents
* web_contents
);
90 virtual const char* GetViewType() const = 0;
92 // This method is called after the guest has been attached to an embedder and
93 // suspended resource loads have been resumed.
95 // This method can be overriden by subclasses. This gives the derived class
96 // an opportunity to perform setup actions after attachment.
97 virtual void DidAttachToEmbedder() {}
99 // This method is called after this GuestViewBase has been initiated.
101 // This gives the derived class an opportunity to perform additional
103 virtual void DidInitialize(const base::DictionaryValue
& create_params
) {}
105 // This method is called when the initial set of frames within the page have
106 // completed loading.
107 virtual void GuestViewDidStopLoading() {}
109 // This method is called before the embedder is destroyed.
110 // |owner_web_contents_| should still be valid during this call. This
111 // allows the derived class to perform some cleanup related to the embedder
113 virtual void EmbedderWillBeDestroyed() {}
115 // This method is called when the embedder's zoom changes.
116 virtual void EmbedderZoomChanged(double old_zoom_level
,
117 double new_zoom_level
) {}
119 // This method is called when the guest WebContents has been destroyed. This
120 // object will be destroyed after this call returns.
122 // This gives the derived class an opportunity to perform some cleanup.
123 virtual void GuestDestroyed() {}
125 // This method is invoked when the guest RenderView is ready, e.g. because we
126 // recreated it after a crash or after reattachment.
128 // This gives the derived class an opportunity to perform some initialization
130 virtual void GuestReady() {}
132 // This method is called when the guest's zoom changes.
133 virtual void GuestZoomChanged(double old_zoom_level
, double new_zoom_level
) {}
135 // This method is called when embedder WebContents's fullscreen is toggled.
137 // If the guest asked the embedder to enter fullscreen, the guest uses this
138 // signal to exit fullscreen state.
139 virtual void EmbedderFullscreenToggled(bool entered_fullscreen
) {}
141 // This method is invoked when the contents auto-resized to give the container
142 // an opportunity to match it if it wishes.
144 // This gives the derived class an opportunity to inform its container element
145 // or perform other actions.
146 virtual void GuestSizeChangedDueToAutoSize(const gfx::Size
& old_size
,
147 const gfx::Size
& new_size
) {}
149 // This method queries whether autosize is supported for this particular view.
150 // By default, autosize is not supported. Derived classes can override this
151 // behavior to support autosize.
152 virtual bool IsAutoSizeSupported() const;
154 // This method is invoked when the contents preferred size changes. This will
155 // only ever fire if IsPreferredSizeSupported returns true.
156 virtual void OnPreferredSizeChanged(const gfx::Size
& pref_size
) {}
158 // This method queries whether preferred size events are enabled for this
159 // view. By default, preferred size events are disabled, since they add a
160 // small amount of overhead.
161 virtual bool IsPreferredSizeModeEnabled() const;
163 // This method queries whether drag-and-drop is enabled for this particular
164 // view. By default, drag-and-drop is disabled. Derived classes can override
165 // this behavior to enable drag-and-drop.
166 virtual bool IsDragAndDropEnabled() const;
168 // This method is called immediately before suspended resource loads have been
169 // resumed on attachment to an embedder.
171 // This method can be overriden by subclasses. This gives the derived class
172 // an opportunity to perform setup actions before attachment.
173 virtual void WillAttachToEmbedder() {}
175 // This method is called when the guest WebContents is about to be destroyed.
177 // This gives the derived class an opportunity to perform some cleanup prior
179 virtual void WillDestroy() {}
181 // This method is to be implemented by the derived class. This indicates
182 // whether zoom should propagate from the embedder to the guest content.
183 virtual bool ZoomPropagatesFromEmbedderToGuest() const;
185 // This method is to be implemented by the derived class. Access to guest
186 // views are determined by the availability of the internal extension API
187 // used to implement the guest view.
189 // This should be the name of the API as it appears in the _api_features.json
191 virtual const char* GetAPINamespace() const = 0;
193 // This method is to be implemented by the derived class. This method is the
194 // task prefix to show for a task produced by this GuestViewBase's derived
196 virtual int GetTaskPrefix() const = 0;
198 // This method is to be implemented by the derived class. Given a set of
199 // initialization parameters, a concrete subclass of GuestViewBase can
200 // create a specialized WebContents that it returns back to GuestViewBase.
201 using WebContentsCreatedCallback
=
202 base::Callback
<void(content::WebContents
*)>;
203 virtual void CreateWebContents(
204 const base::DictionaryValue
& create_params
,
205 const WebContentsCreatedCallback
& callback
) = 0;
207 // This creates a WebContents and initializes |this| GuestViewBase to use the
208 // newly created WebContents.
209 void Init(const base::DictionaryValue
& create_params
,
210 const WebContentsCreatedCallback
& callback
);
212 void InitWithWebContents(const base::DictionaryValue
& create_params
,
213 content::WebContents
* guest_web_contents
);
215 void LoadURLWithParams(
216 const content::NavigationController::LoadURLParams
& load_params
);
218 bool IsViewType(const char* const view_type
) const {
219 return !strcmp(GetViewType(), view_type
);
222 // Used to toggle autosize mode for this GuestView, and set both the automatic
224 void SetSize(const SetSizeParams
& params
);
226 bool initialized() const { return initialized_
; }
228 content::WebContents
* embedder_web_contents() const {
229 return attached() ? owner_web_contents_
: nullptr;
232 content::WebContents
* owner_web_contents() const {
233 return owner_web_contents_
;
236 content::GuestHost
* host() const {
240 // Returns the parameters associated with the element hosting this GuestView
241 // passed in from JavaScript.
242 base::DictionaryValue
* attach_params() const { return attach_params_
.get(); }
244 // Returns whether this guest has an associated embedder.
245 bool attached() const {
246 return element_instance_id_
!= guestview::kInstanceIDNone
;
249 // Returns the instance ID of the <*view> element.
250 int view_instance_id() const { return view_instance_id_
; }
252 // Returns the instance ID of this GuestViewBase.
253 int guest_instance_id() const { return guest_instance_id_
; }
255 // Returns the instance ID of the GuestViewBase's element.
256 int element_instance_id() const { return element_instance_id_
; }
258 // Returns the extension ID of the embedder.
259 const std::string
& owner_extension_id() const {
260 return owner_extension_id_
;
263 // Returns whether this GuestView is embedded in an extension/app.
264 bool in_extension() const { return !owner_extension_id_
.empty(); }
266 bool can_owner_receive_events() const { return !!view_instance_id_
; }
268 // Returns the user browser context of the embedder.
269 content::BrowserContext
* browser_context() const { return browser_context_
; }
271 GuestViewBase
* GetOpener() const {
272 return opener_
.get();
275 // Returns the URL of the owner WebContents.
276 const GURL
& GetOwnerSiteURL() const;
278 // Whether the guest view is inside a plugin document.
279 bool is_full_page_plugin() { return is_full_page_plugin_
; }
281 // Returns the routing ID of the guest proxy in the owner's renderer process.
282 // This value is only valid after attachment or first navigation.
283 int proxy_routing_id() const { return guest_proxy_routing_id_
; }
285 // Destroy this guest.
288 // Saves the attach state of the custom element hosting this GuestView.
289 void SetAttachParams(const base::DictionaryValue
& params
);
290 void SetOpener(GuestViewBase
* opener
);
292 // BrowserPluginGuestDelegate implementation.
293 content::WebContents
* CreateNewGuestWindow(
294 const content::WebContents::CreateParams
& create_params
) final
;
295 void DidAttach(int guest_proxy_routing_id
) final
;
296 void DidDetach() final
;
297 content::WebContents
* GetOwnerWebContents() const final
;
298 void GuestSizeChanged(const gfx::Size
& new_size
) final
;
299 void SetGuestHost(content::GuestHost
* guest_host
) final
;
300 void WillAttach(content::WebContents
* embedder_web_contents
,
301 int browser_plugin_instance_id
,
302 bool is_full_page_plugin
) final
;
304 // ui_zoom::ZoomObserver implementation.
306 const ui_zoom::ZoomController::ZoomChangedEventData
& data
) final
;
308 // Dispatches an event to the guest proxy.
309 void DispatchEventToGuestProxy(Event
* event
);
311 // Dispatches an event to the view.
312 void DispatchEventToView(Event
* event
);
315 explicit GuestViewBase(content::WebContents
* owner_web_contents
);
317 ~GuestViewBase() override
;
319 // Convert sizes in pixels from logical to physical numbers of pixels.
320 // Note that a size can consist of a fractional number of logical pixels
321 // (hence |logical_pixels| is represented as a double), but will always
322 // consist of an integral number of physical pixels (hence the return value
323 // is represented as an int).
324 int LogicalPixelsToPhysicalPixels(double logical_pixels
);
326 // Convert sizes in pixels from physical to logical numbers of pixels.
327 // Note that a size can consist of a fractional number of logical pixels
328 // (hence the return value is represented as a double), but will always
329 // consist of an integral number of physical pixels (hence |physical_pixels|
330 // is represented as an int).
331 double PhysicalPixelsToLogicalPixels(int physical_pixels
);
333 // WebContentsObserver implementation.
334 void DidStopLoading() final
;
335 void RenderViewReady() final
;
336 void WebContentsDestroyed() final
;
337 void DidNavigateMainFrame(
338 const content::LoadCommittedDetails
& details
,
339 const content::FrameNavigateParams
& params
) override
;
341 // WebContentsDelegate implementation.
342 void ActivateContents(content::WebContents
* contents
) final
;
343 void DeactivateContents(content::WebContents
* contents
) final
;
344 void ContentsMouseEvent(content::WebContents
* source
,
345 const gfx::Point
& location
,
346 bool motion
) override
;
347 void ContentsZoomChange(bool zoom_in
) override
;
348 void HandleKeyboardEvent(
349 content::WebContents
* source
,
350 const content::NativeWebKeyboardEvent
& event
) override
;
351 void LoadingStateChanged(content::WebContents
* source
,
352 bool to_different_document
) final
;
353 content::ColorChooser
* OpenColorChooser(
354 content::WebContents
* web_contents
,
356 const std::vector
<content::ColorSuggestion
>& suggestions
) override
;
357 void RunFileChooser(content::WebContents
* web_contents
,
358 const content::FileChooserParams
& params
) override
;
359 bool ShouldFocusPageAfterCrash() final
;
360 bool PreHandleGestureEvent(content::WebContents
* source
,
361 const blink::WebGestureEvent
& event
) override
;
362 void UpdatePreferredSize(content::WebContents
* web_contents
,
363 const gfx::Size
& pref_size
) final
;
364 void UpdateTargetURL(content::WebContents
* source
, const GURL
& url
) override
;
366 void SetGuestZoomLevelToMatchEmbedder();
369 class OwnerContentsObserver
;
371 class OpenerLifetimeObserver
;
373 void DispatchEvent(Event
* event
, int instance_id
);
375 void SendQueuedEvents();
377 void CompleteInit(scoped_ptr
<base::DictionaryValue
> create_params
,
378 const WebContentsCreatedCallback
& callback
,
379 content::WebContents
* guest_web_contents
);
381 // Dispatches the onResize event to the embedder.
382 void DispatchOnResizeEvent(const gfx::Size
& old_size
,
383 const gfx::Size
& new_size
);
385 // Get the zoom factor for the embedder's web contents.
386 double GetEmbedderZoomFactor();
388 void SetUpSizing(const base::DictionaryValue
& params
);
390 void StartTrackingEmbedderZoomLevel();
391 void StopTrackingEmbedderZoomLevel();
393 static void RegisterGuestViewTypes();
395 // This guest tracks the lifetime of the WebContents specified by
396 // |owner_web_contents_|. If |owner_web_contents_| is destroyed then this
397 // guest will also self-destruct.
398 content::WebContents
* owner_web_contents_
;
399 std::string owner_extension_id_
;
400 content::BrowserContext
* const browser_context_
;
402 // |guest_instance_id_| is a profile-wide unique identifier for a guest
404 const int guest_instance_id_
;
406 // |view_instance_id_| is an identifier that's unique within a particular
407 // embedder RenderViewHost for a particular <*view> instance.
408 int view_instance_id_
;
410 // |element_instance_id_| is an identifer that's unique to a particular
411 // GuestViewContainer element.
412 int element_instance_id_
;
414 // |initialized_| indicates whether GuestViewBase::Init has been called for
418 // Indicates that this guest is in the process of being destroyed.
419 bool is_being_destroyed_
;
421 // This is a queue of Events that are destined to be sent to the embedder once
422 // the guest is attached to a particular embedder.
423 std::deque
<linked_ptr
<Event
> > pending_events_
;
425 // The opener guest view.
426 base::WeakPtr
<GuestViewBase
> opener_
;
428 // The parameters associated with the element hosting this GuestView that
429 // are passed in from JavaScript. This will typically be the view instance ID,
430 // and element-specific parameters. These parameters are passed along to new
431 // guests that are created from this guest.
432 scoped_ptr
<base::DictionaryValue
> attach_params_
;
434 // This observer ensures that this guest self-destructs if the embedder goes
436 scoped_ptr
<OwnerContentsObserver
> owner_contents_observer_
;
438 // This observer ensures that if the guest is unattached and its opener goes
439 // away then this guest also self-destructs.
440 scoped_ptr
<OpenerLifetimeObserver
> opener_lifetime_observer_
;
442 // The size of the guest content. Note: In autosize mode, the container
443 // element may not match the size of the guest.
444 gfx::Size guest_size_
;
446 // A pointer to the guest_host.
447 content::GuestHost
* guest_host_
;
449 // Indicates whether autosize mode is enabled or not.
450 bool auto_size_enabled_
;
452 // The maximum size constraints of the container element in autosize mode.
453 gfx::Size max_auto_size_
;
455 // The minimum size constraints of the container element in autosize mode.
456 gfx::Size min_auto_size_
;
458 // The size that will be used when autosize mode is disabled.
459 gfx::Size normal_size_
;
461 // Whether the guest view is inside a plugin document.
462 bool is_full_page_plugin_
;
464 // The routing ID of the proxy to the guest in the owner's renderer process.
465 int guest_proxy_routing_id_
;
467 // This is used to ensure pending tasks will not fire after this object is
469 base::WeakPtrFactory
<GuestViewBase
> weak_ptr_factory_
;
471 DISALLOW_COPY_AND_ASSIGN(GuestViewBase
);
474 } // namespace extensions
476 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_