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

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: Merge patches 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 } 92 }
93 93
94 blink::WebPermissionCallback* 94 blink::WebPermissionCallback*
95 PermissionDispatcher::CallbackInformation::ReleaseCallback() { 95 PermissionDispatcher::CallbackInformation::ReleaseCallback() {
96 return callback_.release(); 96 return callback_.release();
97 } 97 }
98 98
99 PermissionDispatcher::CallbackInformation::~CallbackInformation() { 99 PermissionDispatcher::CallbackInformation::~CallbackInformation() {
100 } 100 }
101 101
102 PermissionDispatcher::MultipleCallbackInformation::MultipleCallbackInformation(
Lalit Maganti (personal) 2015/08/03 23:10:26 Consider merging this with the Callback informatio
103 blink::WebPermissionsCallback* callback,
104 int worker_thread_id)
105 : callback_(callback),
106 worker_thread_id_(worker_thread_id) {
107 }
108
109 blink::WebPermissionsCallback*
110 PermissionDispatcher::MultipleCallbackInformation::callback() const {
111 return callback_.get();
112 }
113
114 int
115 PermissionDispatcher::MultipleCallbackInformation::worker_thread_id() const {
116 return worker_thread_id_;
117 }
118
119 blink::WebPermissionsCallback*
120 PermissionDispatcher::MultipleCallbackInformation::ReleaseCallback() {
121 return callback_.release();
122 }
123
124 PermissionDispatcher::
125 MultipleCallbackInformation::~MultipleCallbackInformation() {
126 }
127
102 PermissionDispatcher::PermissionDispatcher(ServiceRegistry* service_registry) 128 PermissionDispatcher::PermissionDispatcher(ServiceRegistry* service_registry)
103 : service_registry_(service_registry) { 129 : service_registry_(service_registry) {
104 } 130 }
105 131
106 PermissionDispatcher::~PermissionDispatcher() { 132 PermissionDispatcher::~PermissionDispatcher() {
107 } 133 }
108 134
109 void PermissionDispatcher::queryPermission( 135 void PermissionDispatcher::queryPermission(
110 blink::WebPermissionType type, 136 blink::WebPermissionType type,
111 const blink::WebURL& origin, 137 const blink::WebURL& origin,
112 blink::WebPermissionCallback* callback) { 138 blink::WebPermissionCallback* callback) {
113 QueryPermissionInternal( 139 QueryPermissionInternal(
114 type, origin.string().utf8(), callback, kNoWorkerThread); 140 type, origin.string().utf8(), callback, kNoWorkerThread);
115 } 141 }
116 142
117 void PermissionDispatcher::requestPermission( 143 void PermissionDispatcher::requestPermission(
118 blink::WebPermissionType type, 144 blink::WebPermissionType type,
119 const blink::WebURL& origin, 145 const blink::WebURL& origin,
120 blink::WebPermissionCallback* callback) { 146 blink::WebPermissionCallback* callback) {
121 RequestPermissionInternal( 147 RequestPermissionInternal(
122 type, origin.string().utf8(), callback, kNoWorkerThread); 148 type, origin.string().utf8(), callback, kNoWorkerThread);
123 } 149 }
124 150
151 void PermissionDispatcher::requestPermissions(
152 const blink::WebVector<blink::WebPermissionType>& types,
153 const blink::WebURL& origin,
154 blink::WebPermissionsCallback* callback) {
155 RequestPermissionsInternal(
156 types, origin.string().utf8(), callback, kNoWorkerThread);
157 }
158
125 void PermissionDispatcher::revokePermission( 159 void PermissionDispatcher::revokePermission(
126 blink::WebPermissionType type, 160 blink::WebPermissionType type,
127 const blink::WebURL& origin, 161 const blink::WebURL& origin,
128 blink::WebPermissionCallback* callback) { 162 blink::WebPermissionCallback* callback) {
129 RevokePermissionInternal( 163 RevokePermissionInternal(
130 type, origin.string().utf8(), callback, kNoWorkerThread); 164 type, origin.string().utf8(), callback, kNoWorkerThread);
131 } 165 }
132 166
133 void PermissionDispatcher::startListening( 167 void PermissionDispatcher::startListening(
134 blink::WebPermissionType type, 168 blink::WebPermissionType type,
(...skipping 29 matching lines...) Expand all
164 } 198 }
165 199
166 void PermissionDispatcher::RequestPermissionForWorker( 200 void PermissionDispatcher::RequestPermissionForWorker(
167 blink::WebPermissionType type, 201 blink::WebPermissionType type,
168 const std::string& origin, 202 const std::string& origin,
169 blink::WebPermissionCallback* callback, 203 blink::WebPermissionCallback* callback,
170 int worker_thread_id) { 204 int worker_thread_id) {
171 RequestPermissionInternal(type, origin, callback, worker_thread_id); 205 RequestPermissionInternal(type, origin, callback, worker_thread_id);
172 } 206 }
173 207
208 void PermissionDispatcher::RequestPermissionsForWorker(
209 const blink::WebVector<blink::WebPermissionType>& types,
210 const std::string& origin,
211 blink::WebPermissionsCallback* callback,
212 int worker_thread_id) {
213 RequestPermissionsInternal(types, origin, callback, worker_thread_id);
214 }
215
174 void PermissionDispatcher::RevokePermissionForWorker( 216 void PermissionDispatcher::RevokePermissionForWorker(
175 blink::WebPermissionType type, 217 blink::WebPermissionType type,
176 const std::string& origin, 218 const std::string& origin,
177 blink::WebPermissionCallback* callback, 219 blink::WebPermissionCallback* callback,
178 int worker_thread_id) { 220 int worker_thread_id) {
179 RevokePermissionInternal(type, origin, callback, worker_thread_id); 221 RevokePermissionInternal(type, origin, callback, worker_thread_id);
180 } 222 }
181 223
182 void PermissionDispatcher::StartListeningForWorker( 224 void PermissionDispatcher::StartListeningForWorker(
183 blink::WebPermissionType type, 225 blink::WebPermissionType type,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 } 257 }
216 258
217 // static 259 // static
218 void PermissionDispatcher::RunCallbackOnWorkerThread( 260 void PermissionDispatcher::RunCallbackOnWorkerThread(
219 blink::WebPermissionCallback* callback, 261 blink::WebPermissionCallback* callback,
220 scoped_ptr<blink::WebPermissionStatus> status) { 262 scoped_ptr<blink::WebPermissionStatus> status) {
221 callback->onSuccess(status.release()); 263 callback->onSuccess(status.release());
222 delete callback; 264 delete callback;
223 } 265 }
224 266
267 void PermissionDispatcher::RunMultiCallbackOnWorkerThread(
Lalit Maganti (personal) 2015/08/03 23:10:26 Template method?
268 blink::WebPermissionsCallback* callback,
269 scoped_ptr<blink::WebVector<blink::WebPermissionStatus>> statuses) {
270 callback->onSuccess(statuses.release());
271 delete callback;
272 }
273
225 PermissionServicePtr& PermissionDispatcher::GetPermissionServicePtr() { 274 PermissionServicePtr& PermissionDispatcher::GetPermissionServicePtr() {
226 if (!permission_service_.get()) { 275 if (!permission_service_.get()) {
227 service_registry_->ConnectToRemoteService( 276 service_registry_->ConnectToRemoteService(
228 mojo::GetProxy(&permission_service_)); 277 mojo::GetProxy(&permission_service_));
229 } 278 }
230 return permission_service_; 279 return permission_service_;
231 } 280 }
232 281
233 void PermissionDispatcher::QueryPermissionInternal( 282 void PermissionDispatcher::QueryPermissionInternal(
234 blink::WebPermissionType type, 283 blink::WebPermissionType type,
(...skipping 27 matching lines...) Expand all
262 new CallbackInformation(callback, worker_thread_id)); 311 new CallbackInformation(callback, worker_thread_id));
263 GetPermissionServicePtr()->RequestPermission( 312 GetPermissionServicePtr()->RequestPermission(
264 GetPermissionName(type), 313 GetPermissionName(type),
265 origin, 314 origin,
266 blink::WebUserGestureIndicator::isProcessingUserGesture(), 315 blink::WebUserGestureIndicator::isProcessingUserGesture(),
267 base::Bind(&PermissionDispatcher::OnPermissionResponse, 316 base::Bind(&PermissionDispatcher::OnPermissionResponse,
268 base::Unretained(this), 317 base::Unretained(this),
269 request_id)); 318 request_id));
270 } 319 }
271 320
321 void PermissionDispatcher::RequestPermissionsInternal(
322 const blink::WebVector<blink::WebPermissionType>& types,
323 const std::string& origin,
324 blink::WebPermissionsCallback* callback,
325 int worker_thread_id) {
326 // We need to save the |callback| in an IDMap so if |this| gets deleted, the
327 // callback will not leak. In the case of |this| gets deleted, the
328 // |permission_service_| pipe will be destroyed too so OnQueryPermission will
329 // not be called.
330 int request_id = multiple_pending_callbacks_.Add(
331 new MultipleCallbackInformation(callback, worker_thread_id));
332
333 mojo::Array<PermissionName> names(types.size());
334 for (size_t i = 0; i < types.size(); ++i) {
335 names[i] = GetPermissionName(types[i]);
336 }
337
338 GetPermissionServicePtr()->RequestPermissions(
339 names.Pass(),
340 origin,
341 blink::WebUserGestureIndicator::isProcessingUserGesture(),
342 base::Bind(&PermissionDispatcher::OnPermissionsResponse,
343 base::Unretained(this),
344 request_id));
345 }
346
272 void PermissionDispatcher::RevokePermissionInternal( 347 void PermissionDispatcher::RevokePermissionInternal(
273 blink::WebPermissionType type, 348 blink::WebPermissionType type,
274 const std::string& origin, 349 const std::string& origin,
275 blink::WebPermissionCallback* callback, 350 blink::WebPermissionCallback* callback,
276 int worker_thread_id) { 351 int worker_thread_id) {
277 // We need to save the |callback| in an IDMap so if |this| gets deleted, the 352 // We need to save the |callback| in an IDMap so if |this| gets deleted, the
278 // callback will not leak. In the case of |this| gets deleted, the 353 // callback will not leak. In the case of |this| gets deleted, the
279 // |permission_service_| pipe will be destroyed too so OnQueryPermission will 354 // |permission_service_| pipe will be destroyed too so OnQueryPermission will
280 // not be called. 355 // not be called.
281 int request_id = pending_callbacks_.Add( 356 int request_id = pending_callbacks_.Add(
(...skipping 27 matching lines...) Expand all
309 base::Bind(&PermissionDispatcher::RunCallbackOnWorkerThread, 384 base::Bind(&PermissionDispatcher::RunCallbackOnWorkerThread,
310 base::Unretained(callback), 385 base::Unretained(callback),
311 base::Passed(&status))); 386 base::Passed(&status)));
312 return; 387 return;
313 } 388 }
314 389
315 callback_information->callback()->onSuccess(status.release()); 390 callback_information->callback()->onSuccess(status.release());
316 pending_callbacks_.Remove(request_id); 391 pending_callbacks_.Remove(request_id);
317 } 392 }
318 393
394 void PermissionDispatcher::OnPermissionsResponse(
395 int request_id,
396 const mojo::Array<PermissionStatus>& result) {
397 MultipleCallbackInformation* callback_information =
398 multiple_pending_callbacks_.Lookup(request_id);
399 DCHECK(callback_information && callback_information->callback());
400
401 scoped_ptr<blink::WebVector<blink::WebPermissionStatus>> status(
402 new blink::WebVector<blink::WebPermissionStatus>(result.size()));
403 for (size_t i = 0; i < result.size(); i++) {
404 (*status)[i] = GetWebPermissionStatus(result[i]);
405 }
406
407 if (callback_information->worker_thread_id() != kNoWorkerThread) {
408 blink::WebPermissionsCallback* callback =
409 callback_information->ReleaseCallback();
410 int worker_thread_id = callback_information->worker_thread_id();
411 multiple_pending_callbacks_.Remove(request_id);
412
413 // If the worker is no longer running, ::PostTask() will return false and
414 // gracefully fail, destroying the callback too.
415 WorkerTaskRunner::Instance()->PostTask(
416 worker_thread_id,
417 base::Bind(&PermissionDispatcher::RunMultiCallbackOnWorkerThread,
418 base::Unretained(callback),
419 base::Passed(&status)));
420 return;
421 }
422
423 callback_information->callback()->onSuccess(status.release());
424 multiple_pending_callbacks_.Remove(request_id);
425 }
426
319 void PermissionDispatcher::OnPermissionChanged( 427 void PermissionDispatcher::OnPermissionChanged(
320 blink::WebPermissionType type, 428 blink::WebPermissionType type,
321 const std::string& origin, 429 const std::string& origin,
322 WebPermissionObserver* observer, 430 WebPermissionObserver* observer,
323 PermissionStatus status) { 431 PermissionStatus status) {
324 if (!IsObserverRegistered(observer)) 432 if (!IsObserverRegistered(observer))
325 return; 433 return;
326 434
327 observer->permissionChanged(type, GetWebPermissionStatus(status)); 435 observer->permissionChanged(type, GetWebPermissionStatus(status));
328 436
(...skipping 20 matching lines...) Expand all
349 origin, 457 origin,
350 current_status, 458 current_status,
351 base::Bind(&PermissionDispatcher::OnPermissionChanged, 459 base::Bind(&PermissionDispatcher::OnPermissionChanged,
352 base::Unretained(this), 460 base::Unretained(this),
353 type, 461 type,
354 origin, 462 origin,
355 base::Unretained(observer))); 463 base::Unretained(observer)));
356 } 464 }
357 465
358 } // namespace content 466 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698