| 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 "extensions/browser/guest_view/guest_view_manager.h" |
| 6 | 6 |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "content/public/browser/browser_context.h" | 8 #include "content/public/browser/browser_context.h" |
| 9 #include "content/public/browser/render_process_host.h" | 9 #include "content/public/browser/render_process_host.h" |
| 10 #include "content/public/browser/render_view_host.h" | 10 #include "content/public/browser/render_view_host.h" |
| 11 #include "content/public/browser/user_metrics.h" | 11 #include "content/public/browser/user_metrics.h" |
| 12 #include "content/public/browser/web_contents_observer.h" | 12 #include "content/public/browser/web_contents_observer.h" |
| 13 #include "content/public/common/result_codes.h" | 13 #include "content/public/common/result_codes.h" |
| 14 #include "content/public/common/url_constants.h" | 14 #include "content/public/common/url_constants.h" |
| 15 #include "extensions/browser/extension_system.h" | 15 #include "extensions/browser/extension_system.h" |
| 16 #include "extensions/browser/guest_view/guest_view_base.h" | 16 #include "extensions/browser/guest_view/guest_view_base.h" |
| 17 #include "extensions/browser/guest_view/guest_view_manager_factory.h" | 17 #include "extensions/browser/guest_view/guest_view_manager_factory.h" |
| 18 #include "extensions/common/guest_view/guest_view_constants.h" | 18 #include "extensions/common/guest_view/guest_view_constants.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 namespace extensions { | 26 namespace extensions { |
| 27 | 27 |
| 28 // static | 28 // static |
| 29 GuestViewManagerFactory* GuestViewManager::factory_ = NULL; | 29 GuestViewManagerFactory* GuestViewManager::factory_ = nullptr; |
| 30 | 30 |
| 31 GuestViewManager::GuestViewManager(content::BrowserContext* context) | 31 GuestViewManager::GuestViewManager(content::BrowserContext* context) |
| 32 : current_instance_id_(0), last_instance_id_removed_(0), context_(context) { | 32 : current_instance_id_(0), last_instance_id_removed_(0), context_(context) { |
| 33 } | 33 } |
| 34 | 34 |
| 35 GuestViewManager::~GuestViewManager() {} | 35 GuestViewManager::~GuestViewManager() {} |
| 36 | 36 |
| 37 // static. | 37 // static. |
| 38 GuestViewManager* GuestViewManager::FromBrowserContext( | 38 GuestViewManager* GuestViewManager::FromBrowserContext( |
| 39 BrowserContext* context) { | 39 BrowserContext* context) { |
| 40 GuestViewManager* guest_manager = | 40 GuestViewManager* guest_manager = |
| 41 static_cast<GuestViewManager*>(context->GetUserData( | 41 static_cast<GuestViewManager*>(context->GetUserData( |
| 42 guestview::kGuestViewManagerKeyName)); | 42 guestview::kGuestViewManagerKeyName)); |
| 43 if (!guest_manager) { | 43 if (!guest_manager) { |
| 44 if (factory_) { | 44 if (factory_) { |
| 45 guest_manager = factory_->CreateGuestViewManager(context); | 45 guest_manager = factory_->CreateGuestViewManager(context); |
| 46 } else { | 46 } else { |
| 47 guest_manager = new GuestViewManager(context); | 47 guest_manager = new GuestViewManager(context); |
| 48 } | 48 } |
| 49 context->SetUserData(guestview::kGuestViewManagerKeyName, guest_manager); | 49 context->SetUserData(guestview::kGuestViewManagerKeyName, guest_manager); |
| 50 } | 50 } |
| 51 return guest_manager; | 51 return guest_manager; |
| 52 } | 52 } |
| 53 | 53 |
| 54 content::WebContents* GuestViewManager::GetGuestByInstanceIDSafely( | 54 content::WebContents* GuestViewManager::GetGuestByInstanceIDSafely( |
| 55 int guest_instance_id, | 55 int guest_instance_id, |
| 56 int embedder_render_process_id) { | 56 int embedder_render_process_id) { |
| 57 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id, | 57 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id, |
| 58 guest_instance_id)) { | 58 guest_instance_id)) { |
| 59 return NULL; | 59 return nullptr; |
| 60 } | 60 } |
| 61 return GetGuestByInstanceID(guest_instance_id); | 61 return GetGuestByInstanceID(guest_instance_id); |
| 62 } | 62 } |
| 63 | 63 |
| 64 void GuestViewManager::AttachGuest( | 64 void GuestViewManager::AttachGuest( |
| 65 int embedder_render_process_id, | 65 int embedder_render_process_id, |
| 66 int embedder_routing_id, | 66 int embedder_routing_id, |
| 67 int element_instance_id, | 67 int element_instance_id, |
| 68 int guest_instance_id, | 68 int guest_instance_id, |
| 69 const base::DictionaryValue& attach_params) { | 69 const base::DictionaryValue& attach_params) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 | 114 |
| 115 void GuestViewManager::CreateGuest(const std::string& view_type, | 115 void GuestViewManager::CreateGuest(const std::string& view_type, |
| 116 const std::string& embedder_extension_id, | 116 const std::string& embedder_extension_id, |
| 117 content::WebContents* embedder_web_contents, | 117 content::WebContents* embedder_web_contents, |
| 118 const base::DictionaryValue& create_params, | 118 const base::DictionaryValue& create_params, |
| 119 const WebContentsCreatedCallback& callback) { | 119 const WebContentsCreatedCallback& callback) { |
| 120 int guest_instance_id = GetNextInstanceID(); | 120 int guest_instance_id = GetNextInstanceID(); |
| 121 GuestViewBase* guest = | 121 GuestViewBase* guest = |
| 122 GuestViewBase::Create(context_, guest_instance_id, view_type); | 122 GuestViewBase::Create(context_, guest_instance_id, view_type); |
| 123 if (!guest) { | 123 if (!guest) { |
| 124 callback.Run(NULL); | 124 callback.Run(nullptr); |
| 125 return; | 125 return; |
| 126 } | 126 } |
| 127 guest->Init( | 127 guest->Init( |
| 128 embedder_extension_id, embedder_web_contents, create_params, callback); | 128 embedder_extension_id, embedder_web_contents, create_params, callback); |
| 129 } | 129 } |
| 130 | 130 |
| 131 content::WebContents* GuestViewManager::CreateGuestWithWebContentsParams( | 131 content::WebContents* GuestViewManager::CreateGuestWithWebContentsParams( |
| 132 const std::string& view_type, | 132 const std::string& view_type, |
| 133 const std::string& embedder_extension_id, | 133 const std::string& embedder_extension_id, |
| 134 int embedder_render_process_id, | 134 int embedder_render_process_id, |
| 135 const content::WebContents::CreateParams& create_params) { | 135 const content::WebContents::CreateParams& create_params) { |
| 136 int guest_instance_id = GetNextInstanceID(); | 136 int guest_instance_id = GetNextInstanceID(); |
| 137 GuestViewBase* guest = | 137 GuestViewBase* guest = |
| 138 GuestViewBase::Create(context_, guest_instance_id, view_type); | 138 GuestViewBase::Create(context_, guest_instance_id, view_type); |
| 139 if (!guest) | 139 if (!guest) |
| 140 return NULL; | 140 return nullptr; |
| 141 content::WebContents::CreateParams guest_create_params(create_params); | 141 content::WebContents::CreateParams guest_create_params(create_params); |
| 142 guest_create_params.guest_delegate = guest; | 142 guest_create_params.guest_delegate = guest; |
| 143 content::WebContents* guest_web_contents = | 143 content::WebContents* guest_web_contents = |
| 144 WebContents::Create(guest_create_params); | 144 WebContents::Create(guest_create_params); |
| 145 guest->InitWithWebContents(embedder_extension_id, | 145 guest->InitWithWebContents(embedder_extension_id, |
| 146 embedder_render_process_id, | 146 embedder_render_process_id, |
| 147 guest_web_contents); | 147 guest_web_contents); |
| 148 return guest_web_contents; | 148 return guest_web_contents; |
| 149 } | 149 } |
| 150 | 150 |
| 151 content::WebContents* GuestViewManager::GetGuestByInstanceID( | 151 content::WebContents* GuestViewManager::GetGuestByInstanceID( |
| 152 content::WebContents* embedder_web_contents, | 152 content::WebContents* embedder_web_contents, |
| 153 int element_instance_id) { | 153 int element_instance_id) { |
| 154 int guest_instance_id = GetGuestInstanceIDForElementID(embedder_web_contents, | 154 int guest_instance_id = GetGuestInstanceIDForElementID(embedder_web_contents, |
| 155 element_instance_id); | 155 element_instance_id); |
| 156 if (guest_instance_id == guestview::kInstanceIDNone) | 156 if (guest_instance_id == guestview::kInstanceIDNone) |
| 157 return NULL; | 157 return nullptr; |
| 158 | 158 |
| 159 return GetGuestByInstanceID(guest_instance_id); | 159 return GetGuestByInstanceID(guest_instance_id); |
| 160 } | 160 } |
| 161 | 161 |
| 162 int GuestViewManager::GetGuestInstanceIDForElementID( | 162 int GuestViewManager::GetGuestInstanceIDForElementID( |
| 163 content::WebContents* embedder_web_contents, | 163 content::WebContents* embedder_web_contents, |
| 164 int element_instance_id) { | 164 int element_instance_id) { |
| 165 GuestInstanceIDMap::iterator iter = instance_id_map_.find( | 165 GuestInstanceIDMap::iterator iter = instance_id_map_.find( |
| 166 ElementInstanceKey(embedder_web_contents, element_instance_id)); | 166 ElementInstanceKey(embedder_web_contents, element_instance_id)); |
| 167 if (iter == instance_id_map_.end()) | 167 if (iter == instance_id_map_.end()) |
| 168 return guestview::kInstanceIDNone; | 168 return guestview::kInstanceIDNone; |
| 169 return iter->second; | 169 return iter->second; |
| 170 } | 170 } |
| 171 | 171 |
| 172 SiteInstance* GuestViewManager::GetGuestSiteInstance( | 172 SiteInstance* GuestViewManager::GetGuestSiteInstance( |
| 173 const GURL& guest_site) { | 173 const GURL& guest_site) { |
| 174 for (GuestInstanceMap::const_iterator it = | 174 for (GuestInstanceMap::const_iterator it = |
| 175 guest_web_contents_by_instance_id_.begin(); | 175 guest_web_contents_by_instance_id_.begin(); |
| 176 it != guest_web_contents_by_instance_id_.end(); ++it) { | 176 it != guest_web_contents_by_instance_id_.end(); ++it) { |
| 177 if (it->second->GetSiteInstance()->GetSiteURL() == guest_site) | 177 if (it->second->GetSiteInstance()->GetSiteURL() == guest_site) |
| 178 return it->second->GetSiteInstance(); | 178 return it->second->GetSiteInstance(); |
| 179 } | 179 } |
| 180 return NULL; | 180 return nullptr; |
| 181 } | 181 } |
| 182 | 182 |
| 183 bool GuestViewManager::ForEachGuest(WebContents* embedder_web_contents, | 183 bool GuestViewManager::ForEachGuest(WebContents* embedder_web_contents, |
| 184 const GuestCallback& callback) { | 184 const GuestCallback& callback) { |
| 185 for (GuestInstanceMap::iterator it = | 185 for (GuestInstanceMap::iterator it = |
| 186 guest_web_contents_by_instance_id_.begin(); | 186 guest_web_contents_by_instance_id_.begin(); |
| 187 it != guest_web_contents_by_instance_id_.end(); ++it) { | 187 it != guest_web_contents_by_instance_id_.end(); ++it) { |
| 188 WebContents* guest = it->second; | 188 WebContents* guest = it->second; |
| 189 GuestViewBase* guest_view = GuestViewBase::FromWebContents(guest); | 189 GuestViewBase* guest_view = GuestViewBase::FromWebContents(guest); |
| 190 if (embedder_web_contents != guest_view->embedder_web_contents()) | 190 if (embedder_web_contents != guest_view->embedder_web_contents()) |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 } else { | 239 } else { |
| 240 removed_instance_ids_.insert(guest_instance_id); | 240 removed_instance_ids_.insert(guest_instance_id); |
| 241 } | 241 } |
| 242 } | 242 } |
| 243 | 243 |
| 244 content::WebContents* GuestViewManager::GetGuestByInstanceID( | 244 content::WebContents* GuestViewManager::GetGuestByInstanceID( |
| 245 int guest_instance_id) { | 245 int guest_instance_id) { |
| 246 GuestInstanceMap::const_iterator it = | 246 GuestInstanceMap::const_iterator it = |
| 247 guest_web_contents_by_instance_id_.find(guest_instance_id); | 247 guest_web_contents_by_instance_id_.find(guest_instance_id); |
| 248 if (it == guest_web_contents_by_instance_id_.end()) | 248 if (it == guest_web_contents_by_instance_id_.end()) |
| 249 return NULL; | 249 return nullptr; |
| 250 return it->second; | 250 return it->second; |
| 251 } | 251 } |
| 252 | 252 |
| 253 bool GuestViewManager::CanEmbedderAccessInstanceIDMaybeKill( | 253 bool GuestViewManager::CanEmbedderAccessInstanceIDMaybeKill( |
| 254 int embedder_render_process_id, | 254 int embedder_render_process_id, |
| 255 int guest_instance_id) { | 255 int guest_instance_id) { |
| 256 if (!CanEmbedderAccessInstanceID(embedder_render_process_id, | 256 if (!CanEmbedderAccessInstanceID(embedder_render_process_id, |
| 257 guest_instance_id)) { | 257 guest_instance_id)) { |
| 258 // The embedder process is trying to access a guest it does not own. | 258 // The embedder process is trying to access a guest it does not own. |
| 259 content::RecordAction( | 259 content::RecordAction( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 return true; | 296 return true; |
| 297 | 297 |
| 298 GuestViewBase* guest_view = GuestViewBase::FromWebContents(it->second); | 298 GuestViewBase* guest_view = GuestViewBase::FromWebContents(it->second); |
| 299 if (!guest_view) | 299 if (!guest_view) |
| 300 return false; | 300 return false; |
| 301 | 301 |
| 302 return embedder_render_process_id == guest_view->embedder_render_process_id(); | 302 return embedder_render_process_id == guest_view->embedder_render_process_id(); |
| 303 } | 303 } |
| 304 | 304 |
| 305 } // namespace extensions | 305 } // namespace extensions |
| OLD | NEW |