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

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: Fix review comments Created 5 years, 4 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 } 211 }
196 212
197 // static 213 // static
198 void PermissionDispatcher::RunCallbackOnWorkerThread( 214 void PermissionDispatcher::RunCallbackOnWorkerThread(
199 blink::WebPermissionCallback* callback, 215 blink::WebPermissionCallback* callback,
200 scoped_ptr<blink::WebPermissionStatus> status) { 216 scoped_ptr<blink::WebPermissionStatus> status) {
201 callback->onSuccess(status.release()); 217 callback->onSuccess(status.release());
202 delete callback; 218 delete callback;
203 } 219 }
204 220
221 void PermissionDispatcher::RunMultiCallbackOnWorkerThread(
222 blink::WebPermissionsCallback* callback,
223 scoped_ptr<blink::WebVector<blink::WebPermissionStatus>> statuses) {
224 callback->onSuccess(blink::adoptWebPtr(statuses.release()));
225 delete callback;
226 }
227
205 PermissionServicePtr& PermissionDispatcher::GetPermissionServicePtr() { 228 PermissionServicePtr& PermissionDispatcher::GetPermissionServicePtr() {
206 if (!permission_service_.get()) { 229 if (!permission_service_.get()) {
207 service_registry_->ConnectToRemoteService( 230 service_registry_->ConnectToRemoteService(
208 mojo::GetProxy(&permission_service_)); 231 mojo::GetProxy(&permission_service_));
209 } 232 }
210 return permission_service_; 233 return permission_service_;
211 } 234 }
212 235
213 void PermissionDispatcher::QueryPermissionInternal( 236 void PermissionDispatcher::QueryPermissionInternal(
214 blink::WebPermissionType type, 237 blink::WebPermissionType type,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 GetPermissionServicePtr()->RequestPermission( 271 GetPermissionServicePtr()->RequestPermission(
249 GetPermissionName(type), 272 GetPermissionName(type),
250 origin, 273 origin,
251 blink::WebUserGestureIndicator::isProcessingUserGesture(), 274 blink::WebUserGestureIndicator::isProcessingUserGesture(),
252 base::Bind(&PermissionDispatcher::OnPermissionResponse, 275 base::Bind(&PermissionDispatcher::OnPermissionResponse,
253 base::Unretained(this), 276 base::Unretained(this),
254 worker_thread_id, 277 worker_thread_id,
255 callback_key)); 278 callback_key));
256 } 279 }
257 280
281 void PermissionDispatcher::RequestPermissionsInternal(
282 const blink::WebVector<blink::WebPermissionType>& types,
283 const std::string& origin,
284 blink::WebPermissionsCallback* callback,
285 int worker_thread_id) {
286 // We need to save the |callback| in an ScopedVector so if |this| gets
287 // deleted, the callback will not leak. In the case of |this| gets deleted,
288 // the |permission_service_| pipe will be destroyed too so OnQueryPermission
289 // will not be called.
290 uintptr_t callback_key = reinterpret_cast<uintptr_t>(callback);
291 batch_pending_callbacks_.add(callback_key,
292 scoped_ptr<blink::WebPermissionsCallback>(callback));
293
294 mojo::Array<PermissionName> names(types.size());
295 for (size_t i = 0; i < types.size(); ++i)
296 names[i] = GetPermissionName(types[i]);
297
298 GetPermissionServicePtr()->RequestBatchPermission(
299 names.Pass(),
300 origin,
301 blink::WebUserGestureIndicator::isProcessingUserGesture(),
302 base::Bind(&PermissionDispatcher::OnPermissionsResponse,
303 base::Unretained(this),
304 worker_thread_id,
305 callback_key));
306 }
307
258 void PermissionDispatcher::RevokePermissionInternal( 308 void PermissionDispatcher::RevokePermissionInternal(
259 blink::WebPermissionType type, 309 blink::WebPermissionType type,
260 const std::string& origin, 310 const std::string& origin,
261 blink::WebPermissionCallback* callback, 311 blink::WebPermissionCallback* callback,
262 int worker_thread_id) { 312 int worker_thread_id) {
263 // We need to save the |callback| in an ScopedPtrHashMap so if |this| gets 313 // We need to save the |callback| in an ScopedPtrHashMap so if |this| gets
264 // deleted, the callback will not leak. In the case of |this| gets deleted, 314 // deleted, the callback will not leak. In the case of |this| gets deleted,
265 // the |permission_service_| pipe will be destroyed too so OnQueryPermission 315 // the |permission_service_| pipe will be destroyed too so OnQueryPermission
266 // will not be called. 316 // will not be called.
267 uintptr_t callback_key = reinterpret_cast<uintptr_t>(callback); 317 uintptr_t callback_key = reinterpret_cast<uintptr_t>(callback);
(...skipping 28 matching lines...) Expand all
296 base::Bind(&PermissionDispatcher::RunCallbackOnWorkerThread, 346 base::Bind(&PermissionDispatcher::RunCallbackOnWorkerThread,
297 base::Unretained(callback), 347 base::Unretained(callback),
298 base::Passed(&status))); 348 base::Passed(&status)));
299 return; 349 return;
300 } 350 }
301 351
302 callback->onSuccess(status.release()); 352 callback->onSuccess(status.release());
303 pending_callbacks_.erase(callback_key); 353 pending_callbacks_.erase(callback_key);
304 } 354 }
305 355
356 void PermissionDispatcher::OnPermissionsResponse(
357 int worker_thread_id,
358 uintptr_t callback_key,
359 const mojo::Array<PermissionStatus>& result) {
360 blink::WebPermissionsCallback* callback =
361 batch_pending_callbacks_.get(callback_key);
362 scoped_ptr<blink::WebVector<blink::WebPermissionStatus>> statuses(
363 new blink::WebVector<blink::WebPermissionStatus>(result.size()));
364
365 for (size_t i = 0; i < result.size(); i++)
366 statuses->operator[](i) = GetWebPermissionStatus(result[i]);
367
368 if (worker_thread_id != kNoWorkerThread) {
369 batch_pending_callbacks_.take_and_erase(callback_key);
370
371 // If the worker is no longer running, ::PostTask() will return false and
372 // gracefully fail, destroying the callback too.
373 WorkerTaskRunner::Instance()->PostTask(
374 worker_thread_id,
375 base::Bind(&PermissionDispatcher::RunMultiCallbackOnWorkerThread,
376 base::Unretained(callback),
377 base::Passed(&statuses)));
378 return;
379 }
380
381 callback->onSuccess(blink::adoptWebPtr(statuses.release()));
382 batch_pending_callbacks_.erase(callback_key);
383 }
384
306 void PermissionDispatcher::OnPermissionChanged( 385 void PermissionDispatcher::OnPermissionChanged(
307 blink::WebPermissionType type, 386 blink::WebPermissionType type,
308 const std::string& origin, 387 const std::string& origin,
309 WebPermissionObserver* observer, 388 WebPermissionObserver* observer,
310 PermissionStatus status) { 389 PermissionStatus status) {
311 if (!IsObserverRegistered(observer)) 390 if (!IsObserverRegistered(observer))
312 return; 391 return;
313 392
314 observer->permissionChanged(type, GetWebPermissionStatus(status)); 393 observer->permissionChanged(type, GetWebPermissionStatus(status));
315 394
(...skipping 20 matching lines...) Expand all
336 origin, 415 origin,
337 current_status, 416 current_status,
338 base::Bind(&PermissionDispatcher::OnPermissionChanged, 417 base::Bind(&PermissionDispatcher::OnPermissionChanged,
339 base::Unretained(this), 418 base::Unretained(this),
340 type, 419 type,
341 origin, 420 origin,
342 base::Unretained(observer))); 421 base::Unretained(observer)));
343 } 422 }
344 423
345 } // namespace content 424 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698