OLD | NEW |
1 // Copyright 2014 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/guest_view/guest_view_base.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/guest_view/ad_view/ad_view_guest.h" | 8 #include "chrome/browser/guest_view/ad_view/ad_view_guest.h" |
9 #include "chrome/browser/guest_view/guest_view_constants.h" | 9 #include "chrome/browser/guest_view/guest_view_constants.h" |
| 10 #include "chrome/browser/guest_view/guest_view_manager.h" |
10 #include "chrome/browser/guest_view/web_view/web_view_guest.h" | 11 #include "chrome/browser/guest_view/web_view/web_view_guest.h" |
11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/common/content_settings.h" | 13 #include "chrome/common/content_settings.h" |
13 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
14 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
15 #include "content/public/common/url_constants.h" | 16 #include "content/public/common/url_constants.h" |
16 #include "extensions/browser/event_router.h" | 17 #include "extensions/browser/event_router.h" |
17 #include "net/base/escape.h" | 18 #include "net/base/escape.h" |
18 | 19 |
19 using content::WebContents; | 20 using content::WebContents; |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 // <embedder_process_id, guest_instance_id> => GuestViewBase* | |
24 typedef std::map<std::pair<int, int>, GuestViewBase*> EmbedderGuestViewMap; | |
25 static base::LazyInstance<EmbedderGuestViewMap> embedder_guestview_map = | |
26 LAZY_INSTANCE_INITIALIZER; | |
27 | |
28 typedef std::map<WebContents*, GuestViewBase*> WebContentsGuestViewMap; | 24 typedef std::map<WebContents*, GuestViewBase*> WebContentsGuestViewMap; |
29 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map = | 25 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map = |
30 LAZY_INSTANCE_INITIALIZER; | 26 LAZY_INSTANCE_INITIALIZER; |
31 | 27 |
32 } // namespace | 28 } // namespace |
33 | 29 |
34 GuestViewBase::Event::Event(const std::string& name, | 30 GuestViewBase::Event::Event(const std::string& name, |
35 scoped_ptr<base::DictionaryValue> args) | 31 scoped_ptr<base::DictionaryValue> args) |
36 : name_(name), args_(args.Pass()) { | 32 : name_(name), args_(args.Pass()) { |
37 } | 33 } |
38 | 34 |
39 GuestViewBase::Event::~Event() { | 35 GuestViewBase::Event::~Event() { |
40 } | 36 } |
41 | 37 |
42 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() { | 38 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() { |
43 return args_.Pass(); | 39 return args_.Pass(); |
44 } | 40 } |
45 | 41 |
46 GuestViewBase::GuestViewBase(WebContents* guest_web_contents, | 42 GuestViewBase::GuestViewBase(WebContents* guest_web_contents, |
47 const std::string& embedder_extension_id) | 43 const std::string& embedder_extension_id, |
| 44 const base::WeakPtr<GuestViewBase>& opener) |
48 : guest_web_contents_(guest_web_contents), | 45 : guest_web_contents_(guest_web_contents), |
49 embedder_web_contents_(NULL), | 46 embedder_web_contents_(NULL), |
50 embedder_extension_id_(embedder_extension_id), | 47 embedder_extension_id_(embedder_extension_id), |
51 embedder_render_process_id_(0), | 48 embedder_render_process_id_(0), |
52 browser_context_(guest_web_contents->GetBrowserContext()), | 49 browser_context_(guest_web_contents->GetBrowserContext()), |
53 guest_instance_id_(guest_web_contents->GetEmbeddedInstanceID()), | 50 guest_instance_id_(guest_web_contents->GetEmbeddedInstanceID()), |
54 view_instance_id_(guestview::kInstanceIDNone), | 51 view_instance_id_(guestview::kInstanceIDNone), |
| 52 opener_(opener), |
55 weak_ptr_factory_(this) { | 53 weak_ptr_factory_(this) { |
56 webcontents_guestview_map.Get().insert( | 54 webcontents_guestview_map.Get().insert( |
57 std::make_pair(guest_web_contents, this)); | 55 std::make_pair(guest_web_contents, this)); |
58 } | 56 } |
59 | 57 |
60 // static | 58 // static |
61 GuestViewBase* GuestViewBase::Create(WebContents* guest_web_contents, | 59 GuestViewBase* GuestViewBase::Create( |
62 const std::string& embedder_extension_id, | 60 WebContents* guest_web_contents, |
63 const std::string& view_type) { | 61 const std::string& embedder_extension_id, |
| 62 const std::string& view_type, |
| 63 const base::WeakPtr<GuestViewBase>& opener) { |
64 if (view_type == "webview") { | 64 if (view_type == "webview") { |
65 return new WebViewGuest(guest_web_contents, embedder_extension_id); | 65 return new WebViewGuest(guest_web_contents, embedder_extension_id, opener); |
66 } else if (view_type == "adview") { | 66 } else if (view_type == "adview") { |
67 return new AdViewGuest(guest_web_contents, embedder_extension_id); | 67 return new AdViewGuest(guest_web_contents, embedder_extension_id); |
68 } | 68 } |
69 NOTREACHED(); | 69 NOTREACHED(); |
70 return NULL; | 70 return NULL; |
71 } | 71 } |
72 | 72 |
73 // static | 73 // static |
74 GuestViewBase* GuestViewBase::FromWebContents(WebContents* web_contents) { | 74 GuestViewBase* GuestViewBase::FromWebContents(WebContents* web_contents) { |
75 WebContentsGuestViewMap* guest_map = webcontents_guestview_map.Pointer(); | 75 WebContentsGuestViewMap* guest_map = webcontents_guestview_map.Pointer(); |
76 WebContentsGuestViewMap::iterator it = guest_map->find(web_contents); | 76 WebContentsGuestViewMap::iterator it = guest_map->find(web_contents); |
77 return it == guest_map->end() ? NULL : it->second; | 77 return it == guest_map->end() ? NULL : it->second; |
78 } | 78 } |
79 | 79 |
80 // static | 80 // static |
81 GuestViewBase* GuestViewBase::From(int embedder_process_id, | 81 GuestViewBase* GuestViewBase::From(int embedder_process_id, |
82 int guest_instance_id) { | 82 int guest_instance_id) { |
83 EmbedderGuestViewMap* guest_map = embedder_guestview_map.Pointer(); | 83 content::RenderProcessHost* host = |
84 EmbedderGuestViewMap::iterator it = | 84 content::RenderProcessHost::FromID(embedder_process_id); |
85 guest_map->find(std::make_pair(embedder_process_id, guest_instance_id)); | 85 if (!host) |
86 return it == guest_map->end() ? NULL : it->second; | 86 return NULL; |
| 87 |
| 88 content::WebContents* guest_web_contents = |
| 89 GuestViewManager::FromBrowserContext(host->GetBrowserContext())-> |
| 90 GetGuestByInstanceID(guest_instance_id, embedder_process_id); |
| 91 if (!guest_web_contents) |
| 92 return NULL; |
| 93 |
| 94 return GuestViewBase::FromWebContents(guest_web_contents); |
87 } | 95 } |
88 | 96 |
89 // static | 97 // static |
90 bool GuestViewBase::GetGuestPartitionConfigForSite( | 98 bool GuestViewBase::GetGuestPartitionConfigForSite( |
91 const GURL& site, | 99 const GURL& site, |
92 std::string* partition_domain, | 100 std::string* partition_domain, |
93 std::string* partition_name, | 101 std::string* partition_name, |
94 bool* in_memory) { | 102 bool* in_memory) { |
95 if (!site.SchemeIs(content::kGuestScheme)) | 103 if (!site.SchemeIs(content::kGuestScheme)) |
96 return false; | 104 return false; |
(...skipping 24 matching lines...) Expand all Loading... |
121 incognito)); | 129 incognito)); |
122 | 130 |
123 rules->script_rules.push_back( | 131 rules->script_rules.push_back( |
124 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 132 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
125 ContentSettingsPattern::Wildcard(), | 133 ContentSettingsPattern::Wildcard(), |
126 CONTENT_SETTING_ALLOW, | 134 CONTENT_SETTING_ALLOW, |
127 std::string(), | 135 std::string(), |
128 incognito)); | 136 incognito)); |
129 } | 137 } |
130 | 138 |
| 139 base::WeakPtr<GuestViewBase> GuestViewBase::AsWeakPtr() { |
| 140 return weak_ptr_factory_.GetWeakPtr(); |
| 141 } |
| 142 |
131 void GuestViewBase::Attach(content::WebContents* embedder_web_contents, | 143 void GuestViewBase::Attach(content::WebContents* embedder_web_contents, |
132 const base::DictionaryValue& args) { | 144 const base::DictionaryValue& args) { |
133 embedder_web_contents_ = embedder_web_contents; | 145 embedder_web_contents_ = embedder_web_contents; |
134 embedder_render_process_id_ = | 146 embedder_render_process_id_ = |
135 embedder_web_contents->GetRenderProcessHost()->GetID(); | 147 embedder_web_contents->GetRenderProcessHost()->GetID(); |
136 args.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); | 148 args.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); |
137 | 149 |
138 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_); | 150 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_); |
139 embedder_guestview_map.Get().insert(std::make_pair(key, this)); | |
140 | 151 |
141 // GuestViewBase::Attach is called prior to initialization (and initial | 152 // GuestViewBase::Attach is called prior to initialization (and initial |
142 // navigation) of the guest in the content layer in order to permit mapping | 153 // navigation) of the guest in the content layer in order to permit mapping |
143 // the necessary associations between the <*view> element and its guest. This | 154 // the necessary associations between the <*view> element and its guest. This |
144 // is needed by the <webview> WebRequest API to allow intercepting resource | 155 // is needed by the <webview> WebRequest API to allow intercepting resource |
145 // requests during navigation. However, queued events should be fired after | 156 // requests during navigation. However, queued events should be fired after |
146 // content layer initialization in order to ensure that load events (such as | 157 // content layer initialization in order to ensure that load events (such as |
147 // 'loadstop') fire in embedder after the contentWindow is available. | 158 // 'loadstop') fire in embedder after the contentWindow is available. |
148 if (!in_extension()) | 159 if (!in_extension()) |
149 return; | 160 return; |
150 | 161 |
151 base::MessageLoop::current()->PostTask( | 162 base::MessageLoop::current()->PostTask( |
152 FROM_HERE, | 163 FROM_HERE, |
153 base::Bind(&GuestViewBase::SendQueuedEvents, | 164 base::Bind(&GuestViewBase::SendQueuedEvents, |
154 weak_ptr_factory_.GetWeakPtr())); | 165 weak_ptr_factory_.GetWeakPtr())); |
155 } | 166 } |
156 | 167 |
| 168 WebContents* GuestViewBase::GetOpener() const { |
| 169 if (!opener_) |
| 170 return NULL; |
| 171 return opener_->guest_web_contents(); |
| 172 } |
| 173 |
| 174 void GuestViewBase::SetOpener(WebContents* web_contents) { |
| 175 GuestViewBase* guest = FromWebContents(web_contents); |
| 176 if (guest && guest->IsViewType(GetViewType())) { |
| 177 opener_ = guest->AsWeakPtr(); |
| 178 return; |
| 179 } |
| 180 opener_ = base::WeakPtr<GuestViewBase>(); |
| 181 } |
| 182 |
157 GuestViewBase::~GuestViewBase() { | 183 GuestViewBase::~GuestViewBase() { |
158 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_); | 184 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_); |
159 embedder_guestview_map.Get().erase(key); | |
160 | 185 |
161 webcontents_guestview_map.Get().erase(guest_web_contents()); | 186 webcontents_guestview_map.Get().erase(guest_web_contents()); |
162 | 187 |
163 pending_events_.clear(); | 188 pending_events_.clear(); |
164 } | 189 } |
165 | 190 |
166 void GuestViewBase::DispatchEvent(Event* event) { | 191 void GuestViewBase::DispatchEvent(Event* event) { |
167 scoped_ptr<Event> event_ptr(event); | 192 scoped_ptr<Event> event_ptr(event); |
168 if (!in_extension()) { | 193 if (!in_extension()) { |
169 NOTREACHED(); | 194 NOTREACHED(); |
(...skipping 26 matching lines...) Expand all Loading... |
196 void GuestViewBase::SendQueuedEvents() { | 221 void GuestViewBase::SendQueuedEvents() { |
197 if (!attached()) | 222 if (!attached()) |
198 return; | 223 return; |
199 | 224 |
200 while (!pending_events_.empty()) { | 225 while (!pending_events_.empty()) { |
201 linked_ptr<Event> event_ptr = pending_events_.front(); | 226 linked_ptr<Event> event_ptr = pending_events_.front(); |
202 pending_events_.pop_front(); | 227 pending_events_.pop_front(); |
203 DispatchEvent(event_ptr.release()); | 228 DispatchEvent(event_ptr.release()); |
204 } | 229 } |
205 } | 230 } |
OLD | NEW |