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

Side by Side Diff: content/child/permissions/permission_dispatcher.cc

Issue 1260193009: renderer: implement multiple permission requesting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@permissions-request-multiple
Patch Set: Address review comments 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 "content/child/permissions/permission_dispatcher.h" 5 #include "content/child/permissions/permission_dispatcher.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "content/child/worker_task_runner.h" 8 #include "content/child/worker_task_runner.h"
9 #include "content/public/common/service_registry.h" 9 #include "content/public/common/service_registry.h"
10 #include "third_party/WebKit/public/platform/WebURL.h" 10 #include "third_party/WebKit/public/platform/WebURL.h"
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 } 95 }
96 96
97 void PermissionDispatcher::requestPermission( 97 void PermissionDispatcher::requestPermission(
98 blink::WebPermissionType type, 98 blink::WebPermissionType type,
99 const blink::WebURL& origin, 99 const blink::WebURL& origin,
100 blink::WebPermissionCallback* callback) { 100 blink::WebPermissionCallback* callback) {
101 RequestPermissionInternal( 101 RequestPermissionInternal(
102 type, origin.string().utf8(), callback, kNoWorkerThread); 102 type, origin.string().utf8(), callback, kNoWorkerThread);
103 } 103 }
104 104
105 void PermissionDispatcher::requestPermissions(
106 const blink::WebVector<blink::WebPermissionType>& types,
107 const blink::WebURL& origin,
108 blink::WebPermissionsCallback* callback) {
109 RequestPermissionsInternal(
110 types, origin.string().utf8(), callback, kNoWorkerThread);
111 }
112
105 void PermissionDispatcher::revokePermission( 113 void PermissionDispatcher::revokePermission(
106 blink::WebPermissionType type, 114 blink::WebPermissionType type,
107 const blink::WebURL& origin, 115 const blink::WebURL& origin,
108 blink::WebPermissionCallback* callback) { 116 blink::WebPermissionCallback* callback) {
109 RevokePermissionInternal( 117 RevokePermissionInternal(
110 type, origin.string().utf8(), callback, kNoWorkerThread); 118 type, origin.string().utf8(), callback, kNoWorkerThread);
111 } 119 }
112 120
113 void PermissionDispatcher::startListening( 121 void PermissionDispatcher::startListening(
114 blink::WebPermissionType type, 122 blink::WebPermissionType type,
(...skipping 29 matching lines...) Expand all
144 } 152 }
145 153
146 void PermissionDispatcher::RequestPermissionForWorker( 154 void PermissionDispatcher::RequestPermissionForWorker(
147 blink::WebPermissionType type, 155 blink::WebPermissionType type,
148 const std::string& origin, 156 const std::string& origin,
149 blink::WebPermissionCallback* callback, 157 blink::WebPermissionCallback* callback,
150 int worker_thread_id) { 158 int worker_thread_id) {
151 RequestPermissionInternal(type, origin, callback, worker_thread_id); 159 RequestPermissionInternal(type, origin, callback, worker_thread_id);
152 } 160 }
153 161
162 void PermissionDispatcher::RequestPermissionsForWorker(
163 const blink::WebVector<blink::WebPermissionType>& types,
164 const std::string& origin,
165 blink::WebPermissionsCallback* callback,
166 int worker_thread_id) {
167 RequestPermissionsInternal(types, origin, callback, worker_thread_id);
168 }
169
154 void PermissionDispatcher::RevokePermissionForWorker( 170 void PermissionDispatcher::RevokePermissionForWorker(
155 blink::WebPermissionType type, 171 blink::WebPermissionType type,
156 const std::string& origin, 172 const std::string& origin,
157 blink::WebPermissionCallback* callback, 173 blink::WebPermissionCallback* callback,
158 int worker_thread_id) { 174 int worker_thread_id) {
159 RevokePermissionInternal(type, origin, callback, worker_thread_id); 175 RevokePermissionInternal(type, origin, callback, worker_thread_id);
160 } 176 }
161 177
162 void PermissionDispatcher::StartListeningForWorker( 178 void PermissionDispatcher::StartListeningForWorker(
163 blink::WebPermissionType type, 179 blink::WebPermissionType type,
(...skipping 24 matching lines...) Expand all
188 GetPermissionName(type), 204 GetPermissionName(type),
189 origin, 205 origin,
190 GetPermissionStatus(status), 206 GetPermissionStatus(status),
191 base::Bind(&PermissionDispatcher::OnPermissionChangedForWorker, 207 base::Bind(&PermissionDispatcher::OnPermissionChangedForWorker,
192 base::Unretained(this), 208 base::Unretained(this),
193 worker_thread_id, 209 worker_thread_id,
194 callback)); 210 callback));
195 } 211 }
196 212
197 // static 213 // static
198 void PermissionDispatcher::RunCallbackOnWorkerThread( 214 void PermissionDispatcher::RunPermissionCallbackOnWorkerThread(
199 scoped_ptr<blink::WebPermissionCallback> callback, 215 scoped_ptr<blink::WebPermissionCallback> callback,
200 blink::WebPermissionStatus status) { 216 blink::WebPermissionStatus status) {
201 callback->onSuccess(status); 217 callback->onSuccess(status);
202 } 218 }
203 219
220 void PermissionDispatcher::RunPermissionsCallbackOnWorkerThread(
221 scoped_ptr<blink::WebPermissionsCallback> callback,
222 scoped_ptr<blink::WebVector<blink::WebPermissionStatus>> statuses) {
223 callback->onSuccess(blink::adoptWebPtr(statuses.release()));
224 }
225
204 PermissionServicePtr& PermissionDispatcher::GetPermissionServicePtr() { 226 PermissionServicePtr& PermissionDispatcher::GetPermissionServicePtr() {
205 if (!permission_service_.get()) { 227 if (!permission_service_.get()) {
206 service_registry_->ConnectToRemoteService( 228 service_registry_->ConnectToRemoteService(
207 mojo::GetProxy(&permission_service_)); 229 mojo::GetProxy(&permission_service_));
208 } 230 }
209 return permission_service_; 231 return permission_service_;
210 } 232 }
211 233
212 void PermissionDispatcher::QueryPermissionInternal( 234 void PermissionDispatcher::QueryPermissionInternal(
213 blink::WebPermissionType type, 235 blink::WebPermissionType type,
214 const std::string& origin, 236 const std::string& origin,
215 blink::WebPermissionCallback* callback, 237 blink::WebPermissionCallback* callback,
216 int worker_thread_id) { 238 int worker_thread_id) {
217 // We need to save the |callback| in an ScopedPtrHashMap so if |this| gets 239 // We need to save the |callback| in an ScopedPtrHashMap so if |this| gets
218 // deleted, the callback will not leak. In the case of |this| gets deleted, 240 // deleted, the callback will not leak. In the case of |this| gets deleted,
219 // the |permission_service_| pipe will be destroyed too so OnQueryPermission 241 // the |permission_service_| pipe will be destroyed too so OnQueryPermission
220 // will not be called. 242 // will not be called.
221 uintptr_t callback_key = reinterpret_cast<uintptr_t>(callback); 243 uintptr_t callback_key = reinterpret_cast<uintptr_t>(callback);
222 pending_callbacks_.add(callback_key, 244 permission_callbacks_.add(callback_key,
223 scoped_ptr<blink::WebPermissionCallback>(callback)); 245 scoped_ptr<blink::WebPermissionCallback>(callback));
224 246
225 GetPermissionServicePtr()->HasPermission( 247 GetPermissionServicePtr()->HasPermission(
226 GetPermissionName(type), 248 GetPermissionName(type),
227 origin, 249 origin,
228 base::Bind(&PermissionDispatcher::OnPermissionResponse, 250 base::Bind(&PermissionDispatcher::OnPermissionResponse,
229 base::Unretained(this), 251 base::Unretained(this),
230 worker_thread_id, 252 worker_thread_id,
231 callback_key)); 253 callback_key));
232 } 254 }
233 255
234 void PermissionDispatcher::RequestPermissionInternal( 256 void PermissionDispatcher::RequestPermissionInternal(
235 blink::WebPermissionType type, 257 blink::WebPermissionType type,
236 const std::string& origin, 258 const std::string& origin,
237 blink::WebPermissionCallback* callback, 259 blink::WebPermissionCallback* callback,
238 int worker_thread_id) { 260 int worker_thread_id) {
239 // We need to save the |callback| in an ScopedPtrHashMap so if |this| gets 261 // We need to save the |callback| in an ScopedPtrHashMap so if |this| gets
240 // deleted, the callback will not leak. In the case of |this| gets deleted, 262 // deleted, the callback will not leak. In the case of |this| gets deleted,
241 // the |permission_service_| pipe will be destroyed too so OnQueryPermission 263 // the |permission_service_| pipe will be destroyed too so OnQueryPermission
242 // will not be called. 264 // will not be called.
243 uintptr_t callback_key = reinterpret_cast<uintptr_t>(callback); 265 uintptr_t callback_key = reinterpret_cast<uintptr_t>(callback);
244 pending_callbacks_.add(callback_key, 266 permission_callbacks_.add(callback_key,
245 scoped_ptr<blink::WebPermissionCallback>(callback)); 267 scoped_ptr<blink::WebPermissionCallback>(callback));
246 268
247 GetPermissionServicePtr()->RequestPermission( 269 GetPermissionServicePtr()->RequestPermission(
248 GetPermissionName(type), 270 GetPermissionName(type),
249 origin, 271 origin,
250 blink::WebUserGestureIndicator::isProcessingUserGesture(), 272 blink::WebUserGestureIndicator::isProcessingUserGesture(),
251 base::Bind(&PermissionDispatcher::OnPermissionResponse, 273 base::Bind(&PermissionDispatcher::OnPermissionResponse,
252 base::Unretained(this), 274 base::Unretained(this),
253 worker_thread_id, 275 worker_thread_id,
254 callback_key)); 276 callback_key));
255 } 277 }
256 278
279 void PermissionDispatcher::RequestPermissionsInternal(
280 const blink::WebVector<blink::WebPermissionType>& types,
281 const std::string& origin,
282 blink::WebPermissionsCallback* callback,
283 int worker_thread_id) {
284 // We need to save the |callback| in an ScopedVector so if |this| gets
285 // deleted, the callback will not leak. In the case of |this| gets deleted,
286 // the |permission_service_| pipe will be destroyed too so OnQueryPermission
287 // will not be called.
288 uintptr_t callback_key = reinterpret_cast<uintptr_t>(callback);
289 permissions_callbacks_.add(callback_key,
290 scoped_ptr<blink::WebPermissionsCallback>(callback));
291
292 mojo::Array<PermissionName> names(types.size());
293 for (size_t i = 0; i < types.size(); ++i)
294 names[i] = GetPermissionName(types[i]);
295
296 GetPermissionServicePtr()->RequestPermissions(
297 names.Pass(),
298 origin,
299 blink::WebUserGestureIndicator::isProcessingUserGesture(),
300 base::Bind(&PermissionDispatcher::OnRequestPermissionsResponse,
301 base::Unretained(this),
302 worker_thread_id,
303 callback_key));
304 }
305
257 void PermissionDispatcher::RevokePermissionInternal( 306 void PermissionDispatcher::RevokePermissionInternal(
258 blink::WebPermissionType type, 307 blink::WebPermissionType type,
259 const std::string& origin, 308 const std::string& origin,
260 blink::WebPermissionCallback* callback, 309 blink::WebPermissionCallback* callback,
261 int worker_thread_id) { 310 int worker_thread_id) {
262 // We need to save the |callback| in an ScopedPtrHashMap so if |this| gets 311 // We need to save the |callback| in an ScopedPtrHashMap so if |this| gets
263 // deleted, the callback will not leak. In the case of |this| gets deleted, 312 // deleted, the callback will not leak. In the case of |this| gets deleted,
264 // the |permission_service_| pipe will be destroyed too so OnQueryPermission 313 // the |permission_service_| pipe will be destroyed too so OnQueryPermission
265 // will not be called. 314 // will not be called.
266 uintptr_t callback_key = reinterpret_cast<uintptr_t>(callback); 315 uintptr_t callback_key = reinterpret_cast<uintptr_t>(callback);
267 pending_callbacks_.add(callback_key, 316 permission_callbacks_.add(callback_key,
268 scoped_ptr<blink::WebPermissionCallback>(callback)); 317 scoped_ptr<blink::WebPermissionCallback>(callback));
269 318
270 GetPermissionServicePtr()->RevokePermission( 319 GetPermissionServicePtr()->RevokePermission(
271 GetPermissionName(type), 320 GetPermissionName(type),
272 origin, 321 origin,
273 base::Bind(&PermissionDispatcher::OnPermissionResponse, 322 base::Bind(&PermissionDispatcher::OnPermissionResponse,
274 base::Unretained(this), 323 base::Unretained(this),
275 worker_thread_id, 324 worker_thread_id,
276 callback_key)); 325 callback_key));
277 } 326 }
278 327
279 void PermissionDispatcher::OnPermissionResponse( 328 void PermissionDispatcher::OnPermissionResponse(
280 int worker_thread_id, 329 int worker_thread_id,
281 uintptr_t callback_key, 330 uintptr_t callback_key,
282 PermissionStatus result) { 331 PermissionStatus result) {
283 scoped_ptr<blink::WebPermissionCallback> callback = 332 scoped_ptr<blink::WebPermissionCallback> callback =
284 pending_callbacks_.take_and_erase(callback_key); 333 permission_callbacks_.take_and_erase(callback_key);
285 blink::WebPermissionStatus status = GetWebPermissionStatus(result); 334 blink::WebPermissionStatus status = GetWebPermissionStatus(result);
286 335
287 if (worker_thread_id != kNoWorkerThread) { 336 if (worker_thread_id != kNoWorkerThread) {
288 // If the worker is no longer running, ::PostTask() will return false and 337 // If the worker is no longer running, ::PostTask() will return false and
289 // gracefully fail, destroying the callback too. 338 // gracefully fail, destroying the callback too.
290 WorkerTaskRunner::Instance()->PostTask( 339 WorkerTaskRunner::Instance()->PostTask(
291 worker_thread_id, 340 worker_thread_id,
292 base::Bind(&PermissionDispatcher::RunCallbackOnWorkerThread, 341 base::Bind(&PermissionDispatcher::RunPermissionCallbackOnWorkerThread,
293 base::Passed(&callback), status)); 342 base::Passed(&callback), status));
294 return; 343 return;
295 } 344 }
296 345
297 callback->onSuccess(status); 346 callback->onSuccess(status);
298 } 347 }
299 348
349 void PermissionDispatcher::OnRequestPermissionsResponse(
350 int worker_thread_id,
351 uintptr_t callback_key,
352 const mojo::Array<PermissionStatus>& result) {
353 scoped_ptr<blink::WebPermissionsCallback> callback =
354 permissions_callbacks_.take_and_erase(callback_key);
355 scoped_ptr<blink::WebVector<blink::WebPermissionStatus>> statuses(
356 new blink::WebVector<blink::WebPermissionStatus>(result.size()));
357
358 for (size_t i = 0; i < result.size(); i++)
359 statuses->operator[](i) = GetWebPermissionStatus(result[i]);
360
361 if (worker_thread_id != kNoWorkerThread) {
362 // If the worker is no longer running, ::PostTask() will return false and
363 // gracefully fail, destroying the callback too.
364 WorkerTaskRunner::Instance()->PostTask(
365 worker_thread_id,
366 base::Bind(&PermissionDispatcher::RunPermissionsCallbackOnWorkerThread,
367 base::Passed(&callback),
368 base::Passed(&statuses)));
369 return;
370 }
371
372 callback->onSuccess(blink::adoptWebPtr(statuses.release()));
373 }
374
300 void PermissionDispatcher::OnPermissionChanged( 375 void PermissionDispatcher::OnPermissionChanged(
301 blink::WebPermissionType type, 376 blink::WebPermissionType type,
302 const std::string& origin, 377 const std::string& origin,
303 WebPermissionObserver* observer, 378 WebPermissionObserver* observer,
304 PermissionStatus status) { 379 PermissionStatus status) {
305 if (!IsObserverRegistered(observer)) 380 if (!IsObserverRegistered(observer))
306 return; 381 return;
307 382
308 observer->permissionChanged(type, GetWebPermissionStatus(status)); 383 observer->permissionChanged(type, GetWebPermissionStatus(status));
309 384
(...skipping 20 matching lines...) Expand all
330 origin, 405 origin,
331 current_status, 406 current_status,
332 base::Bind(&PermissionDispatcher::OnPermissionChanged, 407 base::Bind(&PermissionDispatcher::OnPermissionChanged,
333 base::Unretained(this), 408 base::Unretained(this),
334 type, 409 type,
335 origin, 410 origin,
336 base::Unretained(observer))); 411 base::Unretained(observer)));
337 } 412 }
338 413
339 } // namespace content 414 } // namespace content
OLDNEW
« no previous file with comments | « content/child/permissions/permission_dispatcher.h ('k') | content/child/permissions/permission_dispatcher_thread_proxy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698