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 CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_
6 #define CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_
10 #include "base/memory/weak_ptr.h"
11 #include "base/values.h"
12 #include "content/public/browser/browser_plugin_guest_delegate.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/browser/web_contents_delegate.h"
15 #include "content/public/browser/web_contents_observer.h"
17 struct RendererContentSettingRules;
19 // A GuestViewBase is the base class browser-side API implementation for a
20 // <*view> tag. GuestViewBase maintains an association between a guest
21 // WebContents and an embedder WebContents. It receives events issued from
22 // the guest and relays them to the embedder.
23 class GuestViewBase : public content::BrowserPluginGuestDelegate,
24 public content::WebContentsDelegate,
25 public content::WebContentsObserver {
29 Event(const std::string& name, scoped_ptr<base::DictionaryValue> args);
32 const std::string& name() const { return name_; }
34 scoped_ptr<base::DictionaryValue> GetArguments();
37 const std::string name_;
38 scoped_ptr<base::DictionaryValue> args_;
41 // Returns a *ViewGuest if this GuestView is of the given view type.
44 if (IsViewType(T::Type))
45 return static_cast<T*>(this);
50 static GuestViewBase* Create(int guest_instance_id,
51 content::WebContents* guest_web_contents,
52 const std::string& embedder_extension_id,
53 const std::string& view_type);
55 static GuestViewBase* FromWebContents(content::WebContents* web_contents);
57 static GuestViewBase* From(int embedder_process_id, int instance_id);
59 static bool IsGuest(content::WebContents* web_contents);
61 // By default, JavaScript and images are enabled in guest content.
62 static void GetDefaultContentSettingRules(RendererContentSettingRules* rules,
65 virtual const char* GetViewType() const = 0;
67 // This method is called after the guest has been attached to an embedder and
68 // suspended resource loads have been resumed.
70 // This method can be overriden by subclasses. This gives the derived class
71 // an opportunity to perform setup actions after attachment.
72 virtual void DidAttachToEmbedder() {}
74 // This method can be overridden by subclasses. This method is called when
75 // the initial set of frames within the page have completed loading.
76 virtual void DidStopLoading() {}
78 // This method is called immediately before suspended resource loads have been
79 // resumed on attachment to an embedder.
81 // This method can be overriden by subclasses. This gives the derived class
82 // an opportunity to perform setup actions before attachment.
83 virtual void WillAttachToEmbedder() {}
85 // This method is called when the guest WebContents is about to be destroyed.
87 // This method can be overridden by subclasses. This gives the derived class
88 // an opportunity to perform some cleanup prior to destruction.
89 virtual void WillDestroy() {}
91 // This method is called when the guest's embedder WebContents has been
92 // destroyed and the guest will be destroyed shortly.
94 // This method can be overridden by subclasses. This gives the derived class
95 // an opportunity to perform some cleanup prior to destruction.
96 virtual void EmbedderDestroyed() {}
98 // This method is called when the guest WebContents has been destroyed. This
99 // object will be destroyed after this call returns.
101 // This method can be overridden by subclasses. This gives the derived class
102 // opportunity to perform some cleanup.
103 virtual void GuestDestroyed() {}
105 // This method queries whether drag-and-drop is enabled for this particular
106 // view. By default, drag-and-drop is disabled. Derived classes can override
107 // this behavior to enable drag-and-drop.
108 virtual bool IsDragAndDropEnabled() const;
110 // Once a guest WebContents is ready, this initiates the association of |this|
111 // GuestView with |guest_web_contents|.
112 void Init(content::WebContents* guest_web_contents,
113 const std::string& embedder_extension_id);
115 bool IsViewType(const char* const view_type) const {
116 return !strcmp(GetViewType(), view_type);
119 base::WeakPtr<GuestViewBase> AsWeakPtr();
121 content::WebContents* embedder_web_contents() const {
122 return embedder_web_contents_;
125 // Returns the guest WebContents.
126 content::WebContents* guest_web_contents() const {
127 return web_contents();
130 // Returns the extra parameters associated with this GuestView passed
131 // in from JavaScript.
132 base::DictionaryValue* extra_params() const {
133 return extra_params_.get();
136 // Returns whether this guest has an associated embedder.
137 bool attached() const { return !!embedder_web_contents_; }
139 // Returns the instance ID of the <*view> element.
140 int view_instance_id() const { return view_instance_id_; }
142 // Returns the instance ID of the guest WebContents.
143 int guest_instance_id() const { return guest_instance_id_; }
145 // Returns the extension ID of the embedder.
146 const std::string& embedder_extension_id() const {
147 return embedder_extension_id_;
150 // Returns whether this GuestView is embedded in an extension/app.
151 bool in_extension() const { return !embedder_extension_id_.empty(); }
153 // Returns the user browser context of the embedder.
154 content::BrowserContext* browser_context() const { return browser_context_; }
156 // Returns the embedder's process ID.
157 int embedder_render_process_id() const { return embedder_render_process_id_; }
159 GuestViewBase* GetOpener() const {
160 return opener_.get();
163 void SetOpener(GuestViewBase* opener);
165 // BrowserPluginGuestDelegate implementation.
166 virtual void Destroy() OVERRIDE FINAL;
167 virtual void DidAttach() OVERRIDE FINAL;
168 virtual void RegisterDestructionCallback(
169 const DestructionCallback& callback) OVERRIDE FINAL;
170 virtual void WillAttach(
171 content::WebContents* embedder_web_contents,
172 const base::DictionaryValue& extra_params) OVERRIDE FINAL;
175 explicit GuestViewBase(int guest_instance_id);
177 virtual ~GuestViewBase();
179 // Dispatches an event |event_name| to the embedder with the |event| fields.
180 void DispatchEvent(Event* event);
183 class EmbedderWebContentsObserver;
185 void SendQueuedEvents();
187 // WebContentsObserver implementation.
188 virtual void DidStopLoading(
189 content::RenderViewHost* render_view_host) OVERRIDE FINAL;
190 virtual void WebContentsDestroyed() OVERRIDE FINAL;
192 // WebContentsDelegate implementation.
193 virtual bool ShouldFocusPageAfterCrash() OVERRIDE FINAL;
194 virtual bool PreHandleGestureEvent(
195 content::WebContents* source,
196 const blink::WebGestureEvent& event) OVERRIDE FINAL;
198 content::WebContents* embedder_web_contents_;
199 std::string embedder_extension_id_;
200 int embedder_render_process_id_;
201 content::BrowserContext* browser_context_;
202 // |guest_instance_id_| is a profile-wide unique identifier for a guest
204 const int guest_instance_id_;
205 // |view_instance_id_| is an identifier that's unique within a particular
206 // embedder RenderViewHost for a particular <*view> instance.
207 int view_instance_id_;
211 // This is a queue of Events that are destined to be sent to the embedder once
212 // the guest is attached to a particular embedder.
213 std::deque<linked_ptr<Event> > pending_events_;
215 // The opener guest view.
216 base::WeakPtr<GuestViewBase> opener_;
218 DestructionCallback destruction_callback_;
220 // The extra parameters associated with this GuestView passed
221 // in from JavaScript. This will typically be the view instance ID,
222 // the API to use, and view-specific parameters. These parameters
223 // are passed along to new guests that are created from this guest.
224 scoped_ptr<base::DictionaryValue> extra_params_;
226 scoped_ptr<EmbedderWebContentsObserver> embedder_web_contents_observer_;
228 // This is used to ensure pending tasks will not fire after this object is
230 base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_;
232 DISALLOW_COPY_AND_ASSIGN(GuestViewBase);
235 #endif // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_