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 "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 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 325 int renderer_id, | 325 int renderer_id, |
| 326 int render_frame_id, | 326 int render_frame_id, |
| 327 bool user_visible, | 327 bool user_visible, |
| 328 const content::PushMessagingService::RegisterCallback& callback) { | 328 const content::PushMessagingService::RegisterCallback& callback) { |
| 329 PushMessagingAppIdentifier app_identifier = | 329 PushMessagingAppIdentifier app_identifier = |
| 330 PushMessagingAppIdentifier::Generate(requesting_origin, | 330 PushMessagingAppIdentifier::Generate(requesting_origin, |
| 331 service_worker_registration_id); | 331 service_worker_registration_id); |
| 332 | 332 |
| 333 if (push_subscription_count_ + pending_push_subscription_count_ >= | 333 if (push_subscription_count_ + pending_push_subscription_count_ >= |
| 334 kMaxRegistrations) { | 334 kMaxRegistrations) { |
| 335 SubscribeEnd(callback, std::string(), | 335 SubscribeEndWithError(callback, |
| 336 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 336 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); |
| 337 return; | 337 return; |
| 338 } | 338 } |
| 339 | 339 |
| 340 content::RenderFrameHost* render_frame_host = | 340 content::RenderFrameHost* render_frame_host = |
| 341 content::RenderFrameHost::FromID(renderer_id, render_frame_id); | 341 content::RenderFrameHost::FromID(renderer_id, render_frame_id); |
| 342 content::WebContents* web_contents = | 342 content::WebContents* web_contents = |
| 343 content::WebContents::FromRenderFrameHost(render_frame_host); | 343 content::WebContents::FromRenderFrameHost(render_frame_host); |
| 344 if (!web_contents) | 344 if (!web_contents) |
| 345 return; | 345 return; |
| 346 | 346 |
| 347 // TODO(miguelg) need to send this over IPC when bubble support is | 347 // TODO(miguelg) need to send this over IPC when bubble support is |
| 348 // implemented. | 348 // implemented. |
| 349 int request_id = -1; | 349 int request_id = -1; |
| 350 | 350 |
| 351 const PermissionRequestID id( | 351 const PermissionRequestID id( |
| 352 renderer_id, render_frame_id, request_id, GURL()); | 352 renderer_id, render_frame_id, request_id, GURL()); |
| 353 | 353 |
| 354 PushMessagingPermissionContext* permission_context = | 354 PushMessagingPermissionContext* permission_context = |
| 355 PushMessagingPermissionContextFactory::GetForProfile(profile_); | 355 PushMessagingPermissionContextFactory::GetForProfile(profile_); |
| 356 DCHECK(permission_context); | 356 DCHECK(permission_context); |
| 357 | 357 |
| 358 if (!user_visible) { | 358 if (!user_visible) { |
| 359 web_contents->GetMainFrame()->AddMessageToConsole( | 359 web_contents->GetMainFrame()->AddMessageToConsole( |
| 360 content::CONSOLE_MESSAGE_LEVEL_ERROR, | 360 content::CONSOLE_MESSAGE_LEVEL_ERROR, |
| 361 kSilentPushUnsupportedMessage); | 361 kSilentPushUnsupportedMessage); |
| 362 | 362 |
| 363 SubscribeEnd(callback, | 363 SubscribeEndWithError(callback, |
| 364 std::string(), | 364 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
| 365 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | |
| 366 return; | 365 return; |
| 367 } | 366 } |
| 368 | 367 |
| 369 // TODO(miguelg): Consider the value of |user_visible| when making the | 368 // TODO(miguelg): Consider the value of |user_visible| when making the |
| 370 // permission request. | 369 // permission request. |
| 371 // TODO(mlamouri): Move requesting Push permission over to using Mojo, and | 370 // TODO(mlamouri): Move requesting Push permission over to using Mojo, and |
| 372 // re-introduce the ability of |user_gesture| when bubbles require this. | 371 // re-introduce the ability of |user_gesture| when bubbles require this. |
| 373 // https://crbug.com/423770. | 372 // https://crbug.com/423770. |
| 374 permission_context->RequestPermission( | 373 permission_context->RequestPermission( |
| 375 web_contents, id, requesting_origin, true /* user_gesture */, | 374 web_contents, id, requesting_origin, true /* user_gesture */, |
| 376 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, | 375 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, |
| 377 weak_factory_.GetWeakPtr(), app_identifier, sender_id, | 376 weak_factory_.GetWeakPtr(), app_identifier, sender_id, |
| 378 callback)); | 377 callback)); |
| 379 } | 378 } |
| 380 | 379 |
| 381 void PushMessagingServiceImpl::SubscribeFromWorker( | 380 void PushMessagingServiceImpl::SubscribeFromWorker( |
| 382 const GURL& requesting_origin, | 381 const GURL& requesting_origin, |
| 383 int64 service_worker_registration_id, | 382 int64 service_worker_registration_id, |
| 384 const std::string& sender_id, | 383 const std::string& sender_id, |
| 385 bool user_visible, | 384 bool user_visible, |
| 386 const content::PushMessagingService::RegisterCallback& register_callback) { | 385 const content::PushMessagingService::RegisterCallback& register_callback) { |
| 387 PushMessagingAppIdentifier app_identifier = | 386 PushMessagingAppIdentifier app_identifier = |
| 388 PushMessagingAppIdentifier::Generate(requesting_origin, | 387 PushMessagingAppIdentifier::Generate(requesting_origin, |
| 389 service_worker_registration_id); | 388 service_worker_registration_id); |
| 390 | 389 |
| 391 if (profile_->GetPrefs()->GetInteger( | 390 if (profile_->GetPrefs()->GetInteger( |
| 392 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { | 391 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { |
| 393 SubscribeEnd(register_callback, std::string(), | 392 SubscribeEndWithError(register_callback, |
| 394 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 393 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); |
| 395 return; | 394 return; |
| 396 } | 395 } |
| 397 | 396 |
| 398 // TODO(peter): Consider |user_visible| when getting the permission status | 397 // TODO(peter): Consider |user_visible| when getting the permission status |
| 399 // for registering from a worker. | 398 // for registering from a worker. |
| 400 | 399 |
| 401 GURL embedding_origin = requesting_origin; | 400 GURL embedding_origin = requesting_origin; |
| 402 blink::WebPushPermissionStatus permission_status = | 401 blink::WebPushPermissionStatus permission_status = |
| 403 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin, | 402 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin, |
| 404 embedding_origin, | 403 embedding_origin, |
| 405 user_visible); | 404 user_visible); |
| 406 if (permission_status != blink::WebPushPermissionStatusGranted) { | 405 if (permission_status != blink::WebPushPermissionStatusGranted) { |
| 407 SubscribeEnd(register_callback, std::string(), | 406 SubscribeEndWithError(register_callback, |
| 408 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 407 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
| 409 return; | 408 return; |
| 410 } | 409 } |
| 411 | 410 |
| 412 IncreasePushSubscriptionCount(1, true /* is_pending */); | 411 IncreasePushSubscriptionCount(1, true /* is_pending */); |
| 413 std::vector<std::string> sender_ids(1, sender_id); | 412 std::vector<std::string> sender_ids(1, sender_id); |
| 414 GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, | 413 GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, |
| 415 base::Bind(&PushMessagingServiceImpl::DidSubscribe, | 414 base::Bind(&PushMessagingServiceImpl::DidSubscribe, |
| 416 weak_factory_.GetWeakPtr(), | 415 weak_factory_.GetWeakPtr(), |
| 417 app_identifier, register_callback)); | 416 app_identifier, register_callback)); |
| 418 } | 417 } |
| 419 | 418 |
| 419 void PushMessagingServiceImpl::GetPublicEncryptionKey( | |
|
johnme
2015/07/09 16:02:53
Nit: Please move this to a new section of the .cc
Peter Beverloo
2015/07/09 18:54:17
Done.
| |
| 420 const GURL& origin, | |
| 421 int64_t service_worker_registration_id, | |
| 422 const PushMessagingService::PublicKeyCallback& callback) { | |
| 423 // TODO(peter): Get the public key from the GCM Driver. Right now we have to | |
| 424 // return success=true here, otherwise subscriptions would fail. | |
| 425 callback.Run(true /* success */, std::vector<uint8_t>()); | |
| 426 } | |
| 427 | |
| 420 blink::WebPushPermissionStatus PushMessagingServiceImpl::GetPermissionStatus( | 428 blink::WebPushPermissionStatus PushMessagingServiceImpl::GetPermissionStatus( |
| 421 const GURL& requesting_origin, | 429 const GURL& requesting_origin, |
| 422 const GURL& embedding_origin, | 430 const GURL& embedding_origin, |
| 423 bool user_visible) { | 431 bool user_visible) { |
| 424 // TODO(peter): Consider |user_visible| when checking Push permission. | 432 // TODO(peter): Consider |user_visible| when checking Push permission. |
| 425 | 433 |
| 426 PushMessagingPermissionContext* permission_context = | 434 PushMessagingPermissionContext* permission_context = |
| 427 PushMessagingPermissionContextFactory::GetForProfile(profile_); | 435 PushMessagingPermissionContextFactory::GetForProfile(profile_); |
| 428 return ToPushPermission(permission_context->GetPermissionStatus( | 436 return ToPushPermission(permission_context->GetPermissionStatus( |
| 429 requesting_origin, embedding_origin)); | 437 requesting_origin, embedding_origin)); |
| 430 } | 438 } |
| 431 | 439 |
| 432 bool PushMessagingServiceImpl::SupportNonVisibleMessages() { | 440 bool PushMessagingServiceImpl::SupportNonVisibleMessages() { |
| 433 return false; | 441 return false; |
| 434 } | 442 } |
| 435 | 443 |
| 436 void PushMessagingServiceImpl::SubscribeEnd( | 444 void PushMessagingServiceImpl::SubscribeEnd( |
| 437 const content::PushMessagingService::RegisterCallback& callback, | 445 const content::PushMessagingService::RegisterCallback& callback, |
| 438 const std::string& subscription_id, | 446 const std::string& subscription_id, |
| 447 const std::vector<uint8_t>& curve25519dh, | |
| 439 content::PushRegistrationStatus status) { | 448 content::PushRegistrationStatus status) { |
| 440 callback.Run(subscription_id, status); | 449 callback.Run(subscription_id, curve25519dh, status); |
| 450 } | |
| 451 | |
| 452 void PushMessagingServiceImpl::SubscribeEndWithError( | |
| 453 const content::PushMessagingService::RegisterCallback& callback, | |
| 454 content::PushRegistrationStatus status) { | |
| 455 SubscribeEnd(callback, std::string() /* subscription_id */, | |
| 456 std::vector<uint8_t>() /* curve25519dh */, status); | |
| 441 } | 457 } |
| 442 | 458 |
| 443 void PushMessagingServiceImpl::DidSubscribe( | 459 void PushMessagingServiceImpl::DidSubscribe( |
| 444 const PushMessagingAppIdentifier& app_identifier, | 460 const PushMessagingAppIdentifier& app_identifier, |
| 445 const content::PushMessagingService::RegisterCallback& callback, | 461 const content::PushMessagingService::RegisterCallback& callback, |
| 446 const std::string& subscription_id, | 462 const std::string& subscription_id, |
| 447 gcm::GCMClient::Result result) { | 463 gcm::GCMClient::Result result) { |
| 448 content::PushRegistrationStatus status = | 464 content::PushRegistrationStatus status = |
| 449 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 465 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
| 450 switch (result) { | 466 switch (result) { |
| 451 case gcm::GCMClient::SUCCESS: | 467 case gcm::GCMClient::SUCCESS: |
| 452 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; | 468 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; |
| 453 app_identifier.PersistToPrefs(profile_); | 469 app_identifier.PersistToPrefs(profile_); |
| 454 IncreasePushSubscriptionCount(1, false /* is_pending */); | 470 IncreasePushSubscriptionCount(1, false /* is_pending */); |
| 455 break; | 471 break; |
| 456 case gcm::GCMClient::INVALID_PARAMETER: | 472 case gcm::GCMClient::INVALID_PARAMETER: |
| 457 case gcm::GCMClient::GCM_DISABLED: | 473 case gcm::GCMClient::GCM_DISABLED: |
| 458 case gcm::GCMClient::ASYNC_OPERATION_PENDING: | 474 case gcm::GCMClient::ASYNC_OPERATION_PENDING: |
| 459 case gcm::GCMClient::SERVER_ERROR: | 475 case gcm::GCMClient::SERVER_ERROR: |
| 460 case gcm::GCMClient::UNKNOWN_ERROR: | 476 case gcm::GCMClient::UNKNOWN_ERROR: |
| 461 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 477 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
| 462 break; | 478 break; |
| 463 case gcm::GCMClient::NETWORK_ERROR: | 479 case gcm::GCMClient::NETWORK_ERROR: |
| 464 case gcm::GCMClient::TTL_EXCEEDED: | 480 case gcm::GCMClient::TTL_EXCEEDED: |
| 465 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; | 481 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; |
| 466 break; | 482 break; |
| 467 } | 483 } |
| 468 SubscribeEnd(callback, subscription_id, status); | 484 |
| 485 // TODO(peter): Hook up getting the keys from the GCM Driver. | |
| 486 std::vector<uint8_t> curve25519dh; | |
| 487 | |
| 488 SubscribeEnd(callback, subscription_id, curve25519dh, status); | |
|
johnme
2015/07/09 16:02:53
Nit: it feels slightly inconsistent that you still
Peter Beverloo
2015/07/09 18:54:16
The alternative would be to return from the gcm::G
| |
| 469 DecreasePushSubscriptionCount(1, true /* was_pending */); | 489 DecreasePushSubscriptionCount(1, true /* was_pending */); |
| 470 } | 490 } |
| 471 | 491 |
| 472 void PushMessagingServiceImpl::DidRequestPermission( | 492 void PushMessagingServiceImpl::DidRequestPermission( |
| 473 const PushMessagingAppIdentifier& app_identifier, | 493 const PushMessagingAppIdentifier& app_identifier, |
| 474 const std::string& sender_id, | 494 const std::string& sender_id, |
| 475 const content::PushMessagingService::RegisterCallback& register_callback, | 495 const content::PushMessagingService::RegisterCallback& register_callback, |
| 476 ContentSetting content_setting) { | 496 ContentSetting content_setting) { |
| 477 if (content_setting != CONTENT_SETTING_ALLOW) { | 497 if (content_setting != CONTENT_SETTING_ALLOW) { |
| 478 SubscribeEnd(register_callback, | 498 SubscribeEndWithError(register_callback, |
| 479 std::string(), | 499 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
| 480 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | |
| 481 return; | 500 return; |
| 482 } | 501 } |
| 483 | 502 |
| 484 IncreasePushSubscriptionCount(1, true /* is_pending */); | 503 IncreasePushSubscriptionCount(1, true /* is_pending */); |
| 485 std::vector<std::string> sender_ids(1, sender_id); | 504 std::vector<std::string> sender_ids(1, sender_id); |
| 486 GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, | 505 GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, |
| 487 base::Bind(&PushMessagingServiceImpl::DidSubscribe, | 506 base::Bind(&PushMessagingServiceImpl::DidSubscribe, |
| 488 weak_factory_.GetWeakPtr(), | 507 weak_factory_.GetWeakPtr(), |
| 489 app_identifier, register_callback)); | 508 app_identifier, register_callback)); |
| 490 } | 509 } |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 667 CONTENT_SETTING_ALLOW; | 686 CONTENT_SETTING_ALLOW; |
| 668 } | 687 } |
| 669 | 688 |
| 670 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { | 689 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { |
| 671 gcm::GCMProfileService* gcm_profile_service = | 690 gcm::GCMProfileService* gcm_profile_service = |
| 672 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 691 gcm::GCMProfileServiceFactory::GetForProfile(profile_); |
| 673 CHECK(gcm_profile_service); | 692 CHECK(gcm_profile_service); |
| 674 CHECK(gcm_profile_service->driver()); | 693 CHECK(gcm_profile_service->driver()); |
| 675 return gcm_profile_service->driver(); | 694 return gcm_profile_service->driver(); |
| 676 } | 695 } |
| OLD | NEW |