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/user_metrics.h" | 11 #include "content/public/browser/user_metrics.h" |
11 #include "content/public/browser/web_contents_observer.h" | 12 #include "content/public/browser/web_contents_observer.h" |
12 #include "content/public/common/result_codes.h" | 13 #include "content/public/common/result_codes.h" |
13 #include "content/public/common/url_constants.h" | 14 #include "content/public/common/url_constants.h" |
14 #include "extensions/browser/extension_system.h" | 15 #include "extensions/browser/extension_system.h" |
15 #include "extensions/browser/guest_view/guest_view_base.h" | 16 #include "extensions/browser/guest_view/guest_view_base.h" |
16 #include "extensions/browser/guest_view/guest_view_constants.h" | 17 #include "extensions/browser/guest_view/guest_view_constants.h" |
17 #include "extensions/browser/guest_view/guest_view_manager_factory.h" | 18 #include "extensions/browser/guest_view/guest_view_manager_factory.h" |
18 #include "net/base/escape.h" | 19 #include "net/base/escape.h" |
19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 content::WebContents* GuestViewManager::GetGuestByInstanceIDSafely( | 54 content::WebContents* GuestViewManager::GetGuestByInstanceIDSafely( |
54 int guest_instance_id, | 55 int guest_instance_id, |
55 int embedder_render_process_id) { | 56 int embedder_render_process_id) { |
56 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id, | 57 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id, |
57 guest_instance_id)) { | 58 guest_instance_id)) { |
58 return NULL; | 59 return NULL; |
59 } | 60 } |
60 return GetGuestByInstanceID(guest_instance_id); | 61 return GetGuestByInstanceID(guest_instance_id); |
61 } | 62 } |
62 | 63 |
| 64 void GuestViewManager::AttachGuest( |
| 65 int embedder_render_process_id, |
| 66 int embedder_routing_id, |
| 67 int element_instance_id, |
| 68 int guest_instance_id, |
| 69 const base::DictionaryValue& attach_params) { |
| 70 content::WebContents* guest_web_contents = |
| 71 GetGuestByInstanceIDSafely(guest_instance_id, embedder_render_process_id); |
| 72 if (!guest_web_contents) |
| 73 return; |
| 74 |
| 75 GuestViewBase* guest_view = |
| 76 GuestViewBase::FromWebContents(guest_web_contents); |
| 77 DCHECK(guest_view); |
| 78 |
| 79 content::RenderViewHost* rvh = |
| 80 content::RenderViewHost::FromID(embedder_render_process_id, |
| 81 embedder_routing_id); |
| 82 content::WebContents* embedder_web_contents = |
| 83 content::WebContents::FromRenderViewHost(rvh); |
| 84 if (!embedder_web_contents) |
| 85 return; |
| 86 ElementInstanceKey key(embedder_web_contents, element_instance_id); |
| 87 |
| 88 GuestInstanceIDMap::iterator it = instance_id_map_.find(key); |
| 89 if (it != instance_id_map_.end()) { |
| 90 int old_guest_instance_id = it->second; |
| 91 // Reattachment to the same guest is not currently supported. |
| 92 if (old_guest_instance_id == guest_instance_id) |
| 93 return; |
| 94 |
| 95 content::WebContents* old_guest_web_contents = |
| 96 GetGuestByInstanceIDSafely(old_guest_instance_id, |
| 97 embedder_render_process_id); |
| 98 if (!old_guest_web_contents) |
| 99 return; |
| 100 |
| 101 GuestViewBase* old_guest_view = |
| 102 GuestViewBase::FromWebContents(old_guest_web_contents); |
| 103 |
| 104 old_guest_view->Destroy(); |
| 105 } |
| 106 instance_id_map_[key] = guest_instance_id; |
| 107 reverse_instance_id_map_.insert(std::make_pair(guest_instance_id, key)); |
| 108 guest_view->SetAttachParams(attach_params); |
| 109 } |
| 110 |
63 int GuestViewManager::GetNextInstanceID() { | 111 int GuestViewManager::GetNextInstanceID() { |
64 return ++current_instance_id_; | 112 return ++current_instance_id_; |
65 } | 113 } |
66 | 114 |
67 void GuestViewManager::CreateGuest(const std::string& view_type, | 115 void GuestViewManager::CreateGuest(const std::string& view_type, |
68 const std::string& embedder_extension_id, | 116 const std::string& embedder_extension_id, |
69 content::WebContents* embedder_web_contents, | 117 content::WebContents* embedder_web_contents, |
70 const base::DictionaryValue& create_params, | 118 const base::DictionaryValue& create_params, |
71 const WebContentsCreatedCallback& callback) { | 119 const WebContentsCreatedCallback& callback) { |
72 int guest_instance_id = GetNextInstanceID(); | 120 int guest_instance_id = GetNextInstanceID(); |
(...skipping 21 matching lines...) Expand all Loading... |
94 guest_create_params.guest_delegate = guest; | 142 guest_create_params.guest_delegate = guest; |
95 content::WebContents* guest_web_contents = | 143 content::WebContents* guest_web_contents = |
96 WebContents::Create(guest_create_params); | 144 WebContents::Create(guest_create_params); |
97 guest->InitWithWebContents(embedder_extension_id, | 145 guest->InitWithWebContents(embedder_extension_id, |
98 embedder_render_process_id, | 146 embedder_render_process_id, |
99 guest_web_contents); | 147 guest_web_contents); |
100 return guest_web_contents; | 148 return guest_web_contents; |
101 } | 149 } |
102 | 150 |
103 void GuestViewManager::MaybeGetGuestByInstanceIDOrKill( | 151 void GuestViewManager::MaybeGetGuestByInstanceIDOrKill( |
104 int guest_instance_id, | 152 content::WebContents* embedder_web_contents, |
105 int embedder_render_process_id, | 153 int element_instance_id, |
106 const GuestByInstanceIDCallback& callback) { | 154 const GuestByInstanceIDCallback& callback) { |
| 155 int guest_instance_id = GetGuestInstanceIDForPluginID(embedder_web_contents, |
| 156 element_instance_id); |
| 157 if (guest_instance_id == guestview::kInstanceIDNone) |
| 158 return; |
| 159 int embedder_render_process_id = |
| 160 embedder_web_contents->GetRenderProcessHost()->GetID(); |
107 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id, | 161 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id, |
108 guest_instance_id)) { | 162 guest_instance_id)) { |
109 // If we kill the embedder, then don't bother calling back. | 163 // If we kill the embedder, then don't bother calling back. |
110 return; | 164 return; |
111 } | 165 } |
112 content::WebContents* guest_web_contents = | 166 content::WebContents* guest_web_contents = |
113 GetGuestByInstanceID(guest_instance_id); | 167 GetGuestByInstanceID(guest_instance_id); |
114 callback.Run(guest_web_contents); | 168 callback.Run(guest_web_contents); |
115 } | 169 } |
116 | 170 |
| 171 int GuestViewManager::GetGuestInstanceIDForPluginID( |
| 172 content::WebContents* embedder_web_contents, |
| 173 int element_instance_id) { |
| 174 GuestInstanceIDMap::iterator iter = instance_id_map_.find( |
| 175 ElementInstanceKey(embedder_web_contents, element_instance_id)); |
| 176 if (iter == instance_id_map_.end()) |
| 177 return guestview::kInstanceIDNone; |
| 178 return iter->second; |
| 179 } |
| 180 |
117 SiteInstance* GuestViewManager::GetGuestSiteInstance( | 181 SiteInstance* GuestViewManager::GetGuestSiteInstance( |
118 const GURL& guest_site) { | 182 const GURL& guest_site) { |
119 for (GuestInstanceMap::const_iterator it = | 183 for (GuestInstanceMap::const_iterator it = |
120 guest_web_contents_by_instance_id_.begin(); | 184 guest_web_contents_by_instance_id_.begin(); |
121 it != guest_web_contents_by_instance_id_.end(); ++it) { | 185 it != guest_web_contents_by_instance_id_.end(); ++it) { |
122 if (it->second->GetSiteInstance()->GetSiteURL() == guest_site) | 186 if (it->second->GetSiteInstance()->GetSiteURL() == guest_site) |
123 return it->second->GetSiteInstance(); | 187 return it->second->GetSiteInstance(); |
124 } | 188 } |
125 return NULL; | 189 return NULL; |
126 } | 190 } |
(...skipping 20 matching lines...) Expand all Loading... |
147 CHECK(CanUseGuestInstanceID(guest_instance_id)); | 211 CHECK(CanUseGuestInstanceID(guest_instance_id)); |
148 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; | 212 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; |
149 } | 213 } |
150 | 214 |
151 void GuestViewManager::RemoveGuest(int guest_instance_id) { | 215 void GuestViewManager::RemoveGuest(int guest_instance_id) { |
152 GuestInstanceMap::iterator it = | 216 GuestInstanceMap::iterator it = |
153 guest_web_contents_by_instance_id_.find(guest_instance_id); | 217 guest_web_contents_by_instance_id_.find(guest_instance_id); |
154 DCHECK(it != guest_web_contents_by_instance_id_.end()); | 218 DCHECK(it != guest_web_contents_by_instance_id_.end()); |
155 guest_web_contents_by_instance_id_.erase(it); | 219 guest_web_contents_by_instance_id_.erase(it); |
156 | 220 |
| 221 GuestInstanceIDReverseMap::iterator id_iter = |
| 222 reverse_instance_id_map_.find(guest_instance_id); |
| 223 if (id_iter != reverse_instance_id_map_.end()) { |
| 224 const ElementInstanceKey& instance_id_key = id_iter->second; |
| 225 instance_id_map_.erase(instance_id_map_.find(instance_id_key)); |
| 226 reverse_instance_id_map_.erase(id_iter); |
| 227 } |
| 228 |
157 // All the instance IDs that lie within [0, last_instance_id_removed_] | 229 // All the instance IDs that lie within [0, last_instance_id_removed_] |
158 // are invalid. | 230 // are invalid. |
159 // The remaining sparse invalid IDs are kept in |removed_instance_ids_| set. | 231 // The remaining sparse invalid IDs are kept in |removed_instance_ids_| set. |
160 // The following code compacts the set by incrementing | 232 // The following code compacts the set by incrementing |
161 // |last_instance_id_removed_|. | 233 // |last_instance_id_removed_|. |
162 if (guest_instance_id == last_instance_id_removed_ + 1) { | 234 if (guest_instance_id == last_instance_id_removed_ + 1) { |
163 ++last_instance_id_removed_; | 235 ++last_instance_id_removed_; |
164 // Compact. | 236 // Compact. |
165 std::set<int>::iterator iter = removed_instance_ids_.begin(); | 237 std::set<int>::iterator iter = removed_instance_ids_.begin(); |
166 while (iter != removed_instance_ids_.end()) { | 238 while (iter != removed_instance_ids_.end()) { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 return true; | 305 return true; |
234 | 306 |
235 GuestViewBase* guest_view = GuestViewBase::FromWebContents(it->second); | 307 GuestViewBase* guest_view = GuestViewBase::FromWebContents(it->second); |
236 if (!guest_view) | 308 if (!guest_view) |
237 return false; | 309 return false; |
238 | 310 |
239 return embedder_render_process_id == guest_view->embedder_render_process_id(); | 311 return embedder_render_process_id == guest_view->embedder_render_process_id(); |
240 } | 312 } |
241 | 313 |
242 } // namespace extensions | 314 } // namespace extensions |
OLD | NEW |