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 COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_BASE_H_
6 #define COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_BASE_H_
10 #include "base/memory/weak_ptr.h"
11 #include "base/values.h"
12 #include "components/guest_view/common/guest_view_constants.h"
13 #include "components/ui/zoom/zoom_observer.h"
14 #include "content/public/browser/browser_plugin_guest_delegate.h"
15 #include "content/public/browser/guest_host.h"
16 #include "content/public/browser/render_process_host_observer.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_delegate.h"
19 #include "content/public/browser/web_contents_observer.h"
21 struct RendererContentSettingRules
;
23 namespace guest_view
{
27 // A struct of parameters for SetSize(). The parameters are all declared as
28 // scoped pointers since they are all optional. Null pointers indicate that the
29 // parameter has not been provided, and the last used value should be used. Note
30 // that when |enable_auto_size| is true, providing |normal_size| is not
31 // meaningful. This is because the normal size of the guestview is overridden
32 // whenever autosizing occurs.
33 struct SetSizeParams
{
37 scoped_ptr
<bool> enable_auto_size
;
38 scoped_ptr
<gfx::Size
> min_size
;
39 scoped_ptr
<gfx::Size
> max_size
;
40 scoped_ptr
<gfx::Size
> normal_size
;
43 // A GuestViewBase is the base class browser-side API implementation for a
44 // <*view> tag. GuestViewBase maintains an association between a guest
45 // WebContents and an owner WebContents. It receives events issued from
46 // the guest and relays them to the owner. GuestViewBase tracks the lifetime
47 // of its owner. A GuestViewBase's owner is referred to as an embedder if
48 // it is attached to a container within the owner's WebContents.
49 class GuestViewBase
: public content::BrowserPluginGuestDelegate
,
50 public content::WebContentsDelegate
,
51 public content::WebContentsObserver
,
52 public ui_zoom::ZoomObserver
{
54 // Returns a *ViewGuest if this GuestView is of the given view type.
57 if (IsViewType(T::Type
))
58 return static_cast<T
*>(this);
63 // Cleans up state when this GuestView is being destroyed.
64 // Note that this cannot be done in the destructor since a GuestView could
65 // potentially be created and destroyed in JavaScript before getting a
66 // GuestViewBase instance. This method can be hidden by a CleanUp() method in
67 // a derived class, in which case the derived method should call this one.
68 static void CleanUp(int embedder_process_id
, int view_instance_id
);
70 static GuestViewBase
* FromWebContents(
71 const content::WebContents
* web_contents
);
73 static GuestViewBase
* From(int owner_process_id
, int instance_id
);
75 // Given a |web_contents|, returns the top level owner WebContents. If
76 // |web_contents| does not belong to a GuestView, it will be returned
78 static content::WebContents
* GetTopLevelWebContents(
79 content::WebContents
* web_contents
);
81 static bool IsGuest(content::WebContents
* web_contents
);
83 virtual const char* GetViewType() const = 0;
85 // This method is called after the guest has been attached to an embedder and
86 // suspended resource loads have been resumed.
88 // This method can be overriden by subclasses. This gives the derived class
89 // an opportunity to perform setup actions after attachment.
90 virtual void DidAttachToEmbedder() {}
92 // This method is called after this GuestViewBase has been initiated.
94 // This gives the derived class an opportunity to perform additional
96 virtual void DidInitialize(const base::DictionaryValue
& create_params
) {}
98 // This method is called when the initial set of frames within the page have
100 virtual void GuestViewDidStopLoading() {}
102 // This method is called when the embedder's zoom changes.
103 virtual void EmbedderZoomChanged(double old_zoom_level
,
104 double new_zoom_level
) {}
106 // This method is called when the guest WebContents has been destroyed. This
107 // object will be destroyed after this call returns.
109 // This gives the derived class an opportunity to perform some cleanup.
110 virtual void GuestDestroyed() {}
112 // This method is invoked when the guest RenderView is ready, e.g. because we
113 // recreated it after a crash or after reattachment.
115 // This gives the derived class an opportunity to perform some initialization
117 virtual void GuestReady() {}
119 // This method is called when the guest's zoom changes.
120 virtual void GuestZoomChanged(double old_zoom_level
, double new_zoom_level
) {}
122 // This method is called when embedder WebContents's fullscreen is toggled.
124 // If the guest asked the embedder to enter fullscreen, the guest uses this
125 // signal to exit fullscreen state.
126 virtual void EmbedderFullscreenToggled(bool entered_fullscreen
) {}
128 // This method is invoked when the contents auto-resized to give the container
129 // an opportunity to match it if it wishes.
131 // This gives the derived class an opportunity to inform its container element
132 // or perform other actions.
133 virtual void GuestSizeChangedDueToAutoSize(const gfx::Size
& old_size
,
134 const gfx::Size
& new_size
) {}
136 // This method queries whether autosize is supported for this particular view.
137 // By default, autosize is not supported. Derived classes can override this
138 // behavior to support autosize.
139 virtual bool IsAutoSizeSupported() const;
141 // This method is invoked when the contents preferred size changes. This will
142 // only ever fire if IsPreferredSizeSupported returns true.
143 virtual void OnPreferredSizeChanged(const gfx::Size
& pref_size
) {}
145 // This method queries whether preferred size events are enabled for this
146 // view. By default, preferred size events are disabled, since they add a
147 // small amount of overhead.
148 virtual bool IsPreferredSizeModeEnabled() const;
150 // This method queries whether drag-and-drop is enabled for this particular
151 // view. By default, drag-and-drop is disabled. Derived classes can override
152 // this behavior to enable drag-and-drop.
153 virtual bool IsDragAndDropEnabled() const;
155 // This method is called immediately before suspended resource loads have been
156 // resumed on attachment to an embedder.
158 // This method can be overriden by subclasses. This gives the derived class
159 // an opportunity to perform setup actions before attachment.
160 virtual void WillAttachToEmbedder() {}
162 // This method is called when the guest WebContents is about to be destroyed.
164 // This gives the derived class an opportunity to perform some cleanup prior
166 virtual void WillDestroy() {}
168 // This method is to be implemented by the derived class. This indicates
169 // whether zoom should propagate from the embedder to the guest content.
170 virtual bool ZoomPropagatesFromEmbedderToGuest() const;
172 // This method is to be implemented by the derived class. Access to guest
173 // views are determined by the availability of the internal extension API
174 // used to implement the guest view.
176 // This should be the name of the API as it appears in the _api_features.json
178 virtual const char* GetAPINamespace() const = 0;
180 // This method is to be implemented by the derived class. This method is the
181 // task prefix to show for a task produced by this GuestViewBase's derived
183 virtual int GetTaskPrefix() const = 0;
185 // This method is to be implemented by the derived class. Given a set of
186 // initialization parameters, a concrete subclass of GuestViewBase can
187 // create a specialized WebContents that it returns back to GuestViewBase.
188 using WebContentsCreatedCallback
=
189 base::Callback
<void(content::WebContents
*)>;
190 virtual void CreateWebContents(
191 const base::DictionaryValue
& create_params
,
192 const WebContentsCreatedCallback
& callback
) = 0;
194 // This creates a WebContents and initializes |this| GuestViewBase to use the
195 // newly created WebContents.
196 void Init(const base::DictionaryValue
& create_params
,
197 const WebContentsCreatedCallback
& callback
);
199 void InitWithWebContents(const base::DictionaryValue
& create_params
,
200 content::WebContents
* guest_web_contents
);
202 void LoadURLWithParams(
203 const content::NavigationController::LoadURLParams
& load_params
);
205 bool IsViewType(const char* const view_type
) const {
206 return !strcmp(GetViewType(), view_type
);
209 // Used to toggle autosize mode for this GuestView, and set both the automatic
211 void SetSize(const SetSizeParams
& params
);
213 bool initialized() const { return initialized_
; }
215 content::WebContents
* embedder_web_contents() const {
216 return attached() ? owner_web_contents_
: nullptr;
219 content::WebContents
* owner_web_contents() const {
220 return owner_web_contents_
;
223 content::GuestHost
* host() const {
227 // Returns the parameters associated with the element hosting this GuestView
228 // passed in from JavaScript.
229 base::DictionaryValue
* attach_params() const { return attach_params_
.get(); }
231 // Returns whether this guest has an associated embedder.
232 bool attached() const {
233 return element_instance_id_
!= kInstanceIDNone
;
236 // Returns the instance ID of the <*view> element.
237 int view_instance_id() const { return view_instance_id_
; }
239 // Returns the instance ID of this GuestViewBase.
240 int guest_instance_id() const { return guest_instance_id_
; }
242 // Returns the instance ID of the GuestViewBase's element.
243 int element_instance_id() const { return element_instance_id_
; }
245 bool can_owner_receive_events() const { return !!view_instance_id_
; }
247 gfx::Size
size() const { return guest_size_
; }
249 // Returns the user browser context of the embedder.
250 content::BrowserContext
* browser_context() const { return browser_context_
; }
252 GuestViewBase
* GetOpener() const {
253 return opener_
.get();
256 // Returns the URL of the owner WebContents.
257 const GURL
& GetOwnerSiteURL() const;
259 // Returns the host of the owner WebContents. For extensions, this is the
261 std::string
owner_host() const { return owner_host_
; }
263 // Whether the guest view is inside a plugin document.
264 bool is_full_page_plugin() const { return is_full_page_plugin_
; }
266 // Returns the routing ID of the guest proxy in the owner's renderer process.
267 // This value is only valid after attachment or first navigation.
268 int proxy_routing_id() const { return guest_proxy_routing_id_
; }
270 // Destroy this guest.
273 // Saves the attach state of the custom element hosting this GuestView.
274 void SetAttachParams(const base::DictionaryValue
& params
);
275 void SetOpener(GuestViewBase
* opener
);
277 // BrowserPluginGuestDelegate implementation.
278 content::WebContents
* CreateNewGuestWindow(
279 const content::WebContents::CreateParams
& create_params
) final
;
280 void DidAttach(int guest_proxy_routing_id
) final
;
281 void DidDetach() final
;
282 content::WebContents
* GetOwnerWebContents() const final
;
283 void GuestSizeChanged(const gfx::Size
& new_size
) final
;
284 void SetGuestHost(content::GuestHost
* guest_host
) final
;
285 void WillAttach(content::WebContents
* embedder_web_contents
,
286 int browser_plugin_instance_id
,
287 bool is_full_page_plugin
,
288 const base::Closure
& callback
) final
;
290 // ui_zoom::ZoomObserver implementation.
292 const ui_zoom::ZoomController::ZoomChangedEventData
& data
) final
;
294 // Dispatches an event to the guest proxy.
295 void DispatchEventToGuestProxy(GuestViewEvent
* event
);
297 // Dispatches an event to the view.
298 void DispatchEventToView(GuestViewEvent
* event
);
301 explicit GuestViewBase(content::WebContents
* owner_web_contents
);
303 ~GuestViewBase() override
;
305 // Convert sizes in pixels from logical to physical numbers of pixels.
306 // Note that a size can consist of a fractional number of logical pixels
307 // (hence |logical_pixels| is represented as a double), but will always
308 // consist of an integral number of physical pixels (hence the return value
309 // is represented as an int).
310 int LogicalPixelsToPhysicalPixels(double logical_pixels
) const;
312 // Convert sizes in pixels from physical to logical numbers of pixels.
313 // Note that a size can consist of a fractional number of logical pixels
314 // (hence the return value is represented as a double), but will always
315 // consist of an integral number of physical pixels (hence |physical_pixels|
316 // is represented as an int).
317 double PhysicalPixelsToLogicalPixels(int physical_pixels
) const;
319 // WebContentsObserver implementation.
320 void DidStopLoading() final
;
321 void RenderViewReady() final
;
322 void WebContentsDestroyed() final
;
323 void DidNavigateMainFrame(
324 const content::LoadCommittedDetails
& details
,
325 const content::FrameNavigateParams
& params
) override
;
327 // WebContentsDelegate implementation.
328 void ActivateContents(content::WebContents
* contents
) final
;
329 void DeactivateContents(content::WebContents
* contents
) final
;
330 void ContentsMouseEvent(content::WebContents
* source
,
331 const gfx::Point
& location
,
332 bool motion
) override
;
333 void ContentsZoomChange(bool zoom_in
) override
;
334 void HandleKeyboardEvent(
335 content::WebContents
* source
,
336 const content::NativeWebKeyboardEvent
& event
) override
;
337 void LoadingStateChanged(content::WebContents
* source
,
338 bool to_different_document
) final
;
339 content::ColorChooser
* OpenColorChooser(
340 content::WebContents
* web_contents
,
342 const std::vector
<content::ColorSuggestion
>& suggestions
) override
;
343 void RunFileChooser(content::WebContents
* web_contents
,
344 const content::FileChooserParams
& params
) override
;
345 bool ShouldFocusPageAfterCrash() final
;
346 bool PreHandleGestureEvent(content::WebContents
* source
,
347 const blink::WebGestureEvent
& event
) override
;
348 void UpdatePreferredSize(content::WebContents
* web_contents
,
349 const gfx::Size
& pref_size
) final
;
350 void UpdateTargetURL(content::WebContents
* source
, const GURL
& url
) override
;
351 bool ShouldResumeRequestsForCreatedWindow() override
;
353 void SetGuestZoomLevelToMatchEmbedder();
355 // Signals that the guest view is ready. The default implementation signals
356 // immediately, but derived class can override this if they need to do
357 // asynchronous setup.
358 virtual void SignalWhenReady(const base::Closure
& callback
);
361 class OwnerContentsObserver
;
363 class OpenerLifetimeObserver
;
365 void SendQueuedEvents();
367 void CompleteInit(scoped_ptr
<base::DictionaryValue
> create_params
,
368 const WebContentsCreatedCallback
& callback
,
369 content::WebContents
* guest_web_contents
);
371 // Dispatches the onResize event to the embedder.
372 void DispatchOnResizeEvent(const gfx::Size
& old_size
,
373 const gfx::Size
& new_size
);
375 // Returns the default size of the guestview.
376 gfx::Size
GetDefaultSize() const;
378 // Get the zoom factor for the embedder's web contents.
379 double GetEmbedderZoomFactor() const;
381 void SetUpSizing(const base::DictionaryValue
& params
);
383 void StartTrackingEmbedderZoomLevel();
384 void StopTrackingEmbedderZoomLevel();
386 // This guest tracks the lifetime of the WebContents specified by
387 // |owner_web_contents_|. If |owner_web_contents_| is destroyed then this
388 // guest will also self-destruct.
389 content::WebContents
* owner_web_contents_
;
390 std::string owner_host_
;
391 content::BrowserContext
* const browser_context_
;
393 // |guest_instance_id_| is a profile-wide unique identifier for a guest
395 const int guest_instance_id_
;
397 // |view_instance_id_| is an identifier that's unique within a particular
398 // embedder RenderViewHost for a particular <*view> instance.
399 int view_instance_id_
;
401 // |element_instance_id_| is an identifer that's unique to a particular
402 // GuestViewContainer element.
403 int element_instance_id_
;
405 // |initialized_| indicates whether GuestViewBase::Init has been called for
409 // Indicates that this guest is in the process of being destroyed.
410 bool is_being_destroyed_
;
412 // This is a queue of Events that are destined to be sent to the embedder once
413 // the guest is attached to a particular embedder.
414 std::deque
<linked_ptr
<GuestViewEvent
> > pending_events_
;
416 // The opener guest view.
417 base::WeakPtr
<GuestViewBase
> opener_
;
419 // The parameters associated with the element hosting this GuestView that
420 // are passed in from JavaScript. This will typically be the view instance ID,
421 // and element-specific parameters. These parameters are passed along to new
422 // guests that are created from this guest.
423 scoped_ptr
<base::DictionaryValue
> attach_params_
;
425 // This observer ensures that this guest self-destructs if the embedder goes
427 scoped_ptr
<OwnerContentsObserver
> owner_contents_observer_
;
429 // This observer ensures that if the guest is unattached and its opener goes
430 // away then this guest also self-destructs.
431 scoped_ptr
<OpenerLifetimeObserver
> opener_lifetime_observer_
;
433 // The size of the guest content. Note: In autosize mode, the container
434 // element may not match the size of the guest.
435 gfx::Size guest_size_
;
437 // A pointer to the guest_host.
438 content::GuestHost
* guest_host_
;
440 // Indicates whether autosize mode is enabled or not.
441 bool auto_size_enabled_
;
443 // The maximum size constraints of the container element in autosize mode.
444 gfx::Size max_auto_size_
;
446 // The minimum size constraints of the container element in autosize mode.
447 gfx::Size min_auto_size_
;
449 // The size that will be used when autosize mode is disabled.
450 gfx::Size normal_size_
;
452 // Whether the guest view is inside a plugin document.
453 bool is_full_page_plugin_
;
455 // The routing ID of the proxy to the guest in the owner's renderer process.
456 int guest_proxy_routing_id_
;
458 // This is used to ensure pending tasks will not fire after this object is
460 base::WeakPtrFactory
<GuestViewBase
> weak_ptr_factory_
;
462 DISALLOW_COPY_AND_ASSIGN(GuestViewBase
);
465 } // namespace guest_view
467 #endif // COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_BASE_H_