blob: 05179445f0f0f1be2defef247e10676637f5a507 [file] [log] [blame]
[email protected]945604a2014-04-28 12:29:591// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]0533cc6d2013-06-27 22:44:052// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]140d6cd92014-08-12 18:26:465#ifndef EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_
6#define EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_
[email protected]0533cc6d2013-06-27 22:44:057
[email protected]738f57a2013-06-29 21:06:548#include <queue>
9
[email protected]f21d36e2014-01-16 19:24:0410#include "base/memory/weak_ptr.h"
[email protected]0533cc6d2013-06-27 22:44:0511#include "base/values.h"
[email protected]4c0e8272013-07-03 23:39:2212#include "content/public/browser/browser_plugin_guest_delegate.h"
[email protected]4858e432014-06-23 18:14:1713#include "content/public/browser/render_process_host_observer.h"
[email protected]0533cc6d2013-06-27 22:44:0514#include "content/public/browser/web_contents.h"
[email protected]aec80ed2014-05-27 00:01:1515#include "content/public/browser/web_contents_delegate.h"
[email protected]70ab2642014-05-30 08:06:5816#include "content/public/browser/web_contents_observer.h"
[email protected]0533cc6d2013-06-27 22:44:0517
[email protected]06153f02013-12-04 03:01:2818struct RendererContentSettingRules;
[email protected]0533cc6d2013-06-27 22:44:0519
[email protected]140d6cd92014-08-12 18:26:4620namespace extensions {
21
[email protected]0e99fdc2014-04-30 05:10:3322// A GuestViewBase is the base class browser-side API implementation for a
23// <*view> tag. GuestViewBase maintains an association between a guest
24// WebContents and an embedder WebContents. It receives events issued from
[email protected]4858e432014-06-23 18:14:1725// the guest and relays them to the embedder. GuestViewBase tracks the lifetime
26// of its embedder render process until it is attached to a particular embedder
27// WebContents. At that point, its lifetime is restricted in scope to the
28// lifetime of its embedder WebContents.
[email protected]aec80ed2014-05-27 00:01:1529class GuestViewBase : public content::BrowserPluginGuestDelegate,
[email protected]4858e432014-06-23 18:14:1730 public content::RenderProcessHostObserver,
[email protected]70ab2642014-05-30 08:06:5831 public content::WebContentsDelegate,
32 public content::WebContentsObserver {
[email protected]0533cc6d2013-06-27 22:44:0533 public:
[email protected]738f57a2013-06-29 21:06:5434 class Event {
35 public:
[email protected]0e99fdc2014-04-30 05:10:3336 Event(const std::string& name, scoped_ptr<base::DictionaryValue> args);
37 ~Event();
[email protected]738f57a2013-06-29 21:06:5438
[email protected]0aad6472013-12-04 18:25:3839 const std::string& name() const { return name_; }
[email protected]738f57a2013-06-29 21:06:5440
[email protected]cb1078de2013-12-23 20:04:2241 scoped_ptr<base::DictionaryValue> GetArguments();
[email protected]738f57a2013-06-29 21:06:5442
43 private:
[email protected]0aad6472013-12-04 18:25:3844 const std::string name_;
[email protected]cb1078de2013-12-23 20:04:2245 scoped_ptr<base::DictionaryValue> args_;
[email protected]738f57a2013-06-29 21:06:5446 };
47
[email protected]0e99fdc2014-04-30 05:10:3348 // Returns a *ViewGuest if this GuestView is of the given view type.
49 template <typename T>
50 T* As() {
[email protected]24569262014-05-06 03:31:3051 if (IsViewType(T::Type))
[email protected]0e99fdc2014-04-30 05:10:3352 return static_cast<T*>(this);
[email protected]24569262014-05-06 03:31:3053
[email protected]0e99fdc2014-04-30 05:10:3354 return NULL;
55 }
[email protected]50c827d2013-09-13 21:36:0956
[email protected]71c63dc2014-07-21 22:49:5357 typedef base::Callback<GuestViewBase*(
58 content::BrowserContext*, int)> GuestCreationCallback;
59 static void RegisterGuestViewType(const std::string& view_type,
60 const GuestCreationCallback& callback);
61
[email protected]38177c32014-06-25 23:20:2362 static GuestViewBase* Create(content::BrowserContext* browser_context,
63 int guest_instance_id,
[email protected]50d326e2014-05-20 17:59:0664 const std::string& view_type);
[email protected]738f57a2013-06-29 21:06:5465
[email protected]0e99fdc2014-04-30 05:10:3366 static GuestViewBase* FromWebContents(content::WebContents* web_contents);
[email protected]0533cc6d2013-06-27 22:44:0567
[email protected]0e99fdc2014-04-30 05:10:3368 static GuestViewBase* From(int embedder_process_id, int instance_id);
[email protected]0533cc6d2013-06-27 22:44:0569
[email protected]a24efc22014-05-26 15:50:2570 static bool IsGuest(content::WebContents* web_contents);
71
[email protected]38fe4372014-05-01 08:38:3272 virtual const char* GetViewType() const = 0;
[email protected]06153f02013-12-04 03:01:2873
[email protected]d84d57b2014-06-20 22:42:3974 // This method is called after the guest has been attached to an embedder and
75 // suspended resource loads have been resumed.
76 //
77 // This method can be overriden by subclasses. This gives the derived class
78 // an opportunity to perform setup actions after attachment.
79 virtual void DidAttachToEmbedder() {}
80
[email protected]4858e432014-06-23 18:14:1781 // This method is called after this GuestViewBase has been initiated.
82 //
83 // This gives the derived class an opportunity to perform additional
84 // initialization.
85 virtual void DidInitialize() {}
86
87 // This method is called when the initial set of frames within the page have
88 // completed loading.
[email protected]feaa8cf2014-05-31 03:57:1489 virtual void DidStopLoading() {}
90
[email protected]4858e432014-06-23 18:14:1791 // This method is called when the guest's embedder WebContents has been
92 // destroyed and the guest will be destroyed shortly.
93 //
94 // This gives the derived class an opportunity to perform some cleanup prior
95 // to destruction.
96 virtual void EmbedderDestroyed() {}
97
98 // This method is called when the guest WebContents has been destroyed. This
99 // object will be destroyed after this call returns.
100 //
101 // This gives the derived class an opportunity to perform some cleanup.
102 virtual void GuestDestroyed() {}
103
[email protected]5ca06862014-08-06 19:09:55104 // This method is invoked when the guest RenderView is ready, e.g. because we
105 // recreated it after a crash.
106 //
107 // This gives the derived class an opportunity to perform some initialization
108 // work.
109 virtual void GuestReady() {}
110
111 // This method is invoked when the contents auto-resized to give the container
112 // an opportunity to match it if it wishes.
113 //
114 // This gives the derived class an opportunity to inform its container element
115 // or perform other actions.
116 virtual void GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
117 const gfx::Size& new_size) {}
118
119 // This method queries whether autosize is supported for this particular view.
120 // By default, autosize is not supported. Derived classes can override this
121 // behavior to support autosize.
122 virtual bool IsAutoSizeSupported() const;
123
[email protected]4858e432014-06-23 18:14:17124 // This method queries whether drag-and-drop is enabled for this particular
125 // view. By default, drag-and-drop is disabled. Derived classes can override
126 // this behavior to enable drag-and-drop.
127 virtual bool IsDragAndDropEnabled() const;
128
[email protected]d84d57b2014-06-20 22:42:39129 // This method is called immediately before suspended resource loads have been
130 // resumed on attachment to an embedder.
131 //
132 // This method can be overriden by subclasses. This gives the derived class
133 // an opportunity to perform setup actions before attachment.
134 virtual void WillAttachToEmbedder() {}
135
[email protected]a868c6c2014-06-04 13:07:43136 // This method is called when the guest WebContents is about to be destroyed.
137 //
[email protected]4858e432014-06-23 18:14:17138 // This gives the derived class an opportunity to perform some cleanup prior
139 // to destruction.
[email protected]a868c6c2014-06-04 13:07:43140 virtual void WillDestroy() {}
141
[email protected]755211fe2014-08-08 19:01:49142 // This method is to be implemented by the derived class. Access to guest
143 // views are determined by the availability of the internal extension API
144 // used to implement the guest view.
145 //
146 // This should be the name of the API as it appears in the _api_features.json
147 // file.
fsamuel99492be2014-08-28 03:50:27148 virtual const char* GetAPINamespace() const = 0;
149
150 // This method is to be implemented by the derived class. This method is the
151 // task prefix to show for a task produced by this GuestViewBase's derived
152 // type.
153 virtual int GetTaskPrefix() const = 0;
[email protected]a2be2f112014-07-12 01:10:05154
[email protected]4858e432014-06-23 18:14:17155 // This method is to be implemented by the derived class. Given a set of
156 // initialization parameters, a concrete subclass of GuestViewBase can
157 // create a specialized WebContents that it returns back to GuestViewBase.
158 typedef base::Callback<void(content::WebContents*)>
159 WebContentsCreatedCallback;
160 virtual void CreateWebContents(
161 const std::string& embedder_extension_id,
162 int embedder_render_process_id,
163 const base::DictionaryValue& create_params,
164 const WebContentsCreatedCallback& callback) = 0;
[email protected]70ab2642014-05-30 08:06:58165
[email protected]4858e432014-06-23 18:14:17166 // This creates a WebContents and initializes |this| GuestViewBase to use the
167 // newly created WebContents.
168 void Init(const std::string& embedder_extension_id,
[email protected]755211fe2014-08-08 19:01:49169 content::WebContents* embedder_web_contents,
[email protected]38177c32014-06-25 23:20:23170 const base::DictionaryValue& create_params,
171 const WebContentsCreatedCallback& callback);
[email protected]a868c6c2014-06-04 13:07:43172
[email protected]4858e432014-06-23 18:14:17173 void InitWithWebContents(
174 const std::string& embedder_extension_id,
175 int embedder_render_process_id,
176 content::WebContents* guest_web_contents);
[email protected]d84d57b2014-06-20 22:42:39177
[email protected]24569262014-05-06 03:31:30178 bool IsViewType(const char* const view_type) const {
179 return !strcmp(GetViewType(), view_type);
180 }
181
[email protected]5ca06862014-08-06 19:09:55182 // Toggles autosize mode for this GuestView.
183 void SetAutoSize(bool enabled,
184 const gfx::Size& min_size,
185 const gfx::Size& max_size);
186
[email protected]24569262014-05-06 03:31:30187 base::WeakPtr<GuestViewBase> AsWeakPtr();
188
[email protected]4858e432014-06-23 18:14:17189 bool initialized() const { return initialized_; }
190
[email protected]0533cc6d2013-06-27 22:44:05191 content::WebContents* embedder_web_contents() const {
192 return embedder_web_contents_;
193 }
194
195 // Returns the guest WebContents.
[email protected]738f57a2013-06-29 21:06:54196 content::WebContents* guest_web_contents() const {
[email protected]70ab2642014-05-30 08:06:58197 return web_contents();
[email protected]738f57a2013-06-29 21:06:54198 }
199
[email protected]2101c4c2014-08-22 00:16:16200 // Returns the parameters associated with the element hosting this GuestView
201 // passed in from JavaScript.
202 base::DictionaryValue* attach_params() const { return attach_params_.get(); }
[email protected]50d326e2014-05-20 17:59:06203
[email protected]738f57a2013-06-29 21:06:54204 // Returns whether this guest has an associated embedder.
205 bool attached() const { return !!embedder_web_contents_; }
206
[email protected]0533cc6d2013-06-27 22:44:05207 // Returns the instance ID of the <*view> element.
208 int view_instance_id() const { return view_instance_id_; }
209
[email protected]2101c4c2014-08-22 00:16:16210 // Returns the instance ID of this GuestViewBase.
211 int guest_instance_id() const { return guest_instance_id_; }
212
[email protected]0533cc6d2013-06-27 22:44:05213 // Returns the extension ID of the embedder.
[email protected]880331f972014-03-05 01:42:53214 const std::string& embedder_extension_id() const {
215 return embedder_extension_id_;
216 }
217
218 // Returns whether this GuestView is embedded in an extension/app.
[email protected]0e99fdc2014-04-30 05:10:33219 bool in_extension() const { return !embedder_extension_id_.empty(); }
[email protected]0533cc6d2013-06-27 22:44:05220
221 // Returns the user browser context of the embedder.
222 content::BrowserContext* browser_context() const { return browser_context_; }
223
224 // Returns the embedder's process ID.
225 int embedder_render_process_id() const { return embedder_render_process_id_; }
226
[email protected]50d326e2014-05-20 17:59:06227 GuestViewBase* GetOpener() const {
228 return opener_.get();
229 }
230
[email protected]2101c4c2014-08-22 00:16:16231 // Sets some additional chrome/ initialization parameters.
232 void SetAttachParams(const base::DictionaryValue& params);
[email protected]50d326e2014-05-20 17:59:06233 void SetOpener(GuestViewBase* opener);
234
[email protected]4858e432014-06-23 18:14:17235 // RenderProcessHostObserver implementation
236 virtual void RenderProcessExited(content::RenderProcessHost* host,
237 base::ProcessHandle handle,
238 base::TerminationStatus status,
239 int exit_code) OVERRIDE;
240
[email protected]aec80ed2014-05-27 00:01:15241 // BrowserPluginGuestDelegate implementation.
[email protected]a868c6c2014-06-04 13:07:43242 virtual void Destroy() OVERRIDE FINAL;
[email protected]d84d57b2014-06-20 22:42:39243 virtual void DidAttach() OVERRIDE FINAL;
[email protected]5ca06862014-08-06 19:09:55244 virtual void ElementSizeChanged(const gfx::Size& old_size,
245 const gfx::Size& new_size) OVERRIDE FINAL;
[email protected]5ca06862014-08-06 19:09:55246 virtual void GuestSizeChanged(const gfx::Size& old_size,
247 const gfx::Size& new_size) OVERRIDE FINAL;
[email protected]aec80ed2014-05-27 00:01:15248 virtual void RegisterDestructionCallback(
[email protected]a868c6c2014-06-04 13:07:43249 const DestructionCallback& callback) OVERRIDE FINAL;
[email protected]d84d57b2014-06-20 22:42:39250 virtual void WillAttach(
[email protected]2101c4c2014-08-22 00:16:16251 content::WebContents* embedder_web_contents) OVERRIDE FINAL;
[email protected]a868c6c2014-06-04 13:07:43252
[email protected]7adb26a72014-07-09 17:44:35253 // Dispatches an event |event_name| to the embedder with the |event| fields.
254 void DispatchEventToEmbedder(Event* event);
255
[email protected]0533cc6d2013-06-27 22:44:05256 protected:
[email protected]38177c32014-06-25 23:20:23257 GuestViewBase(content::BrowserContext* browser_context,
258 int guest_instance_id);
[email protected]d84d57b2014-06-20 22:42:39259
[email protected]0e99fdc2014-04-30 05:10:33260 virtual ~GuestViewBase();
[email protected]0533cc6d2013-06-27 22:44:05261
[email protected]0533cc6d2013-06-27 22:44:05262 private:
[email protected]70ab2642014-05-30 08:06:58263 class EmbedderWebContentsObserver;
264
[email protected]738f57a2013-06-29 21:06:54265 void SendQueuedEvents();
266
[email protected]3d888fa2014-07-11 19:27:16267 void CompleteInit(const std::string& embedder_extension_id,
268 int embedder_render_process_id,
269 const WebContentsCreatedCallback& callback,
270 content::WebContents* guest_web_contents);
[email protected]38177c32014-06-25 23:20:23271
[email protected]52263312014-07-22 17:45:13272 static void RegisterGuestViewTypes();
273
[email protected]a868c6c2014-06-04 13:07:43274 // WebContentsObserver implementation.
275 virtual void DidStopLoading(
276 content::RenderViewHost* render_view_host) OVERRIDE FINAL;
[email protected]5ca06862014-08-06 19:09:55277 virtual void RenderViewReady() OVERRIDE FINAL;
[email protected]a868c6c2014-06-04 13:07:43278 virtual void WebContentsDestroyed() OVERRIDE FINAL;
279
280 // WebContentsDelegate implementation.
281 virtual bool ShouldFocusPageAfterCrash() OVERRIDE FINAL;
282 virtual bool PreHandleGestureEvent(
283 content::WebContents* source,
284 const blink::WebGestureEvent& event) OVERRIDE FINAL;
285
[email protected]0533cc6d2013-06-27 22:44:05286 content::WebContents* embedder_web_contents_;
[email protected]d84d57b2014-06-20 22:42:39287 std::string embedder_extension_id_;
[email protected]738f57a2013-06-29 21:06:54288 int embedder_render_process_id_;
[email protected]d84d57b2014-06-20 22:42:39289 content::BrowserContext* browser_context_;
[email protected]0533cc6d2013-06-27 22:44:05290 // |guest_instance_id_| is a profile-wide unique identifier for a guest
291 // WebContents.
292 const int guest_instance_id_;
293 // |view_instance_id_| is an identifier that's unique within a particular
294 // embedder RenderViewHost for a particular <*view> instance.
[email protected]738f57a2013-06-29 21:06:54295 int view_instance_id_;
296
[email protected]d84d57b2014-06-20 22:42:39297 bool initialized_;
298
[email protected]738f57a2013-06-29 21:06:54299 // This is a queue of Events that are destined to be sent to the embedder once
300 // the guest is attached to a particular embedder.
[email protected]0544ea92014-04-22 21:50:47301 std::deque<linked_ptr<Event> > pending_events_;
[email protected]0533cc6d2013-06-27 22:44:05302
[email protected]24569262014-05-06 03:31:30303 // The opener guest view.
304 base::WeakPtr<GuestViewBase> opener_;
305
[email protected]50d326e2014-05-20 17:59:06306 DestructionCallback destruction_callback_;
307
[email protected]2101c4c2014-08-22 00:16:16308 // The parameters associated with the element hosting this GuestView that
309 // are passed in from JavaScript. This will typically be the view instance ID,
310 // and element-specific parameters. These parameters are passed along to new
311 // guests that are created from this guest.
312 scoped_ptr<base::DictionaryValue> attach_params_;
[email protected]50d326e2014-05-20 17:59:06313
[email protected]70ab2642014-05-30 08:06:58314 scoped_ptr<EmbedderWebContentsObserver> embedder_web_contents_observer_;
315
[email protected]5ca06862014-08-06 19:09:55316 // The size of the container element.
317 gfx::Size element_size_;
318
319 // The size of the guest content. Note: In autosize mode, the container
320 // element may not match the size of the guest.
321 gfx::Size guest_size_;
322
323 // Indicates whether autosize mode is enabled or not.
324 bool auto_size_enabled_;
325
326 // The maximum size constraints of the container element in autosize mode.
327 gfx::Size max_auto_size_;
328
329 // The minimum size constraints of the container element in autosize mode.
330 gfx::Size min_auto_size_;
331
[email protected]f21d36e2014-01-16 19:24:04332 // This is used to ensure pending tasks will not fire after this object is
333 // destroyed.
[email protected]0e99fdc2014-04-30 05:10:33334 base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_;
[email protected]f21d36e2014-01-16 19:24:04335
[email protected]0e99fdc2014-04-30 05:10:33336 DISALLOW_COPY_AND_ASSIGN(GuestViewBase);
[email protected]0533cc6d2013-06-27 22:44:05337};
338
[email protected]140d6cd92014-08-12 18:26:46339} // namespace extensions
340
341#endif // EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_