| 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 "chrome/browser/push_messaging/push_messaging_service_impl.h" | 5 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/barrier_closure.h" | 9 #include "base/barrier_closure.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 const std::string& message_id) { | 308 const std::string& message_id) { |
| 309 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; | 309 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; |
| 310 } | 310 } |
| 311 | 311 |
| 312 // GetPushEndpoint method ------------------------------------------------------ | 312 // GetPushEndpoint method ------------------------------------------------------ |
| 313 | 313 |
| 314 GURL PushMessagingServiceImpl::GetPushEndpoint() { | 314 GURL PushMessagingServiceImpl::GetPushEndpoint() { |
| 315 return GURL(std::string(kPushMessagingEndpoint)); | 315 return GURL(std::string(kPushMessagingEndpoint)); |
| 316 } | 316 } |
| 317 | 317 |
| 318 // GetPublicEncryptionKey method ----------------------------------------------- |
| 319 |
| 320 void PushMessagingServiceImpl::GetPublicEncryptionKey( |
| 321 const GURL& origin, |
| 322 int64_t service_worker_registration_id, |
| 323 const PushMessagingService::PublicKeyCallback& callback) { |
| 324 // TODO(peter): Get the public key from the GCM Driver. Right now we have to |
| 325 // return success=true here, otherwise subscriptions would fail. |
| 326 callback.Run(true /* success */, std::vector<uint8_t>()); |
| 327 } |
| 328 |
| 318 // Subscribe and GetPermissionStatus methods ----------------------------------- | 329 // Subscribe and GetPermissionStatus methods ----------------------------------- |
| 319 | 330 |
| 320 void PushMessagingServiceImpl::SubscribeFromDocument( | 331 void PushMessagingServiceImpl::SubscribeFromDocument( |
| 321 const GURL& requesting_origin, | 332 const GURL& requesting_origin, |
| 322 int64 service_worker_registration_id, | 333 int64 service_worker_registration_id, |
| 323 const std::string& sender_id, | 334 const std::string& sender_id, |
| 324 int renderer_id, | 335 int renderer_id, |
| 325 int render_frame_id, | 336 int render_frame_id, |
| 326 bool user_visible, | 337 bool user_visible, |
| 327 const content::PushMessagingService::RegisterCallback& callback) { | 338 const content::PushMessagingService::RegisterCallback& callback) { |
| 328 PushMessagingAppIdentifier app_identifier = | 339 PushMessagingAppIdentifier app_identifier = |
| 329 PushMessagingAppIdentifier::Generate(requesting_origin, | 340 PushMessagingAppIdentifier::Generate(requesting_origin, |
| 330 service_worker_registration_id); | 341 service_worker_registration_id); |
| 331 | 342 |
| 332 if (push_subscription_count_ + pending_push_subscription_count_ >= | 343 if (push_subscription_count_ + pending_push_subscription_count_ >= |
| 333 kMaxRegistrations) { | 344 kMaxRegistrations) { |
| 334 SubscribeEnd(callback, std::string(), | 345 SubscribeEndWithError(callback, |
| 335 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 346 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); |
| 336 return; | 347 return; |
| 337 } | 348 } |
| 338 | 349 |
| 339 content::RenderFrameHost* render_frame_host = | 350 content::RenderFrameHost* render_frame_host = |
| 340 content::RenderFrameHost::FromID(renderer_id, render_frame_id); | 351 content::RenderFrameHost::FromID(renderer_id, render_frame_id); |
| 341 content::WebContents* web_contents = | 352 content::WebContents* web_contents = |
| 342 content::WebContents::FromRenderFrameHost(render_frame_host); | 353 content::WebContents::FromRenderFrameHost(render_frame_host); |
| 343 if (!web_contents) | 354 if (!web_contents) |
| 344 return; | 355 return; |
| 345 | 356 |
| 346 if (!user_visible) { | 357 if (!user_visible) { |
| 347 web_contents->GetMainFrame()->AddMessageToConsole( | 358 web_contents->GetMainFrame()->AddMessageToConsole( |
| 348 content::CONSOLE_MESSAGE_LEVEL_ERROR, | 359 content::CONSOLE_MESSAGE_LEVEL_ERROR, |
| 349 kSilentPushUnsupportedMessage); | 360 kSilentPushUnsupportedMessage); |
| 350 | 361 |
| 351 SubscribeEnd(callback, | 362 SubscribeEndWithError(callback, |
| 352 std::string(), | 363 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
| 353 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | |
| 354 return; | 364 return; |
| 355 } | 365 } |
| 356 | 366 |
| 357 // Push does not allow permission requests from iframes. | 367 // Push does not allow permission requests from iframes. |
| 358 int request_id = -1; | 368 int request_id = -1; |
| 359 | 369 |
| 360 profile_->GetPermissionManager()->RequestPermission( | 370 profile_->GetPermissionManager()->RequestPermission( |
| 361 content::PermissionType::PUSH_MESSAGING, web_contents->GetMainFrame(), | 371 content::PermissionType::PUSH_MESSAGING, web_contents->GetMainFrame(), |
| 362 request_id, requesting_origin, true /* user_gesture */, | 372 request_id, requesting_origin, true /* user_gesture */, |
| 363 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, | 373 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, |
| 364 weak_factory_.GetWeakPtr(), app_identifier, sender_id, | 374 weak_factory_.GetWeakPtr(), app_identifier, sender_id, |
| 365 callback)); | 375 callback)); |
| 366 } | 376 } |
| 367 | 377 |
| 368 void PushMessagingServiceImpl::SubscribeFromWorker( | 378 void PushMessagingServiceImpl::SubscribeFromWorker( |
| 369 const GURL& requesting_origin, | 379 const GURL& requesting_origin, |
| 370 int64 service_worker_registration_id, | 380 int64 service_worker_registration_id, |
| 371 const std::string& sender_id, | 381 const std::string& sender_id, |
| 372 bool user_visible, | 382 bool user_visible, |
| 373 const content::PushMessagingService::RegisterCallback& register_callback) { | 383 const content::PushMessagingService::RegisterCallback& register_callback) { |
| 374 PushMessagingAppIdentifier app_identifier = | 384 PushMessagingAppIdentifier app_identifier = |
| 375 PushMessagingAppIdentifier::Generate(requesting_origin, | 385 PushMessagingAppIdentifier::Generate(requesting_origin, |
| 376 service_worker_registration_id); | 386 service_worker_registration_id); |
| 377 | 387 |
| 378 if (profile_->GetPrefs()->GetInteger( | 388 if (profile_->GetPrefs()->GetInteger( |
| 379 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { | 389 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { |
| 380 SubscribeEnd(register_callback, std::string(), | 390 SubscribeEndWithError(register_callback, |
| 381 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 391 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); |
| 382 return; | 392 return; |
| 383 } | 393 } |
| 384 | 394 |
| 385 GURL embedding_origin = requesting_origin; | 395 GURL embedding_origin = requesting_origin; |
| 386 blink::WebPushPermissionStatus permission_status = | 396 blink::WebPushPermissionStatus permission_status = |
| 387 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin, | 397 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin, |
| 388 embedding_origin, | 398 embedding_origin, |
| 389 user_visible); | 399 user_visible); |
| 390 if (permission_status != blink::WebPushPermissionStatusGranted) { | 400 if (permission_status != blink::WebPushPermissionStatusGranted) { |
| 391 SubscribeEnd(register_callback, std::string(), | 401 SubscribeEndWithError(register_callback, |
| 392 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 402 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
| 393 return; | 403 return; |
| 394 } | 404 } |
| 395 | 405 |
| 396 IncreasePushSubscriptionCount(1, true /* is_pending */); | 406 IncreasePushSubscriptionCount(1, true /* is_pending */); |
| 397 std::vector<std::string> sender_ids(1, sender_id); | 407 std::vector<std::string> sender_ids(1, sender_id); |
| 398 GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, | 408 GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, |
| 399 base::Bind(&PushMessagingServiceImpl::DidSubscribe, | 409 base::Bind(&PushMessagingServiceImpl::DidSubscribe, |
| 400 weak_factory_.GetWeakPtr(), | 410 weak_factory_.GetWeakPtr(), |
| 401 app_identifier, register_callback)); | 411 app_identifier, register_callback)); |
| 402 } | 412 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 413 embedding_origin)); | 423 embedding_origin)); |
| 414 } | 424 } |
| 415 | 425 |
| 416 bool PushMessagingServiceImpl::SupportNonVisibleMessages() { | 426 bool PushMessagingServiceImpl::SupportNonVisibleMessages() { |
| 417 return false; | 427 return false; |
| 418 } | 428 } |
| 419 | 429 |
| 420 void PushMessagingServiceImpl::SubscribeEnd( | 430 void PushMessagingServiceImpl::SubscribeEnd( |
| 421 const content::PushMessagingService::RegisterCallback& callback, | 431 const content::PushMessagingService::RegisterCallback& callback, |
| 422 const std::string& subscription_id, | 432 const std::string& subscription_id, |
| 433 const std::vector<uint8_t>& curve25519dh, |
| 423 content::PushRegistrationStatus status) { | 434 content::PushRegistrationStatus status) { |
| 424 callback.Run(subscription_id, status); | 435 callback.Run(subscription_id, curve25519dh, status); |
| 436 } |
| 437 |
| 438 void PushMessagingServiceImpl::SubscribeEndWithError( |
| 439 const content::PushMessagingService::RegisterCallback& callback, |
| 440 content::PushRegistrationStatus status) { |
| 441 SubscribeEnd(callback, std::string() /* subscription_id */, |
| 442 std::vector<uint8_t>() /* curve25519dh */, status); |
| 425 } | 443 } |
| 426 | 444 |
| 427 void PushMessagingServiceImpl::DidSubscribe( | 445 void PushMessagingServiceImpl::DidSubscribe( |
| 428 const PushMessagingAppIdentifier& app_identifier, | 446 const PushMessagingAppIdentifier& app_identifier, |
| 429 const content::PushMessagingService::RegisterCallback& callback, | 447 const content::PushMessagingService::RegisterCallback& callback, |
| 430 const std::string& subscription_id, | 448 const std::string& subscription_id, |
| 431 gcm::GCMClient::Result result) { | 449 gcm::GCMClient::Result result) { |
| 432 content::PushRegistrationStatus status = | 450 content::PushRegistrationStatus status = |
| 433 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 451 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
| 452 std::vector<uint8_t> curve25519dh; |
| 453 |
| 434 switch (result) { | 454 switch (result) { |
| 435 case gcm::GCMClient::SUCCESS: | 455 case gcm::GCMClient::SUCCESS: |
| 436 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; | 456 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; |
| 437 app_identifier.PersistToPrefs(profile_); | 457 app_identifier.PersistToPrefs(profile_); |
| 458 |
| 459 // TODO(peter): Hook up getting the keys from the GCM Driver. |
| 460 |
| 438 IncreasePushSubscriptionCount(1, false /* is_pending */); | 461 IncreasePushSubscriptionCount(1, false /* is_pending */); |
| 439 break; | 462 break; |
| 440 case gcm::GCMClient::INVALID_PARAMETER: | 463 case gcm::GCMClient::INVALID_PARAMETER: |
| 441 case gcm::GCMClient::GCM_DISABLED: | 464 case gcm::GCMClient::GCM_DISABLED: |
| 442 case gcm::GCMClient::ASYNC_OPERATION_PENDING: | 465 case gcm::GCMClient::ASYNC_OPERATION_PENDING: |
| 443 case gcm::GCMClient::SERVER_ERROR: | 466 case gcm::GCMClient::SERVER_ERROR: |
| 444 case gcm::GCMClient::UNKNOWN_ERROR: | 467 case gcm::GCMClient::UNKNOWN_ERROR: |
| 445 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 468 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
| 446 break; | 469 break; |
| 447 case gcm::GCMClient::NETWORK_ERROR: | 470 case gcm::GCMClient::NETWORK_ERROR: |
| 448 case gcm::GCMClient::TTL_EXCEEDED: | 471 case gcm::GCMClient::TTL_EXCEEDED: |
| 449 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; | 472 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; |
| 450 break; | 473 break; |
| 451 } | 474 } |
| 452 SubscribeEnd(callback, subscription_id, status); | 475 |
| 476 SubscribeEnd(callback, subscription_id, curve25519dh, status); |
| 453 DecreasePushSubscriptionCount(1, true /* was_pending */); | 477 DecreasePushSubscriptionCount(1, true /* was_pending */); |
| 454 } | 478 } |
| 455 | 479 |
| 456 void PushMessagingServiceImpl::DidRequestPermission( | 480 void PushMessagingServiceImpl::DidRequestPermission( |
| 457 const PushMessagingAppIdentifier& app_identifier, | 481 const PushMessagingAppIdentifier& app_identifier, |
| 458 const std::string& sender_id, | 482 const std::string& sender_id, |
| 459 const content::PushMessagingService::RegisterCallback& register_callback, | 483 const content::PushMessagingService::RegisterCallback& register_callback, |
| 460 content::PermissionStatus permission_status) { | 484 content::PermissionStatus permission_status) { |
| 461 if (permission_status != content::PERMISSION_STATUS_GRANTED) { | 485 if (permission_status != content::PERMISSION_STATUS_GRANTED) { |
| 462 SubscribeEnd(register_callback, | 486 SubscribeEndWithError(register_callback, |
| 463 std::string(), | 487 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
| 464 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | |
| 465 return; | 488 return; |
| 466 } | 489 } |
| 467 | 490 |
| 468 IncreasePushSubscriptionCount(1, true /* is_pending */); | 491 IncreasePushSubscriptionCount(1, true /* is_pending */); |
| 469 std::vector<std::string> sender_ids(1, sender_id); | 492 std::vector<std::string> sender_ids(1, sender_id); |
| 470 GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, | 493 GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, |
| 471 base::Bind(&PushMessagingServiceImpl::DidSubscribe, | 494 base::Bind(&PushMessagingServiceImpl::DidSubscribe, |
| 472 weak_factory_.GetWeakPtr(), | 495 weak_factory_.GetWeakPtr(), |
| 473 app_identifier, register_callback)); | 496 app_identifier, register_callback)); |
| 474 } | 497 } |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 blink::WebPushPermissionStatusGranted; | 672 blink::WebPushPermissionStatusGranted; |
| 650 } | 673 } |
| 651 | 674 |
| 652 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { | 675 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { |
| 653 gcm::GCMProfileService* gcm_profile_service = | 676 gcm::GCMProfileService* gcm_profile_service = |
| 654 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 677 gcm::GCMProfileServiceFactory::GetForProfile(profile_); |
| 655 CHECK(gcm_profile_service); | 678 CHECK(gcm_profile_service); |
| 656 CHECK(gcm_profile_service->driver()); | 679 CHECK(gcm_profile_service->driver()); |
| 657 return gcm_profile_service->driver(); | 680 return gcm_profile_service->driver(); |
| 658 } | 681 } |
| OLD | NEW |