OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 Loading... | |
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, |
215 const std::string& origin, | 238 const std::string& origin, |
216 blink::WebPermissionCallback* callback, | 239 blink::WebPermissionCallback* callback, |
217 int worker_thread_id) { | 240 int worker_thread_id) { |
218 // We need to save the |callback| in an ScopedVector so if |this| gets | 241 // We need to save the |callback| in an ScopedVector so if |this| gets |
219 // deleted, the callback will not leak. In the case of |this| gets deleted, | 242 // deleted, the callback will not leak. In the case of |this| gets deleted, |
220 // the |permission_service_| pipe will be destroyed too so OnQueryPermission | 243 // the |permission_service_| pipe will be destroyed too so OnQueryPermission |
221 // will not be called. | 244 // will not be called. |
222 PendingCallbackVector::iterator iterator = | 245 PendingCallbackVector::iterator iterator = |
223 pending_callbacks_.insert(pending_callbacks_.end(), callback); | 246 pending_callbacks_.insert(pending_callbacks_.end(), callback); |
224 | |
mlamouri (slow - plz ping)
2015/08/21 10:24:41
nit: revert that change
Lalit Maganti
2015/08/25 16:58:27
Done.
| |
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 iterator)); | 253 iterator)); |
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 ScopedVector so if |this| gets | 261 // We need to save the |callback| in an ScopedVector 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 PendingCallbackVector::iterator iterator = | 265 PendingCallbackVector::iterator iterator = |
244 pending_callbacks_.insert(pending_callbacks_.end(), callback); | 266 pending_callbacks_.insert(pending_callbacks_.end(), callback); |
245 | |
mlamouri (slow - plz ping)
2015/08/21 10:24:41
ditto
Lalit Maganti
2015/08/25 16:58:27
Done.
| |
246 GetPermissionServicePtr()->RequestPermission( | 267 GetPermissionServicePtr()->RequestPermission( |
247 GetPermissionName(type), | 268 GetPermissionName(type), |
248 origin, | 269 origin, |
249 blink::WebUserGestureIndicator::isProcessingUserGesture(), | 270 blink::WebUserGestureIndicator::isProcessingUserGesture(), |
250 base::Bind(&PermissionDispatcher::OnPermissionResponse, | 271 base::Bind(&PermissionDispatcher::OnPermissionResponse, |
251 base::Unretained(this), | 272 base::Unretained(this), |
252 worker_thread_id, | 273 worker_thread_id, |
253 iterator)); | 274 iterator)); |
254 } | 275 } |
255 | 276 |
277 void PermissionDispatcher::RequestPermissionsInternal( | |
278 const blink::WebVector<blink::WebPermissionType>& types, | |
279 const std::string& origin, | |
280 blink::WebPermissionsCallback* callback, | |
281 int worker_thread_id) { | |
282 // We need to save the |callback| in an ScopedVector so if |this| gets | |
283 // deleted, the callback will not leak. In the case of |this| gets deleted, | |
284 // the |permission_service_| pipe will be destroyed too so OnQueryPermission | |
285 // will not be called. | |
286 PendingBatchCallbackVector::iterator iterator = | |
287 batch_pending_callbacks_.insert(batch_pending_callbacks_.end(), callback); | |
mlamouri (slow - plz ping)
2015/08/21 10:24:41
I guess this needs to be rebased.
Lalit Maganti
2015/08/25 16:58:27
Done.
| |
288 | |
289 mojo::Array<PermissionName> names(types.size()); | |
290 for (size_t i = 0; i < types.size(); ++i) | |
291 names[i] = GetPermissionName(types[i]); | |
292 | |
293 GetPermissionServicePtr()->RequestBatchPermission( | |
294 names.Pass(), | |
295 origin, | |
296 blink::WebUserGestureIndicator::isProcessingUserGesture(), | |
297 base::Bind(&PermissionDispatcher::OnPermissionsResponse, | |
298 base::Unretained(this), | |
299 worker_thread_id, | |
300 iterator)); | |
301 } | |
302 | |
256 void PermissionDispatcher::RevokePermissionInternal( | 303 void PermissionDispatcher::RevokePermissionInternal( |
257 blink::WebPermissionType type, | 304 blink::WebPermissionType type, |
258 const std::string& origin, | 305 const std::string& origin, |
259 blink::WebPermissionCallback* callback, | 306 blink::WebPermissionCallback* callback, |
260 int worker_thread_id) { | 307 int worker_thread_id) { |
261 // We need to save the |callback| in an ScopedVector so if |this| gets | 308 // We need to save the |callback| in an ScopedVector so if |this| gets |
262 // deleted, the callback will not leak. In the case of |this| gets deleted, | 309 // deleted, the callback will not leak. In the case of |this| gets deleted, |
263 // the |permission_service_| pipe will be destroyed too so OnQueryPermission | 310 // the |permission_service_| pipe will be destroyed too so OnQueryPermission |
264 // will not be called. | 311 // will not be called. |
265 PendingCallbackVector::iterator iterator = | 312 PendingCallbackVector::iterator iterator = |
266 pending_callbacks_.insert(pending_callbacks_.end(), callback); | 313 pending_callbacks_.insert(pending_callbacks_.end(), callback); |
267 | |
268 GetPermissionServicePtr()->RevokePermission( | 314 GetPermissionServicePtr()->RevokePermission( |
269 GetPermissionName(type), | 315 GetPermissionName(type), |
270 origin, | 316 origin, |
271 base::Bind(&PermissionDispatcher::OnPermissionResponse, | 317 base::Bind(&PermissionDispatcher::OnPermissionResponse, |
272 base::Unretained(this), | 318 base::Unretained(this), |
273 worker_thread_id, | 319 worker_thread_id, |
274 iterator)); | 320 iterator)); |
275 } | 321 } |
276 | 322 |
277 void PermissionDispatcher::OnPermissionResponse( | 323 void PermissionDispatcher::OnPermissionResponse( |
(...skipping 14 matching lines...) Expand all Loading... | |
292 base::Bind(&PermissionDispatcher::RunCallbackOnWorkerThread, | 338 base::Bind(&PermissionDispatcher::RunCallbackOnWorkerThread, |
293 base::Unretained(callback), | 339 base::Unretained(callback), |
294 base::Passed(&status))); | 340 base::Passed(&status))); |
295 return; | 341 return; |
296 } | 342 } |
297 | 343 |
298 callback->onSuccess(status.release()); | 344 callback->onSuccess(status.release()); |
299 pending_callbacks_.erase(callback_iterator); | 345 pending_callbacks_.erase(callback_iterator); |
300 } | 346 } |
301 | 347 |
348 void PermissionDispatcher::OnPermissionsResponse( | |
349 int worker_thread_id, | |
350 PendingBatchCallbackVector::iterator callback_iterator, | |
351 const mojo::Array<PermissionStatus>& result) { | |
352 blink::WebPermissionsCallback* callback = *callback_iterator; | |
353 scoped_ptr<blink::WebVector<blink::WebPermissionStatus>> statuses( | |
354 new blink::WebVector<blink::WebPermissionStatus>(result.size())); | |
355 | |
356 for (size_t i = 0; i < result.size(); i++) | |
357 statuses->operator[](i) = GetWebPermissionStatus(result[i]); | |
358 | |
359 if (worker_thread_id != kNoWorkerThread) { | |
360 batch_pending_callbacks_.weak_erase(callback_iterator); | |
361 | |
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::RunMultiCallbackOnWorkerThread, | |
367 base::Unretained(callback), | |
368 base::Passed(&statuses))); | |
369 return; | |
370 } | |
371 | |
372 callback->onSuccess(blink::adoptWebPtr(statuses.release())); | |
373 batch_pending_callbacks_.erase(callback_iterator); | |
374 } | |
375 | |
302 void PermissionDispatcher::OnPermissionChanged( | 376 void PermissionDispatcher::OnPermissionChanged( |
303 blink::WebPermissionType type, | 377 blink::WebPermissionType type, |
304 const std::string& origin, | 378 const std::string& origin, |
305 WebPermissionObserver* observer, | 379 WebPermissionObserver* observer, |
306 PermissionStatus status) { | 380 PermissionStatus status) { |
307 if (!IsObserverRegistered(observer)) | 381 if (!IsObserverRegistered(observer)) |
308 return; | 382 return; |
309 | 383 |
310 observer->permissionChanged(type, GetWebPermissionStatus(status)); | 384 observer->permissionChanged(type, GetWebPermissionStatus(status)); |
311 | 385 |
(...skipping 20 matching lines...) Expand all Loading... | |
332 origin, | 406 origin, |
333 current_status, | 407 current_status, |
334 base::Bind(&PermissionDispatcher::OnPermissionChanged, | 408 base::Bind(&PermissionDispatcher::OnPermissionChanged, |
335 base::Unretained(this), | 409 base::Unretained(this), |
336 type, | 410 type, |
337 origin, | 411 origin, |
338 base::Unretained(observer))); | 412 base::Unretained(observer))); |
339 } | 413 } |
340 | 414 |
341 } // namespace content | 415 } // namespace content |
OLD | NEW |