| 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; |
| 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 | 412 |
| 426 BrowserContext* browser_context = web_contents->GetBrowserContext(); | 413 BrowserContext* browser_context = web_contents->GetBrowserContext(); |
| 427 | 414 |
| 428 // It's valid for embedders to return a null permission manager. | 415 // It's valid for embedders to return a null permission manager. |
| 429 // Immediately reject the permission request when this happens. | 416 // Immediately reject the permission request when this happens. |
| 430 if (!browser_context->GetPermissionManager()) { | 417 if (!browser_context->GetPermissionManager()) { |
| 431 BrowserThread::PostTask( | 418 BrowserThread::PostTask( |
| 432 BrowserThread::IO, FROM_HERE, | 419 BrowserThread::IO, FROM_HERE, |
| 433 base::Bind( | 420 base::Bind( |
| 434 &PushMessagingManager::SendSubscriptionError, io_parent_, | 421 &PushMessagingManager::SendSubscriptionError, io_parent_, |
| 435 callback, data, | 422 data, |
| 436 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); | 423 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); |
| 437 | 424 |
| 438 return; | 425 return; |
| 439 } | 426 } |
| 440 | 427 |
| 441 // Request push messaging permission (which will fail, since | 428 // Request push messaging permission (which will fail, since |
| 442 // notifications aren't supported in incognito), so the website can't | 429 // notifications aren't supported in incognito), so the website can't |
| 443 // detect whether incognito is active. | 430 // detect whether incognito is active. |
| 444 browser_context->GetPermissionManager()->RequestPermission( | 431 browser_context->GetPermissionManager()->RequestPermission( |
| 445 PermissionType::PUSH_MESSAGING, render_frame_host, | 432 PermissionType::PUSH_MESSAGING, render_frame_host, |
| 446 data.requesting_origin, false /* user_gesture */, | 433 data.requesting_origin, false /* user_gesture */, |
| 447 base::Bind( | 434 base::Bind( |
| 448 &PushMessagingManager::Core::DidRequestPermissionInIncognito, | 435 &PushMessagingManager::Core::DidRequestPermissionInIncognito, |
| 449 weak_factory_ui_to_ui_.GetWeakPtr(), callback, data)); | 436 weak_factory_ui_to_ui_.GetWeakPtr(), data)); |
| 450 } | 437 } |
| 451 } | 438 } |
| 452 } | 439 } |
| 453 return; | 440 return; |
| 454 } | 441 } |
| 455 | 442 |
| 456 if (data.FromDocument()) { | 443 if (data.FromDocument()) { |
| 457 push_service->SubscribeFromDocument( | 444 push_service->SubscribeFromDocument( |
| 458 data.requesting_origin, data.service_worker_registration_id, | 445 data.requesting_origin, data.service_worker_registration_id, |
| 459 render_process_id_, data.render_frame_id, data.options, | 446 render_process_id_, data.render_frame_id, data.options, |
| 460 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), | 447 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), |
| 461 callback, data)); | 448 data)); |
| 462 } else { | 449 } else { |
| 463 push_service->SubscribeFromWorker( | 450 push_service->SubscribeFromWorker( |
| 464 data.requesting_origin, data.service_worker_registration_id, | 451 data.requesting_origin, data.service_worker_registration_id, |
| 465 data.options, | 452 data.options, |
| 466 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), | 453 base::Bind(&Core::DidRegister, weak_factory_ui_to_ui_.GetWeakPtr(), |
| 467 callback, data)); | 454 data)); |
| 468 } | 455 } |
| 469 } | 456 } |
| 470 | 457 |
| 471 void PushMessagingManager::Core::DidRequestPermissionInIncognito( | 458 void PushMessagingManager::Core::DidRequestPermissionInIncognito( |
| 472 const SubscribeCallback& callback, | |
| 473 const RegisterData& data, | 459 const RegisterData& data, |
| 474 blink::mojom::PermissionStatus status) { | 460 blink::mojom::PermissionStatus status) { |
| 475 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 461 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 476 // Notification permission should always be denied in incognito. | 462 // Notification permission should always be denied in incognito. |
| 477 DCHECK_EQ(blink::mojom::PermissionStatus::DENIED, status); | 463 DCHECK_EQ(blink::mojom::PermissionStatus::DENIED, status); |
| 478 BrowserThread::PostTask( | 464 BrowserThread::PostTask( |
| 479 BrowserThread::IO, FROM_HERE, | 465 BrowserThread::IO, FROM_HERE, |
| 480 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, | 466 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, data, |
| 481 callback, data, | |
| 482 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); | 467 PUSH_REGISTRATION_STATUS_INCOGNITO_PERMISSION_DENIED)); |
| 483 } | 468 } |
| 484 | 469 |
| 485 void PushMessagingManager::Core::DidRegister( | 470 void PushMessagingManager::Core::DidRegister( |
| 486 const SubscribeCallback& callback, | |
| 487 const RegisterData& data, | 471 const RegisterData& data, |
| 488 const std::string& push_registration_id, | 472 const std::string& push_registration_id, |
| 489 const std::vector<uint8_t>& p256dh, | 473 const std::vector<uint8_t>& p256dh, |
| 490 const std::vector<uint8_t>& auth, | 474 const std::vector<uint8_t>& auth, |
| 491 PushRegistrationStatus status) { | 475 PushRegistrationStatus status) { |
| 492 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 476 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 493 if (status == PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE) { | 477 if (status == PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE) { |
| 494 BrowserThread::PostTask( | 478 BrowserThread::PostTask( |
| 495 BrowserThread::IO, FROM_HERE, | 479 BrowserThread::IO, FROM_HERE, |
| 496 base::Bind(&PushMessagingManager::PersistRegistrationOnIO, io_parent_, | 480 base::Bind(&PushMessagingManager::PersistRegistrationOnIO, io_parent_, |
| 497 callback, data, push_registration_id, p256dh, auth)); | 481 data, push_registration_id, p256dh, auth)); |
| 498 } else { | 482 } else { |
| 499 BrowserThread::PostTask( | 483 BrowserThread::PostTask( |
| 500 BrowserThread::IO, FROM_HERE, | 484 BrowserThread::IO, FROM_HERE, |
| 501 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, | 485 base::Bind(&PushMessagingManager::SendSubscriptionError, io_parent_, |
| 502 callback, data, status)); | 486 data, status)); |
| 503 } | 487 } |
| 504 } | 488 } |
| 505 | 489 |
| 506 void PushMessagingManager::PersistRegistrationOnIO( | 490 void PushMessagingManager::PersistRegistrationOnIO( |
| 507 const SubscribeCallback& callback, | |
| 508 const RegisterData& data, | 491 const RegisterData& data, |
| 509 const std::string& push_registration_id, | 492 const std::string& push_registration_id, |
| 510 const std::vector<uint8_t>& p256dh, | 493 const std::vector<uint8_t>& p256dh, |
| 511 const std::vector<uint8_t>& auth) { | 494 const std::vector<uint8_t>& auth) { |
| 512 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 495 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 513 service_worker_context_->StoreRegistrationUserData( | 496 service_worker_context_->StoreRegistrationUserData( |
| 514 data.service_worker_registration_id, data.requesting_origin, | 497 data.service_worker_registration_id, data.requesting_origin, |
| 515 {{kPushRegistrationIdServiceWorkerKey, push_registration_id}, | 498 {{kPushRegistrationIdServiceWorkerKey, push_registration_id}, |
| 516 {kPushSenderIdServiceWorkerKey, data.options.sender_info}}, | 499 {kPushSenderIdServiceWorkerKey, data.options.sender_info}}, |
| 517 base::Bind(&PushMessagingManager::DidPersistRegistrationOnIO, | 500 base::Bind(&PushMessagingManager::DidPersistRegistrationOnIO, |
| 518 weak_factory_io_to_io_.GetWeakPtr(), callback, data, | 501 weak_factory_io_to_io_.GetWeakPtr(), data, |
| 519 push_registration_id, p256dh, auth)); | 502 push_registration_id, p256dh, auth)); |
| 520 } | 503 } |
| 521 | 504 |
| 522 void PushMessagingManager::DidPersistRegistrationOnIO( | 505 void PushMessagingManager::DidPersistRegistrationOnIO( |
| 523 const SubscribeCallback& callback, | |
| 524 const RegisterData& data, | 506 const RegisterData& data, |
| 525 const std::string& push_registration_id, | 507 const std::string& push_registration_id, |
| 526 const std::vector<uint8_t>& p256dh, | 508 const std::vector<uint8_t>& p256dh, |
| 527 const std::vector<uint8_t>& auth, | 509 const std::vector<uint8_t>& auth, |
| 528 ServiceWorkerStatusCode service_worker_status) { | 510 ServiceWorkerStatusCode service_worker_status) { |
| 529 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 511 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 530 if (service_worker_status == SERVICE_WORKER_OK) { | 512 if (service_worker_status == SERVICE_WORKER_OK) { |
| 531 SendSubscriptionSuccess(callback, data, | 513 SendSubscriptionSuccess(data, |
| 532 PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE, | 514 PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE, |
| 533 push_registration_id, p256dh, auth); | 515 push_registration_id, p256dh, auth); |
| 534 } else { | 516 } else { |
| 535 // TODO(johnme): Unregister, so PushMessagingServiceImpl can decrease count. | 517 // TODO(johnme): Unregister, so PushMessagingServiceImpl can decrease count. |
| 536 SendSubscriptionError(callback, data, | 518 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_STORAGE_ERROR); |
| 537 PUSH_REGISTRATION_STATUS_STORAGE_ERROR); | |
| 538 } | 519 } |
| 539 } | 520 } |
| 540 | 521 |
| 541 void PushMessagingManager::SendSubscriptionError( | 522 void PushMessagingManager::SendSubscriptionError( |
| 542 const SubscribeCallback& callback, | |
| 543 const RegisterData& data, | 523 const RegisterData& data, |
| 544 PushRegistrationStatus status) { | 524 PushRegistrationStatus status) { |
| 545 // Only called from IO thread, but would be safe to call from UI thread. | 525 // Only called from IO thread, but would be safe to call from UI thread. |
| 546 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 526 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 547 callback.Run(status, base::nullopt /* endpoint */, | 527 data.callback.Run(status, base::nullopt /* endpoint */, |
| 548 base::nullopt /* options */, base::nullopt /* p256dh */, | 528 base::nullopt /* options */, base::nullopt /* p256dh */, |
| 549 base::nullopt /* auth */); | 529 base::nullopt /* auth */); |
| 550 RecordRegistrationStatus(status); | 530 RecordRegistrationStatus(status); |
| 551 } | 531 } |
| 552 | 532 |
| 553 void PushMessagingManager::SendSubscriptionSuccess( | 533 void PushMessagingManager::SendSubscriptionSuccess( |
| 554 const SubscribeCallback& callback, | |
| 555 const RegisterData& data, | 534 const RegisterData& data, |
| 556 PushRegistrationStatus status, | 535 PushRegistrationStatus status, |
| 557 const std::string& push_subscription_id, | 536 const std::string& push_subscription_id, |
| 558 const std::vector<uint8_t>& p256dh, | 537 const std::vector<uint8_t>& p256dh, |
| 559 const std::vector<uint8_t>& auth) { | 538 const std::vector<uint8_t>& auth) { |
| 560 // Only called from IO thread, but would be safe to call from UI thread. | 539 // Only called from IO thread, but would be safe to call from UI thread. |
| 561 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 540 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 562 if (!service_available_) { | 541 if (!service_available_) { |
| 563 // This shouldn't be possible in incognito mode, since we've already checked | 542 // This shouldn't be possible in incognito mode, since we've already checked |
| 564 // that we have an existing registration. Hence it's ok to throw an error. | 543 // that we have an existing registration. Hence it's ok to throw an error. |
| 565 DCHECK(!ui_core_->is_incognito()); | 544 DCHECK(!ui_core_->is_incognito()); |
| 566 SendSubscriptionError(callback, data, | 545 SendSubscriptionError(data, PUSH_REGISTRATION_STATUS_SERVICE_NOT_AVAILABLE); |
| 567 PUSH_REGISTRATION_STATUS_SERVICE_NOT_AVAILABLE); | |
| 568 return; | 546 return; |
| 569 } | 547 } |
| 570 | 548 |
| 571 const GURL endpoint = CreateEndpoint( | 549 const GURL endpoint = CreateEndpoint( |
| 572 IsApplicationServerKey(data.options.sender_info), push_subscription_id); | 550 IsApplicationServerKey(data.options.sender_info), push_subscription_id); |
| 573 | 551 |
| 574 callback.Run(status, endpoint, data.options, p256dh, auth); | 552 data.callback.Run(status, endpoint, data.options, p256dh, auth); |
| 575 | 553 |
| 576 RecordRegistrationStatus(status); | 554 RecordRegistrationStatus(status); |
| 577 } | 555 } |
| 578 | 556 |
| 579 // Unsubscribe methods on both IO and UI threads, merged in order of use from | 557 // Unsubscribe methods on both IO and UI threads, merged in order of use from |
| 580 // PushMessagingManager and Core. | 558 // PushMessagingManager and Core. |
| 581 // ----------------------------------------------------------------------------- | 559 // ----------------------------------------------------------------------------- |
| 582 | 560 |
| 583 void PushMessagingManager::Unsubscribe(int64_t service_worker_registration_id, | 561 void PushMessagingManager::Unsubscribe(int64_t service_worker_registration_id, |
| 584 const UnsubscribeCallback& callback) { | 562 const UnsubscribeCallback& callback) { |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 916 PushMessagingService* PushMessagingManager::Core::service() { | 894 PushMessagingService* PushMessagingManager::Core::service() { |
| 917 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 895 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 918 RenderProcessHost* process_host = | 896 RenderProcessHost* process_host = |
| 919 RenderProcessHost::FromID(render_process_id_); | 897 RenderProcessHost::FromID(render_process_id_); |
| 920 return process_host | 898 return process_host |
| 921 ? process_host->GetBrowserContext()->GetPushMessagingService() | 899 ? process_host->GetBrowserContext()->GetPushMessagingService() |
| 922 : nullptr; | 900 : nullptr; |
| 923 } | 901 } |
| 924 | 902 |
| 925 } // namespace content | 903 } // namespace content |
| OLD | NEW |