| 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" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
| 13 #include "content/public/browser/render_process_host.h" | 13 #include "content/public/browser/render_process_host.h" |
| 14 #include "content/public/browser/user_metrics.h" | 14 #include "content/public/browser/user_metrics.h" |
| 15 #include "content/public/browser/web_contents_observer.h" | 15 #include "content/public/browser/web_contents_observer.h" |
| 16 #include "content/public/common/result_codes.h" | 16 #include "content/public/common/result_codes.h" |
| 17 #include "content/public/common/url_constants.h" | 17 #include "content/public/common/url_constants.h" |
| 18 #include "extensions/browser/extension_system.h" | 18 #include "extensions/browser/extension_system.h" |
| 19 #include "net/base/escape.h" | 19 #include "net/base/escape.h" |
| 20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 21 | 21 |
| 22 using content::BrowserContext; | 22 using content::BrowserContext; |
| 23 using content::SiteInstance; | 23 using content::SiteInstance; |
| 24 using content::WebContents; | 24 using content::WebContents; |
| 25 | 25 |
| 26 // A WebContents does not immediately have a RenderProcessHost. It acquires one | |
| 27 // on initial navigation. This observer exists until that initial navigation in | |
| 28 // order to grab the ID if tis RenderProcessHost so that it can register it as | |
| 29 // a guest. | |
| 30 class GuestWebContentsObserver | |
| 31 : public content::WebContentsObserver { | |
| 32 public: | |
| 33 explicit GuestWebContentsObserver(WebContents* guest_web_contents) | |
| 34 : WebContentsObserver(guest_web_contents) { | |
| 35 } | |
| 36 | |
| 37 virtual ~GuestWebContentsObserver() { | |
| 38 } | |
| 39 | |
| 40 // WebContentsObserver: | |
| 41 virtual void DidStartProvisionalLoadForFrame( | |
| 42 int64 frame_id, | |
| 43 int64 parent_frame_id, | |
| 44 bool is_main_frame, | |
| 45 const GURL& validated_url, | |
| 46 bool is_error_page, | |
| 47 bool is_iframe_srcdoc, | |
| 48 content::RenderViewHost* render_view_host) OVERRIDE { | |
| 49 GuestViewManager::FromBrowserContext(web_contents()->GetBrowserContext())-> | |
| 50 AddRenderProcessHostID(web_contents()->GetRenderProcessHost()->GetID()); | |
| 51 delete this; | |
| 52 } | |
| 53 | |
| 54 virtual void WebContentsDestroyed() OVERRIDE { | |
| 55 delete this; | |
| 56 } | |
| 57 | |
| 58 private: | |
| 59 DISALLOW_COPY_AND_ASSIGN(GuestWebContentsObserver); | |
| 60 }; | |
| 61 | |
| 62 GuestViewManager::GuestViewManager(content::BrowserContext* context) | 26 GuestViewManager::GuestViewManager(content::BrowserContext* context) |
| 63 : current_instance_id_(0), last_instance_id_removed_(0), context_(context) { | 27 : current_instance_id_(0), last_instance_id_removed_(0), context_(context) { |
| 64 } | 28 } |
| 65 | 29 |
| 66 GuestViewManager::~GuestViewManager() {} | 30 GuestViewManager::~GuestViewManager() {} |
| 67 | 31 |
| 68 // static. | 32 // static. |
| 69 GuestViewManager* GuestViewManager::FromBrowserContext( | 33 GuestViewManager* GuestViewManager::FromBrowserContext( |
| 70 BrowserContext* context) { | 34 BrowserContext* context) { |
| 71 GuestViewManager* guest_manager = | 35 GuestViewManager* guest_manager = |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 return true; | 150 return true; |
| 187 } | 151 } |
| 188 return false; | 152 return false; |
| 189 } | 153 } |
| 190 | 154 |
| 191 void GuestViewManager::AddGuest(int guest_instance_id, | 155 void GuestViewManager::AddGuest(int guest_instance_id, |
| 192 WebContents* guest_web_contents) { | 156 WebContents* guest_web_contents) { |
| 193 CHECK(!ContainsKey(guest_web_contents_by_instance_id_, guest_instance_id)); | 157 CHECK(!ContainsKey(guest_web_contents_by_instance_id_, guest_instance_id)); |
| 194 CHECK(CanUseGuestInstanceID(guest_instance_id)); | 158 CHECK(CanUseGuestInstanceID(guest_instance_id)); |
| 195 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; | 159 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; |
| 196 // This will add the RenderProcessHost ID when we get one. | |
| 197 new GuestWebContentsObserver(guest_web_contents); | |
| 198 } | 160 } |
| 199 | 161 |
| 200 void GuestViewManager::RemoveGuest(int guest_instance_id) { | 162 void GuestViewManager::RemoveGuest(int guest_instance_id) { |
| 201 GuestInstanceMap::iterator it = | 163 GuestInstanceMap::iterator it = |
| 202 guest_web_contents_by_instance_id_.find(guest_instance_id); | 164 guest_web_contents_by_instance_id_.find(guest_instance_id); |
| 203 DCHECK(it != guest_web_contents_by_instance_id_.end()); | 165 DCHECK(it != guest_web_contents_by_instance_id_.end()); |
| 204 render_process_host_id_multiset_.erase( | |
| 205 it->second->GetRenderProcessHost()->GetID()); | |
| 206 guest_web_contents_by_instance_id_.erase(it); | 166 guest_web_contents_by_instance_id_.erase(it); |
| 207 | 167 |
| 208 // All the instance IDs that lie within [0, last_instance_id_removed_] | 168 // All the instance IDs that lie within [0, last_instance_id_removed_] |
| 209 // are invalid. | 169 // are invalid. |
| 210 // The remaining sparse invalid IDs are kept in |removed_instance_ids_| set. | 170 // The remaining sparse invalid IDs are kept in |removed_instance_ids_| set. |
| 211 // The following code compacts the set by incrementing | 171 // The following code compacts the set by incrementing |
| 212 // |last_instance_id_removed_|. | 172 // |last_instance_id_removed_|. |
| 213 if (guest_instance_id == last_instance_id_removed_ + 1) { | 173 if (guest_instance_id == last_instance_id_removed_ + 1) { |
| 214 ++last_instance_id_removed_; | 174 ++last_instance_id_removed_; |
| 215 // Compact. | 175 // Compact. |
| 216 std::set<int>::iterator iter = removed_instance_ids_.begin(); | 176 std::set<int>::iterator iter = removed_instance_ids_.begin(); |
| 217 while (iter != removed_instance_ids_.end()) { | 177 while (iter != removed_instance_ids_.end()) { |
| 218 int instance_id = *iter; | 178 int instance_id = *iter; |
| 219 // The sparse invalid IDs must not lie within | 179 // The sparse invalid IDs must not lie within |
| 220 // [0, last_instance_id_removed_] | 180 // [0, last_instance_id_removed_] |
| 221 DCHECK(instance_id > last_instance_id_removed_); | 181 DCHECK(instance_id > last_instance_id_removed_); |
| 222 if (instance_id != last_instance_id_removed_ + 1) | 182 if (instance_id != last_instance_id_removed_ + 1) |
| 223 break; | 183 break; |
| 224 ++last_instance_id_removed_; | 184 ++last_instance_id_removed_; |
| 225 removed_instance_ids_.erase(iter++); | 185 removed_instance_ids_.erase(iter++); |
| 226 } | 186 } |
| 227 } else { | 187 } else { |
| 228 removed_instance_ids_.insert(guest_instance_id); | 188 removed_instance_ids_.insert(guest_instance_id); |
| 229 } | 189 } |
| 230 } | 190 } |
| 231 | 191 |
| 232 void GuestViewManager::AddRenderProcessHostID(int render_process_host_id) { | |
| 233 render_process_host_id_multiset_.insert(render_process_host_id); | |
| 234 } | |
| 235 | |
| 236 content::WebContents* GuestViewManager::GetGuestByInstanceID( | 192 content::WebContents* GuestViewManager::GetGuestByInstanceID( |
| 237 int guest_instance_id, | 193 int guest_instance_id, |
| 238 int embedder_render_process_id) { | 194 int embedder_render_process_id) { |
| 239 GuestInstanceMap::const_iterator it = | 195 GuestInstanceMap::const_iterator it = |
| 240 guest_web_contents_by_instance_id_.find(guest_instance_id); | 196 guest_web_contents_by_instance_id_.find(guest_instance_id); |
| 241 if (it == guest_web_contents_by_instance_id_.end()) | 197 if (it == guest_web_contents_by_instance_id_.end()) |
| 242 return NULL; | 198 return NULL; |
| 243 return it->second; | 199 return it->second; |
| 244 } | 200 } |
| 245 | 201 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 return false; | 258 return false; |
| 303 | 259 |
| 304 return embedder_render_process_id == | 260 return embedder_render_process_id == |
| 305 guest->GetOpener()->embedder_web_contents()->GetRenderProcessHost()-> | 261 guest->GetOpener()->embedder_web_contents()->GetRenderProcessHost()-> |
| 306 GetID(); | 262 GetID(); |
| 307 } | 263 } |
| 308 | 264 |
| 309 return embedder_render_process_id == | 265 return embedder_render_process_id == |
| 310 guest->embedder_web_contents()->GetRenderProcessHost()->GetID(); | 266 guest->embedder_web_contents()->GetRenderProcessHost()->GetID(); |
| 311 } | 267 } |
| OLD | NEW |