Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1652)

Side by Side Diff: extensions/browser/guest_view/guest_view_manager.cc

Issue 921473006: GuestView: Fix message routing across embedder navigations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More cleanup Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_frame_host.h" 9 #include "content/public/browser/render_frame_host.h"
10 #include "content/public/browser/render_process_host.h" 10 #include "content/public/browser/render_process_host.h"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 content::WebContents* GuestViewManager::GetGuestByInstanceIDSafely( 63 content::WebContents* GuestViewManager::GetGuestByInstanceIDSafely(
64 int guest_instance_id, 64 int guest_instance_id,
65 int embedder_render_process_id) { 65 int embedder_render_process_id) {
66 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id, 66 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id,
67 guest_instance_id)) { 67 guest_instance_id)) {
68 return nullptr; 68 return nullptr;
69 } 69 }
70 return GetGuestByInstanceID(guest_instance_id); 70 return GetGuestByInstanceID(guest_instance_id);
71 } 71 }
72 72
73 void GuestViewManager::AttachGuest( 73 void GuestViewManager::AttachGuest(int embedder_process_id,
74 int embedder_render_process_id, 74 int element_instance_id,
75 int embedder_routing_id, 75 int guest_instance_id,
76 int element_instance_id, 76 const base::DictionaryValue& attach_params) {
77 int guest_instance_id, 77 auto guest_view = GuestViewBase::From(embedder_process_id, guest_instance_id);
78 const base::DictionaryValue& attach_params) { 78 if (!guest_view)
79 content::WebContents* guest_web_contents =
80 GetGuestByInstanceIDSafely(guest_instance_id, embedder_render_process_id);
81 if (!guest_web_contents)
82 return; 79 return;
83 80
84 auto guest_view = GuestViewBase::FromWebContents(guest_web_contents); 81 ElementInstanceKey key(embedder_process_id, element_instance_id);
85 DCHECK(guest_view);
86
87 auto rvh = content::RenderViewHost::FromID(embedder_render_process_id,
88 embedder_routing_id);
89 // We need to check that rvh is not nullptr because there may be a race
90 // between AttachGuest and destroying the embedder (i.e. when the embedder is
91 // destroyed immediately after the guest is created).
92 if (!rvh)
93 return;
94 auto owner_web_contents = content::WebContents::FromRenderViewHost(rvh);
95 if (!owner_web_contents)
96 return;
97 ElementInstanceKey key(owner_web_contents, element_instance_id);
98
99 auto it = instance_id_map_.find(key); 82 auto it = instance_id_map_.find(key);
83 // If there is an existing guest attached to the element, then destroy the
84 // existing guest.
100 if (it != instance_id_map_.end()) { 85 if (it != instance_id_map_.end()) {
101 int old_guest_instance_id = it->second; 86 int old_guest_instance_id = it->second;
102 // Reattachment to the same guest is not currently supported.
103 if (old_guest_instance_id == guest_instance_id) 87 if (old_guest_instance_id == guest_instance_id)
104 return; 88 return;
105 89
106 auto old_guest_web_contents = 90 auto old_guest_view = GuestViewBase::From(embedder_process_id,
107 GetGuestByInstanceIDSafely(old_guest_instance_id, 91 old_guest_instance_id);
108 embedder_render_process_id);
109 if (!old_guest_web_contents)
110 return;
111
112 auto old_guest_view =
113 GuestViewBase::FromWebContents(old_guest_web_contents);
114
115 old_guest_view->Destroy(); 92 old_guest_view->Destroy();
116 } 93 }
117 instance_id_map_[key] = guest_instance_id; 94 instance_id_map_[key] = guest_instance_id;
118 reverse_instance_id_map_[guest_instance_id] = key; 95 reverse_instance_id_map_[guest_instance_id] = key;
119 guest_view->SetAttachParams(attach_params); 96 guest_view->SetAttachParams(attach_params);
120 } 97 }
121 98
122 void GuestViewManager::DetachGuest(GuestViewBase* guest, 99 void GuestViewManager::DetachGuest(GuestViewBase* guest) {
123 int element_instance_id) {
124 if (!guest->attached()) 100 if (!guest->attached())
125 return; 101 return;
126 102
127 ElementInstanceKey key(guest->owner_web_contents(), element_instance_id); 103 auto reverse_it = reverse_instance_id_map_.find(guest->guest_instance_id());
128 auto it = instance_id_map_.find(key); 104 if (reverse_it == reverse_instance_id_map_.end())
129 // There's nothing to do if this key does not exist in the map.
130 if (it == instance_id_map_.end())
131 return; 105 return;
132 106
133 int guest_instance_id = it->second; 107 const ElementInstanceKey& key = reverse_it->second;
134 instance_id_map_.erase(key);
135 108
136 auto reverse_it = reverse_instance_id_map_.find(guest->guest_instance_id()); 109 auto it = instance_id_map_.find(key);
137 DCHECK(reverse_it != reverse_instance_id_map_.end()); 110 DCHECK(it != instance_id_map_.end());
138 DCHECK(reverse_it->second == key); 111
139 reverse_instance_id_map_.erase(guest_instance_id); 112 reverse_instance_id_map_.erase(reverse_it);
113 instance_id_map_.erase(it);
140 } 114 }
141 115
142 int GuestViewManager::GetNextInstanceID() { 116 int GuestViewManager::GetNextInstanceID() {
143 return ++current_instance_id_; 117 return ++current_instance_id_;
144 } 118 }
145 119
146 void GuestViewManager::CreateGuest(const std::string& view_type, 120 void GuestViewManager::CreateGuest(const std::string& view_type,
147 content::WebContents* owner_web_contents, 121 content::WebContents* owner_web_contents,
148 const base::DictionaryValue& create_params, 122 const base::DictionaryValue& create_params,
149 const WebContentsCreatedCallback& callback) { 123 const WebContentsCreatedCallback& callback) {
(...skipping 13 matching lines...) Expand all
163 if (!guest) 137 if (!guest)
164 return nullptr; 138 return nullptr;
165 content::WebContents::CreateParams guest_create_params(create_params); 139 content::WebContents::CreateParams guest_create_params(create_params);
166 guest_create_params.guest_delegate = guest; 140 guest_create_params.guest_delegate = guest;
167 auto guest_web_contents = WebContents::Create(guest_create_params); 141 auto guest_web_contents = WebContents::Create(guest_create_params);
168 guest->InitWithWebContents(base::DictionaryValue(), guest_web_contents); 142 guest->InitWithWebContents(base::DictionaryValue(), guest_web_contents);
169 return guest_web_contents; 143 return guest_web_contents;
170 } 144 }
171 145
172 content::WebContents* GuestViewManager::GetGuestByInstanceID( 146 content::WebContents* GuestViewManager::GetGuestByInstanceID(
173 content::WebContents* owner_web_contents, 147 int owner_process_id,
174 int element_instance_id) { 148 int element_instance_id) {
175 int guest_instance_id = GetGuestInstanceIDForElementID(owner_web_contents, 149 int guest_instance_id = GetGuestInstanceIDForElementID(owner_process_id,
176 element_instance_id); 150 element_instance_id);
177 if (guest_instance_id == guestview::kInstanceIDNone) 151 if (guest_instance_id == guestview::kInstanceIDNone)
178 return nullptr; 152 return nullptr;
179 153
180 return GetGuestByInstanceID(guest_instance_id); 154 return GetGuestByInstanceID(guest_instance_id);
181 } 155 }
182 156
183 int GuestViewManager::GetGuestInstanceIDForElementID( 157 int GuestViewManager::GetGuestInstanceIDForElementID(int owner_process_id,
184 content::WebContents* owner_web_contents, 158 int element_instance_id) {
185 int element_instance_id) {
186 auto iter = instance_id_map_.find( 159 auto iter = instance_id_map_.find(
187 ElementInstanceKey(owner_web_contents, element_instance_id)); 160 ElementInstanceKey(owner_process_id, element_instance_id));
188 if (iter == instance_id_map_.end()) 161 if (iter == instance_id_map_.end())
189 return guestview::kInstanceIDNone; 162 return guestview::kInstanceIDNone;
190 return iter->second; 163 return iter->second;
191 } 164 }
192 165
193 SiteInstance* GuestViewManager::GetGuestSiteInstance( 166 SiteInstance* GuestViewManager::GetGuestSiteInstance(
194 const GURL& guest_site) { 167 const GURL& guest_site) {
195 for (const auto& guest : guest_web_contents_by_instance_id_) { 168 for (const auto& guest : guest_web_contents_by_instance_id_) {
196 if (guest.second->GetSiteInstance()->GetSiteURL() == guest_site) 169 if (guest.second->GetSiteInstance()->GetSiteURL() == guest_site)
197 return guest.second->GetSiteInstance(); 170 return guest.second->GetSiteInstance();
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 280
308 auto guest_view = GuestViewBase::FromWebContents(it->second); 281 auto guest_view = GuestViewBase::FromWebContents(it->second);
309 if (!guest_view) 282 if (!guest_view)
310 return false; 283 return false;
311 284
312 return embedder_render_process_id == 285 return embedder_render_process_id ==
313 guest_view->owner_web_contents()->GetRenderProcessHost()->GetID(); 286 guest_view->owner_web_contents()->GetRenderProcessHost()->GetID();
314 } 287 }
315 288
316 } // namespace extensions 289 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698