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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 } // anonymous namespace | 70 } // anonymous namespace |
71 | 71 |
72 // static | 72 // static |
73 bool PermissionDispatcher::IsObservable(blink::WebPermissionType type) { | 73 bool PermissionDispatcher::IsObservable(blink::WebPermissionType type) { |
74 return type == blink::WebPermissionTypeGeolocation || | 74 return type == blink::WebPermissionTypeGeolocation || |
75 type == blink::WebPermissionTypeNotifications || | 75 type == blink::WebPermissionTypeNotifications || |
76 type == blink::WebPermissionTypePushNotifications || | 76 type == blink::WebPermissionTypePushNotifications || |
77 type == blink::WebPermissionTypeMidiSysEx; | 77 type == blink::WebPermissionTypeMidiSysEx; |
78 } | 78 } |
79 | 79 |
80 PermissionDispatcher::CallbackInformation::CallbackInformation( | |
81 blink::WebPermissionCallback* callback, | |
82 int worker_thread_id) | |
83 : callback_(callback), | |
84 worker_thread_id_(worker_thread_id) { | |
85 } | |
86 | |
87 blink::WebPermissionCallback* | |
88 PermissionDispatcher::CallbackInformation::callback() const { | |
89 return callback_.get(); | |
90 } | |
91 | |
92 int PermissionDispatcher::CallbackInformation::worker_thread_id() const { | |
93 return worker_thread_id_; | |
94 } | |
95 | |
96 blink::WebPermissionCallback* | |
97 PermissionDispatcher::CallbackInformation::ReleaseCallback() { | |
98 return callback_.release(); | |
99 } | |
100 | |
101 PermissionDispatcher::CallbackInformation::~CallbackInformation() { | |
102 } | |
103 | |
104 PermissionDispatcher::PermissionDispatcher(ServiceRegistry* service_registry) | 80 PermissionDispatcher::PermissionDispatcher(ServiceRegistry* service_registry) |
105 : service_registry_(service_registry) { | 81 : service_registry_(service_registry) { |
106 } | 82 } |
107 | 83 |
108 PermissionDispatcher::~PermissionDispatcher() { | 84 PermissionDispatcher::~PermissionDispatcher() { |
109 } | 85 } |
110 | 86 |
111 void PermissionDispatcher::queryPermission( | 87 void PermissionDispatcher::queryPermission( |
112 blink::WebPermissionType type, | 88 blink::WebPermissionType type, |
113 const blink::WebURL& origin, | 89 const blink::WebURL& origin, |
114 blink::WebPermissionCallback* callback) { | 90 blink::WebPermissionCallback* callback) { |
115 QueryPermissionInternal( | 91 QueryPermissionInternal( |
116 type, origin.string().utf8(), callback, kNoWorkerThread); | 92 type, origin.string().utf8(), callback, kNoWorkerThread); |
117 } | 93 } |
118 | 94 |
119 void PermissionDispatcher::requestPermission( | 95 void PermissionDispatcher::requestPermission( |
120 blink::WebPermissionType type, | 96 blink::WebPermissionType type, |
121 const blink::WebURL& origin, | 97 const blink::WebURL& origin, |
122 blink::WebPermissionCallback* callback) { | 98 blink::WebPermissionCallback* callback) { |
123 RequestPermissionInternal( | 99 RequestPermissionInternal( |
124 type, origin.string().utf8(), callback, kNoWorkerThread); | 100 type, origin.string().utf8(), callback, kNoWorkerThread); |
125 } | 101 } |
126 | 102 |
103 void PermissionDispatcher::requestPermissions( | |
104 const blink::WebVector<blink::WebPermissionType>& types, | |
105 const blink::WebURL& origin, | |
106 blink::WebPermissionsCallback* callback) { | |
107 RequestPermissionsInternal( | |
108 types, origin.string().utf8(), callback, kNoWorkerThread); | |
109 } | |
110 | |
127 void PermissionDispatcher::revokePermission( | 111 void PermissionDispatcher::revokePermission( |
128 blink::WebPermissionType type, | 112 blink::WebPermissionType type, |
129 const blink::WebURL& origin, | 113 const blink::WebURL& origin, |
130 blink::WebPermissionCallback* callback) { | 114 blink::WebPermissionCallback* callback) { |
131 RevokePermissionInternal( | 115 RevokePermissionInternal( |
132 type, origin.string().utf8(), callback, kNoWorkerThread); | 116 type, origin.string().utf8(), callback, kNoWorkerThread); |
133 } | 117 } |
134 | 118 |
135 void PermissionDispatcher::startListening( | 119 void PermissionDispatcher::startListening( |
136 blink::WebPermissionType type, | 120 blink::WebPermissionType type, |
(...skipping 29 matching lines...) Expand all Loading... | |
166 } | 150 } |
167 | 151 |
168 void PermissionDispatcher::RequestPermissionForWorker( | 152 void PermissionDispatcher::RequestPermissionForWorker( |
169 blink::WebPermissionType type, | 153 blink::WebPermissionType type, |
170 const std::string& origin, | 154 const std::string& origin, |
171 blink::WebPermissionCallback* callback, | 155 blink::WebPermissionCallback* callback, |
172 int worker_thread_id) { | 156 int worker_thread_id) { |
173 RequestPermissionInternal(type, origin, callback, worker_thread_id); | 157 RequestPermissionInternal(type, origin, callback, worker_thread_id); |
174 } | 158 } |
175 | 159 |
160 void PermissionDispatcher::RequestPermissionsForWorker( | |
161 const blink::WebVector<blink::WebPermissionType>& types, | |
162 const std::string& origin, | |
163 blink::WebPermissionsCallback* callback, | |
164 int worker_thread_id) { | |
165 RequestPermissionsInternal(types, origin, callback, worker_thread_id); | |
166 } | |
167 | |
176 void PermissionDispatcher::RevokePermissionForWorker( | 168 void PermissionDispatcher::RevokePermissionForWorker( |
177 blink::WebPermissionType type, | 169 blink::WebPermissionType type, |
178 const std::string& origin, | 170 const std::string& origin, |
179 blink::WebPermissionCallback* callback, | 171 blink::WebPermissionCallback* callback, |
180 int worker_thread_id) { | 172 int worker_thread_id) { |
181 RevokePermissionInternal(type, origin, callback, worker_thread_id); | 173 RevokePermissionInternal(type, origin, callback, worker_thread_id); |
182 } | 174 } |
183 | 175 |
184 void PermissionDispatcher::StartListeningForWorker( | 176 void PermissionDispatcher::StartListeningForWorker( |
185 blink::WebPermissionType type, | 177 blink::WebPermissionType type, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 } | 209 } |
218 | 210 |
219 // static | 211 // static |
220 void PermissionDispatcher::RunCallbackOnWorkerThread( | 212 void PermissionDispatcher::RunCallbackOnWorkerThread( |
221 blink::WebPermissionCallback* callback, | 213 blink::WebPermissionCallback* callback, |
222 scoped_ptr<blink::WebPermissionStatus> status) { | 214 scoped_ptr<blink::WebPermissionStatus> status) { |
223 callback->onSuccess(status.release()); | 215 callback->onSuccess(status.release()); |
224 delete callback; | 216 delete callback; |
225 } | 217 } |
226 | 218 |
219 void PermissionDispatcher::RunMultiCallbackOnWorkerThread( | |
220 blink::WebPermissionsCallback* callback, | |
221 scoped_ptr<blink::WebVector<blink::WebPermissionStatus>> statuses) { | |
222 callback->onSuccess( | |
223 blink::adoptWebPtr<blink::WebVector<blink::WebPermissionStatus>>( | |
224 statuses.release())); | |
225 delete callback; | |
226 } | |
227 | |
227 PermissionServicePtr& PermissionDispatcher::GetPermissionServicePtr() { | 228 PermissionServicePtr& PermissionDispatcher::GetPermissionServicePtr() { |
228 if (!permission_service_.get()) { | 229 if (!permission_service_.get()) { |
229 service_registry_->ConnectToRemoteService( | 230 service_registry_->ConnectToRemoteService( |
230 mojo::GetProxy(&permission_service_)); | 231 mojo::GetProxy(&permission_service_)); |
231 } | 232 } |
232 return permission_service_; | 233 return permission_service_; |
233 } | 234 } |
234 | 235 |
235 void PermissionDispatcher::QueryPermissionInternal( | 236 void PermissionDispatcher::QueryPermissionInternal( |
236 blink::WebPermissionType type, | 237 blink::WebPermissionType type, |
237 const std::string& origin, | 238 const std::string& origin, |
238 blink::WebPermissionCallback* callback, | 239 blink::WebPermissionCallback* callback, |
239 int worker_thread_id) { | 240 int worker_thread_id) { |
240 // We need to save the |callback| in an IDMap so if |this| gets deleted, the | 241 // We need to save the |callback| in an IDMap so if |this| gets deleted, the |
241 // callback will not leak. In the case of |this| gets deleted, the | 242 // callback will not leak. In the case of |this| gets deleted, the |
242 // |permission_service_| pipe will be destroyed too so OnQueryPermission will | 243 // |permission_service_| pipe will be destroyed too so OnQueryPermission will |
243 // not be called. | 244 // not be called. |
244 int request_id = pending_callbacks_.Add( | 245 int request_id = pending_callbacks_.Add( |
245 new CallbackInformation(callback, worker_thread_id)); | 246 new CallbackInformation<blink::WebPermissionCallback>( |
247 callback, worker_thread_id)); | |
246 GetPermissionServicePtr()->HasPermission( | 248 GetPermissionServicePtr()->HasPermission( |
247 GetPermissionName(type), | 249 GetPermissionName(type), |
248 origin, | 250 origin, |
249 base::Bind(&PermissionDispatcher::OnPermissionResponse, | 251 base::Bind(&PermissionDispatcher::OnPermissionResponse, |
250 base::Unretained(this), | 252 base::Unretained(this), |
251 request_id)); | 253 request_id)); |
252 } | 254 } |
253 | 255 |
254 void PermissionDispatcher::RequestPermissionInternal( | 256 void PermissionDispatcher::RequestPermissionInternal( |
255 blink::WebPermissionType type, | 257 blink::WebPermissionType type, |
256 const std::string& origin, | 258 const std::string& origin, |
257 blink::WebPermissionCallback* callback, | 259 blink::WebPermissionCallback* callback, |
258 int worker_thread_id) { | 260 int worker_thread_id) { |
259 // We need to save the |callback| in an IDMap so if |this| gets deleted, the | 261 // We need to save the |callback| in an IDMap so if |this| gets deleted, the |
260 // callback will not leak. In the case of |this| gets deleted, the | 262 // callback will not leak. In the case of |this| gets deleted, the |
261 // |permission_service_| pipe will be destroyed too so OnQueryPermission will | 263 // |permission_service_| pipe will be destroyed too so OnQueryPermission will |
262 // not be called. | 264 // not be called. |
263 int request_id = pending_callbacks_.Add( | 265 int request_id = pending_callbacks_.Add( |
264 new CallbackInformation(callback, worker_thread_id)); | 266 new SingleCallbackInfo(callback, worker_thread_id)); |
265 GetPermissionServicePtr()->RequestPermission( | 267 GetPermissionServicePtr()->RequestPermission( |
266 GetPermissionName(type), | 268 GetPermissionName(type), |
267 origin, | 269 origin, |
268 blink::WebUserGestureIndicator::isProcessingUserGesture(), | 270 blink::WebUserGestureIndicator::isProcessingUserGesture(), |
269 base::Bind(&PermissionDispatcher::OnPermissionResponse, | 271 base::Bind(&PermissionDispatcher::OnPermissionResponse, |
270 base::Unretained(this), | 272 base::Unretained(this), |
271 request_id)); | 273 request_id)); |
272 } | 274 } |
273 | 275 |
276 void PermissionDispatcher::RequestPermissionsInternal( | |
277 const blink::WebVector<blink::WebPermissionType>& types, | |
278 const std::string& origin, | |
279 blink::WebPermissionsCallback* callback, | |
280 int worker_thread_id) { | |
281 // We need to save the |callback| in an IDMap so if |this| gets deleted, the | |
282 // callback will not leak. In the case of |this| gets deleted, the | |
283 // |permission_service_| pipe will be destroyed too so OnQueryPermission will | |
284 // not be called. | |
285 int request_id = multiple_pending_callbacks_.Add( | |
286 new MultiCallbackInfo(callback, worker_thread_id)); | |
287 | |
288 mojo::Array<PermissionName> names(types.size()); | |
289 for (size_t i = 0; i < types.size(); ++i) { | |
290 names[i] = GetPermissionName(types[i]); | |
291 } | |
mlamouri (slow - plz ping)
2015/08/18 13:37:15
nit: no { }
Lalit Maganti
2015/08/20 14:23:30
Done.
| |
292 | |
293 GetPermissionServicePtr()->RequestPermissions( | |
294 names.Pass(), | |
295 origin, | |
296 blink::WebUserGestureIndicator::isProcessingUserGesture(), | |
297 base::Bind(&PermissionDispatcher::OnPermissionsResponse, | |
298 base::Unretained(this), | |
299 request_id)); | |
300 } | |
301 | |
274 void PermissionDispatcher::RevokePermissionInternal( | 302 void PermissionDispatcher::RevokePermissionInternal( |
275 blink::WebPermissionType type, | 303 blink::WebPermissionType type, |
276 const std::string& origin, | 304 const std::string& origin, |
277 blink::WebPermissionCallback* callback, | 305 blink::WebPermissionCallback* callback, |
278 int worker_thread_id) { | 306 int worker_thread_id) { |
279 // We need to save the |callback| in an IDMap so if |this| gets deleted, the | 307 // We need to save the |callback| in an IDMap so if |this| gets deleted, the |
280 // callback will not leak. In the case of |this| gets deleted, the | 308 // callback will not leak. In the case of |this| gets deleted, the |
281 // |permission_service_| pipe will be destroyed too so OnQueryPermission will | 309 // |permission_service_| pipe will be destroyed too so OnQueryPermission will |
282 // not be called. | 310 // not be called. |
283 int request_id = pending_callbacks_.Add( | 311 int request_id = pending_callbacks_.Add( |
284 new CallbackInformation(callback, worker_thread_id)); | 312 new SingleCallbackInfo(callback, worker_thread_id)); |
285 GetPermissionServicePtr()->RevokePermission( | 313 GetPermissionServicePtr()->RevokePermission( |
286 GetPermissionName(type), | 314 GetPermissionName(type), |
287 origin, | 315 origin, |
288 base::Bind(&PermissionDispatcher::OnPermissionResponse, | 316 base::Bind(&PermissionDispatcher::OnPermissionResponse, |
289 base::Unretained(this), | 317 base::Unretained(this), |
290 request_id)); | 318 request_id)); |
291 } | 319 } |
292 | 320 |
293 void PermissionDispatcher::OnPermissionResponse(int request_id, | 321 void PermissionDispatcher::OnPermissionResponse(int request_id, |
294 PermissionStatus result) { | 322 PermissionStatus result) { |
295 CallbackInformation* callback_information = | 323 SingleCallbackInfo* callback_information = |
296 pending_callbacks_.Lookup(request_id); | 324 pending_callbacks_.Lookup(request_id); |
297 DCHECK(callback_information && callback_information->callback()); | 325 DCHECK(callback_information && callback_information->callback()); |
298 scoped_ptr<blink::WebPermissionStatus> status( | 326 scoped_ptr<blink::WebPermissionStatus> status( |
299 new blink::WebPermissionStatus(GetWebPermissionStatus(result))); | 327 new blink::WebPermissionStatus(GetWebPermissionStatus(result))); |
300 | 328 |
301 if (callback_information->worker_thread_id() != kNoWorkerThread) { | 329 if (callback_information->worker_thread_id() != kNoWorkerThread) { |
302 blink::WebPermissionCallback* callback = | 330 blink::WebPermissionCallback* callback = |
303 callback_information->ReleaseCallback(); | 331 callback_information->ReleaseCallback(); |
304 int worker_thread_id = callback_information->worker_thread_id(); | 332 int worker_thread_id = callback_information->worker_thread_id(); |
305 pending_callbacks_.Remove(request_id); | 333 pending_callbacks_.Remove(request_id); |
306 | 334 |
307 // If the worker is no longer running, ::PostTask() will return false and | 335 // If the worker is no longer running, ::PostTask() will return false and |
308 // gracefully fail, destroying the callback too. | 336 // gracefully fail, destroying the callback too. |
309 WorkerTaskRunner::Instance()->PostTask( | 337 WorkerTaskRunner::Instance()->PostTask( |
310 worker_thread_id, | 338 worker_thread_id, |
311 base::Bind(&PermissionDispatcher::RunCallbackOnWorkerThread, | 339 base::Bind(&PermissionDispatcher::RunCallbackOnWorkerThread, |
312 base::Unretained(callback), | 340 base::Unretained(callback), |
313 base::Passed(&status))); | 341 base::Passed(&status))); |
314 return; | 342 return; |
315 } | 343 } |
316 | 344 |
317 callback_information->callback()->onSuccess(status.release()); | 345 callback_information->callback()->onSuccess(status.release()); |
318 pending_callbacks_.Remove(request_id); | 346 pending_callbacks_.Remove(request_id); |
319 } | 347 } |
320 | 348 |
349 void PermissionDispatcher::OnPermissionsResponse( | |
350 int request_id, | |
351 const mojo::Array<PermissionStatus>& result) { | |
352 MultiCallbackInfo* callback_information = | |
353 multiple_pending_callbacks_.Lookup(request_id); | |
354 DCHECK(callback_information && callback_information->callback()); | |
355 | |
356 scoped_ptr<blink::WebVector<blink::WebPermissionStatus>> status( | |
mlamouri (slow - plz ping)
2015/08/18 13:37:15
nit: statuses
Lalit Maganti
2015/08/20 14:23:30
Done.
| |
357 new blink::WebVector<blink::WebPermissionStatus>(result.size())); | |
358 for (size_t i = 0; i < result.size(); i++) { | |
359 (*status)[i] = GetWebPermissionStatus(result[i]); | |
360 } | |
mlamouri (slow - plz ping)
2015/08/18 13:37:15
nit: remove { }
Lalit Maganti
2015/08/20 14:23:30
Done.
| |
361 | |
362 if (callback_information->worker_thread_id() != kNoWorkerThread) { | |
363 blink::WebPermissionsCallback* callback = | |
364 callback_information->ReleaseCallback(); | |
365 int worker_thread_id = callback_information->worker_thread_id(); | |
366 multiple_pending_callbacks_.Remove(request_id); | |
367 | |
368 // If the worker is no longer running, ::PostTask() will return false and | |
369 // gracefully fail, destroying the callback too. | |
370 WorkerTaskRunner::Instance()->PostTask( | |
371 worker_thread_id, | |
372 base::Bind(&PermissionDispatcher::RunMultiCallbackOnWorkerThread, | |
373 base::Unretained(callback), | |
374 base::Passed(&status))); | |
375 return; | |
376 } | |
377 | |
378 callback_information->callback()->onSuccess( | |
379 blink::adoptWebPtr<blink::WebVector<blink::WebPermissionStatus>>( | |
380 status.release())); | |
mlamouri (slow - plz ping)
2015/08/18 13:37:15
wouldn't status.release be enough?
Lalit Maganti
2015/08/20 14:23:30
Although that would make so much sense, WebPtrs do
| |
381 multiple_pending_callbacks_.Remove(request_id); | |
382 } | |
383 | |
321 void PermissionDispatcher::OnPermissionChanged( | 384 void PermissionDispatcher::OnPermissionChanged( |
322 blink::WebPermissionType type, | 385 blink::WebPermissionType type, |
323 const std::string& origin, | 386 const std::string& origin, |
324 WebPermissionObserver* observer, | 387 WebPermissionObserver* observer, |
325 PermissionStatus status) { | 388 PermissionStatus status) { |
326 if (!IsObserverRegistered(observer)) | 389 if (!IsObserverRegistered(observer)) |
327 return; | 390 return; |
328 | 391 |
329 observer->permissionChanged(type, GetWebPermissionStatus(status)); | 392 observer->permissionChanged(type, GetWebPermissionStatus(status)); |
330 | 393 |
(...skipping 20 matching lines...) Expand all Loading... | |
351 origin, | 414 origin, |
352 current_status, | 415 current_status, |
353 base::Bind(&PermissionDispatcher::OnPermissionChanged, | 416 base::Bind(&PermissionDispatcher::OnPermissionChanged, |
354 base::Unretained(this), | 417 base::Unretained(this), |
355 type, | 418 type, |
356 origin, | 419 origin, |
357 base::Unretained(observer))); | 420 base::Unretained(observer))); |
358 } | 421 } |
359 | 422 |
360 } // namespace content | 423 } // namespace content |
OLD | NEW |