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

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

Issue 261013005: BrowserPlugin: Move CreateGuest to chrome (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@guestview_manager_simplify_api
Patch Set: Fixed content_browsertests after a change Created 6 years, 7 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 "chrome/browser/guest_view/guest_view_manager.h" 5 #include "chrome/browser/guest_view/guest_view_manager.h"
6 6
7 #include "base/strings/stringprintf.h"
7 #include "chrome/browser/extensions/extension_service.h" 8 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/guest_view/guest_view_base.h" 9 #include "chrome/browser/guest_view/guest_view_base.h"
9 #include "chrome/browser/guest_view/guest_view_constants.h" 10 #include "chrome/browser/guest_view/guest_view_constants.h"
10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
11 #include "content/public/browser/browser_context.h" 12 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/render_process_host.h" 13 #include "content/public/browser/render_process_host.h"
13 #include "content/public/browser/user_metrics.h" 14 #include "content/public/browser/user_metrics.h"
14 #include "content/public/browser/web_contents_observer.h" 15 #include "content/public/browser/web_contents_observer.h"
15 #include "content/public/common/result_codes.h" 16 #include "content/public/common/result_codes.h"
17 #include "content/public/common/url_constants.h"
16 #include "extensions/browser/extension_system.h" 18 #include "extensions/browser/extension_system.h"
19 #include "net/base/escape.h"
17 #include "url/gurl.h" 20 #include "url/gurl.h"
18 21
19 using content::BrowserContext; 22 using content::BrowserContext;
20 using content::SiteInstance; 23 using content::SiteInstance;
21 using content::WebContents; 24 using content::WebContents;
22 25
23 // A WebContents does not immediately have a RenderProcessHost. It acquires one 26 // A WebContents does not immediately have a RenderProcessHost. It acquires one
24 // on initial navigation. This observer exists until that initial navigation in 27 // on initial navigation. This observer exists until that initial navigation in
25 // order to grab the ID if tis RenderProcessHost so that it can register it as 28 // order to grab the ID if tis RenderProcessHost so that it can register it as
26 // a guest. 29 // a guest.
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 guest_instance_id)) { 85 guest_instance_id)) {
83 return NULL; 86 return NULL;
84 } 87 }
85 return GetGuestByInstanceID(guest_instance_id, embedder_render_process_id); 88 return GetGuestByInstanceID(guest_instance_id, embedder_render_process_id);
86 } 89 }
87 90
88 int GuestViewManager::GetNextInstanceID() { 91 int GuestViewManager::GetNextInstanceID() {
89 return ++current_instance_id_; 92 return ++current_instance_id_;
90 } 93 }
91 94
92 void GuestViewManager::AddGuest(int guest_instance_id, 95 content::WebContents* GuestViewManager::CreateGuest(
93 WebContents* guest_web_contents) { 96 content::SiteInstance* embedder_site_instance,
94 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) == 97 int instance_id,
95 guest_web_contents_by_instance_id_.end()); 98 const std::string& storage_partition_id,
96 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; 99 bool persist_storage,
97 // This will add the RenderProcessHost ID when we get one. 100 scoped_ptr<base::DictionaryValue> extra_params) {
98 new GuestWebContentsObserver(guest_web_contents); 101 content::RenderProcessHost* embedder_process_host =
99 } 102 embedder_site_instance->GetProcess();
103 // Validate that the partition id coming from the renderer is valid UTF-8,
104 // since we depend on this in other parts of the code, such as FilePath
105 // creation. If the validation fails, treat it as a bad message and kill the
106 // renderer process.
107 if (!base::IsStringUTF8(storage_partition_id)) {
108 content::RecordAction(
109 base::UserMetricsAction("BadMessageTerminate_BPGM"));
110 base::KillProcess(
111 embedder_process_host->GetHandle(),
112 content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
113 return NULL;
114 }
100 115
101 void GuestViewManager::RemoveGuest(int guest_instance_id) { 116 const GURL& embedder_site_url = embedder_site_instance->GetSiteURL();
102 GuestInstanceMap::iterator it = 117 const std::string& host = embedder_site_url.host();
103 guest_web_contents_by_instance_id_.find(guest_instance_id); 118
104 DCHECK(it != guest_web_contents_by_instance_id_.end()); 119 std::string url_encoded_partition = net::EscapeQueryParamValue(
105 render_process_host_id_multiset_.erase( 120 storage_partition_id, false);
106 it->second->GetRenderProcessHost()->GetID()); 121 // The SiteInstance of a given webview tag is based on the fact that it's
107 guest_web_contents_by_instance_id_.erase(it); 122 // a guest process in addition to which platform application the tag
123 // belongs to and what storage partition is in use, rather than the URL
124 // that the tag is being navigated to.
125 GURL guest_site(base::StringPrintf("%s://%s/%s?%s",
126 content::kGuestScheme,
127 host.c_str(),
128 persist_storage ? "persist" : "",
129 url_encoded_partition.c_str()));
130
131 // If we already have a webview tag in the same app using the same storage
132 // partition, we should use the same SiteInstance so the existing tag and
133 // the new tag can script each other.
134 SiteInstance* guest_site_instance = GetGuestSiteInstance(guest_site);
135 if (!guest_site_instance) {
136 // Create the SiteInstance in a new BrowsingInstance, which will ensure
137 // that webview tags are also not allowed to send messages across
138 // different partitions.
139 guest_site_instance = SiteInstance::CreateForURL(
140 embedder_site_instance->GetBrowserContext(), guest_site);
141 }
142 WebContents::CreateParams create_params(
143 embedder_site_instance->GetBrowserContext(),
144 guest_site_instance);
145 create_params.guest_instance_id = instance_id;
146 create_params.guest_extra_params.reset(extra_params.release());
147 return WebContents::Create(create_params);
108 } 148 }
109 149
110 void GuestViewManager::MaybeGetGuestByInstanceIDOrKill( 150 void GuestViewManager::MaybeGetGuestByInstanceIDOrKill(
111 int guest_instance_id, 151 int guest_instance_id,
112 int embedder_render_process_id, 152 int embedder_render_process_id,
113 const GuestByInstanceIDCallback& callback) { 153 const GuestByInstanceIDCallback& callback) {
114 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id, 154 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id,
115 guest_instance_id)) { 155 guest_instance_id)) {
116 // If we kill the embedder, then don't bother calling back. 156 // If we kill the embedder, then don't bother calling back.
117 return; 157 return;
(...skipping 22 matching lines...) Expand all
140 WebContents* guest = it->second; 180 WebContents* guest = it->second;
141 if (embedder_web_contents != guest->GetEmbedderWebContents()) 181 if (embedder_web_contents != guest->GetEmbedderWebContents())
142 continue; 182 continue;
143 183
144 if (callback.Run(guest)) 184 if (callback.Run(guest))
145 return true; 185 return true;
146 } 186 }
147 return false; 187 return false;
148 } 188 }
149 189
190 void GuestViewManager::AddGuest(int guest_instance_id,
191 WebContents* guest_web_contents) {
192 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) ==
193 guest_web_contents_by_instance_id_.end());
194 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents;
195 // This will add the RenderProcessHost ID when we get one.
196 new GuestWebContentsObserver(guest_web_contents);
197 }
198
199 void GuestViewManager::RemoveGuest(int guest_instance_id) {
200 GuestInstanceMap::iterator it =
201 guest_web_contents_by_instance_id_.find(guest_instance_id);
202 DCHECK(it != guest_web_contents_by_instance_id_.end());
203 render_process_host_id_multiset_.erase(
204 it->second->GetRenderProcessHost()->GetID());
205 guest_web_contents_by_instance_id_.erase(it);
206 }
207
150 void GuestViewManager::AddRenderProcessHostID(int render_process_host_id) { 208 void GuestViewManager::AddRenderProcessHostID(int render_process_host_id) {
151 render_process_host_id_multiset_.insert(render_process_host_id); 209 render_process_host_id_multiset_.insert(render_process_host_id);
152 } 210 }
153 211
154 content::WebContents* GuestViewManager::GetGuestByInstanceID( 212 content::WebContents* GuestViewManager::GetGuestByInstanceID(
155 int guest_instance_id, 213 int guest_instance_id,
156 int embedder_render_process_id) { 214 int embedder_render_process_id) {
157 GuestInstanceMap::const_iterator it = 215 GuestInstanceMap::const_iterator it =
158 guest_web_contents_by_instance_id_.find(guest_instance_id); 216 guest_web_contents_by_instance_id_.find(guest_instance_id);
159 if (it == guest_web_contents_by_instance_id_.end()) 217 if (it == guest_web_contents_by_instance_id_.end())
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 return false; 272 return false;
215 273
216 return embedder_render_process_id == 274 return embedder_render_process_id ==
217 guest->GetOpener()->GetEmbedderWebContents()->GetRenderProcessHost()-> 275 guest->GetOpener()->GetEmbedderWebContents()->GetRenderProcessHost()->
218 GetID(); 276 GetID();
219 } 277 }
220 278
221 return embedder_render_process_id == 279 return embedder_render_process_id ==
222 guest->embedder_web_contents()->GetRenderProcessHost()->GetID(); 280 guest->embedder_web_contents()->GetRenderProcessHost()->GetID();
223 } 281 }
OLDNEW
« no previous file with comments | « chrome/browser/guest_view/guest_view_manager.h ('k') | content/browser/browser_plugin/browser_plugin_embedder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698