| 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 "chrome/browser/extensions/extension_service.h" | 7 #include "chrome/browser/extensions/extension_service.h" |
| 8 #include "chrome/browser/guest_view/guest_view_base.h" | 8 #include "chrome/browser/guest_view/guest_view_base.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/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 GuestViewManager* guest_manager = | 68 GuestViewManager* guest_manager = |
| 69 static_cast<GuestViewManager*>(context->GetUserData( | 69 static_cast<GuestViewManager*>(context->GetUserData( |
| 70 guestview::kGuestViewManagerKeyName)); | 70 guestview::kGuestViewManagerKeyName)); |
| 71 if (!guest_manager) { | 71 if (!guest_manager) { |
| 72 guest_manager = new GuestViewManager(context); | 72 guest_manager = new GuestViewManager(context); |
| 73 context->SetUserData(guestview::kGuestViewManagerKeyName, guest_manager); | 73 context->SetUserData(guestview::kGuestViewManagerKeyName, guest_manager); |
| 74 } | 74 } |
| 75 return guest_manager; | 75 return guest_manager; |
| 76 } | 76 } |
| 77 | 77 |
| 78 content::WebContents* GuestViewManager::GetGuestByInstanceIDSafely( |
| 79 int guest_instance_id, |
| 80 int embedder_render_process_id) { |
| 81 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id, |
| 82 guest_instance_id)) { |
| 83 return NULL; |
| 84 } |
| 85 return GetGuestByInstanceID(guest_instance_id, embedder_render_process_id); |
| 86 } |
| 87 |
| 78 int GuestViewManager::GetNextInstanceID() { | 88 int GuestViewManager::GetNextInstanceID() { |
| 79 return ++current_instance_id_; | 89 return ++current_instance_id_; |
| 80 } | 90 } |
| 81 | 91 |
| 82 void GuestViewManager::AddGuest(int guest_instance_id, | 92 void GuestViewManager::AddGuest(int guest_instance_id, |
| 83 WebContents* guest_web_contents) { | 93 WebContents* guest_web_contents) { |
| 84 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) == | 94 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) == |
| 85 guest_web_contents_by_instance_id_.end()); | 95 guest_web_contents_by_instance_id_.end()); |
| 86 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; | 96 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; |
| 87 // This will add the RenderProcessHost ID when we get one. | 97 // This will add the RenderProcessHost ID when we get one. |
| 88 new GuestWebContentsObserver(guest_web_contents); | 98 new GuestWebContentsObserver(guest_web_contents); |
| 89 } | 99 } |
| 90 | 100 |
| 91 void GuestViewManager::RemoveGuest(int guest_instance_id) { | 101 void GuestViewManager::RemoveGuest(int guest_instance_id) { |
| 92 GuestInstanceMap::iterator it = | 102 GuestInstanceMap::iterator it = |
| 93 guest_web_contents_by_instance_id_.find(guest_instance_id); | 103 guest_web_contents_by_instance_id_.find(guest_instance_id); |
| 94 DCHECK(it != guest_web_contents_by_instance_id_.end()); | 104 DCHECK(it != guest_web_contents_by_instance_id_.end()); |
| 95 render_process_host_id_multiset_.erase( | 105 render_process_host_id_multiset_.erase( |
| 96 it->second->GetRenderProcessHost()->GetID()); | 106 it->second->GetRenderProcessHost()->GetID()); |
| 97 guest_web_contents_by_instance_id_.erase(it); | 107 guest_web_contents_by_instance_id_.erase(it); |
| 98 } | 108 } |
| 99 | 109 |
| 110 void GuestViewManager::MaybeGetGuestByInstanceIDOrKill( |
| 111 int guest_instance_id, |
| 112 int embedder_render_process_id, |
| 113 const GuestByInstanceIDCallback& callback) { |
| 114 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id, |
| 115 guest_instance_id)) { |
| 116 // If we kill the embedder, then don't bother calling back. |
| 117 return; |
| 118 } |
| 119 content::WebContents* guest_web_contents = |
| 120 GetGuestByInstanceID(guest_instance_id, embedder_render_process_id); |
| 121 callback.Run(guest_web_contents); |
| 122 } |
| 123 |
| 124 SiteInstance* GuestViewManager::GetGuestSiteInstance( |
| 125 const GURL& guest_site) { |
| 126 for (GuestInstanceMap::const_iterator it = |
| 127 guest_web_contents_by_instance_id_.begin(); |
| 128 it != guest_web_contents_by_instance_id_.end(); ++it) { |
| 129 if (it->second->GetSiteInstance()->GetSiteURL() == guest_site) |
| 130 return it->second->GetSiteInstance(); |
| 131 } |
| 132 return NULL; |
| 133 } |
| 134 |
| 135 bool GuestViewManager::ForEachGuest(WebContents* embedder_web_contents, |
| 136 const GuestCallback& callback) { |
| 137 for (GuestInstanceMap::iterator it = |
| 138 guest_web_contents_by_instance_id_.begin(); |
| 139 it != guest_web_contents_by_instance_id_.end(); ++it) { |
| 140 WebContents* guest = it->second; |
| 141 if (embedder_web_contents != guest->GetEmbedderWebContents()) |
| 142 continue; |
| 143 |
| 144 if (callback.Run(guest)) |
| 145 return true; |
| 146 } |
| 147 return false; |
| 148 } |
| 149 |
| 150 void GuestViewManager::AddRenderProcessHostID(int render_process_host_id) { |
| 151 render_process_host_id_multiset_.insert(render_process_host_id); |
| 152 } |
| 153 |
| 100 content::WebContents* GuestViewManager::GetGuestByInstanceID( | 154 content::WebContents* GuestViewManager::GetGuestByInstanceID( |
| 101 int guest_instance_id, | 155 int guest_instance_id, |
| 102 int embedder_render_process_id) { | 156 int embedder_render_process_id) { |
| 103 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id, | |
| 104 guest_instance_id)) { | |
| 105 return NULL; | |
| 106 } | |
| 107 GuestInstanceMap::const_iterator it = | 157 GuestInstanceMap::const_iterator it = |
| 108 guest_web_contents_by_instance_id_.find(guest_instance_id); | 158 guest_web_contents_by_instance_id_.find(guest_instance_id); |
| 109 if (it == guest_web_contents_by_instance_id_.end()) | 159 if (it == guest_web_contents_by_instance_id_.end()) |
| 110 return NULL; | 160 return NULL; |
| 111 return it->second; | 161 return it->second; |
| 112 } | 162 } |
| 113 | 163 |
| 114 bool GuestViewManager::CanEmbedderAccessInstanceIDMaybeKill( | 164 bool GuestViewManager::CanEmbedderAccessInstanceIDMaybeKill( |
| 115 int embedder_render_process_id, | 165 int embedder_render_process_id, |
| 116 int guest_instance_id) { | 166 int guest_instance_id) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 if (it == guest_web_contents_by_instance_id_.end()) | 198 if (it == guest_web_contents_by_instance_id_.end()) |
| 149 return true; | 199 return true; |
| 150 | 200 |
| 151 GuestViewBase* guest_view = GuestViewBase::FromWebContents(it->second); | 201 GuestViewBase* guest_view = GuestViewBase::FromWebContents(it->second); |
| 152 if (!guest_view) | 202 if (!guest_view) |
| 153 return false; | 203 return false; |
| 154 | 204 |
| 155 return CanEmbedderAccessGuest(embedder_render_process_id, guest_view); | 205 return CanEmbedderAccessGuest(embedder_render_process_id, guest_view); |
| 156 } | 206 } |
| 157 | 207 |
| 158 SiteInstance* GuestViewManager::GetGuestSiteInstance( | |
| 159 const GURL& guest_site) { | |
| 160 for (GuestInstanceMap::const_iterator it = | |
| 161 guest_web_contents_by_instance_id_.begin(); | |
| 162 it != guest_web_contents_by_instance_id_.end(); ++it) { | |
| 163 if (it->second->GetSiteInstance()->GetSiteURL() == guest_site) | |
| 164 return it->second->GetSiteInstance(); | |
| 165 } | |
| 166 return NULL; | |
| 167 } | |
| 168 | |
| 169 bool GuestViewManager::ForEachGuest(WebContents* embedder_web_contents, | |
| 170 const GuestCallback& callback) { | |
| 171 for (GuestInstanceMap::iterator it = | |
| 172 guest_web_contents_by_instance_id_.begin(); | |
| 173 it != guest_web_contents_by_instance_id_.end(); ++it) { | |
| 174 WebContents* guest = it->second; | |
| 175 if (embedder_web_contents != guest->GetEmbedderWebContents()) | |
| 176 continue; | |
| 177 | |
| 178 if (callback.Run(guest)) | |
| 179 return true; | |
| 180 } | |
| 181 return false; | |
| 182 } | |
| 183 | |
| 184 void GuestViewManager::AddRenderProcessHostID(int render_process_host_id) { | |
| 185 render_process_host_id_multiset_.insert(render_process_host_id); | |
| 186 } | |
| 187 | |
| 188 bool GuestViewManager::CanEmbedderAccessGuest(int embedder_render_process_id, | 208 bool GuestViewManager::CanEmbedderAccessGuest(int embedder_render_process_id, |
| 189 GuestViewBase* guest) { | 209 GuestViewBase* guest) { |
| 190 // The embedder can access the guest if it has not been attached and its | 210 // The embedder can access the guest if it has not been attached and its |
| 191 // opener's embedder lives in the same process as the given embedder. | 211 // opener's embedder lives in the same process as the given embedder. |
| 192 if (!guest->attached()) { | 212 if (!guest->attached()) { |
| 193 if (!guest->GetOpener()) | 213 if (!guest->GetOpener()) |
| 194 return false; | 214 return false; |
| 195 | 215 |
| 196 return embedder_render_process_id == | 216 return embedder_render_process_id == |
| 197 guest->GetOpener()->GetEmbedderWebContents()->GetRenderProcessHost()-> | 217 guest->GetOpener()->GetEmbedderWebContents()->GetRenderProcessHost()-> |
| 198 GetID(); | 218 GetID(); |
| 199 } | 219 } |
| 200 | 220 |
| 201 return embedder_render_process_id == | 221 return embedder_render_process_id == |
| 202 guest->embedder_web_contents()->GetRenderProcessHost()->GetID(); | 222 guest->embedder_web_contents()->GetRenderProcessHost()->GetID(); |
| 203 } | 223 } |
| OLD | NEW |