blob: 2fd6acc53eb598e3887628c444e6c68013f7d894 [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]0e99fdc2014-04-30 05:10:335#ifndef CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_
6#define CHROME_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]0533cc6d2013-06-27 22:44:0513#include "content/public/browser/web_contents.h"
[email protected]aec80ed2014-05-27 00:01:1514#include "content/public/browser/web_contents_delegate.h"
[email protected]70ab2642014-05-30 08:06:5815#include "content/public/browser/web_contents_observer.h"
[email protected]0533cc6d2013-06-27 22:44:0516
[email protected]06153f02013-12-04 03:01:2817struct RendererContentSettingRules;
[email protected]0533cc6d2013-06-27 22:44:0518
[email protected]0e99fdc2014-04-30 05:10:3319// 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.
[email protected]aec80ed2014-05-27 00:01:1523class GuestViewBase : public content::BrowserPluginGuestDelegate,
[email protected]70ab2642014-05-30 08:06:5824 public content::WebContentsDelegate,
25 public content::WebContentsObserver {
[email protected]0533cc6d2013-06-27 22:44:0526 public:
[email protected]738f57a2013-06-29 21:06:5427 class Event {
28 public:
[email protected]0e99fdc2014-04-30 05:10:3329 Event(const std::string& name, scoped_ptr<base::DictionaryValue> args);
30 ~Event();
[email protected]738f57a2013-06-29 21:06:5431
[email protected]0aad6472013-12-04 18:25:3832 const std::string& name() const { return name_; }
[email protected]738f57a2013-06-29 21:06:5433
[email protected]cb1078de2013-12-23 20:04:2234 scoped_ptr<base::DictionaryValue> GetArguments();
[email protected]738f57a2013-06-29 21:06:5435
36 private:
[email protected]0aad6472013-12-04 18:25:3837 const std::string name_;
[email protected]cb1078de2013-12-23 20:04:2238 scoped_ptr<base::DictionaryValue> args_;
[email protected]738f57a2013-06-29 21:06:5439 };
40
[email protected]0e99fdc2014-04-30 05:10:3341 // Returns a *ViewGuest if this GuestView is of the given view type.
42 template <typename T>
43 T* As() {
[email protected]24569262014-05-06 03:31:3044 if (IsViewType(T::Type))
[email protected]0e99fdc2014-04-30 05:10:3345 return static_cast<T*>(this);
[email protected]24569262014-05-06 03:31:3046
[email protected]0e99fdc2014-04-30 05:10:3347 return NULL;
48 }
[email protected]50c827d2013-09-13 21:36:0949
[email protected]50d326e2014-05-20 17:59:0650 static GuestViewBase* Create(int guest_instance_id,
51 content::WebContents* guest_web_contents,
[email protected]0e99fdc2014-04-30 05:10:3352 const std::string& embedder_extension_id,
[email protected]50d326e2014-05-20 17:59:0653 const std::string& view_type);
[email protected]738f57a2013-06-29 21:06:5454
[email protected]0e99fdc2014-04-30 05:10:3355 static GuestViewBase* FromWebContents(content::WebContents* web_contents);
[email protected]0533cc6d2013-06-27 22:44:0556
[email protected]0e99fdc2014-04-30 05:10:3357 static GuestViewBase* From(int embedder_process_id, int instance_id);
[email protected]0533cc6d2013-06-27 22:44:0558
[email protected]a24efc22014-05-26 15:50:2559 static bool IsGuest(content::WebContents* web_contents);
60
[email protected]0e99fdc2014-04-30 05:10:3361 // For GuestViewBases, we create special guest processes, which host the
[email protected]3f24f97c2013-11-17 21:53:3362 // tag content separately from the main application that embeds the tag.
[email protected]0e99fdc2014-04-30 05:10:3363 // A GuestViewBase can specify both the partition name and whether the storage
[email protected]3f24f97c2013-11-17 21:53:3364 // for that partition should be persisted. Each tag gets a SiteInstance with
65 // a specially formatted URL, based on the application it is hosted by and
66 // the partition requested by it. The format for that URL is:
67 // chrome-guest://partition_domain/persist?partition_name
68 static bool GetGuestPartitionConfigForSite(const GURL& site,
69 std::string* partition_domain,
70 std::string* partition_name,
71 bool* in_memory);
72
[email protected]06153f02013-12-04 03:01:2873 // By default, JavaScript and images are enabled in guest content.
[email protected]0e99fdc2014-04-30 05:10:3374 static void GetDefaultContentSettingRules(RendererContentSettingRules* rules,
75 bool incognito);
76
[email protected]38fe4372014-05-01 08:38:3277 virtual const char* GetViewType() const = 0;
[email protected]06153f02013-12-04 03:01:2878
[email protected]70ab2642014-05-30 08:06:5879 // This method can be overridden by subclasses. It indicates that this guest's
80 // embedder has been destroyed and the guest will be destroyed shortly. This
81 // method gives derived classes the opportunity to perform some cleanup.
82 virtual void EmbedderDestroyed() {}
83
[email protected]24569262014-05-06 03:31:3084 bool IsViewType(const char* const view_type) const {
85 return !strcmp(GetViewType(), view_type);
86 }
87
88 base::WeakPtr<GuestViewBase> AsWeakPtr();
89
[email protected]738f57a2013-06-29 21:06:5490 virtual void Attach(content::WebContents* embedder_web_contents,
[email protected]738f57a2013-06-29 21:06:5491 const base::DictionaryValue& args);
92
[email protected]0533cc6d2013-06-27 22:44:0593 content::WebContents* embedder_web_contents() const {
94 return embedder_web_contents_;
95 }
96
97 // Returns the guest WebContents.
[email protected]738f57a2013-06-29 21:06:5498 content::WebContents* guest_web_contents() const {
[email protected]70ab2642014-05-30 08:06:5899 return web_contents();
[email protected]738f57a2013-06-29 21:06:54100 }
101
[email protected]50d326e2014-05-20 17:59:06102 // Returns the extra parameters associated with this GuestView passed
103 // in from JavaScript.
104 base::DictionaryValue* extra_params() const {
105 return extra_params_.get();
106 }
107
[email protected]738f57a2013-06-29 21:06:54108 // Returns whether this guest has an associated embedder.
109 bool attached() const { return !!embedder_web_contents_; }
110
[email protected]0533cc6d2013-06-27 22:44:05111 // Returns the instance ID of the <*view> element.
112 int view_instance_id() const { return view_instance_id_; }
113
114 // Returns the instance ID of the guest WebContents.
115 int guest_instance_id() const { return guest_instance_id_; }
116
117 // Returns the extension ID of the embedder.
[email protected]880331f972014-03-05 01:42:53118 const std::string& embedder_extension_id() const {
119 return embedder_extension_id_;
120 }
121
122 // Returns whether this GuestView is embedded in an extension/app.
[email protected]0e99fdc2014-04-30 05:10:33123 bool in_extension() const { return !embedder_extension_id_.empty(); }
[email protected]0533cc6d2013-06-27 22:44:05124
125 // Returns the user browser context of the embedder.
126 content::BrowserContext* browser_context() const { return browser_context_; }
127
128 // Returns the embedder's process ID.
129 int embedder_render_process_id() const { return embedder_render_process_id_; }
130
[email protected]50d326e2014-05-20 17:59:06131 GuestViewBase* GetOpener() const {
132 return opener_.get();
133 }
134
135 void SetOpener(GuestViewBase* opener);
136
[email protected]70ab2642014-05-30 08:06:58137 // WebContentsObserver implementation.
138 virtual void WebContentsDestroyed() OVERRIDE;
139
[email protected]aec80ed2014-05-27 00:01:15140 // WebContentsDelegate implementation.
141 virtual bool ShouldFocusPageAfterCrash() OVERRIDE;
[email protected]87acf4d2014-05-22 22:18:49142 virtual bool PreHandleGestureEvent(
143 content::WebContents* source,
144 const blink::WebGestureEvent& event) OVERRIDE;
[email protected]24569262014-05-06 03:31:30145
[email protected]aec80ed2014-05-27 00:01:15146 // BrowserPluginGuestDelegate implementation.
147 virtual void Destroy() OVERRIDE;
148 virtual void RegisterDestructionCallback(
149 const DestructionCallback& callback) OVERRIDE;
[email protected]0533cc6d2013-06-27 22:44:05150 protected:
[email protected]50d326e2014-05-20 17:59:06151 GuestViewBase(int guest_instance_id,
152 content::WebContents* guest_web_contents,
153 const std::string& embedder_extension_id);
[email protected]0e99fdc2014-04-30 05:10:33154 virtual ~GuestViewBase();
[email protected]0533cc6d2013-06-27 22:44:05155
156 // Dispatches an event |event_name| to the embedder with the |event| fields.
[email protected]738f57a2013-06-29 21:06:54157 void DispatchEvent(Event* event);
[email protected]0533cc6d2013-06-27 22:44:05158
159 private:
[email protected]70ab2642014-05-30 08:06:58160 class EmbedderWebContentsObserver;
161
[email protected]738f57a2013-06-29 21:06:54162 void SendQueuedEvents();
163
[email protected]0533cc6d2013-06-27 22:44:05164 content::WebContents* embedder_web_contents_;
[email protected]880331f972014-03-05 01:42:53165 const std::string embedder_extension_id_;
[email protected]738f57a2013-06-29 21:06:54166 int embedder_render_process_id_;
[email protected]70e7c0e2013-11-08 00:33:21167 content::BrowserContext* const browser_context_;
[email protected]0533cc6d2013-06-27 22:44:05168 // |guest_instance_id_| is a profile-wide unique identifier for a guest
169 // WebContents.
170 const int guest_instance_id_;
171 // |view_instance_id_| is an identifier that's unique within a particular
172 // embedder RenderViewHost for a particular <*view> instance.
[email protected]738f57a2013-06-29 21:06:54173 int view_instance_id_;
174
175 // This is a queue of Events that are destined to be sent to the embedder once
176 // the guest is attached to a particular embedder.
[email protected]0544ea92014-04-22 21:50:47177 std::deque<linked_ptr<Event> > pending_events_;
[email protected]0533cc6d2013-06-27 22:44:05178
[email protected]24569262014-05-06 03:31:30179 // The opener guest view.
180 base::WeakPtr<GuestViewBase> opener_;
181
[email protected]50d326e2014-05-20 17:59:06182 DestructionCallback destruction_callback_;
183
184 // The extra parameters associated with this GuestView passed
185 // in from JavaScript. This will typically be the view instance ID,
186 // the API to use, and view-specific parameters. These parameters
187 // are passed along to new guests that are created from this guest.
188 scoped_ptr<base::DictionaryValue> extra_params_;
189
[email protected]70ab2642014-05-30 08:06:58190 scoped_ptr<EmbedderWebContentsObserver> embedder_web_contents_observer_;
191
[email protected]f21d36e2014-01-16 19:24:04192 // This is used to ensure pending tasks will not fire after this object is
193 // destroyed.
[email protected]0e99fdc2014-04-30 05:10:33194 base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_;
[email protected]f21d36e2014-01-16 19:24:04195
[email protected]0e99fdc2014-04-30 05:10:33196 DISALLOW_COPY_AND_ASSIGN(GuestViewBase);
[email protected]0533cc6d2013-06-27 22:44:05197};
198
[email protected]0e99fdc2014-04-30 05:10:33199#endif // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_