OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ | 5 #ifndef CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_ |
6 #define CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ | 6 #define CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_ |
7 | 7 |
8 #include <queue> | 8 #include <queue> |
9 | 9 |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "content/public/browser/browser_plugin_guest_delegate.h" | 12 #include "content/public/browser/browser_plugin_guest_delegate.h" |
13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
14 | 14 |
15 class AdViewGuest; | 15 class AdViewGuest; |
16 class WebViewGuest; | 16 class WebViewGuest; |
17 struct RendererContentSettingRules; | 17 struct RendererContentSettingRules; |
18 | 18 |
19 // A GuestViewBase is the base class browser-side API implementation for a | 19 // A GuestView is the base class browser-side API implementation for a <*view> |
20 // <*view> tag. GuestViewBase maintains an association between a guest | 20 // tag. GuestView maintains an association between a guest WebContents and an |
21 // WebContents and an embedder WebContents. It receives events issued from | 21 // embedder WebContents. It receives events issued from the guest and relays |
22 // the guest and relays them to the embedder. | 22 // them to the embedder. |
23 class GuestViewBase : public content::BrowserPluginGuestDelegate { | 23 class GuestView : public content::BrowserPluginGuestDelegate { |
24 public: | 24 public: |
| 25 enum Type { |
| 26 WEBVIEW, |
| 27 ADVIEW, |
| 28 UNKNOWN |
| 29 }; |
| 30 |
25 class Event { | 31 class Event { |
26 public: | 32 public: |
27 Event(const std::string& name, scoped_ptr<base::DictionaryValue> args); | 33 Event(const std::string& name, scoped_ptr<base::DictionaryValue> args); |
28 ~Event(); | 34 ~Event(); |
29 | 35 |
30 const std::string& name() const { return name_; } | 36 const std::string& name() const { return name_; } |
31 | 37 |
32 scoped_ptr<base::DictionaryValue> GetArguments(); | 38 scoped_ptr<base::DictionaryValue> GetArguments(); |
33 | 39 |
34 private: | 40 private: |
35 const std::string name_; | 41 const std::string name_; |
36 scoped_ptr<base::DictionaryValue> args_; | 42 scoped_ptr<base::DictionaryValue> args_; |
37 }; | 43 }; |
38 | 44 |
39 // Returns a *ViewGuest if this GuestView is of the given view type. | 45 static Type GetViewTypeFromString(const std::string& api_type); |
40 template <typename T> | |
41 T* As() { | |
42 if (GetViewType() == T::Type) { | |
43 return static_cast<T*>(this); | |
44 } | |
45 return NULL; | |
46 } | |
47 | 46 |
48 static GuestViewBase* Create(content::WebContents* guest_web_contents, | 47 static GuestView* Create(content::WebContents* guest_web_contents, |
49 const std::string& embedder_extension_id, | 48 const std::string& embedder_extension_id, |
50 const std::string& view_type); | 49 Type view_type); |
51 | 50 |
52 static GuestViewBase* FromWebContents(content::WebContents* web_contents); | 51 static GuestView* FromWebContents(content::WebContents* web_contents); |
53 | 52 |
54 static GuestViewBase* From(int embedder_process_id, int instance_id); | 53 static GuestView* From(int embedder_process_id, int instance_id); |
55 | 54 |
56 // For GuestViewBases, we create special guest processes, which host the | 55 // For GuestViews, we create special guest processes, which host the |
57 // tag content separately from the main application that embeds the tag. | 56 // tag content separately from the main application that embeds the tag. |
58 // A GuestViewBase can specify both the partition name and whether the storage | 57 // A GuestView can specify both the partition name and whether the storage |
59 // for that partition should be persisted. Each tag gets a SiteInstance with | 58 // for that partition should be persisted. Each tag gets a SiteInstance with |
60 // a specially formatted URL, based on the application it is hosted by and | 59 // a specially formatted URL, based on the application it is hosted by and |
61 // the partition requested by it. The format for that URL is: | 60 // the partition requested by it. The format for that URL is: |
62 // chrome-guest://partition_domain/persist?partition_name | 61 // chrome-guest://partition_domain/persist?partition_name |
63 static bool GetGuestPartitionConfigForSite(const GURL& site, | 62 static bool GetGuestPartitionConfigForSite(const GURL& site, |
64 std::string* partition_domain, | 63 std::string* partition_domain, |
65 std::string* partition_name, | 64 std::string* partition_name, |
66 bool* in_memory); | 65 bool* in_memory); |
67 | 66 |
68 // By default, JavaScript and images are enabled in guest content. | 67 // By default, JavaScript and images are enabled in guest content. |
69 static void GetDefaultContentSettingRules(RendererContentSettingRules* rules, | 68 static void GetDefaultContentSettingRules( |
70 bool incognito); | 69 RendererContentSettingRules* rules, bool incognito); |
71 | |
72 virtual const std::string& GetViewType() const = 0; | |
73 | 70 |
74 virtual void Attach(content::WebContents* embedder_web_contents, | 71 virtual void Attach(content::WebContents* embedder_web_contents, |
75 const base::DictionaryValue& args); | 72 const base::DictionaryValue& args); |
76 | 73 |
77 content::WebContents* embedder_web_contents() const { | 74 content::WebContents* embedder_web_contents() const { |
78 return embedder_web_contents_; | 75 return embedder_web_contents_; |
79 } | 76 } |
80 | 77 |
81 // Returns the guest WebContents. | 78 // Returns the guest WebContents. |
82 content::WebContents* guest_web_contents() const { | 79 content::WebContents* guest_web_contents() const { |
83 return guest_web_contents_; | 80 return guest_web_contents_; |
84 } | 81 } |
85 | 82 |
| 83 virtual Type GetViewType() const; |
| 84 |
| 85 // Returns a WebViewGuest if this GuestView belongs to a <webview>. |
| 86 virtual WebViewGuest* AsWebView() = 0; |
| 87 |
| 88 // Returns an AdViewGuest if the GuestView belongs to an <adview>. |
| 89 virtual AdViewGuest* AsAdView() = 0; |
| 90 |
86 // Returns whether this guest has an associated embedder. | 91 // Returns whether this guest has an associated embedder. |
87 bool attached() const { return !!embedder_web_contents_; } | 92 bool attached() const { return !!embedder_web_contents_; } |
88 | 93 |
89 // Returns the instance ID of the <*view> element. | 94 // Returns the instance ID of the <*view> element. |
90 int view_instance_id() const { return view_instance_id_; } | 95 int view_instance_id() const { return view_instance_id_; } |
91 | 96 |
92 // Returns the instance ID of the guest WebContents. | 97 // Returns the instance ID of the guest WebContents. |
93 int guest_instance_id() const { return guest_instance_id_; } | 98 int guest_instance_id() const { return guest_instance_id_; } |
94 | 99 |
95 // Returns the extension ID of the embedder. | 100 // Returns the extension ID of the embedder. |
96 const std::string& embedder_extension_id() const { | 101 const std::string& embedder_extension_id() const { |
97 return embedder_extension_id_; | 102 return embedder_extension_id_; |
98 } | 103 } |
99 | 104 |
100 // Returns whether this GuestView is embedded in an extension/app. | 105 // Returns whether this GuestView is embedded in an extension/app. |
101 bool in_extension() const { return !embedder_extension_id_.empty(); } | 106 bool in_extension() const { |
| 107 return !embedder_extension_id_.empty(); |
| 108 } |
102 | 109 |
103 // Returns the user browser context of the embedder. | 110 // Returns the user browser context of the embedder. |
104 content::BrowserContext* browser_context() const { return browser_context_; } | 111 content::BrowserContext* browser_context() const { return browser_context_; } |
105 | 112 |
106 // Returns the embedder's process ID. | 113 // Returns the embedder's process ID. |
107 int embedder_render_process_id() const { return embedder_render_process_id_; } | 114 int embedder_render_process_id() const { return embedder_render_process_id_; } |
108 | 115 |
109 protected: | 116 protected: |
110 GuestViewBase(content::WebContents* guest_web_contents, | 117 GuestView(content::WebContents* guest_web_contents, |
111 const std::string& embedder_extension_id); | 118 const std::string& embedder_extension_id); |
112 virtual ~GuestViewBase(); | 119 virtual ~GuestView(); |
113 | 120 |
114 // Dispatches an event |event_name| to the embedder with the |event| fields. | 121 // Dispatches an event |event_name| to the embedder with the |event| fields. |
115 void DispatchEvent(Event* event); | 122 void DispatchEvent(Event* event); |
116 | 123 |
117 private: | 124 private: |
118 void SendQueuedEvents(); | 125 void SendQueuedEvents(); |
119 | 126 |
120 content::WebContents* const guest_web_contents_; | 127 content::WebContents* const guest_web_contents_; |
121 content::WebContents* embedder_web_contents_; | 128 content::WebContents* embedder_web_contents_; |
122 const std::string embedder_extension_id_; | 129 const std::string embedder_extension_id_; |
123 int embedder_render_process_id_; | 130 int embedder_render_process_id_; |
124 content::BrowserContext* const browser_context_; | 131 content::BrowserContext* const browser_context_; |
125 // |guest_instance_id_| is a profile-wide unique identifier for a guest | 132 // |guest_instance_id_| is a profile-wide unique identifier for a guest |
126 // WebContents. | 133 // WebContents. |
127 const int guest_instance_id_; | 134 const int guest_instance_id_; |
128 // |view_instance_id_| is an identifier that's unique within a particular | 135 // |view_instance_id_| is an identifier that's unique within a particular |
129 // embedder RenderViewHost for a particular <*view> instance. | 136 // embedder RenderViewHost for a particular <*view> instance. |
130 int view_instance_id_; | 137 int view_instance_id_; |
131 | 138 |
132 // This is a queue of Events that are destined to be sent to the embedder once | 139 // This is a queue of Events that are destined to be sent to the embedder once |
133 // the guest is attached to a particular embedder. | 140 // the guest is attached to a particular embedder. |
134 std::deque<linked_ptr<Event> > pending_events_; | 141 std::deque<linked_ptr<Event> > pending_events_; |
135 | 142 |
136 // This is used to ensure pending tasks will not fire after this object is | 143 // This is used to ensure pending tasks will not fire after this object is |
137 // destroyed. | 144 // destroyed. |
138 base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_; | 145 base::WeakPtrFactory<GuestView> weak_ptr_factory_; |
139 | 146 |
140 DISALLOW_COPY_AND_ASSIGN(GuestViewBase); | 147 DISALLOW_COPY_AND_ASSIGN(GuestView); |
141 }; | 148 }; |
142 | 149 |
143 #endif // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ | 150 #endif // CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_ |
OLD | NEW |