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_manager.h" | 5 #include "chrome/browser/guest_view/guest_view_manager.h" |
6 | 6 |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/guest_view/guest_view_base.h" | 9 #include "chrome/browser/guest_view/guest_view_base.h" |
10 #include "chrome/browser/guest_view/guest_view_constants.h" | 10 #include "chrome/browser/guest_view/guest_view_constants.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 | 53 |
54 virtual void WebContentsDestroyed() OVERRIDE { | 54 virtual void WebContentsDestroyed() OVERRIDE { |
55 delete this; | 55 delete this; |
56 } | 56 } |
57 | 57 |
58 private: | 58 private: |
59 DISALLOW_COPY_AND_ASSIGN(GuestWebContentsObserver); | 59 DISALLOW_COPY_AND_ASSIGN(GuestWebContentsObserver); |
60 }; | 60 }; |
61 | 61 |
62 GuestViewManager::GuestViewManager(content::BrowserContext* context) | 62 GuestViewManager::GuestViewManager(content::BrowserContext* context) |
63 : current_instance_id_(0), | 63 : current_instance_id_(0), last_instance_id_removed_(0), context_(context) { |
64 context_(context) {} | 64 } |
65 | 65 |
66 GuestViewManager::~GuestViewManager() {} | 66 GuestViewManager::~GuestViewManager() {} |
67 | 67 |
68 // static. | 68 // static. |
69 GuestViewManager* GuestViewManager::FromBrowserContext( | 69 GuestViewManager* GuestViewManager::FromBrowserContext( |
70 BrowserContext* context) { | 70 BrowserContext* context) { |
71 GuestViewManager* guest_manager = | 71 GuestViewManager* guest_manager = |
72 static_cast<GuestViewManager*>(context->GetUserData( | 72 static_cast<GuestViewManager*>(context->GetUserData( |
73 guestview::kGuestViewManagerKeyName)); | 73 guestview::kGuestViewManagerKeyName)); |
74 if (!guest_manager) { | 74 if (!guest_manager) { |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 continue; | 183 continue; |
184 | 184 |
185 if (callback.Run(guest)) | 185 if (callback.Run(guest)) |
186 return true; | 186 return true; |
187 } | 187 } |
188 return false; | 188 return false; |
189 } | 189 } |
190 | 190 |
191 void GuestViewManager::AddGuest(int guest_instance_id, | 191 void GuestViewManager::AddGuest(int guest_instance_id, |
192 WebContents* guest_web_contents) { | 192 WebContents* guest_web_contents) { |
193 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) == | 193 CHECK(!ContainsKey(guest_web_contents_by_instance_id_, guest_instance_id)); |
194 guest_web_contents_by_instance_id_.end()); | 194 CHECK(CanUseGuestInstanceID(guest_instance_id)); |
195 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; | 195 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; |
196 // This will add the RenderProcessHost ID when we get one. | 196 // This will add the RenderProcessHost ID when we get one. |
197 new GuestWebContentsObserver(guest_web_contents); | 197 new GuestWebContentsObserver(guest_web_contents); |
198 } | 198 } |
199 | 199 |
200 void GuestViewManager::RemoveGuest(int guest_instance_id) { | 200 void GuestViewManager::RemoveGuest(int guest_instance_id) { |
201 GuestInstanceMap::iterator it = | 201 GuestInstanceMap::iterator it = |
202 guest_web_contents_by_instance_id_.find(guest_instance_id); | 202 guest_web_contents_by_instance_id_.find(guest_instance_id); |
203 DCHECK(it != guest_web_contents_by_instance_id_.end()); | 203 DCHECK(it != guest_web_contents_by_instance_id_.end()); |
204 render_process_host_id_multiset_.erase( | 204 render_process_host_id_multiset_.erase( |
205 it->second->GetRenderProcessHost()->GetID()); | 205 it->second->GetRenderProcessHost()->GetID()); |
206 guest_web_contents_by_instance_id_.erase(it); | 206 guest_web_contents_by_instance_id_.erase(it); |
| 207 |
| 208 // All the instance IDs that lie within [0, last_instance_id_removed_] |
| 209 // are invalid. |
| 210 // The remaining sparse invalid IDs are kept in |removed_instance_ids_| set. |
| 211 // The following code compacts the set by incrementing |
| 212 // |last_instance_id_removed_|. |
| 213 if (guest_instance_id == last_instance_id_removed_ + 1) { |
| 214 ++last_instance_id_removed_; |
| 215 // Compact. |
| 216 std::set<int>::iterator iter = removed_instance_ids_.begin(); |
| 217 while (iter != removed_instance_ids_.end()) { |
| 218 int instance_id = *iter; |
| 219 // The sparse invalid IDs must not lie within |
| 220 // [0, last_instance_id_removed_] |
| 221 DCHECK(instance_id > last_instance_id_removed_); |
| 222 if (instance_id != last_instance_id_removed_ + 1) |
| 223 break; |
| 224 ++last_instance_id_removed_; |
| 225 removed_instance_ids_.erase(iter++); |
| 226 } |
| 227 } else { |
| 228 removed_instance_ids_.insert(guest_instance_id); |
| 229 } |
207 } | 230 } |
208 | 231 |
209 void GuestViewManager::AddRenderProcessHostID(int render_process_host_id) { | 232 void GuestViewManager::AddRenderProcessHostID(int render_process_host_id) { |
210 render_process_host_id_multiset_.insert(render_process_host_id); | 233 render_process_host_id_multiset_.insert(render_process_host_id); |
211 } | 234 } |
212 | 235 |
213 content::WebContents* GuestViewManager::GetGuestByInstanceID( | 236 content::WebContents* GuestViewManager::GetGuestByInstanceID( |
214 int guest_instance_id, | 237 int guest_instance_id, |
215 int embedder_render_process_id) { | 238 int embedder_render_process_id) { |
216 GuestInstanceMap::const_iterator it = | 239 GuestInstanceMap::const_iterator it = |
(...skipping 13 matching lines...) Expand all Loading... |
230 base::UserMetricsAction("BadMessageTerminate_BPGM")); | 253 base::UserMetricsAction("BadMessageTerminate_BPGM")); |
231 base::KillProcess( | 254 base::KillProcess( |
232 content::RenderProcessHost::FromID(embedder_render_process_id)-> | 255 content::RenderProcessHost::FromID(embedder_render_process_id)-> |
233 GetHandle(), | 256 GetHandle(), |
234 content::RESULT_CODE_KILLED_BAD_MESSAGE, false); | 257 content::RESULT_CODE_KILLED_BAD_MESSAGE, false); |
235 return false; | 258 return false; |
236 } | 259 } |
237 return true; | 260 return true; |
238 } | 261 } |
239 | 262 |
| 263 bool GuestViewManager::CanUseGuestInstanceID(int guest_instance_id) { |
| 264 if (guest_instance_id <= last_instance_id_removed_) |
| 265 return false; |
| 266 return !ContainsKey(removed_instance_ids_, guest_instance_id); |
| 267 } |
| 268 |
240 bool GuestViewManager::CanEmbedderAccessInstanceID( | 269 bool GuestViewManager::CanEmbedderAccessInstanceID( |
241 int embedder_render_process_id, | 270 int embedder_render_process_id, |
242 int guest_instance_id) { | 271 int guest_instance_id) { |
243 // The embedder is trying to access a guest with a negative or zero | 272 // The embedder is trying to access a guest with a negative or zero |
244 // instance ID. | 273 // instance ID. |
245 if (guest_instance_id <= guestview::kInstanceIDNone) | 274 if (guest_instance_id <= guestview::kInstanceIDNone) |
246 return false; | 275 return false; |
247 | 276 |
248 // The embedder is trying to access an instance ID that has not yet been | 277 // The embedder is trying to access an instance ID that has not yet been |
249 // allocated by GuestViewManager. This could cause instance ID | 278 // allocated by GuestViewManager. This could cause instance ID |
(...skipping 23 matching lines...) Expand all Loading... |
273 return false; | 302 return false; |
274 | 303 |
275 return embedder_render_process_id == | 304 return embedder_render_process_id == |
276 guest->GetOpener()->embedder_web_contents()->GetRenderProcessHost()-> | 305 guest->GetOpener()->embedder_web_contents()->GetRenderProcessHost()-> |
277 GetID(); | 306 GetID(); |
278 } | 307 } |
279 | 308 |
280 return embedder_render_process_id == | 309 return embedder_render_process_id == |
281 guest->embedder_web_contents()->GetRenderProcessHost()->GetID(); | 310 guest->embedder_web_contents()->GetRenderProcessHost()->GetID(); |
282 } | 311 } |
OLD | NEW |