OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/guestview/guestview.h" | 5 #include "chrome/browser/guest_view/guest_view_base.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "chrome/browser/guestview/adview/adview_guest.h" | 8 #include "chrome/browser/guest_view/ad_view/ad_view_guest.h" |
9 #include "chrome/browser/guestview/guestview_constants.h" | 9 #include "chrome/browser/guest_view/guest_view_constants.h" |
10 #include "chrome/browser/guestview/webview/webview_guest.h" | 10 #include "chrome/browser/guest_view/web_view/web_view_guest.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/common/content_settings.h" | 12 #include "chrome/common/content_settings.h" |
13 #include "content/public/browser/render_process_host.h" | 13 #include "content/public/browser/render_process_host.h" |
14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
15 #include "content/public/common/url_constants.h" | 15 #include "content/public/common/url_constants.h" |
16 #include "extensions/browser/event_router.h" | 16 #include "extensions/browser/event_router.h" |
17 #include "net/base/escape.h" | 17 #include "net/base/escape.h" |
18 | 18 |
19 using content::WebContents; | 19 using content::WebContents; |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 // <embedder_process_id, guest_instance_id> => GuestView* | 23 // <embedder_process_id, guest_instance_id> => GuestViewBase* |
24 typedef std::map<std::pair<int, int>, GuestView*> EmbedderGuestViewMap; | 24 typedef std::map<std::pair<int, int>, GuestViewBase*> EmbedderGuestViewMap; |
25 static base::LazyInstance<EmbedderGuestViewMap> embedder_guestview_map = | 25 static base::LazyInstance<EmbedderGuestViewMap> embedder_guestview_map = |
26 LAZY_INSTANCE_INITIALIZER; | 26 LAZY_INSTANCE_INITIALIZER; |
27 | 27 |
28 typedef std::map<WebContents*, GuestView*> WebContentsGuestViewMap; | 28 typedef std::map<WebContents*, GuestViewBase*> WebContentsGuestViewMap; |
29 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map = | 29 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map = |
30 LAZY_INSTANCE_INITIALIZER; | 30 LAZY_INSTANCE_INITIALIZER; |
31 | 31 |
32 } // namespace | 32 } // namespace |
33 | 33 |
34 GuestView::Event::Event(const std::string& name, | 34 GuestViewBase::Event::Event(const std::string& name, |
35 scoped_ptr<base::DictionaryValue> args) | 35 scoped_ptr<base::DictionaryValue> args) |
36 : name_(name), | 36 : name_(name), args_(args.Pass()) { |
37 args_(args.Pass()) { | |
38 } | 37 } |
39 | 38 |
40 GuestView::Event::~Event() { | 39 GuestViewBase::Event::~Event() { |
41 } | 40 } |
42 | 41 |
43 scoped_ptr<base::DictionaryValue> GuestView::Event::GetArguments() { | 42 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() { |
44 return args_.Pass(); | 43 return args_.Pass(); |
45 } | 44 } |
46 | 45 |
47 GuestView::GuestView(WebContents* guest_web_contents, | 46 GuestViewBase::GuestViewBase(WebContents* guest_web_contents, |
48 const std::string& embedder_extension_id) | 47 const std::string& embedder_extension_id) |
49 : guest_web_contents_(guest_web_contents), | 48 : guest_web_contents_(guest_web_contents), |
50 embedder_web_contents_(NULL), | 49 embedder_web_contents_(NULL), |
51 embedder_extension_id_(embedder_extension_id), | 50 embedder_extension_id_(embedder_extension_id), |
52 embedder_render_process_id_(0), | 51 embedder_render_process_id_(0), |
53 browser_context_(guest_web_contents->GetBrowserContext()), | 52 browser_context_(guest_web_contents->GetBrowserContext()), |
54 guest_instance_id_(guest_web_contents->GetEmbeddedInstanceID()), | 53 guest_instance_id_(guest_web_contents->GetEmbeddedInstanceID()), |
55 view_instance_id_(guestview::kInstanceIDNone), | 54 view_instance_id_(guestview::kInstanceIDNone), |
56 weak_ptr_factory_(this) { | 55 weak_ptr_factory_(this) { |
57 webcontents_guestview_map.Get().insert( | 56 webcontents_guestview_map.Get().insert( |
58 std::make_pair(guest_web_contents, this)); | 57 std::make_pair(guest_web_contents, this)); |
59 } | 58 } |
60 | 59 |
61 // static | 60 // static |
62 GuestView::Type GuestView::GetViewTypeFromString(const std::string& api_type) { | 61 GuestViewBase* GuestViewBase::Create(WebContents* guest_web_contents, |
63 if (api_type == "adview") { | 62 const std::string& embedder_extension_id, |
64 return GuestView::ADVIEW; | 63 const std::string& view_type) { |
65 } else if (api_type == "webview") { | 64 if (view_type == "webview") { |
66 return GuestView::WEBVIEW; | 65 return new WebViewGuest(guest_web_contents, embedder_extension_id); |
| 66 } else if (view_type == "adview") { |
| 67 return new AdViewGuest(guest_web_contents, embedder_extension_id); |
67 } | 68 } |
68 return GuestView::UNKNOWN; | 69 NOTREACHED(); |
| 70 return NULL; |
69 } | 71 } |
70 | 72 |
71 // static | 73 // static |
72 GuestView* GuestView::Create(WebContents* guest_web_contents, | 74 GuestViewBase* GuestViewBase::FromWebContents(WebContents* web_contents) { |
73 const std::string& embedder_extension_id, | |
74 GuestView::Type view_type) { | |
75 switch (view_type) { | |
76 case GuestView::WEBVIEW: | |
77 return new WebViewGuest(guest_web_contents, embedder_extension_id); | |
78 case GuestView::ADVIEW: | |
79 return new AdViewGuest(guest_web_contents, embedder_extension_id); | |
80 default: | |
81 NOTREACHED(); | |
82 return NULL; | |
83 } | |
84 } | |
85 | |
86 // static | |
87 GuestView* GuestView::FromWebContents(WebContents* web_contents) { | |
88 WebContentsGuestViewMap* guest_map = webcontents_guestview_map.Pointer(); | 75 WebContentsGuestViewMap* guest_map = webcontents_guestview_map.Pointer(); |
89 WebContentsGuestViewMap::iterator it = guest_map->find(web_contents); | 76 WebContentsGuestViewMap::iterator it = guest_map->find(web_contents); |
90 return it == guest_map->end() ? NULL : it->second; | 77 return it == guest_map->end() ? NULL : it->second; |
91 } | 78 } |
92 | 79 |
93 // static | 80 // static |
94 GuestView* GuestView::From(int embedder_process_id, int guest_instance_id) { | 81 GuestViewBase* GuestViewBase::From(int embedder_process_id, |
| 82 int guest_instance_id) { |
95 EmbedderGuestViewMap* guest_map = embedder_guestview_map.Pointer(); | 83 EmbedderGuestViewMap* guest_map = embedder_guestview_map.Pointer(); |
96 EmbedderGuestViewMap::iterator it = guest_map->find( | 84 EmbedderGuestViewMap::iterator it = |
97 std::make_pair(embedder_process_id, guest_instance_id)); | 85 guest_map->find(std::make_pair(embedder_process_id, guest_instance_id)); |
98 return it == guest_map->end() ? NULL : it->second; | 86 return it == guest_map->end() ? NULL : it->second; |
99 } | 87 } |
100 | 88 |
101 // static | 89 // static |
102 bool GuestView::GetGuestPartitionConfigForSite(const GURL& site, | 90 bool GuestViewBase::GetGuestPartitionConfigForSite( |
103 std::string* partition_domain, | 91 const GURL& site, |
104 std::string* partition_name, | 92 std::string* partition_domain, |
105 bool* in_memory) { | 93 std::string* partition_name, |
| 94 bool* in_memory) { |
106 if (!site.SchemeIs(content::kGuestScheme)) | 95 if (!site.SchemeIs(content::kGuestScheme)) |
107 return false; | 96 return false; |
108 | 97 |
109 // Since guest URLs are only used for packaged apps, there must be an app | 98 // Since guest URLs are only used for packaged apps, there must be an app |
110 // id in the URL. | 99 // id in the URL. |
111 CHECK(site.has_host()); | 100 CHECK(site.has_host()); |
112 *partition_domain = site.host(); | 101 *partition_domain = site.host(); |
113 // Since persistence is optional, the path must either be empty or the | 102 // Since persistence is optional, the path must either be empty or the |
114 // literal string. | 103 // literal string. |
115 *in_memory = (site.path() != "/persist"); | 104 *in_memory = (site.path() != "/persist"); |
116 // The partition name is user supplied value, which we have encoded when the | 105 // The partition name is user supplied value, which we have encoded when the |
117 // URL was created, so it needs to be decoded. | 106 // URL was created, so it needs to be decoded. |
118 *partition_name = net::UnescapeURLComponent(site.query(), | 107 *partition_name = |
119 net::UnescapeRule::NORMAL); | 108 net::UnescapeURLComponent(site.query(), net::UnescapeRule::NORMAL); |
120 return true; | 109 return true; |
121 } | 110 } |
122 | 111 |
123 // static | 112 // static |
124 void GuestView::GetDefaultContentSettingRules( | 113 void GuestViewBase::GetDefaultContentSettingRules( |
125 RendererContentSettingRules* rules, bool incognito) { | 114 RendererContentSettingRules* rules, |
126 rules->image_rules.push_back(ContentSettingPatternSource( | 115 bool incognito) { |
127 ContentSettingsPattern::Wildcard(), | 116 rules->image_rules.push_back( |
128 ContentSettingsPattern::Wildcard(), | 117 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
129 CONTENT_SETTING_ALLOW, | 118 ContentSettingsPattern::Wildcard(), |
130 std::string(), | 119 CONTENT_SETTING_ALLOW, |
131 incognito)); | 120 std::string(), |
| 121 incognito)); |
132 | 122 |
133 rules->script_rules.push_back(ContentSettingPatternSource( | 123 rules->script_rules.push_back( |
134 ContentSettingsPattern::Wildcard(), | 124 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
135 ContentSettingsPattern::Wildcard(), | 125 ContentSettingsPattern::Wildcard(), |
136 CONTENT_SETTING_ALLOW, | 126 CONTENT_SETTING_ALLOW, |
137 std::string(), | 127 std::string(), |
138 incognito)); | 128 incognito)); |
139 } | 129 } |
140 | 130 |
141 void GuestView::Attach(content::WebContents* embedder_web_contents, | 131 void GuestViewBase::Attach(content::WebContents* embedder_web_contents, |
142 const base::DictionaryValue& args) { | 132 const base::DictionaryValue& args) { |
143 embedder_web_contents_ = embedder_web_contents; | 133 embedder_web_contents_ = embedder_web_contents; |
144 embedder_render_process_id_ = | 134 embedder_render_process_id_ = |
145 embedder_web_contents->GetRenderProcessHost()->GetID(); | 135 embedder_web_contents->GetRenderProcessHost()->GetID(); |
146 args.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); | 136 args.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); |
147 | 137 |
148 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_); | 138 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_); |
149 embedder_guestview_map.Get().insert(std::make_pair(key, this)); | 139 embedder_guestview_map.Get().insert(std::make_pair(key, this)); |
150 | 140 |
151 // GuestView::Attach is called prior to initialization (and initial | 141 // GuestViewBase::Attach is called prior to initialization (and initial |
152 // navigation) of the guest in the content layer in order to permit mapping | 142 // navigation) of the guest in the content layer in order to permit mapping |
153 // the necessary associations between the <*view> element and its guest. This | 143 // the necessary associations between the <*view> element and its guest. This |
154 // is needed by the <webview> WebRequest API to allow intercepting resource | 144 // is needed by the <webview> WebRequest API to allow intercepting resource |
155 // requests during navigation. However, queued events should be fired after | 145 // requests during navigation. However, queued events should be fired after |
156 // content layer initialization in order to ensure that load events (such as | 146 // content layer initialization in order to ensure that load events (such as |
157 // 'loadstop') fire in embedder after the contentWindow is available. | 147 // 'loadstop') fire in embedder after the contentWindow is available. |
158 if (!in_extension()) | 148 if (!in_extension()) |
159 return; | 149 return; |
160 | 150 |
161 base::MessageLoop::current()->PostTask( | 151 base::MessageLoop::current()->PostTask( |
162 FROM_HERE, | 152 FROM_HERE, |
163 base::Bind(&GuestView::SendQueuedEvents, | 153 base::Bind(&GuestViewBase::SendQueuedEvents, |
164 weak_ptr_factory_.GetWeakPtr())); | 154 weak_ptr_factory_.GetWeakPtr())); |
165 } | 155 } |
166 | 156 |
167 GuestView::Type GuestView::GetViewType() const { | 157 GuestViewBase::~GuestViewBase() { |
168 return GuestView::UNKNOWN; | |
169 } | |
170 | |
171 WebViewGuest* GuestView::AsWebView() { | |
172 return NULL; | |
173 } | |
174 | |
175 AdViewGuest* GuestView::AsAdView() { | |
176 return NULL; | |
177 } | |
178 | |
179 GuestView::~GuestView() { | |
180 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_); | 158 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_); |
181 embedder_guestview_map.Get().erase(key); | 159 embedder_guestview_map.Get().erase(key); |
182 | 160 |
183 webcontents_guestview_map.Get().erase(guest_web_contents()); | 161 webcontents_guestview_map.Get().erase(guest_web_contents()); |
184 | 162 |
185 pending_events_.clear(); | 163 pending_events_.clear(); |
186 } | 164 } |
187 | 165 |
188 void GuestView::DispatchEvent(Event* event) { | 166 void GuestViewBase::DispatchEvent(Event* event) { |
189 scoped_ptr<Event> event_ptr(event); | 167 scoped_ptr<Event> event_ptr(event); |
190 if (!in_extension()) { | 168 if (!in_extension()) { |
191 NOTREACHED(); | 169 NOTREACHED(); |
192 return; | 170 return; |
193 } | 171 } |
194 | 172 |
195 if (!attached()) { | 173 if (!attached()) { |
196 pending_events_.push_back(linked_ptr<Event>(event_ptr.release())); | 174 pending_events_.push_back(linked_ptr<Event>(event_ptr.release())); |
197 return; | 175 return; |
198 } | 176 } |
199 | 177 |
200 Profile* profile = Profile::FromBrowserContext(browser_context_); | 178 Profile* profile = Profile::FromBrowserContext(browser_context_); |
201 | 179 |
202 extensions::EventFilteringInfo info; | 180 extensions::EventFilteringInfo info; |
203 info.SetURL(GURL()); | 181 info.SetURL(GURL()); |
204 info.SetInstanceID(guest_instance_id_); | 182 info.SetInstanceID(guest_instance_id_); |
205 scoped_ptr<base::ListValue> args(new base::ListValue()); | 183 scoped_ptr<base::ListValue> args(new base::ListValue()); |
206 args->Append(event->GetArguments().release()); | 184 args->Append(event->GetArguments().release()); |
207 | 185 |
208 extensions::EventRouter::DispatchEvent( | 186 extensions::EventRouter::DispatchEvent( |
209 embedder_web_contents_, profile, embedder_extension_id_, | 187 embedder_web_contents_, |
210 event->name(), args.Pass(), | 188 profile, |
211 extensions::EventRouter::USER_GESTURE_UNKNOWN, info); | 189 embedder_extension_id_, |
| 190 event->name(), |
| 191 args.Pass(), |
| 192 extensions::EventRouter::USER_GESTURE_UNKNOWN, |
| 193 info); |
212 } | 194 } |
213 | 195 |
214 void GuestView::SendQueuedEvents() { | 196 void GuestViewBase::SendQueuedEvents() { |
215 if (!attached()) | 197 if (!attached()) |
216 return; | 198 return; |
217 | 199 |
218 while (!pending_events_.empty()) { | 200 while (!pending_events_.empty()) { |
219 linked_ptr<Event> event_ptr = pending_events_.front(); | 201 linked_ptr<Event> event_ptr = pending_events_.front(); |
220 pending_events_.pop_front(); | 202 pending_events_.pop_front(); |
221 DispatchEvent(event_ptr.release()); | 203 DispatchEvent(event_ptr.release()); |
222 } | 204 } |
223 } | 205 } |
OLD | NEW |