| 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 "extensions/browser/guest_view/guest_view_manager.h" | 5 #include "components/guest_view/browser/guest_view_manager.h" |
| 6 | 6 |
| 7 #include "base/macros.h" |
| 7 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "components/guest_view/browser/guest_view_base.h" |
| 10 #include "components/guest_view/browser/guest_view_manager_delegate.h" |
| 11 #include "components/guest_view/browser/guest_view_manager_factory.h" |
| 12 #include "components/guest_view/common/guest_view_constants.h" |
| 8 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
| 9 #include "content/public/browser/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
| 10 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
| 11 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
| 12 #include "content/public/browser/user_metrics.h" | 17 #include "content/public/browser/user_metrics.h" |
| 13 #include "content/public/browser/web_contents_observer.h" | 18 #include "content/public/browser/web_contents_observer.h" |
| 14 #include "content/public/common/child_process_host.h" | 19 #include "content/public/common/child_process_host.h" |
| 15 #include "content/public/common/result_codes.h" | 20 #include "content/public/common/result_codes.h" |
| 16 #include "content/public/common/url_constants.h" | 21 #include "content/public/common/url_constants.h" |
| 17 #include "extensions/browser/guest_view/guest_view_base.h" | |
| 18 #include "extensions/browser/guest_view/guest_view_manager_delegate.h" | |
| 19 #include "extensions/browser/guest_view/guest_view_manager_factory.h" | |
| 20 #include "extensions/common/guest_view/guest_view_constants.h" | |
| 21 #include "net/base/escape.h" | |
| 22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 23 | 23 |
| 24 using content::BrowserContext; | 24 using content::BrowserContext; |
| 25 using content::SiteInstance; | 25 using content::SiteInstance; |
| 26 using content::WebContents; | 26 using content::WebContents; |
| 27 using guestview::GuestViewManagerDelegate; | |
| 28 | 27 |
| 29 namespace extensions { | 28 namespace guest_view { |
| 30 | 29 |
| 31 // static | 30 // static |
| 32 GuestViewManagerFactory* GuestViewManager::factory_ = nullptr; | 31 GuestViewManagerFactory* GuestViewManager::factory_ = nullptr; |
| 33 | 32 |
| 34 GuestViewManager::GuestViewManager( | 33 GuestViewManager::GuestViewManager( |
| 35 content::BrowserContext* context, | 34 content::BrowserContext* context, |
| 36 scoped_ptr<GuestViewManagerDelegate> delegate) | 35 scoped_ptr<GuestViewManagerDelegate> delegate) |
| 37 : current_instance_id_(0), | 36 : current_instance_id_(0), |
| 38 last_instance_id_removed_(0), | 37 last_instance_id_removed_(0), |
| 39 context_(context), | 38 context_(context), |
| 40 delegate_(delegate.Pass()) { | 39 delegate_(delegate.Pass()) { |
| 41 } | 40 } |
| 42 | 41 |
| 43 GuestViewManager::~GuestViewManager() {} | 42 GuestViewManager::~GuestViewManager() {} |
| 44 | 43 |
| 45 // static | 44 // static |
| 46 GuestViewManager* GuestViewManager::CreateWithDelegate( | 45 GuestViewManager* GuestViewManager::CreateWithDelegate( |
| 47 BrowserContext* context, | 46 BrowserContext* context, |
| 48 scoped_ptr<GuestViewManagerDelegate> delegate) { | 47 scoped_ptr<GuestViewManagerDelegate> delegate) { |
| 49 GuestViewManager* guest_manager = FromBrowserContext(context); | 48 GuestViewManager* guest_manager = FromBrowserContext(context); |
| 50 if (!guest_manager) { | 49 if (!guest_manager) { |
| 51 if (factory_) { | 50 if (factory_) { |
| 52 guest_manager = | 51 guest_manager = |
| 53 factory_->CreateGuestViewManager(context, delegate.Pass()); | 52 factory_->CreateGuestViewManager(context, delegate.Pass()); |
| 54 } else { | 53 } else { |
| 55 guest_manager = new GuestViewManager(context, delegate.Pass()); | 54 guest_manager = new GuestViewManager(context, delegate.Pass()); |
| 56 } | 55 } |
| 57 context->SetUserData(guestview::kGuestViewManagerKeyName, guest_manager); | 56 context->SetUserData(kGuestViewManagerKeyName, guest_manager); |
| 58 } | 57 } |
| 59 return guest_manager; | 58 return guest_manager; |
| 60 } | 59 } |
| 61 | 60 |
| 62 // static | 61 // static |
| 63 GuestViewManager* GuestViewManager::FromBrowserContext( | 62 GuestViewManager* GuestViewManager::FromBrowserContext( |
| 64 BrowserContext* context) { | 63 BrowserContext* context) { |
| 65 return static_cast<GuestViewManager*>(context->GetUserData( | 64 return static_cast<GuestViewManager*>(context->GetUserData( |
| 66 guestview::kGuestViewManagerKeyName)); | 65 kGuestViewManagerKeyName)); |
| 67 } | 66 } |
| 68 | 67 |
| 69 content::WebContents* GuestViewManager::GetGuestByInstanceIDSafely( | 68 content::WebContents* GuestViewManager::GetGuestByInstanceIDSafely( |
| 70 int guest_instance_id, | 69 int guest_instance_id, |
| 71 int embedder_render_process_id) { | 70 int embedder_render_process_id) { |
| 72 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id, | 71 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id, |
| 73 guest_instance_id)) { | 72 guest_instance_id)) { |
| 74 return nullptr; | 73 return nullptr; |
| 75 } | 74 } |
| 76 return GetGuestByInstanceID(guest_instance_id); | 75 return GetGuestByInstanceID(guest_instance_id); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 auto guest_web_contents = WebContents::Create(guest_create_params); | 150 auto guest_web_contents = WebContents::Create(guest_create_params); |
| 152 guest->InitWithWebContents(base::DictionaryValue(), guest_web_contents); | 151 guest->InitWithWebContents(base::DictionaryValue(), guest_web_contents); |
| 153 return guest_web_contents; | 152 return guest_web_contents; |
| 154 } | 153 } |
| 155 | 154 |
| 156 content::WebContents* GuestViewManager::GetGuestByInstanceID( | 155 content::WebContents* GuestViewManager::GetGuestByInstanceID( |
| 157 int owner_process_id, | 156 int owner_process_id, |
| 158 int element_instance_id) { | 157 int element_instance_id) { |
| 159 int guest_instance_id = GetGuestInstanceIDForElementID(owner_process_id, | 158 int guest_instance_id = GetGuestInstanceIDForElementID(owner_process_id, |
| 160 element_instance_id); | 159 element_instance_id); |
| 161 if (guest_instance_id == guestview::kInstanceIDNone) | 160 if (guest_instance_id == kInstanceIDNone) |
| 162 return nullptr; | 161 return nullptr; |
| 163 | 162 |
| 164 return GetGuestByInstanceID(guest_instance_id); | 163 return GetGuestByInstanceID(guest_instance_id); |
| 165 } | 164 } |
| 166 | 165 |
| 167 int GuestViewManager::GetGuestInstanceIDForElementID(int owner_process_id, | 166 int GuestViewManager::GetGuestInstanceIDForElementID(int owner_process_id, |
| 168 int element_instance_id) { | 167 int element_instance_id) { |
| 169 auto iter = instance_id_map_.find( | 168 auto iter = instance_id_map_.find( |
| 170 ElementInstanceKey(owner_process_id, element_instance_id)); | 169 ElementInstanceKey(owner_process_id, element_instance_id)); |
| 171 if (iter == instance_id_map_.end()) | 170 if (iter == instance_id_map_.end()) |
| 172 return guestview::kInstanceIDNone; | 171 return kInstanceIDNone; |
| 173 return iter->second; | 172 return iter->second; |
| 174 } | 173 } |
| 175 | 174 |
| 176 SiteInstance* GuestViewManager::GetGuestSiteInstance( | 175 SiteInstance* GuestViewManager::GetGuestSiteInstance( |
| 177 const GURL& guest_site) { | 176 const GURL& guest_site) { |
| 178 for (const auto& guest : guest_web_contents_by_instance_id_) { | 177 for (const auto& guest : guest_web_contents_by_instance_id_) { |
| 179 if (guest.second->GetSiteInstance()->GetSiteURL() == guest_site) | 178 if (guest.second->GetSiteInstance()->GetSiteURL() == guest_site) |
| 180 return guest.second->GetSiteInstance(); | 179 return guest.second->GetSiteInstance(); |
| 181 } | 180 } |
| 182 return nullptr; | 181 return nullptr; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 } | 265 } |
| 267 | 266 |
| 268 bool GuestViewManager::IsGuestAvailableToContext(GuestViewBase* guest) { | 267 bool GuestViewManager::IsGuestAvailableToContext(GuestViewBase* guest) { |
| 269 return delegate_->IsGuestAvailableToContext(guest); | 268 return delegate_->IsGuestAvailableToContext(guest); |
| 270 } | 269 } |
| 271 | 270 |
| 272 void GuestViewManager::DispatchEvent(const std::string& event_name, | 271 void GuestViewManager::DispatchEvent(const std::string& event_name, |
| 273 scoped_ptr<base::DictionaryValue> args, | 272 scoped_ptr<base::DictionaryValue> args, |
| 274 GuestViewBase* guest, | 273 GuestViewBase* guest, |
| 275 int instance_id) { | 274 int instance_id) { |
| 275 // TODO(fsamuel): GuestViewManager should probably do something more useful |
| 276 // here like log an error if the event could not be dispatched. |
| 276 delegate_->DispatchEvent(event_name, args.Pass(), guest, instance_id); | 277 delegate_->DispatchEvent(event_name, args.Pass(), guest, instance_id); |
| 277 } | 278 } |
| 278 | 279 |
| 279 content::WebContents* GuestViewManager::GetGuestByInstanceID( | 280 content::WebContents* GuestViewManager::GetGuestByInstanceID( |
| 280 int guest_instance_id) { | 281 int guest_instance_id) { |
| 281 auto it = guest_web_contents_by_instance_id_.find(guest_instance_id); | 282 auto it = guest_web_contents_by_instance_id_.find(guest_instance_id); |
| 282 if (it == guest_web_contents_by_instance_id_.end()) | 283 if (it == guest_web_contents_by_instance_id_.end()) |
| 283 return nullptr; | 284 return nullptr; |
| 284 return it->second; | 285 return it->second; |
| 285 } | 286 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 315 return true; | 316 return true; |
| 316 } | 317 } |
| 317 return false; | 318 return false; |
| 318 } | 319 } |
| 319 | 320 |
| 320 bool GuestViewManager::CanEmbedderAccessInstanceID( | 321 bool GuestViewManager::CanEmbedderAccessInstanceID( |
| 321 int embedder_render_process_id, | 322 int embedder_render_process_id, |
| 322 int guest_instance_id) { | 323 int guest_instance_id) { |
| 323 // The embedder is trying to access a guest with a negative or zero | 324 // The embedder is trying to access a guest with a negative or zero |
| 324 // instance ID. | 325 // instance ID. |
| 325 if (guest_instance_id <= guestview::kInstanceIDNone) | 326 if (guest_instance_id <= kInstanceIDNone) |
| 326 return false; | 327 return false; |
| 327 | 328 |
| 328 // The embedder is trying to access an instance ID that has not yet been | 329 // The embedder is trying to access an instance ID that has not yet been |
| 329 // allocated by GuestViewManager. This could cause instance ID | 330 // allocated by GuestViewManager. This could cause instance ID |
| 330 // collisions in the future, and potentially give one embedder access to a | 331 // collisions in the future, and potentially give one embedder access to a |
| 331 // guest it does not own. | 332 // guest it does not own. |
| 332 if (guest_instance_id > current_instance_id_) | 333 if (guest_instance_id > current_instance_id_) |
| 333 return false; | 334 return false; |
| 334 | 335 |
| 335 // We might get some late arriving messages at tear down. Let's let the | 336 // We might get some late arriving messages at tear down. Let's let the |
| (...skipping 29 matching lines...) Expand all Loading... |
| 365 | 366 |
| 366 return element_instance_id < other.element_instance_id; | 367 return element_instance_id < other.element_instance_id; |
| 367 } | 368 } |
| 368 | 369 |
| 369 bool GuestViewManager::ElementInstanceKey::operator==( | 370 bool GuestViewManager::ElementInstanceKey::operator==( |
| 370 const GuestViewManager::ElementInstanceKey& other) const { | 371 const GuestViewManager::ElementInstanceKey& other) const { |
| 371 return (embedder_process_id == other.embedder_process_id) && | 372 return (embedder_process_id == other.embedder_process_id) && |
| 372 (element_instance_id == other.element_instance_id); | 373 (element_instance_id == other.element_instance_id); |
| 373 } | 374 } |
| 374 | 375 |
| 375 } // namespace extensions | 376 } // namespace guest_view |
| OLD | NEW |