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

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: Removed unnecessary includes 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 content::StorageInfo& storage_info,
96 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents; 99 scoped_ptr<base::DictionaryValue> extra_params) {
97 // This will add the RenderProcessHost ID when we get one. 100 content::RenderProcessHost* embedder_process_host =
98 new GuestWebContentsObserver(guest_web_contents); 101 embedder_site_instance->GetProcess();
99 } 102 // Validate that the partition id coming from the renderer is valid UTF-8,
103 // since we depend on this in other parts of the code, such as FilePath
104 // creation. If the validation fails, treat it as a bad message and kill the
105 // renderer process.
106 if (!IsStringUTF8(storage_info.partition_id)) {
107 content::RecordAction(
108 base::UserMetricsAction("BadMessageTerminate_BPGM"));
109 base::KillProcess(
110 embedder_process_host->GetHandle(),
111 content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
112 return NULL;
113 }
100 114
101 void GuestViewManager::RemoveGuest(int guest_instance_id) { 115 const GURL& embedder_site_url = embedder_site_instance->GetSiteURL();
102 GuestInstanceMap::iterator it = 116 const std::string& host = embedder_site_url.host();
103 guest_web_contents_by_instance_id_.find(guest_instance_id); 117
104 DCHECK(it != guest_web_contents_by_instance_id_.end()); 118 std::string url_encoded_partition = net::EscapeQueryParamValue(
105 render_process_host_id_multiset_.erase( 119 storage_info.partition_id, false);
106 it->second->GetRenderProcessHost()->GetID()); 120 // The SiteInstance of a given webview tag is based on the fact that it's
107 guest_web_contents_by_instance_id_.erase(it); 121 // a guest process in addition to which platform application the tag
122 // belongs to and what storage partition is in use, rather than the URL
123 // that the tag is being navigated to.
124 GURL guest_site(base::StringPrintf("%s://%s/%s?%s",
125 content::kGuestScheme,
126 host.c_str(),
127 storage_info.persist ? "persist" : "",
128 url_encoded_partition.c_str()));
129
130 // If we already have a webview tag in the same app using the same storage
131 // partition, we should use the same SiteInstance so the existing tag and
132 // the new tag can script each other.
133 SiteInstance* guest_site_instance = GetGuestSiteInstance(guest_site);
134 if (!guest_site_instance) {
135 // Create the SiteInstance in a new BrowsingInstance, which will ensure
136 // that webview tags are also not allowed to send messages across
137 // different partitions.
138 guest_site_instance = SiteInstance::CreateForURL(
139 embedder_site_instance->GetBrowserContext(), guest_site);
140 }
141 WebContents::CreateParams create_params(
142 embedder_site_instance->GetBrowserContext(),
143 guest_site_instance);
144 create_params.guest_instance_id = instance_id;
145 create_params.guest_extra_params.reset(extra_params.release());
146 return WebContents::Create(create_params);
108 } 147 }
109 148
110 void GuestViewManager::MaybeGetGuestByInstanceIDOrKill( 149 void GuestViewManager::MaybeGetGuestByInstanceIDOrKill(
111 int guest_instance_id, 150 int guest_instance_id,
112 int embedder_render_process_id, 151 int embedder_render_process_id,
113 const GuestByInstanceIDCallback& callback) { 152 const GuestByInstanceIDCallback& callback) {
114 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id, 153 if (!CanEmbedderAccessInstanceIDMaybeKill(embedder_render_process_id,
115 guest_instance_id)) { 154 guest_instance_id)) {
116 // If we kill the embedder, then don't bother calling back. 155 // If we kill the embedder, then don't bother calling back.
117 return; 156 return;
(...skipping 22 matching lines...) Expand all
140 WebContents* guest = it->second; 179 WebContents* guest = it->second;
141 if (embedder_web_contents != guest->GetEmbedderWebContents()) 180 if (embedder_web_contents != guest->GetEmbedderWebContents())
142 continue; 181 continue;
143 182
144 if (callback.Run(guest)) 183 if (callback.Run(guest))
145 return true; 184 return true;
146 } 185 }
147 return false; 186 return false;
148 } 187 }
149 188
189 void GuestViewManager::AddGuest(int guest_instance_id,
190 WebContents* guest_web_contents) {
191 DCHECK(guest_web_contents_by_instance_id_.find(guest_instance_id) ==
192 guest_web_contents_by_instance_id_.end());
193 guest_web_contents_by_instance_id_[guest_instance_id] = guest_web_contents;
194 // This will add the RenderProcessHost ID when we get one.
195 new GuestWebContentsObserver(guest_web_contents);
196 }
197
198 void GuestViewManager::RemoveGuest(int guest_instance_id) {
199 GuestInstanceMap::iterator it =
200 guest_web_contents_by_instance_id_.find(guest_instance_id);
201 DCHECK(it != guest_web_contents_by_instance_id_.end());
202 render_process_host_id_multiset_.erase(
203 it->second->GetRenderProcessHost()->GetID());
204 guest_web_contents_by_instance_id_.erase(it);
205 }
206
150 void GuestViewManager::AddRenderProcessHostID(int render_process_host_id) { 207 void GuestViewManager::AddRenderProcessHostID(int render_process_host_id) {
151 render_process_host_id_multiset_.insert(render_process_host_id); 208 render_process_host_id_multiset_.insert(render_process_host_id);
152 } 209 }
153 210
154 content::WebContents* GuestViewManager::GetGuestByInstanceID( 211 content::WebContents* GuestViewManager::GetGuestByInstanceID(
155 int guest_instance_id, 212 int guest_instance_id,
156 int embedder_render_process_id) { 213 int embedder_render_process_id) {
157 GuestInstanceMap::const_iterator it = 214 GuestInstanceMap::const_iterator it =
158 guest_web_contents_by_instance_id_.find(guest_instance_id); 215 guest_web_contents_by_instance_id_.find(guest_instance_id);
159 if (it == guest_web_contents_by_instance_id_.end()) 216 if (it == guest_web_contents_by_instance_id_.end())
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 return false; 271 return false;
215 272
216 return embedder_render_process_id == 273 return embedder_render_process_id ==
217 guest->GetOpener()->GetEmbedderWebContents()->GetRenderProcessHost()-> 274 guest->GetOpener()->GetEmbedderWebContents()->GetRenderProcessHost()->
218 GetID(); 275 GetID();
219 } 276 }
220 277
221 return embedder_render_process_id == 278 return embedder_render_process_id ==
222 guest->embedder_web_contents()->GetRenderProcessHost()->GetID(); 279 guest->embedder_web_contents()->GetRenderProcessHost()->GetID();
223 } 280 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698