Add callback in UserScriptLoader to notify users when scripts are loaded.
[chromium-blink-merge.git] / components / guest_view / browser / guest_view_base.h
blob04d3f413ae42e9e546745a098e8526f0f87898c0
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_
8 #include <queue>
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 {
25 class GuestViewEvent;
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 {
34 SetSizeParams();
35 ~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 {
53 public:
54 // Returns a *ViewGuest if this GuestView is of the given view type.
55 template <typename T>
56 T* As() {
57 if (IsViewType(T::Type))
58 return static_cast<T*>(this);
60 return nullptr;
63 static GuestViewBase* FromWebContents(
64 const content::WebContents* web_contents);
66 static GuestViewBase* From(int owner_process_id, int instance_id);
68 // Given a |web_contents|, returns the top level owner WebContents. If
69 // |web_contents| does not belong to a GuestView, it will be returned
70 // unchanged.
71 static content::WebContents* GetTopLevelWebContents(
72 content::WebContents* web_contents);
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
88 // initialization.
89 virtual void DidInitialize(const base::DictionaryValue& create_params) {}
91 // This method is called when the initial set of frames within the page have
92 // completed loading.
93 virtual void GuestViewDidStopLoading() {}
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
98 // web contents.
99 virtual void EmbedderWillBeDestroyed() {}
101 // This method is called when the embedder's zoom changes.
102 virtual void EmbedderZoomChanged(double old_zoom_level,
103 double new_zoom_level) {}
105 // This method is called when the guest WebContents has been destroyed. This
106 // object will be destroyed after this call returns.
108 // This gives the derived class an opportunity to perform some cleanup.
109 virtual void GuestDestroyed() {}
111 // This method is invoked when the guest RenderView is ready, e.g. because we
112 // recreated it after a crash or after reattachment.
114 // This gives the derived class an opportunity to perform some initialization
115 // work.
116 virtual void GuestReady() {}
118 // This method is called when the guest's zoom changes.
119 virtual void GuestZoomChanged(double old_zoom_level, double new_zoom_level) {}
121 // This method is called when embedder WebContents's fullscreen is toggled.
123 // If the guest asked the embedder to enter fullscreen, the guest uses this
124 // signal to exit fullscreen state.
125 virtual void EmbedderFullscreenToggled(bool entered_fullscreen) {}
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
164 // to destruction.
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
176 // file.
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
181 // type.
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 void LoadURLWithParams(
202 const content::NavigationController::LoadURLParams& load_params);
204 bool IsViewType(const char* const view_type) const {
205 return !strcmp(GetViewType(), view_type);
208 // Used to toggle autosize mode for this GuestView, and set both the automatic
209 // and normal sizes.
210 void SetSize(const SetSizeParams& params);
212 bool initialized() const { return initialized_; }
214 content::WebContents* embedder_web_contents() const {
215 return attached() ? owner_web_contents_ : nullptr;
218 content::WebContents* owner_web_contents() const {
219 return owner_web_contents_;
222 content::GuestHost* host() const {
223 return guest_host_;
226 // Returns the parameters associated with the element hosting this GuestView
227 // passed in from JavaScript.
228 base::DictionaryValue* attach_params() const { return attach_params_.get(); }
230 // Returns whether this guest has an associated embedder.
231 bool attached() const {
232 return element_instance_id_ != kInstanceIDNone;
235 // Returns the instance ID of the <*view> element.
236 int view_instance_id() const { return view_instance_id_; }
238 // Returns the instance ID of this GuestViewBase.
239 int guest_instance_id() const { return guest_instance_id_; }
241 // Returns the instance ID of the GuestViewBase's element.
242 int element_instance_id() const { return element_instance_id_; }
244 bool can_owner_receive_events() const { return !!view_instance_id_; }
246 // Returns the user browser context of the embedder.
247 content::BrowserContext* browser_context() const { return browser_context_; }
249 GuestViewBase* GetOpener() const {
250 return opener_.get();
253 // Returns the URL of the owner WebContents.
254 const GURL& GetOwnerSiteURL() const;
256 // Returns the host of the owner WebContents. For extensions, this is the
257 // extension ID.
258 std::string owner_host() const { return owner_host_; }
260 // Whether the guest view is inside a plugin document.
261 bool is_full_page_plugin() const { return is_full_page_plugin_; }
263 // Returns the routing ID of the guest proxy in the owner's renderer process.
264 // This value is only valid after attachment or first navigation.
265 int proxy_routing_id() const { return guest_proxy_routing_id_; }
267 // Destroy this guest.
268 void Destroy();
270 // Saves the attach state of the custom element hosting this GuestView.
271 void SetAttachParams(const base::DictionaryValue& params);
272 void SetOpener(GuestViewBase* opener);
274 // BrowserPluginGuestDelegate implementation.
275 content::WebContents* CreateNewGuestWindow(
276 const content::WebContents::CreateParams& create_params) final;
277 void DidAttach(int guest_proxy_routing_id) final;
278 void DidDetach() final;
279 content::WebContents* GetOwnerWebContents() const final;
280 void GuestSizeChanged(const gfx::Size& new_size) final;
281 void SetGuestHost(content::GuestHost* guest_host) final;
282 void WillAttach(content::WebContents* embedder_web_contents,
283 int browser_plugin_instance_id,
284 bool is_full_page_plugin,
285 const base::Closure& callback) final;
287 // ui_zoom::ZoomObserver implementation.
288 void OnZoomChanged(
289 const ui_zoom::ZoomController::ZoomChangedEventData& data) final;
291 // Dispatches an event to the guest proxy.
292 void DispatchEventToGuestProxy(GuestViewEvent* event);
294 // Dispatches an event to the view.
295 void DispatchEventToView(GuestViewEvent* event);
297 protected:
298 explicit GuestViewBase(content::WebContents* owner_web_contents);
300 ~GuestViewBase() override;
302 // Convert sizes in pixels from logical to physical numbers of pixels.
303 // Note that a size can consist of a fractional number of logical pixels
304 // (hence |logical_pixels| is represented as a double), but will always
305 // consist of an integral number of physical pixels (hence the return value
306 // is represented as an int).
307 int LogicalPixelsToPhysicalPixels(double logical_pixels) const;
309 // Convert sizes in pixels from physical to logical numbers of pixels.
310 // Note that a size can consist of a fractional number of logical pixels
311 // (hence the return value is represented as a double), but will always
312 // consist of an integral number of physical pixels (hence |physical_pixels|
313 // is represented as an int).
314 double PhysicalPixelsToLogicalPixels(int physical_pixels) const;
316 // WebContentsObserver implementation.
317 void DidStopLoading() final;
318 void RenderViewReady() final;
319 void WebContentsDestroyed() final;
320 void DidNavigateMainFrame(
321 const content::LoadCommittedDetails& details,
322 const content::FrameNavigateParams& params) override;
324 // WebContentsDelegate implementation.
325 void ActivateContents(content::WebContents* contents) final;
326 void DeactivateContents(content::WebContents* contents) final;
327 void ContentsMouseEvent(content::WebContents* source,
328 const gfx::Point& location,
329 bool motion) override;
330 void ContentsZoomChange(bool zoom_in) override;
331 void HandleKeyboardEvent(
332 content::WebContents* source,
333 const content::NativeWebKeyboardEvent& event) override;
334 void LoadingStateChanged(content::WebContents* source,
335 bool to_different_document) final;
336 content::ColorChooser* OpenColorChooser(
337 content::WebContents* web_contents,
338 SkColor color,
339 const std::vector<content::ColorSuggestion>& suggestions) override;
340 void RunFileChooser(content::WebContents* web_contents,
341 const content::FileChooserParams& params) override;
342 bool ShouldFocusPageAfterCrash() final;
343 bool PreHandleGestureEvent(content::WebContents* source,
344 const blink::WebGestureEvent& event) override;
345 void UpdatePreferredSize(content::WebContents* web_contents,
346 const gfx::Size& pref_size) final;
347 void UpdateTargetURL(content::WebContents* source, const GURL& url) override;
348 bool ShouldResumeRequestsForCreatedWindow() override;
350 void SetGuestZoomLevelToMatchEmbedder();
352 // Signals that the guest view is ready. The default implementation signals
353 // immediately, but derived class can override this if they need to do
354 // asynchronous setup.
355 virtual void SignalWhenReady(const base::Closure& callback);
357 private:
358 class OwnerContentsObserver;
360 class OpenerLifetimeObserver;
362 void SendQueuedEvents();
364 void CompleteInit(scoped_ptr<base::DictionaryValue> create_params,
365 const WebContentsCreatedCallback& callback,
366 content::WebContents* guest_web_contents);
368 // Dispatches the onResize event to the embedder.
369 void DispatchOnResizeEvent(const gfx::Size& old_size,
370 const gfx::Size& new_size);
372 // Returns the default size of the guestview.
373 gfx::Size GetDefaultSize() const;
375 // Get the zoom factor for the embedder's web contents.
376 double GetEmbedderZoomFactor() const;
378 void SetUpSizing(const base::DictionaryValue& params);
380 void StartTrackingEmbedderZoomLevel();
381 void StopTrackingEmbedderZoomLevel();
383 // This guest tracks the lifetime of the WebContents specified by
384 // |owner_web_contents_|. If |owner_web_contents_| is destroyed then this
385 // guest will also self-destruct.
386 content::WebContents* owner_web_contents_;
387 std::string owner_host_;
388 content::BrowserContext* const browser_context_;
390 // |guest_instance_id_| is a profile-wide unique identifier for a guest
391 // WebContents.
392 const int guest_instance_id_;
394 // |view_instance_id_| is an identifier that's unique within a particular
395 // embedder RenderViewHost for a particular <*view> instance.
396 int view_instance_id_;
398 // |element_instance_id_| is an identifer that's unique to a particular
399 // GuestViewContainer element.
400 int element_instance_id_;
402 // |initialized_| indicates whether GuestViewBase::Init has been called for
403 // this object.
404 bool initialized_;
406 // Indicates that this guest is in the process of being destroyed.
407 bool is_being_destroyed_;
409 // This is a queue of Events that are destined to be sent to the embedder once
410 // the guest is attached to a particular embedder.
411 std::deque<linked_ptr<GuestViewEvent> > pending_events_;
413 // The opener guest view.
414 base::WeakPtr<GuestViewBase> opener_;
416 // The parameters associated with the element hosting this GuestView that
417 // are passed in from JavaScript. This will typically be the view instance ID,
418 // and element-specific parameters. These parameters are passed along to new
419 // guests that are created from this guest.
420 scoped_ptr<base::DictionaryValue> attach_params_;
422 // This observer ensures that this guest self-destructs if the embedder goes
423 // away.
424 scoped_ptr<OwnerContentsObserver> owner_contents_observer_;
426 // This observer ensures that if the guest is unattached and its opener goes
427 // away then this guest also self-destructs.
428 scoped_ptr<OpenerLifetimeObserver> opener_lifetime_observer_;
430 // The size of the guest content. Note: In autosize mode, the container
431 // element may not match the size of the guest.
432 gfx::Size guest_size_;
434 // A pointer to the guest_host.
435 content::GuestHost* guest_host_;
437 // Indicates whether autosize mode is enabled or not.
438 bool auto_size_enabled_;
440 // The maximum size constraints of the container element in autosize mode.
441 gfx::Size max_auto_size_;
443 // The minimum size constraints of the container element in autosize mode.
444 gfx::Size min_auto_size_;
446 // The size that will be used when autosize mode is disabled.
447 gfx::Size normal_size_;
449 // Whether the guest view is inside a plugin document.
450 bool is_full_page_plugin_;
452 // The routing ID of the proxy to the guest in the owner's renderer process.
453 int guest_proxy_routing_id_;
455 // This is used to ensure pending tasks will not fire after this object is
456 // destroyed.
457 base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_;
459 DISALLOW_COPY_AND_ASSIGN(GuestViewBase);
462 } // namespace guest_view
464 #endif // COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_BASE_H_