Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/browser/push_messaging/push_messaging_manager.h" | 5 #include "content/browser/push_messaging/push_messaging_manager.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 | 108 |
| 109 } // namespace | 109 } // namespace |
| 110 | 110 |
| 111 struct PushMessagingManager::RegisterData { | 111 struct PushMessagingManager::RegisterData { |
| 112 RegisterData(); | 112 RegisterData(); |
| 113 RegisterData(const RegisterData& other) = default; | 113 RegisterData(const RegisterData& other) = default; |
| 114 bool FromDocument() const; | 114 bool FromDocument() const; |
| 115 GURL requesting_origin; | 115 GURL requesting_origin; |
| 116 int64_t service_worker_registration_id; | 116 int64_t service_worker_registration_id; |
| 117 PushSubscriptionOptions options; | 117 PushSubscriptionOptions options; |
| 118 SubscribeCallback callback; | |
|
Peter Beverloo
2017/02/23 13:37:39
This will be copied, but since the callback's bind
ke.he
2017/02/24 03:24:30
Thanks!
| |
| 118 // The following member should only be read if FromDocument() is true. | 119 // The following member should only be read if FromDocument() is true. |
| 119 int render_frame_id; | 120 int render_frame_id; |
| 120 }; | 121 }; |
| 121 | 122 |
| 122 // Inner core of the PushMessagingManager which lives on the UI thread. | 123 // Inner core of the PushMessagingManager which lives on the UI thread. |
| 123 class PushMessagingManager::Core { | 124 class PushMessagingManager::Core { |
| 124 public: | 125 public: |
| 125 Core(const base::WeakPtr<PushMessagingManager>& io_parent, | 126 Core(const base::WeakPtr<PushMessagingManager>& io_parent, |
| 126 int render_process_id); | 127 int render_process_id); |
| 127 | 128 |
| 128 // Public Register methods on UI thread -------------------------------------- | 129 // Public Register methods on UI thread -------------------------------------- |
| 129 | 130 |
| 130 // Called via PostTask from IO thread. | 131 // Called via PostTask from IO thread. |
| 131 void RegisterOnUI(const SubscribeCallback& callback, | 132 void RegisterOnUI(const RegisterData& data); |
| 132 const RegisterData& data); | |
| 133 | 133 |
| 134 // Public Unregister methods on UI thread ------------------------------------ | 134 // Public Unregister methods on UI thread ------------------------------------ |
| 135 | 135 |
| 136 // Called via PostTask from IO thread. | 136 // Called via PostTask from IO thread. |
| 137 void UnregisterFromService(const UnsubscribeCallback& callback, | 137 void UnregisterFromService(const UnsubscribeCallback& callback, |
| 138 int64_t service_worker_registration_id, | 138 int64_t service_worker_registration_id, |
| 139 const GURL& requesting_origin, | 139 const GURL& requesting_origin, |
| 140 const std::string& sender_id); | 140 const std::string& sender_id); |
| 141 | 141 |
| 142 // Public GetPermission methods on UI thread --------------------------------- | 142 // Public GetPermission methods on UI thread --------------------------------- |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 163 PushMessagingService* service(); | 163 PushMessagingService* service(); |
| 164 | 164 |
| 165 private: | 165 private: |
| 166 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | 166 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |
| 167 friend class base::DeleteHelper<Core>; | 167 friend class base::DeleteHelper<Core>; |
| 168 | 168 |
| 169 ~Core(); | 169 ~Core(); |
| 170 | 170 |
| 171 // Private Register methods on UI thread ------------------------------------- | 171 // Private Register methods on UI thread ------------------------------------- |
| 172 | 172 |
| 173 void DidRequestPermissionInIncognito(const SubscribeCallback& callback, | 173 void DidRequestPermissionInIncognito(const RegisterData& data, |
| 174 const RegisterData& data, | |
| 175 blink::mojom::PermissionStatus status); | 174 blink::mojom::PermissionStatus status); |
| 176 | 175 |
| 177 void DidRegister(const SubscribeCallback& callback, | 176 void DidRegister(const RegisterData& data, |
| 178 const RegisterData& data, | |
| 179 const std::string& push_registration_id, | 177 const std::string& push_registration_id, |
| 180 const std::vector<uint8_t>& p256dh, | 178 const std::vector<uint8_t>& p256dh, |
| 181 const std::vector<uint8_t>& auth, | 179 const std::vector<uint8_t>& auth, |
| 182 PushRegistrationStatus status); | 180 PushRegistrationStatus status); |
| 183 | 181 |
| 184 // Private Unregister methods on UI thread ----------------------------------- | 182 // Private Unregister methods on UI thread ----------------------------------- |
| 185 | 183 |
| 186 void DidUnregisterFromService(const UnsubscribeCallback& callback, | 184 void DidUnregisterFromService(const UnsubscribeCallback& callback, |
| 187 int64_t service_worker_registration_id, | 185 int64_t service_worker_registration_id, |
| 188 PushUnregistrationStatus unregistration_status); | 186 PushUnregistrationStatus unregistration_status); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 const PushSubscriptionOptions& options, | 257 const PushSubscriptionOptions& options, |
| 260 const SubscribeCallback& callback) { | 258 const SubscribeCallback& callback) { |
| 261 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 259 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 262 // TODO(mvanouwerkerk): Validate arguments? | 260 // TODO(mvanouwerkerk): Validate arguments? |
| 263 RegisterData data; | 261 RegisterData data; |
| 264 | 262 |
| 265 // Will be ChildProcessHost::kInvalidUniqueID in requests from Service Worker. | 263 // Will be ChildProcessHost::kInvalidUniqueID in requests from Service Worker. |
| 266 data.render_frame_id = render_frame_id; | 264 data.render_frame_id = render_frame_id; |
| 267 | 265 |
| 268 data.service_worker_registration_id = service_worker_registration_id; | 266 data.service_worker_registration_id = service_worker_registration_id; |
| 267 data.callback = callback; | |
| 269 data.options = options; | 268 data.options = options; |
| 270 | 269 |
| 271 ServiceWorkerRegistration* service_worker_registration = | 270 ServiceWorkerRegistration* service_worker_registration = |
| 272 service_worker_context_->GetLiveRegistration( | 271 service_worker_context_->GetLiveRegistration( |
| 273 data.service_worker_registration_id); | 272 data.service_worker_registration_id); |
| 274 if (!service_worker_registration || | 273 if (!service_worker_registration || |
| 275 !service_worker_registration->active_version()) { | 274 !service_worker_registration->active_version()) { |
| 276 SendSubscriptionError(callback, data, | 275 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_NO_SERVICE_WORKER); |
| 277 PUSH_REGISTRATION_STATUS_NO_SERVICE_WORKER); | |
| 278 return; | 276 return; |
| 279 } | 277 } |
| 280 data.requesting_origin = service_worker_registration->pattern().GetOrigin(); | 278 data.requesting_origin = service_worker_registration->pattern().GetOrigin(); |
| 281 | 279 |
| 282 DCHECK(!(data.options.sender_info.empty() && data.FromDocument())); | 280 DCHECK(!(data.options.sender_info.empty() && data.FromDocument())); |
| 283 | 281 |
| 284 service_worker_context_->GetRegistrationUserData( | 282 service_worker_context_->GetRegistrationUserData( |
| 285 data.service_worker_registration_id, | 283 data.service_worker_registration_id, |
| 286 {kPushRegistrationIdServiceWorkerKey, kPushSenderIdServiceWorkerKey}, | 284 {kPushRegistrationIdServiceWorkerKey, kPushSenderIdServiceWorkerKey}, |
| 287 base::Bind(&PushMessagingManager::DidCheckForExistingRegistration, | 285 base::Bind(&PushMessagingManager::DidCheckForExistingRegistration, |
| 288 weak_factory_io_to_io_.GetWeakPtr(), callback, data)); | 286 weak_factory_io_to_io_.GetWeakPtr(), data)); |
| 289 } | 287 } |
| 290 | 288 |
| 291 void PushMessagingManager::DidCheckForExistingRegistration( | 289 void PushMessagingManager::DidCheckForExistingRegistration( |
| 292 const SubscribeCallback& callback, | |
| 293 const RegisterData& data, | 290 const RegisterData& data, |
| 294 const std::vector<std::string>& push_registration_id_and_sender_id, | 291 const std::vector<std::string>& push_registration_id_and_sender_id, |
| 295 ServiceWorkerStatusCode service_worker_status) { | 292 ServiceWorkerStatusCode service_worker_status) { |
| 296 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 293 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 297 if (service_worker_status == SERVICE_WORKER_OK) { | 294 if (service_worker_status == SERVICE_WORKER_OK) { |
| 298 DCHECK_EQ(2u, push_registration_id_and_sender_id.size()); | 295 DCHECK_EQ(2u, push_registration_id_and_sender_id.size()); |
| 299 const auto& push_registration_id = push_registration_id_and_sender_id[0]; | 296 const auto& push_registration_id = push_registration_id_and_sender_id[0]; |
| 300 const auto& stored_sender_id = push_registration_id_and_sender_id[1]; | 297 const auto& stored_sender_id = push_registration_id_and_sender_id[1]; |
| 301 std::string fixed_sender_id = | 298 std::string fixed_sender_id = |
| 302 FixSenderInfo(data.options.sender_info, stored_sender_id); | 299 FixSenderInfo(data.options.sender_info, stored_sender_id); |
| 303 if (fixed_sender_id.empty()) { | 300 if (fixed_sender_id.empty()) { |
| 304 SendSubscriptionError(callback, data, | 301 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_NO_SENDER_ID); |
| 305 PUSH_REGISTRATION_STATUS_NO_SENDER_ID); | |
| 306 return; | 302 return; |
| 307 } | 303 } |
| 308 if (fixed_sender_id != stored_sender_id) { | 304 if (fixed_sender_id != stored_sender_id) { |
| 309 SendSubscriptionError(callback, data, | 305 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_SENDER_ID_MISMATCH); |
| 310 PUSH_REGISTRATION_STATUS_SENDER_ID_MISMATCH); | |
| 311 return; | 306 return; |
| 312 } | 307 } |
| 313 auto callback_ui = base::Bind(&PushMessagingManager::DidGetEncryptionKeys, | 308 auto callback = base::Bind(&PushMessagingManager::DidGetEncryptionKeys, |
| 314 weak_factory_io_to_io_.GetWeakPtr(), callback, | 309 weak_factory_io_to_io_.GetWeakPtr(), data, |
| 315 data, push_registration_id); | 310 push_registration_id); |
| 316 BrowserThread::PostTask( | 311 BrowserThread::PostTask( |
| 317 BrowserThread::UI, FROM_HERE, | 312 BrowserThread::UI, FROM_HERE, |
| 318 base::Bind(&Core::GetEncryptionInfoOnUI, | 313 base::Bind(&Core::GetEncryptionInfoOnUI, |
| 319 base::Unretained(ui_core_.get()), data.requesting_origin, | 314 base::Unretained(ui_core_.get()), data.requesting_origin, |
| 320 data.service_worker_registration_id, fixed_sender_id, | 315 data.service_worker_registration_id, fixed_sender_id, |
| 321 callback_ui)); | 316 callback)); |
| 322 return; | 317 return; |
| 323 } | 318 } |
| 324 // TODO(johnme): The spec allows the register algorithm to reject with an | 319 // TODO(johnme): The spec allows the register algorithm to reject with an |
| 325 // AbortError when accessing storage fails. Perhaps we should do that if | 320 // AbortError when accessing storage fails. Perhaps we should do that if |
| 326 // service_worker_status != SERVICE_WORKER_ERROR_NOT_FOUND instead of | 321 // service_worker_status != SERVICE_WORKER_ERROR_NOT_FOUND instead of |
| 327 // attempting to do a fresh registration? | 322 // attempting to do a fresh registration? |
| 328 // https://w3c.github.io/push-api/#widl-PushRegistrationManager-register-Promi se-PushRegistration | 323 // https://w3c.github.io/push-api/#widl-PushRegistrationManager-register-Promi se-PushRegistration |
| 329 if (!data.options.sender_info.empty()) { | 324 if (!data.options.sender_info.empty()) { |
| 330 BrowserThread::PostTask( | 325 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 331 BrowserThread::UI, FROM_HERE, | 326 base::Bind(&Core::RegisterOnUI, |
| 332 base::Bind(&Core::RegisterOnUI, base::Unretained(ui_core_.get()), | 327 base::Unretained(ui_core_.get()), data)); |
| 333 callback, data)); | |
| 334 } else { | 328 } else { |
| 335 // There is no existing registration and the sender_info passed in was | 329 // There is no existing registration and the sender_info passed in was |
| 336 // empty, but perhaps there is a stored sender id we can use. | 330 // empty, but perhaps there is a stored sender id we can use. |
| 337 service_worker_context_->GetRegistrationUserData( | 331 service_worker_context_->GetRegistrationUserData( |
| 338 data.service_worker_registration_id, {kPushSenderIdServiceWorkerKey}, | 332 data.service_worker_registration_id, {kPushSenderIdServiceWorkerKey}, |
| 339 base::Bind(&PushMessagingManager::DidGetSenderIdFromStorage, | 333 base::Bind(&PushMessagingManager::DidGetSenderIdFromStorage, |
| 340 weak_factory_io_to_io_.GetWeakPtr(), callback, data)); | 334 weak_factory_io_to_io_.GetWeakPtr(), data)); |
| 341 } | 335 } |
| 342 } | 336 } |
| 343 | 337 |
| 344 void PushMessagingManager::DidGetEncryptionKeys( | 338 void PushMessagingManager::DidGetEncryptionKeys( |
| 345 const SubscribeCallback& callback, | |
| 346 const RegisterData& data, | 339 const RegisterData& data, |
| 347 const std::string& push_registration_id, | 340 const std::string& push_registration_id, |
| 348 bool success, | 341 bool success, |
| 349 const std::vector<uint8_t>& p256dh, | 342 const std::vector<uint8_t>& p256dh, |
| 350 const std::vector<uint8_t>& auth) { | 343 const std::vector<uint8_t>& auth) { |
| 351 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 344 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 352 if (!success) { | 345 if (!success) { |
| 353 SendSubscriptionError(callback, data, | 346 SendSubscriptionError(data, |
| 354 PUSH_REGISTRATION_STATUS_PUBLIC_KEY_UNAVAILABLE); | 347 PUSH_REGISTRATION_STATUS_PUBLIC_KEY_UNAVAILABLE); |
| 355 return; | 348 return; |
| 356 } | 349 } |
| 357 | 350 |
| 358 SendSubscriptionSuccess(callback, data, | 351 SendSubscriptionSuccess(data, PUSH_REGISTRATION_STATUS_SUCCESS_FROM_CACHE, |
| 359 PUSH_REGISTRATION_STATUS_SUCCESS_FROM_CACHE, | |
| 360 push_registration_id, p256dh, auth); | 352 push_registration_id, p256dh, auth); |
| 361 } | 353 } |
| 362 | 354 |
| 363 void PushMessagingManager::DidGetSenderIdFromStorage( | 355 void PushMessagingManager::DidGetSenderIdFromStorage( |
| 364 const SubscribeCallback& callback, | |
| 365 const RegisterData& data, | 356 const RegisterData& data, |
| 366 const std::vector<std::string>& stored_sender_id, | 357 const std::vector<std::string>& stored_sender_id, |
| 367 ServiceWorkerStatusCode service_worker_status) { | 358 ServiceWorkerStatusCode service_worker_status) { |
| 368 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 359 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 369 if (service_worker_status != SERVICE_WORKER_OK) { | 360 if (service_worker_status != SERVICE_WORKER_OK) { |
| 370 SendSubscriptionError(callback, data, | 361 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_NO_SENDER_ID); |
| 371 PUSH_REGISTRATION_STATUS_NO_SENDER_ID); | |
| 372 return; | 362 return; |
| 373 } | 363 } |
| 374 DCHECK_EQ(1u, stored_sender_id.size()); | 364 DCHECK_EQ(1u, stored_sender_id.size()); |
| 375 // We should only be here because no sender info was supplied to subscribe(). | 365 // We should only be here because no sender info was supplied to subscribe(). |
| 376 DCHECK(data.options.sender_info.empty()); | 366 DCHECK(data.options.sender_info.empty()); |
| 377 std::string fixed_sender_id = | 367 std::string fixed_sender_id = |
| 378 FixSenderInfo(data.options.sender_info, stored_sender_id[0]); | 368 FixSenderInfo(data.options.sender_info, stored_sender_id[0]); |
| 379 if (fixed_sender_id.empty()) { | 369 if (fixed_sender_id.empty()) { |
| 380 SendSubscriptionError(callback, data, | 370 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_NO_SENDER_ID); |
| 381 PUSH_REGISTRATION_STATUS_NO_SENDER_ID); | |
| 382 return; | 371 return; |
| 383 } | 372 } |
| 384 RegisterData mutated_data = data; | 373 RegisterData mutated_data = data; |
| 385 mutated_data.options.sender_info = fixed_sender_id; | 374 mutated_data.options.sender_info = fixed_sender_id; |
| 386 BrowserThread::PostTask( | 375 BrowserThread::PostTask( |
| 387 BrowserThread::UI, FROM_HERE, | 376 BrowserThread::UI, FROM_HERE, |
| 388 base::Bind(&Core::RegisterOnUI, base::Unretained(ui_core_.get()), | 377 base::Bind(&Core::RegisterOnUI, base::Unretained(ui_core_.get()), |
| 389 callback, mutated_data)); | 378 mutated_data)); |
| 390 } | 379 } |
| 391 | 380 |
| 392 void PushMessagingManager::Core::RegisterOnUI( | 381 void PushMessagingManager::Core::RegisterOnUI( |
| 393 const SubscribeCallback& callback, | |
| 394 const PushMessagingManager::RegisterData& data) { | 382 const PushMessagingManager::RegisterData& data) { |
| 395 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 383 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 396 PushMessagingService* push_service = service(); | 384 PushMessagingService* push_service = service(); |
| 397 if (!push_service) { | 385 if (!push_service) { |
| 398 if (!is_incognito()) { | 386 if (!is_incognito()) { |
| 399 // This might happen if InstanceIDProfileService::IsInstanceIDEnabled | 387 // This might happen if InstanceIDProfileService::IsInstanceIDEnabled |
| 400 // returns false because the Instance ID kill switch was enabled. | 388 // returns false because the Instance ID kill switch was enabled. |
| 401 // TODO(johnme): Might be better not to expose the API in this case. | 389 // TODO(johnme): Might be better not to expose the API in this case. |
| 402 BrowserThread::PostTask( | 390 BrowserThread::PostTask( |
| 403 BrowserThread::IO, FROM_HERE, | 391 BrowserThread::IO, FROM_HERE, |
| 404 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, | 392 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, |
| 405 callback, data, | 393 data, PUSH_REGISTRATION_STATUS_SERVICE_NOT_AVAILABLE)); |
| 406 PUSH_REGISTRATION_STATUS_SERVICE_NOT_AVAILABLE)); | |
| 407 } else { | 394 } else { |
| 408 // Prevent websites from detecting incognito mode, by emulating what would | 395 // Prevent websites from detecting incognito mode, by emulating what would |
| 409 // have happened if we had a PushMessagingService available. | 396 // have happened if we had a PushMessagingService available. |
| 410 if (!data.FromDocument() || !data.options.user_visible_only) { | 397 if (!data.FromDocument() || !data.options.user_visible_only) { |
| 411 // Throw a permission denied error under the same circumstances. | 398 // Throw a permission denied error under the same circumstances. |
| 412 BrowserThread::PostTask( | 399 BrowserThread::PostTask( |
| 413 BrowserThread::IO, FROM_HERE, | 400 BrowserThread::IO, FROM_HERE, |
| 414 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, | 401 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, |
| 415 callback, data, | 402 data, |
| 416 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); | 403 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); |
| 417 } else { | 404 } else { |
| 418 RenderFrameHost* render_frame_host = | 405 RenderFrameHost* render_frame_host = |
| 419 RenderFrameHost::FromID(render_process_id_, data.render_frame_id); | 406 RenderFrameHost::FromID(render_process_id_, data.render_frame_id); |
| 420 WebContents* web_contents = | 407 WebContents* web_contents = |
| 421 WebContents::FromRenderFrameHost(render_frame_host); | 408 WebContents::FromRenderFrameHost(render_frame_host); |
| 422 if (web_contents) { | 409 if (web_contents) { |
| 423 web_contents->GetMainFrame()->AddMessageToConsole( | 410 web_contents->GetMainFrame()->AddMessageToConsole( |
| 424 CONSOLE_MESSAGE_LEVEL_ERROR, kIncognitoPushUnsupportedMessage); | 411 CONSOLE_MESSAGE_LEVEL_ERROR, kIncognitoPushUnsupportedMessage); |
| 425 // Request push messaging permission (which will fail, since | 412 // Request push messaging permission (which will fail, since |
| 426 // notifications aren't supported in incognito), so the website can't | 413 // notifications aren't supported in incognito), so the website can't |
| 427 // detect whether incognito is active. | 414 // detect whether incognito is active. |
| 428 web_contents->GetBrowserContext() | 415 web_contents->GetBrowserContext() |
| 429 ->GetPermissionManager() | 416 ->GetPermissionManager() |
| 430 ->RequestPermission( | 417 ->RequestPermission( |
| 431 PermissionType::PUSH_MESSAGING, render_frame_host, | 418 PermissionType::PUSH_MESSAGING, render_frame_host, |
| 432 data.requesting_origin, false /* user_gesture */, | 419 data.requesting_origin, false /* user_gesture */, |
| 433 base::Bind(&PushMessagingManager::Core:: | 420 base::Bind(&PushMessagingManager::Core:: |
| 434 DidRequestPermissionInIncognito, | 421 DidRequestPermissionInIncognito, |
| 435 weak_factory_ui_to_ui_.GetWeakPtr(), callback, | 422 weak_factory_ui_to_ui_.GetWeakPtr(), data)); |
| 436 data)); | |
| 437 } | 423 } |
| 438 } | 424 } |
| 439 } | 425 } |
| 440 return; | 426 return; |
| 441 } | 427 } |
| 442 | 428 |
| 443 if (data.FromDocument()) { | 429 if (data.FromDocument()) { |
| 444 push_service->SubscribeFromDocument( | 430 push_service->SubscribeFromDocument( |
| 445 data.requesting_origin, data.service_worker_registration_id, | 431 data.requesting_origin, data.service_worker_registration_id, |
| 446 render_process_id_, data.render_frame_id, data.options, | 432 render_process_id_, data.render_frame_id, data.options, |
| 447 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), | 433 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), |
| 448 callback, data)); | 434 data)); |
| 449 } else { | 435 } else { |
| 450 push_service->SubscribeFromWorker( | 436 push_service->SubscribeFromWorker( |
| 451 data.requesting_origin, data.service_worker_registration_id, | 437 data.requesting_origin, data.service_worker_registration_id, |
| 452 data.options, | 438 data.options, |
| 453 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), | 439 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), |
| 454 callback, data)); | 440 data)); |
| 455 } | 441 } |
| 456 } | 442 } |
| 457 | 443 |
| 458 void PushMessagingManager::Core::DidRequestPermissionInIncognito( | 444 void PushMessagingManager::Core::DidRequestPermissionInIncognito( |
| 459 const SubscribeCallback& callback, | |
| 460 const RegisterData& data, | 445 const RegisterData& data, |
| 461 blink::mojom::PermissionStatus status) { | 446 blink::mojom::PermissionStatus status) { |
| 462 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 447 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 463 // Notification permission should always be denied in incognito. | 448 // Notification permission should always be denied in incognito. |
| 464 DCHECK_EQ(blink::mojom::PermissionStatus::DENIED, status); | 449 DCHECK_EQ(blink::mojom::PermissionStatus::DENIED, status); |
| 465 BrowserThread::PostTask( | 450 BrowserThread::PostTask( |
| 466 BrowserThread::IO, FROM_HERE, | 451 BrowserThread::IO, FROM_HERE, |
| 467 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, | 452 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, data, |
| 468 callback, data, | |
| 469 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); | 453 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); |
| 470 } | 454 } |
| 471 | 455 |
| 472 void PushMessagingManager::Core::DidRegister( | 456 void PushMessagingManager::Core::DidRegister( |
| 473 const SubscribeCallback& callback, | |
| 474 const RegisterData& data, | 457 const RegisterData& data, |
| 475 const std::string& push_registration_id, | 458 const std::string& push_registration_id, |
| 476 const std::vector<uint8_t>& p256dh, | 459 const std::vector<uint8_t>& p256dh, |
| 477 const std::vector<uint8_t>& auth, | 460 const std::vector<uint8_t>& auth, |
| 478 PushRegistrationStatus status) { | 461 PushRegistrationStatus status) { |
| 479 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 462 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 480 if (status == PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE) { | 463 if (status == PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE) { |
| 481 BrowserThread::PostTask( | 464 BrowserThread::PostTask( |
| 482 BrowserThread::IO, FROM_HERE, | 465 BrowserThread::IO, FROM_HERE, |
| 483 base::Bind(&PushMessagingManager::PersistRegistrationOnIO, io_parent_, | 466 base::Bind(&PushMessagingManager::PersistRegistrationOnIO, io_parent_, |
| 484 callback, data, push_registration_id, p256dh, auth)); | 467 data, push_registration_id, p256dh, auth)); |
| 485 } else { | 468 } else { |
| 486 BrowserThread::PostTask( | 469 BrowserThread::PostTask( |
| 487 BrowserThread::IO, FROM_HERE, | 470 BrowserThread::IO, FROM_HERE, |
| 488 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, | 471 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, |
| 489 callback, data, status)); | 472 data, status)); |
| 490 } | 473 } |
| 491 } | 474 } |
| 492 | 475 |
| 493 void PushMessagingManager::PersistRegistrationOnIO( | 476 void PushMessagingManager::PersistRegistrationOnIO( |
| 494 const SubscribeCallback& callback, | |
| 495 const RegisterData& data, | 477 const RegisterData& data, |
| 496 const std::string& push_registration_id, | 478 const std::string& push_registration_id, |
| 497 const std::vector<uint8_t>& p256dh, | 479 const std::vector<uint8_t>& p256dh, |
| 498 const std::vector<uint8_t>& auth) { | 480 const std::vector<uint8_t>& auth) { |
| 499 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 481 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 500 service_worker_context_->StoreRegistrationUserData( | 482 service_worker_context_->StoreRegistrationUserData( |
| 501 data.service_worker_registration_id, data.requesting_origin, | 483 data.service_worker_registration_id, data.requesting_origin, |
| 502 {{kPushRegistrationIdServiceWorkerKey, push_registration_id}, | 484 {{kPushRegistrationIdServiceWorkerKey, push_registration_id}, |
| 503 {kPushSenderIdServiceWorkerKey, data.options.sender_info}}, | 485 {kPushSenderIdServiceWorkerKey, data.options.sender_info}}, |
| 504 base::Bind(&PushMessagingManager::DidPersistRegistrationOnIO, | 486 base::Bind(&PushMessagingManager::DidPersistRegistrationOnIO, |
| 505 weak_factory_io_to_io_.GetWeakPtr(), callback, data, | 487 weak_factory_io_to_io_.GetWeakPtr(), data, |
| 506 push_registration_id, p256dh, auth)); | 488 push_registration_id, p256dh, auth)); |
| 507 } | 489 } |
| 508 | 490 |
| 509 void PushMessagingManager::DidPersistRegistrationOnIO( | 491 void PushMessagingManager::DidPersistRegistrationOnIO( |
| 510 const SubscribeCallback& callback, | |
| 511 const RegisterData& data, | 492 const RegisterData& data, |
| 512 const std::string& push_registration_id, | 493 const std::string& push_registration_id, |
| 513 const std::vector<uint8_t>& p256dh, | 494 const std::vector<uint8_t>& p256dh, |
| 514 const std::vector<uint8_t>& auth, | 495 const std::vector<uint8_t>& auth, |
| 515 ServiceWorkerStatusCode service_worker_status) { | 496 ServiceWorkerStatusCode service_worker_status) { |
| 516 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 497 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 517 if (service_worker_status == SERVICE_WORKER_OK) { | 498 if (service_worker_status == SERVICE_WORKER_OK) { |
| 518 SendSubscriptionSuccess(callback, data, | 499 SendSubscriptionSuccess(data, |
| 519 PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE, | 500 PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE, |
| 520 push_registration_id, p256dh, auth); | 501 push_registration_id, p256dh, auth); |
| 521 } else { | 502 } else { |
| 522 // TODO(johnme): Unregister, so PushMessagingServiceImpl can decrease count. | 503 // TODO(johnme): Unregister, so PushMessagingServiceImpl can decrease count. |
| 523 SendSubscriptionError(callback, data, | 504 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_STORAGE_ERROR); |
| 524 PUSH_REGISTRATION_STATUS_STORAGE_ERROR); | |
| 525 } | 505 } |
| 526 } | 506 } |
| 527 | 507 |
| 528 void PushMessagingManager::SendSubscriptionError( | 508 void PushMessagingManager::SendSubscriptionError( |
| 529 const SubscribeCallback& callback, | |
| 530 const RegisterData& data, | 509 const RegisterData& data, |
| 531 PushRegistrationStatus status) { | 510 PushRegistrationStatus status) { |
| 532 // Only called from IO thread, but would be safe to call from UI thread. | 511 // Only called from IO thread, but would be safe to call from UI thread. |
| 533 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 512 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 534 callback.Run(status, base::nullopt /* endpoint */, | 513 DCHECK(data.callback); |
|
Peter Beverloo
2017/02/23 13:37:39
nit: I wouldn't add this DCHECK, it's really uncom
ke.he
2017/02/24 03:24:30
Done. (and elsewhere)
| |
| 535 base::nullopt /* options */, base::nullopt /* p256dh */, | 514 data.callback.Run(status, base::nullopt /* endpoint */, |
| 536 base::nullopt /* auth */); | 515 base::nullopt /* options */, base::nullopt /* p256dh */, |
| 516 base::nullopt /* auth */); | |
| 537 RecordRegistrationStatus(status); | 517 RecordRegistrationStatus(status); |
| 538 } | 518 } |
| 539 | 519 |
| 540 void PushMessagingManager::SendSubscriptionSuccess( | 520 void PushMessagingManager::SendSubscriptionSuccess( |
| 541 const SubscribeCallback& callback, | |
| 542 const RegisterData& data, | 521 const RegisterData& data, |
| 543 PushRegistrationStatus status, | 522 PushRegistrationStatus status, |
| 544 const std::string& push_subscription_id, | 523 const std::string& push_subscription_id, |
| 545 const std::vector<uint8_t>& p256dh, | 524 const std::vector<uint8_t>& p256dh, |
| 546 const std::vector<uint8_t>& auth) { | 525 const std::vector<uint8_t>& auth) { |
| 547 // Only called from IO thread, but would be safe to call from UI thread. | 526 // Only called from IO thread, but would be safe to call from UI thread. |
| 548 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 527 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 549 if (!service_available_) { | 528 if (!service_available_) { |
| 550 // This shouldn't be possible in incognito mode, since we've already checked | 529 // This shouldn't be possible in incognito mode, since we've already checked |
| 551 // that we have an existing registration. Hence it's ok to throw an error. | 530 // that we have an existing registration. Hence it's ok to throw an error. |
| 552 DCHECK(!ui_core_->is_incognito()); | 531 DCHECK(!ui_core_->is_incognito()); |
| 553 SendSubscriptionError(callback, data, | 532 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_SERVICE_NOT_AVAILABLE); |
| 554 PUSH_REGISTRATION_STATUS_SERVICE_NOT_AVAILABLE); | |
| 555 return; | 533 return; |
| 556 } | 534 } |
| 557 | 535 |
| 558 const GURL endpoint = CreateEndpoint( | 536 const GURL endpoint = CreateEndpoint( |
| 559 IsApplicationServerKey(data.options.sender_info), push_subscription_id); | 537 IsApplicationServerKey(data.options.sender_info), push_subscription_id); |
| 560 | 538 |
| 561 callback.Run(status, endpoint, data.options, p256dh, auth); | 539 DCHECK(data.callback); |
| 540 data.callback.Run(status, endpoint, data.options, p256dh, auth); | |
| 562 | 541 |
| 563 RecordRegistrationStatus(status); | 542 RecordRegistrationStatus(status); |
| 564 } | 543 } |
| 565 | 544 |
| 566 // Unsubscribe methods on both IO and UI threads, merged in order of use from | 545 // Unsubscribe methods on both IO and UI threads, merged in order of use from |
| 567 // PushMessagingManager and Core. | 546 // PushMessagingManager and Core. |
| 568 // ----------------------------------------------------------------------------- | 547 // ----------------------------------------------------------------------------- |
| 569 | 548 |
| 570 void PushMessagingManager::Unsubscribe(int64_t service_worker_registration_id, | 549 void PushMessagingManager::Unsubscribe(int64_t service_worker_registration_id, |
| 571 const UnsubscribeCallback& callback) { | 550 const UnsubscribeCallback& callback) { |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 903 PushMessagingService* PushMessagingManager::Core::service() { | 882 PushMessagingService* PushMessagingManager::Core::service() { |
| 904 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 883 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 905 RenderProcessHost* process_host = | 884 RenderProcessHost* process_host = |
| 906 RenderProcessHost::FromID(render_process_id_); | 885 RenderProcessHost::FromID(render_process_id_); |
| 907 return process_host | 886 return process_host |
| 908 ? process_host->GetBrowserContext()->GetPushMessagingService() | 887 ? process_host->GetBrowserContext()->GetPushMessagingService() |
| 909 : nullptr; | 888 : nullptr; |
| 910 } | 889 } |
| 911 | 890 |
| 912 } // namespace content | 891 } // namespace content |
| OLD | NEW |