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

Side by Side Diff: android_webview/browser/aw_permission_manager.cc

Issue 1342833002: permissions: handle request ids for permissions in permission manager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix compile Created 5 years, 3 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "android_webview/browser/aw_permission_manager.h" 5 #include "android_webview/browser/aw_permission_manager.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "android_webview/browser/aw_browser_permission_request_delegate.h" 9 #include "android_webview/browser/aw_browser_permission_request_delegate.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/containers/hash_tables.h" 11 #include "base/containers/hash_tables.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/weak_ptr.h"
14 #include "content/public/browser/permission_type.h" 13 #include "content/public/browser/permission_type.h"
15 #include "content/public/browser/render_frame_host.h" 14 #include "content/public/browser/render_frame_host.h"
16 #include "content/public/browser/render_process_host.h" 15 #include "content/public/browser/render_process_host.h"
17 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
18 17
19 using content::PermissionStatus; 18 using content::PermissionStatus;
20 using content::PermissionType; 19 using content::PermissionType;
21 20
22 namespace android_webview { 21 namespace android_webview {
23 22
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 } 130 }
132 131
133 using StatusMap = base::hash_map<std::string, PermissionStatus>; 132 using StatusMap = base::hash_map<std::string, PermissionStatus>;
134 StatusMap pmi_result_cache_; 133 StatusMap pmi_result_cache_;
135 134
136 base::WeakPtrFactory<LastRequestResultCache> weak_factory_; 135 base::WeakPtrFactory<LastRequestResultCache> weak_factory_;
137 136
138 DISALLOW_COPY_AND_ASSIGN(LastRequestResultCache); 137 DISALLOW_COPY_AND_ASSIGN(LastRequestResultCache);
139 }; 138 };
140 139
141 namespace { 140 struct AwPermissionManager::PendingRequest {
142 141 PermissionType permission;
143 void CallbackPermisisonStatusWrapper( 142 GURL requesting_origin;
144 const base::WeakPtr<LastRequestResultCache>& result_cache, 143 };
145 const base::Callback<void(PermissionStatus)>& callback,
146 PermissionType permission,
147 const GURL& requesting_origin,
148 const GURL& embedding_origin,
149 bool allowed) {
150 PermissionStatus status = allowed ? content::PERMISSION_STATUS_GRANTED
151 : content::PERMISSION_STATUS_DENIED;
152 if (result_cache.get()) {
153 result_cache->SetResult(permission, requesting_origin, embedding_origin,
154 status);
155 }
156
157 callback.Run(status);
158 }
159
160 } // anonymous namespace
161 144
162 AwPermissionManager::AwPermissionManager() 145 AwPermissionManager::AwPermissionManager()
163 : content::PermissionManager(), result_cache_(new LastRequestResultCache) { 146 : content::PermissionManager(),
147 result_cache_(new LastRequestResultCache),
148 weak_ptr_factory_(this) {
164 } 149 }
165 150
166 AwPermissionManager::~AwPermissionManager() { 151 AwPermissionManager::~AwPermissionManager() {
167 } 152 }
168 153
169 void AwPermissionManager::RequestPermission( 154 int AwPermissionManager::RequestPermission(
170 PermissionType permission, 155 PermissionType permission,
171 content::RenderFrameHost* render_frame_host, 156 content::RenderFrameHost* render_frame_host,
172 int request_id, 157 const GURL& requesting_origin,
173 const GURL& origin,
174 bool user_gesture, 158 bool user_gesture,
175 const base::Callback<void(PermissionStatus)>& callback) { 159 const base::Callback<void(PermissionStatus)>& callback) {
176 int render_process_id = render_frame_host->GetProcess()->GetID(); 160 int render_process_id = render_frame_host->GetProcess()->GetID();
177 int render_frame_id = render_frame_host->GetRoutingID(); 161 int render_frame_id = render_frame_host->GetRoutingID();
178 AwBrowserPermissionRequestDelegate* delegate = 162 AwBrowserPermissionRequestDelegate* delegate =
179 AwBrowserPermissionRequestDelegate::FromID(render_process_id, 163 AwBrowserPermissionRequestDelegate::FromID(render_process_id,
180 render_frame_id); 164 render_frame_id);
181 if (!delegate) { 165 if (!delegate) {
182 DVLOG(0) << "Dropping permission request for " 166 DVLOG(0) << "Dropping permission request for "
183 << static_cast<int>(permission); 167 << static_cast<int>(permission);
184 callback.Run(content::PERMISSION_STATUS_DENIED); 168 callback.Run(content::PERMISSION_STATUS_DENIED);
185 return; 169 return -1;
186 } 170 }
187 171
188 const GURL& embedding_origin = 172 const GURL& embedding_origin =
189 content::WebContents::FromRenderFrameHost(render_frame_host) 173 content::WebContents::FromRenderFrameHost(render_frame_host)
190 ->GetLastCommittedURL().GetOrigin(); 174 ->GetLastCommittedURL().GetOrigin();
191 175
176 int request_id = -1;
192 switch (permission) { 177 switch (permission) {
193 case PermissionType::GEOLOCATION: 178 case PermissionType::GEOLOCATION:
179 request_id = AddPendingRequestToMap(permission, requesting_origin);
194 delegate->RequestGeolocationPermission( 180 delegate->RequestGeolocationPermission(
195 origin, base::Bind(&CallbackPermisisonStatusWrapper, 181 requesting_origin,
196 result_cache_->GetWeakPtr(), callback, permission, 182 base::Bind(&AwPermissionManager::OnRequestResponse,
197 origin, embedding_origin)); 183 weak_ptr_factory_.GetWeakPtr(), request_id,
184 result_cache_->GetWeakPtr(), callback, permission,
185 requesting_origin, embedding_origin));
198 break; 186 break;
199 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: 187 case PermissionType::PROTECTED_MEDIA_IDENTIFIER:
188 request_id = AddPendingRequestToMap(permission, requesting_origin);
200 delegate->RequestProtectedMediaIdentifierPermission( 189 delegate->RequestProtectedMediaIdentifierPermission(
201 origin, base::Bind(&CallbackPermisisonStatusWrapper, 190 requesting_origin,
202 result_cache_->GetWeakPtr(), callback, permission, 191 base::Bind(&AwPermissionManager::OnRequestResponse,
203 origin, embedding_origin)); 192 weak_ptr_factory_.GetWeakPtr(), request_id,
193 result_cache_->GetWeakPtr(), callback, permission,
194 requesting_origin, embedding_origin));
204 break; 195 break;
205 case PermissionType::MIDI_SYSEX: 196 case PermissionType::MIDI_SYSEX:
197 request_id = AddPendingRequestToMap(permission, requesting_origin);
206 delegate->RequestMIDISysexPermission( 198 delegate->RequestMIDISysexPermission(
207 origin, base::Bind(&CallbackPermisisonStatusWrapper, 199 requesting_origin,
208 result_cache_->GetWeakPtr(), callback, permission, 200 base::Bind(&AwPermissionManager::OnRequestResponse,
209 origin, embedding_origin)); 201 weak_ptr_factory_.GetWeakPtr(), request_id,
202 result_cache_->GetWeakPtr(), callback, permission,
203 requesting_origin, embedding_origin));
210 break; 204 break;
211 case PermissionType::AUDIO_CAPTURE: 205 case PermissionType::AUDIO_CAPTURE:
212 case PermissionType::VIDEO_CAPTURE: 206 case PermissionType::VIDEO_CAPTURE:
213 case PermissionType::NOTIFICATIONS: 207 case PermissionType::NOTIFICATIONS:
214 case PermissionType::PUSH_MESSAGING: 208 case PermissionType::PUSH_MESSAGING:
215 case PermissionType::DURABLE_STORAGE: 209 case PermissionType::DURABLE_STORAGE:
216 NOTIMPLEMENTED() << "RequestPermission is not implemented for " 210 NOTIMPLEMENTED() << "RequestPermission is not implemented for "
217 << static_cast<int>(permission); 211 << static_cast<int>(permission);
218 callback.Run(content::PERMISSION_STATUS_DENIED); 212 callback.Run(content::PERMISSION_STATUS_DENIED);
219 break; 213 break;
220 case PermissionType::MIDI: 214 case PermissionType::MIDI:
221 callback.Run(content::PERMISSION_STATUS_GRANTED); 215 callback.Run(content::PERMISSION_STATUS_GRANTED);
222 break; 216 break;
223 case PermissionType::NUM: 217 case PermissionType::NUM:
224 NOTREACHED() << "PermissionType::NUM was not expected here."; 218 NOTREACHED() << "PermissionType::NUM was not expected here.";
225 callback.Run(content::PERMISSION_STATUS_DENIED); 219 callback.Run(content::PERMISSION_STATUS_DENIED);
226 break; 220 break;
227 } 221 }
222 return request_id;
223 }
224
225 // static
226 void AwPermissionManager::OnRequestResponse(
227 const base::WeakPtr<AwPermissionManager>& manager,
228 int request_id,
229 const base::WeakPtr<LastRequestResultCache>& result_cache,
230 const base::Callback<void(PermissionStatus)>& callback,
231 PermissionType permission,
232 const GURL& requesting_origin,
233 const GURL& embedding_origin,
234 bool allowed) {
235 PermissionStatus status = allowed ? content::PERMISSION_STATUS_GRANTED
236 : content::PERMISSION_STATUS_DENIED;
237 if (result_cache.get()) {
238 result_cache->SetResult(permission, requesting_origin, embedding_origin,
239 status);
240 }
241 if (manager.get()) {
242 manager->pending_requests_.Remove(request_id);
243 }
244
245 callback.Run(status);
246 }
247
248 int AwPermissionManager::AddPendingRequestToMap(
249 PermissionType permission,
250 const GURL& requesting_origin) {
251 PendingRequest* request = new PendingRequest();
252 request->permission = permission;
253 request->requesting_origin = requesting_origin;
254 return pending_requests_.Add(request);
228 } 255 }
229 256
230 void AwPermissionManager::CancelPermissionRequest( 257 void AwPermissionManager::CancelPermissionRequest(
231 PermissionType permission,
232 content::RenderFrameHost* render_frame_host, 258 content::RenderFrameHost* render_frame_host,
233 int request_id, 259 int request_id) {
234 const GURL& origin) { 260 PendingRequest* pending_request = pending_requests_.Lookup(request_id);
261 if (!pending_request)
262 return;
263
235 // The caller is canceling (presumably) the most recent request. Assuming the 264 // The caller is canceling (presumably) the most recent request. Assuming the
236 // request did not complete, the user did not respond to the requset. 265 // request did not complete, the user did not respond to the requset.
237 // Thus, assume we do not know the result. 266 // Thus, assume we do not know the result.
238 const GURL& embedding_origin = 267 const GURL& embedding_origin =
239 content::WebContents::FromRenderFrameHost(render_frame_host) 268 content::WebContents::FromRenderFrameHost(render_frame_host)
240 ->GetLastCommittedURL().GetOrigin(); 269 ->GetLastCommittedURL().GetOrigin();
241 result_cache_->ClearResult(permission, origin, embedding_origin); 270 result_cache_->ClearResult(
271 pending_request->permission,
272 pending_request->requesting_origin,
273 embedding_origin);
242 274
243 int render_process_id = render_frame_host->GetProcess()->GetID(); 275 int render_process_id = render_frame_host->GetProcess()->GetID();
244 int render_frame_id = render_frame_host->GetRoutingID(); 276 int render_frame_id = render_frame_host->GetRoutingID();
245 AwBrowserPermissionRequestDelegate* delegate = 277 AwBrowserPermissionRequestDelegate* delegate =
246 AwBrowserPermissionRequestDelegate::FromID(render_process_id, 278 AwBrowserPermissionRequestDelegate::FromID(render_process_id,
247 render_frame_id); 279 render_frame_id);
248 if (!delegate) 280 if (!delegate)
249 return; 281 return;
250 282
251 switch (permission) { 283 switch (pending_request->permission) {
252 case PermissionType::GEOLOCATION: 284 case PermissionType::GEOLOCATION:
253 delegate->CancelGeolocationPermissionRequests(origin); 285 delegate->CancelGeolocationPermissionRequests(
286 pending_request->requesting_origin);
254 break; 287 break;
255 case PermissionType::PROTECTED_MEDIA_IDENTIFIER: 288 case PermissionType::PROTECTED_MEDIA_IDENTIFIER:
256 delegate->CancelProtectedMediaIdentifierPermissionRequests(origin); 289 delegate->CancelProtectedMediaIdentifierPermissionRequests(
290 pending_request->requesting_origin);
257 break; 291 break;
258 case PermissionType::MIDI_SYSEX: 292 case PermissionType::MIDI_SYSEX:
259 delegate->CancelMIDISysexPermissionRequests(origin); 293 delegate->CancelMIDISysexPermissionRequests(
294 pending_request->requesting_origin);
260 break; 295 break;
261 case PermissionType::NOTIFICATIONS: 296 case PermissionType::NOTIFICATIONS:
262 case PermissionType::PUSH_MESSAGING: 297 case PermissionType::PUSH_MESSAGING:
263 case PermissionType::DURABLE_STORAGE: 298 case PermissionType::DURABLE_STORAGE:
264 case PermissionType::AUDIO_CAPTURE: 299 case PermissionType::AUDIO_CAPTURE:
265 case PermissionType::VIDEO_CAPTURE: 300 case PermissionType::VIDEO_CAPTURE:
266 NOTIMPLEMENTED() << "CancelPermission not implemented for " 301 NOTIMPLEMENTED() << "CancelPermission not implemented for "
267 << static_cast<int>(permission); 302 << static_cast<int>(pending_request->permission);
268 break; 303 break;
269 case PermissionType::MIDI: 304 case PermissionType::MIDI:
270 // There is nothing to cancel so this is simply ignored. 305 // There is nothing to cancel so this is simply ignored.
271 break; 306 break;
272 case PermissionType::NUM: 307 case PermissionType::NUM:
273 NOTREACHED() << "PermissionType::NUM was not expected here."; 308 NOTREACHED() << "PermissionType::NUM was not expected here.";
274 break; 309 break;
275 } 310 }
276 } 311 }
277 312
(...skipping 30 matching lines...) Expand all
308 const GURL& embedding_origin, 343 const GURL& embedding_origin,
309 const base::Callback<void(PermissionStatus)>& callback) { 344 const base::Callback<void(PermissionStatus)>& callback) {
310 return -1; 345 return -1;
311 } 346 }
312 347
313 void AwPermissionManager::UnsubscribePermissionStatusChange( 348 void AwPermissionManager::UnsubscribePermissionStatusChange(
314 int subscription_id) { 349 int subscription_id) {
315 } 350 }
316 351
317 } // namespace android_webview 352 } // namespace android_webview
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698