Disable accessible touch exploration by default.
[chromium-blink-merge.git] / chrome / browser / guest_view / guest_view_base.h
blob1ca4dde4d98c3fde10917aca07227872bbc5f5c2
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_
8 #include <queue>
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/render_process_host_observer.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/web_contents_delegate.h"
16 #include "content/public/browser/web_contents_observer.h"
18 struct RendererContentSettingRules;
20 // A GuestViewBase is the base class browser-side API implementation for a
21 // <*view> tag. GuestViewBase maintains an association between a guest
22 // WebContents and an embedder WebContents. It receives events issued from
23 // the guest and relays them to the embedder. GuestViewBase tracks the lifetime
24 // of its embedder render process until it is attached to a particular embedder
25 // WebContents. At that point, its lifetime is restricted in scope to the
26 // lifetime of its embedder WebContents.
27 class GuestViewBase : public content::BrowserPluginGuestDelegate,
28 public content::RenderProcessHostObserver,
29 public content::WebContentsDelegate,
30 public content::WebContentsObserver {
31 public:
32 class Event {
33 public:
34 Event(const std::string& name, scoped_ptr<base::DictionaryValue> args);
35 ~Event();
37 const std::string& name() const { return name_; }
39 scoped_ptr<base::DictionaryValue> GetArguments();
41 private:
42 const std::string name_;
43 scoped_ptr<base::DictionaryValue> args_;
46 // Returns a *ViewGuest if this GuestView is of the given view type.
47 template <typename T>
48 T* As() {
49 if (IsViewType(T::Type))
50 return static_cast<T*>(this);
52 return NULL;
55 typedef base::Callback<GuestViewBase*(
56 content::BrowserContext*, int)> GuestCreationCallback;
57 static void RegisterGuestViewType(const std::string& view_type,
58 const GuestCreationCallback& callback);
60 static GuestViewBase* Create(content::BrowserContext* browser_context,
61 int guest_instance_id,
62 const std::string& view_type);
64 static GuestViewBase* FromWebContents(content::WebContents* web_contents);
66 static GuestViewBase* From(int embedder_process_id, int instance_id);
68 static bool IsGuest(content::WebContents* web_contents);
70 // By default, JavaScript and images are enabled in guest content.
71 static void GetDefaultContentSettingRules(RendererContentSettingRules* rules,
72 bool incognito);
74 virtual const char* GetViewType() const = 0;
76 // This method is called after the guest has been attached to an embedder and
77 // suspended resource loads have been resumed.
79 // This method can be overriden by subclasses. This gives the derived class
80 // an opportunity to perform setup actions after attachment.
81 virtual void DidAttachToEmbedder() {}
83 // This method is called after this GuestViewBase has been initiated.
85 // This gives the derived class an opportunity to perform additional
86 // initialization.
87 virtual void DidInitialize() {}
89 // This method is called when the initial set of frames within the page have
90 // completed loading.
91 virtual void DidStopLoading() {}
93 // This method is called when the guest's embedder WebContents has been
94 // destroyed and the guest will be destroyed shortly.
96 // This gives the derived class an opportunity to perform some cleanup prior
97 // to destruction.
98 virtual void EmbedderDestroyed() {}
100 // This method is called when the guest WebContents has been destroyed. This
101 // object will be destroyed after this call returns.
103 // This gives the derived class an opportunity to perform some cleanup.
104 virtual void GuestDestroyed() {}
106 // This method queries whether drag-and-drop is enabled for this particular
107 // view. By default, drag-and-drop is disabled. Derived classes can override
108 // this behavior to enable drag-and-drop.
109 virtual bool IsDragAndDropEnabled() const;
111 // This method is called immediately before suspended resource loads have been
112 // resumed on attachment to an embedder.
114 // This method can be overriden by subclasses. This gives the derived class
115 // an opportunity to perform setup actions before attachment.
116 virtual void WillAttachToEmbedder() {}
118 // This method is called when the guest WebContents is about to be destroyed.
120 // This gives the derived class an opportunity to perform some cleanup prior
121 // to destruction.
122 virtual void WillDestroy() {}
124 // This method is to be implemented by the derived class. It determines
125 // whether the guest view type of the derived class can be used by the
126 // provided embedder extension ID.
127 virtual bool CanEmbedderUseGuestView(
128 const std::string& embedder_extension_id) = 0;
130 // This method is to be implemented by the derived class. Given a set of
131 // initialization parameters, a concrete subclass of GuestViewBase can
132 // create a specialized WebContents that it returns back to GuestViewBase.
133 typedef base::Callback<void(content::WebContents*)>
134 WebContentsCreatedCallback;
135 virtual void CreateWebContents(
136 const std::string& embedder_extension_id,
137 int embedder_render_process_id,
138 const base::DictionaryValue& create_params,
139 const WebContentsCreatedCallback& callback) = 0;
141 // This creates a WebContents and initializes |this| GuestViewBase to use the
142 // newly created WebContents.
143 void Init(const std::string& embedder_extension_id,
144 int embedder_render_process_id,
145 const base::DictionaryValue& create_params,
146 const WebContentsCreatedCallback& callback);
148 void InitWithWebContents(
149 const std::string& embedder_extension_id,
150 int embedder_render_process_id,
151 content::WebContents* guest_web_contents);
153 bool IsViewType(const char* const view_type) const {
154 return !strcmp(GetViewType(), view_type);
157 base::WeakPtr<GuestViewBase> AsWeakPtr();
159 bool initialized() const { return initialized_; }
161 content::WebContents* embedder_web_contents() const {
162 return embedder_web_contents_;
165 // Returns the guest WebContents.
166 content::WebContents* guest_web_contents() const {
167 return web_contents();
170 // Returns the extra parameters associated with this GuestView passed
171 // in from JavaScript.
172 base::DictionaryValue* extra_params() const {
173 return extra_params_.get();
176 // Returns whether this guest has an associated embedder.
177 bool attached() const { return !!embedder_web_contents_; }
179 // Returns the instance ID of the <*view> element.
180 int view_instance_id() const { return view_instance_id_; }
182 // Returns the extension ID of the embedder.
183 const std::string& embedder_extension_id() const {
184 return embedder_extension_id_;
187 // Returns whether this GuestView is embedded in an extension/app.
188 bool in_extension() const { return !embedder_extension_id_.empty(); }
190 // Returns the user browser context of the embedder.
191 content::BrowserContext* browser_context() const { return browser_context_; }
193 // Returns the embedder's process ID.
194 int embedder_render_process_id() const { return embedder_render_process_id_; }
196 GuestViewBase* GetOpener() const {
197 return opener_.get();
200 void SetOpener(GuestViewBase* opener);
202 // RenderProcessHostObserver implementation
203 virtual void RenderProcessExited(content::RenderProcessHost* host,
204 base::ProcessHandle handle,
205 base::TerminationStatus status,
206 int exit_code) OVERRIDE;
208 // BrowserPluginGuestDelegate implementation.
209 virtual void Destroy() OVERRIDE FINAL;
210 virtual void DidAttach() OVERRIDE FINAL;
211 virtual int GetGuestInstanceID() const OVERRIDE;
212 virtual void RegisterDestructionCallback(
213 const DestructionCallback& callback) OVERRIDE FINAL;
214 virtual void WillAttach(
215 content::WebContents* embedder_web_contents,
216 const base::DictionaryValue& extra_params) OVERRIDE FINAL;
218 // Dispatches an event |event_name| to the embedder with the |event| fields.
219 void DispatchEventToEmbedder(Event* event);
221 protected:
222 GuestViewBase(content::BrowserContext* browser_context,
223 int guest_instance_id);
225 virtual ~GuestViewBase();
227 private:
228 class EmbedderWebContentsObserver;
230 void SendQueuedEvents();
232 void CompleteInit(const std::string& embedder_extension_id,
233 int embedder_render_process_id,
234 const WebContentsCreatedCallback& callback,
235 content::WebContents* guest_web_contents);
237 static void RegisterGuestViewTypes();
239 // WebContentsObserver implementation.
240 virtual void DidStopLoading(
241 content::RenderViewHost* render_view_host) OVERRIDE FINAL;
242 virtual void WebContentsDestroyed() OVERRIDE FINAL;
244 // WebContentsDelegate implementation.
245 virtual bool ShouldFocusPageAfterCrash() OVERRIDE FINAL;
246 virtual bool PreHandleGestureEvent(
247 content::WebContents* source,
248 const blink::WebGestureEvent& event) OVERRIDE FINAL;
250 content::WebContents* embedder_web_contents_;
251 std::string embedder_extension_id_;
252 int embedder_render_process_id_;
253 content::BrowserContext* browser_context_;
254 // |guest_instance_id_| is a profile-wide unique identifier for a guest
255 // WebContents.
256 const int guest_instance_id_;
257 // |view_instance_id_| is an identifier that's unique within a particular
258 // embedder RenderViewHost for a particular <*view> instance.
259 int view_instance_id_;
261 bool initialized_;
263 // This is a queue of Events that are destined to be sent to the embedder once
264 // the guest is attached to a particular embedder.
265 std::deque<linked_ptr<Event> > pending_events_;
267 // The opener guest view.
268 base::WeakPtr<GuestViewBase> opener_;
270 DestructionCallback destruction_callback_;
272 // The extra parameters associated with this GuestView passed
273 // in from JavaScript. This will typically be the view instance ID,
274 // the API to use, and view-specific parameters. These parameters
275 // are passed along to new guests that are created from this guest.
276 scoped_ptr<base::DictionaryValue> extra_params_;
278 scoped_ptr<EmbedderWebContentsObserver> embedder_web_contents_observer_;
280 // This is used to ensure pending tasks will not fire after this object is
281 // destroyed.
282 base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_;
284 DISALLOW_COPY_AND_ASSIGN(GuestViewBase);
287 #endif // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_