Roll src/third_party/WebKit 3aea697:d9c6159 (svn 201973:201974)
[chromium-blink-merge.git] / components / guest_view / browser / guest_view_base.h
blob6c7864506a7c48eed7b0250bf9a4a2763bce7877
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 // 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(content::BrowserContext* browser_context,
69 int embedder_process_id,
70 int view_instance_id);
72 static GuestViewBase* FromWebContents(
73 const content::WebContents* web_contents);
75 static GuestViewBase* From(int owner_process_id, int instance_id);
77 // Given a |web_contents|, returns the top level owner WebContents. If
78 // |web_contents| does not belong to a GuestView, it will be returned
79 // unchanged.
80 static content::WebContents* GetTopLevelWebContents(
81 content::WebContents* web_contents);
83 static bool IsGuest(content::WebContents* web_contents);
85 virtual const char* GetViewType() const = 0;
87 // This method is called after the guest has been attached to an embedder and
88 // suspended resource loads have been resumed.
90 // This method can be overriden by subclasses. This gives the derived class
91 // an opportunity to perform setup actions after attachment.
92 virtual void DidAttachToEmbedder() {}
94 // This method is called after this GuestViewBase has been initiated.
96 // This gives the derived class an opportunity to perform additional
97 // initialization.
98 virtual void DidInitialize(const base::DictionaryValue& create_params) {}
100 // This method is called when the initial set of frames within the page have
101 // completed loading.
102 virtual void GuestViewDidStopLoading() {}
104 // This method is called when the embedder's zoom changes.
105 virtual void EmbedderZoomChanged(double old_zoom_level,
106 double new_zoom_level) {}
108 // This method is called when the guest WebContents has been destroyed. This
109 // object will be destroyed after this call returns.
111 // This gives the derived class an opportunity to perform some cleanup.
112 virtual void GuestDestroyed() {}
114 // This method is invoked when the guest RenderView is ready, e.g. because we
115 // recreated it after a crash or after reattachment.
117 // This gives the derived class an opportunity to perform some initialization
118 // work.
119 virtual void GuestReady() {}
121 // This method is called when the guest's zoom changes.
122 virtual void GuestZoomChanged(double old_zoom_level, double new_zoom_level) {}
124 // This method is called when embedder WebContents's fullscreen is toggled.
126 // If the guest asked the embedder to enter fullscreen, the guest uses this
127 // signal to exit fullscreen state.
128 virtual void EmbedderFullscreenToggled(bool entered_fullscreen) {}
130 // This method is invoked when the contents auto-resized to give the container
131 // an opportunity to match it if it wishes.
133 // This gives the derived class an opportunity to inform its container element
134 // or perform other actions.
135 virtual void GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
136 const gfx::Size& new_size) {}
138 // This method queries whether autosize is supported for this particular view.
139 // By default, autosize is not supported. Derived classes can override this
140 // behavior to support autosize.
141 virtual bool IsAutoSizeSupported() const;
143 // This method is invoked when the contents preferred size changes. This will
144 // only ever fire if IsPreferredSizeSupported returns true.
145 virtual void OnPreferredSizeChanged(const gfx::Size& pref_size) {}
147 // This method queries whether preferred size events are enabled for this
148 // view. By default, preferred size events are disabled, since they add a
149 // small amount of overhead.
150 virtual bool IsPreferredSizeModeEnabled() const;
152 // This method is called immediately before suspended resource loads have been
153 // resumed on attachment to an embedder.
155 // This method can be overriden by subclasses. This gives the derived class
156 // an opportunity to perform setup actions before attachment.
157 virtual void WillAttachToEmbedder() {}
159 // This method is called when the guest WebContents is about to be destroyed.
161 // This gives the derived class an opportunity to perform some cleanup prior
162 // to destruction.
163 virtual void WillDestroy() {}
165 // This method is to be implemented by the derived class. This indicates
166 // whether zoom should propagate from the embedder to the guest content.
167 virtual bool ZoomPropagatesFromEmbedderToGuest() const;
169 // This method is to be implemented by the derived class. Access to guest
170 // views are determined by the availability of the internal extension API
171 // used to implement the guest view.
173 // This should be the name of the API as it appears in the _api_features.json
174 // file.
175 virtual const char* GetAPINamespace() const = 0;
177 // This method is to be implemented by the derived class. This method is the
178 // task prefix to show for a task produced by this GuestViewBase's derived
179 // type.
180 virtual int GetTaskPrefix() const = 0;
182 // This method is to be implemented by the derived class. Given a set of
183 // initialization parameters, a concrete subclass of GuestViewBase can
184 // create a specialized WebContents that it returns back to GuestViewBase.
185 using WebContentsCreatedCallback =
186 base::Callback<void(content::WebContents*)>;
187 virtual void CreateWebContents(
188 const base::DictionaryValue& create_params,
189 const WebContentsCreatedCallback& callback) = 0;
191 // This creates a WebContents and initializes |this| GuestViewBase to use the
192 // newly created WebContents.
193 void Init(const base::DictionaryValue& create_params,
194 const WebContentsCreatedCallback& callback);
196 void InitWithWebContents(const base::DictionaryValue& create_params,
197 content::WebContents* guest_web_contents);
199 void LoadURLWithParams(
200 const content::NavigationController::LoadURLParams& load_params);
202 bool IsViewType(const char* const view_type) const {
203 return !strcmp(GetViewType(), view_type);
206 // Used to toggle autosize mode for this GuestView, and set both the automatic
207 // and normal sizes.
208 void SetSize(const SetSizeParams& params);
210 bool initialized() const { return initialized_; }
212 content::WebContents* embedder_web_contents() const {
213 return attached() ? owner_web_contents_ : nullptr;
216 content::WebContents* owner_web_contents() const {
217 return owner_web_contents_;
220 content::GuestHost* host() const {
221 return guest_host_;
224 // Returns the parameters associated with the element hosting this GuestView
225 // passed in from JavaScript.
226 base::DictionaryValue* attach_params() const { return attach_params_.get(); }
228 // Returns whether this guest has an associated embedder.
229 bool attached() const {
230 return element_instance_id_ != kInstanceIDNone;
233 // Returns the instance ID of the <*view> element.
234 int view_instance_id() const { return view_instance_id_; }
236 // Returns the instance ID of this GuestViewBase.
237 int guest_instance_id() const { return guest_instance_id_; }
239 // Returns the instance ID of the GuestViewBase's element.
240 int element_instance_id() const { return element_instance_id_; }
242 bool can_owner_receive_events() const { return !!view_instance_id_; }
244 gfx::Size size() const { return guest_size_; }
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 content::WebContents* CreateNewGuestWindow(
275 const content::WebContents::CreateParams& create_params) final;
276 void DidAttach(int guest_proxy_routing_id) final;
277 void DidDetach() final;
278 content::WebContents* GetOwnerWebContents() const final;
279 bool Find(int request_id,
280 const base::string16& search_text,
281 const blink::WebFindOptions& options) final;
282 bool StopFinding(content::StopFindAction action) 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.
291 void OnZoomChanged(
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);
300 protected:
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,
341 SkColor color,
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;
352 void FindReply(content::WebContents* source,
353 int request_id,
354 int number_of_matches,
355 const gfx::Rect& selection_rect,
356 int active_match_ordinal,
357 bool final_update) override;
359 void SetGuestZoomLevelToMatchEmbedder();
361 // Signals that the guest view is ready. The default implementation signals
362 // immediately, but derived class can override this if they need to do
363 // asynchronous setup.
364 virtual void SignalWhenReady(const base::Closure& callback);
366 // Returns true if this guest should handle find requests for its
367 // embedder. This should generally be true for guests that make up the
368 // entirety of the embedder's content.
369 virtual bool ShouldHandleFindRequestsForEmbedder() const;
371 // BrowserPluginGuestDelegate implementation.
372 void SetContextMenuPosition(const gfx::Point& position) override;
374 private:
375 class OwnerContentsObserver;
377 class OpenerLifetimeObserver;
379 void SendQueuedEvents();
381 void CompleteInit(scoped_ptr<base::DictionaryValue> create_params,
382 const WebContentsCreatedCallback& callback,
383 content::WebContents* guest_web_contents);
385 // Dispatches the onResize event to the embedder.
386 void DispatchOnResizeEvent(const gfx::Size& old_size,
387 const gfx::Size& new_size);
389 // Returns the default size of the guestview.
390 gfx::Size GetDefaultSize() const;
392 // Get the zoom factor for the embedder's web contents.
393 double GetEmbedderZoomFactor() const;
395 void SetUpSizing(const base::DictionaryValue& params);
397 void StartTrackingEmbedderZoomLevel();
398 void StopTrackingEmbedderZoomLevel();
400 // This guest tracks the lifetime of the WebContents specified by
401 // |owner_web_contents_|. If |owner_web_contents_| is destroyed then this
402 // guest will also self-destruct.
403 content::WebContents* owner_web_contents_;
404 std::string owner_host_;
405 content::BrowserContext* const browser_context_;
407 // |guest_instance_id_| is a profile-wide unique identifier for a guest
408 // WebContents.
409 const int guest_instance_id_;
411 // |view_instance_id_| is an identifier that's unique within a particular
412 // embedder RenderViewHost for a particular <*view> instance.
413 int view_instance_id_;
415 // |element_instance_id_| is an identifer that's unique to a particular
416 // GuestViewContainer element.
417 int element_instance_id_;
419 // |initialized_| indicates whether GuestViewBase::Init has been called for
420 // this object.
421 bool initialized_;
423 // Indicates that this guest is in the process of being destroyed.
424 bool is_being_destroyed_;
426 // This is a queue of Events that are destined to be sent to the embedder once
427 // the guest is attached to a particular embedder.
428 std::deque<linked_ptr<GuestViewEvent> > pending_events_;
430 // The opener guest view.
431 base::WeakPtr<GuestViewBase> opener_;
433 // The parameters associated with the element hosting this GuestView that
434 // are passed in from JavaScript. This will typically be the view instance ID,
435 // and element-specific parameters. These parameters are passed along to new
436 // guests that are created from this guest.
437 scoped_ptr<base::DictionaryValue> attach_params_;
439 // This observer ensures that this guest self-destructs if the embedder goes
440 // away.
441 scoped_ptr<OwnerContentsObserver> owner_contents_observer_;
443 // This observer ensures that if the guest is unattached and its opener goes
444 // away then this guest also self-destructs.
445 scoped_ptr<OpenerLifetimeObserver> opener_lifetime_observer_;
447 // The size of the guest content. Note: In autosize mode, the container
448 // element may not match the size of the guest.
449 gfx::Size guest_size_;
451 // A pointer to the guest_host.
452 content::GuestHost* guest_host_;
454 // Indicates whether autosize mode is enabled or not.
455 bool auto_size_enabled_;
457 // The maximum size constraints of the container element in autosize mode.
458 gfx::Size max_auto_size_;
460 // The minimum size constraints of the container element in autosize mode.
461 gfx::Size min_auto_size_;
463 // The size that will be used when autosize mode is disabled.
464 gfx::Size normal_size_;
466 // Whether the guest view is inside a plugin document.
467 bool is_full_page_plugin_;
469 // The routing ID of the proxy to the guest in the owner's renderer process.
470 int guest_proxy_routing_id_;
472 // This is used to ensure pending tasks will not fire after this object is
473 // destroyed.
474 base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_;
476 DISALLOW_COPY_AND_ASSIGN(GuestViewBase);
479 } // namespace guest_view
481 #endif // COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_BASE_H_