GuestView: Cleanup
[chromium-blink-merge.git] / extensions / browser / guest_view / guest_view_base.h
blobe746ff6b98840905109ae19472945b232252a3be
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_
8 #include <queue>
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 GuestViewBase is the base class browser-side API implementation for a
26 // <*view> tag. GuestViewBase maintains an association between a guest
27 // WebContents and an owner WebContents. It receives events issued from
28 // the guest and relays them to the owner. GuestViewBase tracks the lifetime
29 // of its owner. A GuestViewBase's owner is referred to as an embedder if
30 // it is attached to a container within the owner's WebContents.
31 class GuestViewBase : public content::BrowserPluginGuestDelegate,
32 public content::WebContentsDelegate,
33 public content::WebContentsObserver,
34 public ui_zoom::ZoomObserver {
35 public:
36 class Event {
37 public:
38 Event(const std::string& name, scoped_ptr<base::DictionaryValue> args);
39 ~Event();
41 const std::string& name() const { return name_; }
43 scoped_ptr<base::DictionaryValue> GetArguments();
45 private:
46 const std::string name_;
47 scoped_ptr<base::DictionaryValue> args_;
50 // Returns a *ViewGuest if this GuestView is of the given view type.
51 template <typename T>
52 T* As() {
53 if (IsViewType(T::Type))
54 return static_cast<T*>(this);
56 return NULL;
59 typedef base::Callback<GuestViewBase*(
60 content::BrowserContext*,
61 content::WebContents*,
62 int)> GuestCreationCallback;
63 static void RegisterGuestViewType(const std::string& view_type,
64 const GuestCreationCallback& callback);
66 static GuestViewBase* Create(content::BrowserContext* browser_context,
67 content::WebContents* owner_web_contents,
68 int guest_instance_id,
69 const std::string& view_type);
71 static GuestViewBase* FromWebContents(content::WebContents* web_contents);
73 static GuestViewBase* From(int owner_process_id, int instance_id);
75 static bool IsGuest(content::WebContents* web_contents);
77 virtual const char* GetViewType() const = 0;
79 // This method is called after the guest has been attached to an embedder and
80 // suspended resource loads have been resumed.
82 // This method can be overriden by subclasses. This gives the derived class
83 // an opportunity to perform setup actions after attachment.
84 virtual void DidAttachToEmbedder() {}
86 // This method is called after this GuestViewBase has been initiated.
88 // This gives the derived class an opportunity to perform additional
89 // initialization.
90 virtual void DidInitialize(const base::DictionaryValue& create_params) {}
92 // This method is called when the initial set of frames within the page have
93 // completed loading.
94 virtual void DidStopLoading() {}
96 // This method is called before the embedder is destroyed.
97 // |owner_web_contents_| should still be valid during this call. This
98 // allows the derived class to perform some cleanup related to the embedder
99 // web contents.
100 virtual void EmbedderWillBeDestroyed() {}
102 // This method is called when the guest WebContents has been destroyed. This
103 // object will be destroyed after this call returns.
105 // This gives the derived class an opportunity to perform some cleanup.
106 virtual void GuestDestroyed() {}
108 // This method is invoked when the guest RenderView is ready, e.g. because we
109 // recreated it after a crash or after reattachment.
111 // This gives the derived class an opportunity to perform some initialization
112 // work.
113 virtual void GuestReady() {}
115 // This method is invoked when the contents auto-resized to give the container
116 // an opportunity to match it if it wishes.
118 // This gives the derived class an opportunity to inform its container element
119 // or perform other actions.
120 virtual void GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
121 const gfx::Size& new_size) {}
123 // This method queries whether autosize is supported for this particular view.
124 // By default, autosize is not supported. Derived classes can override this
125 // behavior to support autosize.
126 virtual bool IsAutoSizeSupported() const;
128 // This method is invoked when the contents preferred size changes. This will
129 // only ever fire if IsPreferredSizeSupported returns true.
130 virtual void OnPreferredSizeChanged(const gfx::Size& pref_size) {}
132 // This method queries whether preferred size events are enabled for this
133 // view. By default, preferred size events are disabled, since they add a
134 // small amount of overhead.
135 virtual bool IsPreferredSizeModeEnabled() const;
137 // This method queries whether drag-and-drop is enabled for this particular
138 // view. By default, drag-and-drop is disabled. Derived classes can override
139 // this behavior to enable drag-and-drop.
140 virtual bool IsDragAndDropEnabled() const;
142 // This method is called immediately before suspended resource loads have been
143 // resumed on attachment to an embedder.
145 // This method can be overriden by subclasses. This gives the derived class
146 // an opportunity to perform setup actions before attachment.
147 virtual void WillAttachToEmbedder() {}
149 // This method is called when the guest WebContents is about to be destroyed.
151 // This gives the derived class an opportunity to perform some cleanup prior
152 // to destruction.
153 virtual void WillDestroy() {}
155 // This method is to be implemented by the derived class. This indicates
156 // whether zoom should propagate from the embedder to the guest content.
157 virtual bool ZoomPropagatesFromEmbedderToGuest() const;
159 // This method is to be implemented by the derived class. Access to guest
160 // views are determined by the availability of the internal extension API
161 // used to implement the guest view.
163 // This should be the name of the API as it appears in the _api_features.json
164 // file.
165 virtual const char* GetAPINamespace() const = 0;
167 // This method is to be implemented by the derived class. This method is the
168 // task prefix to show for a task produced by this GuestViewBase's derived
169 // type.
170 virtual int GetTaskPrefix() const = 0;
172 // This method is to be implemented by the derived class. Given a set of
173 // initialization parameters, a concrete subclass of GuestViewBase can
174 // create a specialized WebContents that it returns back to GuestViewBase.
175 typedef base::Callback<void(content::WebContents*)>
176 WebContentsCreatedCallback;
177 virtual void CreateWebContents(
178 const base::DictionaryValue& create_params,
179 const WebContentsCreatedCallback& callback) = 0;
181 // This creates a WebContents and initializes |this| GuestViewBase to use the
182 // newly created WebContents.
183 void Init(const base::DictionaryValue& create_params,
184 const WebContentsCreatedCallback& callback);
186 void InitWithWebContents(const base::DictionaryValue& create_params,
187 content::WebContents* guest_web_contents);
189 bool IsViewType(const char* const view_type) const {
190 return !strcmp(GetViewType(), view_type);
193 // Toggles autosize mode for this GuestView.
194 void SetAutoSize(bool enabled,
195 const gfx::Size& min_size,
196 const gfx::Size& max_size);
198 bool initialized() const { return initialized_; }
200 content::WebContents* embedder_web_contents() const {
201 return attached() ? owner_web_contents_ : NULL;
204 content::WebContents* owner_web_contents() const {
205 return owner_web_contents_;
208 // Returns the parameters associated with the element hosting this GuestView
209 // passed in from JavaScript.
210 base::DictionaryValue* attach_params() const { return attach_params_.get(); }
212 // Returns whether this guest has an associated embedder.
213 bool attached() const {
214 return element_instance_id_ != guestview::kInstanceIDNone;
217 // Returns the instance ID of the <*view> element.
218 int view_instance_id() const { return view_instance_id_; }
220 // Returns the instance ID of this GuestViewBase.
221 int guest_instance_id() const { return guest_instance_id_; }
223 // Returns the instance ID of the GuestViewBase's element.
224 int element_instance_id() const { return element_instance_id_; }
226 // Returns the extension ID of the embedder.
227 const std::string& owner_extension_id() const {
228 return owner_extension_id_;
231 // Returns whether this GuestView is embedded in an extension/app.
232 bool in_extension() const { return !owner_extension_id_.empty(); }
234 bool can_owner_receive_events() const { return !!view_instance_id_; }
236 // Returns the user browser context of the embedder.
237 content::BrowserContext* browser_context() const { return browser_context_; }
239 GuestViewBase* GetOpener() const {
240 return opener_.get();
243 // Returns the URL of the owner WebContents.
244 const GURL& GetOwnerSiteURL() const;
246 // Whether the guest view is inside a plugin document.
247 bool is_full_page_plugin() { return is_full_page_plugin_; }
249 // Destroy this guest.
250 void Destroy();
252 // Saves the attach state of the custom element hosting this GuestView.
253 void SetAttachParams(const base::DictionaryValue& params);
254 void SetOpener(GuestViewBase* opener);
256 // BrowserPluginGuestDelegate implementation.
257 content::WebContents* CreateNewGuestWindow(
258 const content::WebContents::CreateParams& create_params) final;
259 void DidAttach(int guest_proxy_routing_id) final;
260 void DidDetach() final;
261 void ElementSizeChanged(const gfx::Size& size) final;
262 content::WebContents* GetOwnerWebContents() const final;
263 void GuestSizeChanged(const gfx::Size& old_size,
264 const gfx::Size& new_size) final;
265 void RegisterDestructionCallback(const DestructionCallback& callback) final;
266 void SetGuestSizer(content::GuestSizer* guest_sizer) final;
267 void WillAttach(content::WebContents* embedder_web_contents,
268 int browser_plugin_instance_id,
269 bool is_full_page_plugin) final;
271 // ui_zoom::ZoomObserver implementation.
272 void OnZoomChanged(
273 const ui_zoom::ZoomController::ZoomChangedEventData& data) override;
275 // Dispatches an event |event_name| to the embedder with the |event| fields.
276 void DispatchEventToEmbedder(Event* event);
278 protected:
279 GuestViewBase(content::BrowserContext* browser_context,
280 content::WebContents* owner_web_contents,
281 int guest_instance_id);
283 ~GuestViewBase() override;
285 private:
286 class OwnerLifetimeObserver;
288 class OpenerLifetimeObserver;
290 void SendQueuedEvents();
292 void CompleteInit(scoped_ptr<base::DictionaryValue> create_params,
293 const WebContentsCreatedCallback& callback,
294 content::WebContents* guest_web_contents);
296 void SetUpAutoSize(const base::DictionaryValue& params);
298 void StartTrackingEmbedderZoomLevel();
299 void StopTrackingEmbedderZoomLevel();
301 static void RegisterGuestViewTypes();
303 // WebContentsObserver implementation.
304 void DidStopLoading(content::RenderViewHost* render_view_host) final;
305 void RenderViewReady() final;
306 void WebContentsDestroyed() final;
308 // WebContentsDelegate implementation.
309 void ActivateContents(content::WebContents* contents) final;
310 void DeactivateContents(content::WebContents* contents) final;
311 void ContentsZoomChange(bool zoom_in) override;
312 void HandleKeyboardEvent(
313 content::WebContents* source,
314 const content::NativeWebKeyboardEvent& event) override;
315 void RunFileChooser(content::WebContents* web_contents,
316 const content::FileChooserParams& params) override;
317 bool ShouldFocusPageAfterCrash() final;
318 bool PreHandleGestureEvent(content::WebContents* source,
319 const blink::WebGestureEvent& event) final;
320 void UpdatePreferredSize(content::WebContents* web_contents,
321 const gfx::Size& pref_size) final;
323 // This guest tracks the lifetime of the WebContents specified by
324 // |owner_web_contents_|. If |owner_web_contents_| is destroyed then this
325 // guest will also self-destruct.
326 content::WebContents* owner_web_contents_;
327 std::string owner_extension_id_;
328 content::BrowserContext* browser_context_;
330 // |guest_instance_id_| is a profile-wide unique identifier for a guest
331 // WebContents.
332 const int guest_instance_id_;
334 // |view_instance_id_| is an identifier that's unique within a particular
335 // embedder RenderViewHost for a particular <*view> instance.
336 int view_instance_id_;
338 // |element_instance_id_| is an identifer that's unique to a particular
339 // GuestViewContainer element.
340 int element_instance_id_;
342 // |initialized_| indicates whether GuestViewBase::Init has been called for
343 // this object.
344 bool initialized_;
346 // Indicates that this guest is in the process of being destroyed.
347 bool is_being_destroyed_;
349 // This is a queue of Events that are destined to be sent to the embedder once
350 // the guest is attached to a particular embedder.
351 std::deque<linked_ptr<Event> > pending_events_;
353 // The opener guest view.
354 base::WeakPtr<GuestViewBase> opener_;
356 DestructionCallback destruction_callback_;
358 // The parameters associated with the element hosting this GuestView that
359 // are passed in from JavaScript. This will typically be the view instance ID,
360 // and element-specific parameters. These parameters are passed along to new
361 // guests that are created from this guest.
362 scoped_ptr<base::DictionaryValue> attach_params_;
364 // This observer ensures that this guest self-destructs if the embedder goes
365 // away.
366 scoped_ptr<OwnerLifetimeObserver> owner_lifetime_observer_;
368 // This observer ensures that if the guest is unattached and its opener goes
369 // away then this guest also self-destructs.
370 scoped_ptr<OpenerLifetimeObserver> opener_lifetime_observer_;
372 // The size of the container element.
373 gfx::Size element_size_;
375 // The size of the guest content. Note: In autosize mode, the container
376 // element may not match the size of the guest.
377 gfx::Size guest_size_;
379 // A pointer to the guest_sizer.
380 content::GuestSizer* guest_sizer_;
382 // Indicates whether autosize mode is enabled or not.
383 bool auto_size_enabled_;
385 // The maximum size constraints of the container element in autosize mode.
386 gfx::Size max_auto_size_;
388 // The minimum size constraints of the container element in autosize mode.
389 gfx::Size min_auto_size_;
391 // Whether the guest view is inside a plugin document.
392 bool is_full_page_plugin_;
394 // This is used to ensure pending tasks will not fire after this object is
395 // destroyed.
396 base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_;
398 DISALLOW_COPY_AND_ASSIGN(GuestViewBase);
401 } // namespace extensions
403 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_