OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ | |
6 #define CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ | |
7 | |
8 #include <queue> | |
9 | |
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" | |
17 | |
18 struct RendererContentSettingRules; | |
19 | |
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(); | |
36 | |
37 const std::string& name() const { return name_; } | |
38 | |
39 scoped_ptr<base::DictionaryValue> GetArguments(); | |
40 | |
41 private: | |
42 const std::string name_; | |
43 scoped_ptr<base::DictionaryValue> args_; | |
44 }; | |
45 | |
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); | |
51 | |
52 return NULL; | |
53 } | |
54 | |
55 typedef base::Callback<GuestViewBase*( | |
56 content::BrowserContext*, int)> GuestCreationCallback; | |
57 static void RegisterGuestViewType(const std::string& view_type, | |
58 const GuestCreationCallback& callback); | |
59 | |
60 static GuestViewBase* Create(content::BrowserContext* browser_context, | |
61 int guest_instance_id, | |
62 const std::string& view_type); | |
63 | |
64 static GuestViewBase* FromWebContents(content::WebContents* web_contents); | |
65 | |
66 static GuestViewBase* From(int embedder_process_id, int instance_id); | |
67 | |
68 static bool IsGuest(content::WebContents* web_contents); | |
69 | |
70 virtual const char* GetViewType() const = 0; | |
71 | |
72 // This method is called after the guest has been attached to an embedder and | |
73 // suspended resource loads have been resumed. | |
74 // | |
75 // This method can be overriden by subclasses. This gives the derived class | |
76 // an opportunity to perform setup actions after attachment. | |
77 virtual void DidAttachToEmbedder() {} | |
78 | |
79 // This method is called after this GuestViewBase has been initiated. | |
80 // | |
81 // This gives the derived class an opportunity to perform additional | |
82 // initialization. | |
83 virtual void DidInitialize() {} | |
84 | |
85 // This method is called when the initial set of frames within the page have | |
86 // completed loading. | |
87 virtual void DidStopLoading() {} | |
88 | |
89 // This method is called when the guest's embedder WebContents has been | |
90 // destroyed and the guest will be destroyed shortly. | |
91 // | |
92 // This gives the derived class an opportunity to perform some cleanup prior | |
93 // to destruction. | |
94 virtual void EmbedderDestroyed() {} | |
95 | |
96 // This method is called when the guest WebContents has been destroyed. This | |
97 // object will be destroyed after this call returns. | |
98 // | |
99 // This gives the derived class an opportunity to perform some cleanup. | |
100 virtual void GuestDestroyed() {} | |
101 | |
102 // This method is invoked when the guest RenderView is ready, e.g. because we | |
103 // recreated it after a crash. | |
104 // | |
105 // This gives the derived class an opportunity to perform some initialization | |
106 // work. | |
107 virtual void GuestReady() {} | |
108 | |
109 // This method is invoked when the contents auto-resized to give the container | |
110 // an opportunity to match it if it wishes. | |
111 // | |
112 // This gives the derived class an opportunity to inform its container element | |
113 // or perform other actions. | |
114 virtual void GuestSizeChangedDueToAutoSize(const gfx::Size& old_size, | |
115 const gfx::Size& new_size) {} | |
116 | |
117 // This method queries whether autosize is supported for this particular view. | |
118 // By default, autosize is not supported. Derived classes can override this | |
119 // behavior to support autosize. | |
120 virtual bool IsAutoSizeSupported() const; | |
121 | |
122 // This method queries whether drag-and-drop is enabled for this particular | |
123 // view. By default, drag-and-drop is disabled. Derived classes can override | |
124 // this behavior to enable drag-and-drop. | |
125 virtual bool IsDragAndDropEnabled() const; | |
126 | |
127 // This method is called immediately before suspended resource loads have been | |
128 // resumed on attachment to an embedder. | |
129 // | |
130 // This method can be overriden by subclasses. This gives the derived class | |
131 // an opportunity to perform setup actions before attachment. | |
132 virtual void WillAttachToEmbedder() {} | |
133 | |
134 // This method is called when the guest WebContents is about to be destroyed. | |
135 // | |
136 // This gives the derived class an opportunity to perform some cleanup prior | |
137 // to destruction. | |
138 virtual void WillDestroy() {} | |
139 | |
140 // This method is to be implemented by the derived class. Access to guest | |
141 // views are determined by the availability of the internal extension API | |
142 // used to implement the guest view. | |
143 // | |
144 // This should be the name of the API as it appears in the _api_features.json | |
145 // file. | |
146 virtual const char* GetAPINamespace() = 0; | |
147 | |
148 // This method is to be implemented by the derived class. Given a set of | |
149 // initialization parameters, a concrete subclass of GuestViewBase can | |
150 // create a specialized WebContents that it returns back to GuestViewBase. | |
151 typedef base::Callback<void(content::WebContents*)> | |
152 WebContentsCreatedCallback; | |
153 virtual void CreateWebContents( | |
154 const std::string& embedder_extension_id, | |
155 int embedder_render_process_id, | |
156 const base::DictionaryValue& create_params, | |
157 const WebContentsCreatedCallback& callback) = 0; | |
158 | |
159 // This creates a WebContents and initializes |this| GuestViewBase to use the | |
160 // newly created WebContents. | |
161 void Init(const std::string& embedder_extension_id, | |
162 content::WebContents* embedder_web_contents, | |
163 const base::DictionaryValue& create_params, | |
164 const WebContentsCreatedCallback& callback); | |
165 | |
166 void InitWithWebContents( | |
167 const std::string& embedder_extension_id, | |
168 int embedder_render_process_id, | |
169 content::WebContents* guest_web_contents); | |
170 | |
171 bool IsViewType(const char* const view_type) const { | |
172 return !strcmp(GetViewType(), view_type); | |
173 } | |
174 | |
175 // Toggles autosize mode for this GuestView. | |
176 void SetAutoSize(bool enabled, | |
177 const gfx::Size& min_size, | |
178 const gfx::Size& max_size); | |
179 | |
180 base::WeakPtr<GuestViewBase> AsWeakPtr(); | |
181 | |
182 bool initialized() const { return initialized_; } | |
183 | |
184 content::WebContents* embedder_web_contents() const { | |
185 return embedder_web_contents_; | |
186 } | |
187 | |
188 // Returns the guest WebContents. | |
189 content::WebContents* guest_web_contents() const { | |
190 return web_contents(); | |
191 } | |
192 | |
193 // Returns the extra parameters associated with this GuestView passed | |
194 // in from JavaScript. | |
195 base::DictionaryValue* extra_params() const { | |
196 return extra_params_.get(); | |
197 } | |
198 | |
199 // Returns whether this guest has an associated embedder. | |
200 bool attached() const { return !!embedder_web_contents_; } | |
201 | |
202 // Returns the instance ID of the <*view> element. | |
203 int view_instance_id() const { return view_instance_id_; } | |
204 | |
205 // Returns the extension ID of the embedder. | |
206 const std::string& embedder_extension_id() const { | |
207 return embedder_extension_id_; | |
208 } | |
209 | |
210 // Returns whether this GuestView is embedded in an extension/app. | |
211 bool in_extension() const { return !embedder_extension_id_.empty(); } | |
212 | |
213 // Returns the user browser context of the embedder. | |
214 content::BrowserContext* browser_context() const { return browser_context_; } | |
215 | |
216 // Returns the embedder's process ID. | |
217 int embedder_render_process_id() const { return embedder_render_process_id_; } | |
218 | |
219 GuestViewBase* GetOpener() const { | |
220 return opener_.get(); | |
221 } | |
222 | |
223 void SetOpener(GuestViewBase* opener); | |
224 | |
225 // RenderProcessHostObserver implementation | |
226 virtual void RenderProcessExited(content::RenderProcessHost* host, | |
227 base::ProcessHandle handle, | |
228 base::TerminationStatus status, | |
229 int exit_code) OVERRIDE; | |
230 | |
231 // BrowserPluginGuestDelegate implementation. | |
232 virtual void Destroy() OVERRIDE FINAL; | |
233 virtual void DidAttach() OVERRIDE FINAL; | |
234 virtual void ElementSizeChanged(const gfx::Size& old_size, | |
235 const gfx::Size& new_size) OVERRIDE FINAL; | |
236 virtual int GetGuestInstanceID() const OVERRIDE; | |
237 virtual void GuestSizeChanged(const gfx::Size& old_size, | |
238 const gfx::Size& new_size) OVERRIDE FINAL; | |
239 virtual void RegisterDestructionCallback( | |
240 const DestructionCallback& callback) OVERRIDE FINAL; | |
241 virtual void WillAttach( | |
242 content::WebContents* embedder_web_contents, | |
243 const base::DictionaryValue& extra_params) OVERRIDE FINAL; | |
244 | |
245 // Dispatches an event |event_name| to the embedder with the |event| fields. | |
246 void DispatchEventToEmbedder(Event* event); | |
247 | |
248 protected: | |
249 GuestViewBase(content::BrowserContext* browser_context, | |
250 int guest_instance_id); | |
251 | |
252 virtual ~GuestViewBase(); | |
253 | |
254 private: | |
255 class EmbedderWebContentsObserver; | |
256 | |
257 void SendQueuedEvents(); | |
258 | |
259 void CompleteInit(const std::string& embedder_extension_id, | |
260 int embedder_render_process_id, | |
261 const WebContentsCreatedCallback& callback, | |
262 content::WebContents* guest_web_contents); | |
263 | |
264 static void RegisterGuestViewTypes(); | |
265 | |
266 // WebContentsObserver implementation. | |
267 virtual void DidStopLoading( | |
268 content::RenderViewHost* render_view_host) OVERRIDE FINAL; | |
269 virtual void RenderViewReady() OVERRIDE FINAL; | |
270 virtual void WebContentsDestroyed() OVERRIDE FINAL; | |
271 | |
272 // WebContentsDelegate implementation. | |
273 virtual bool ShouldFocusPageAfterCrash() OVERRIDE FINAL; | |
274 virtual bool PreHandleGestureEvent( | |
275 content::WebContents* source, | |
276 const blink::WebGestureEvent& event) OVERRIDE FINAL; | |
277 | |
278 content::WebContents* embedder_web_contents_; | |
279 std::string embedder_extension_id_; | |
280 int embedder_render_process_id_; | |
281 content::BrowserContext* browser_context_; | |
282 // |guest_instance_id_| is a profile-wide unique identifier for a guest | |
283 // WebContents. | |
284 const int guest_instance_id_; | |
285 // |view_instance_id_| is an identifier that's unique within a particular | |
286 // embedder RenderViewHost for a particular <*view> instance. | |
287 int view_instance_id_; | |
288 | |
289 bool initialized_; | |
290 | |
291 // This is a queue of Events that are destined to be sent to the embedder once | |
292 // the guest is attached to a particular embedder. | |
293 std::deque<linked_ptr<Event> > pending_events_; | |
294 | |
295 // The opener guest view. | |
296 base::WeakPtr<GuestViewBase> opener_; | |
297 | |
298 DestructionCallback destruction_callback_; | |
299 | |
300 // The extra parameters associated with this GuestView passed | |
301 // in from JavaScript. This will typically be the view instance ID, | |
302 // the API to use, and view-specific parameters. These parameters | |
303 // are passed along to new guests that are created from this guest. | |
304 scoped_ptr<base::DictionaryValue> extra_params_; | |
305 | |
306 scoped_ptr<EmbedderWebContentsObserver> embedder_web_contents_observer_; | |
307 | |
308 // The size of the container element. | |
309 gfx::Size element_size_; | |
310 | |
311 // The size of the guest content. Note: In autosize mode, the container | |
312 // element may not match the size of the guest. | |
313 gfx::Size guest_size_; | |
314 | |
315 // Indicates whether autosize mode is enabled or not. | |
316 bool auto_size_enabled_; | |
317 | |
318 // The maximum size constraints of the container element in autosize mode. | |
319 gfx::Size max_auto_size_; | |
320 | |
321 // The minimum size constraints of the container element in autosize mode. | |
322 gfx::Size min_auto_size_; | |
323 | |
324 // This is used to ensure pending tasks will not fire after this object is | |
325 // destroyed. | |
326 base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_; | |
327 | |
328 DISALLOW_COPY_AND_ASSIGN(GuestViewBase); | |
329 }; | |
330 | |
331 #endif // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ | |
OLD | NEW |