| 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 26 matching lines...) Expand all Loading... |
| 37 #include "components/rappor/rappor_utils.h" | 37 #include "components/rappor/rappor_utils.h" |
| 38 #include "content/public/browser/browser_context.h" | 38 #include "content/public/browser/browser_context.h" |
| 39 #include "content/public/browser/permission_type.h" | 39 #include "content/public/browser/permission_type.h" |
| 40 #include "content/public/browser/render_frame_host.h" | 40 #include "content/public/browser/render_frame_host.h" |
| 41 #include "content/public/browser/service_worker_context.h" | 41 #include "content/public/browser/service_worker_context.h" |
| 42 #include "content/public/browser/storage_partition.h" | 42 #include "content/public/browser/storage_partition.h" |
| 43 #include "content/public/browser/web_contents.h" | 43 #include "content/public/browser/web_contents.h" |
| 44 #include "content/public/common/child_process_host.h" | 44 #include "content/public/common/child_process_host.h" |
| 45 #include "content/public/common/content_switches.h" | 45 #include "content/public/common/content_switches.h" |
| 46 #include "content/public/common/push_messaging_status.h" | 46 #include "content/public/common/push_messaging_status.h" |
| 47 #include "content/public/common/push_subscription_options.h" | |
| 48 #include "ui/base/l10n/l10n_util.h" | 47 #include "ui/base/l10n/l10n_util.h" |
| 49 | 48 |
| 50 #if BUILDFLAG(ENABLE_BACKGROUND) | 49 #if BUILDFLAG(ENABLE_BACKGROUND) |
| 51 #include "chrome/browser/background/background_mode_manager.h" | 50 #include "chrome/browser/background/background_mode_manager.h" |
| 52 #endif | 51 #endif |
| 53 | 52 |
| 54 namespace { | 53 namespace { |
| 55 const int kMaxRegistrations = 1000000; | 54 const int kMaxRegistrations = 1000000; |
| 56 | 55 |
| 57 // Chrome does not yet support silent push messages, and requires websites to | 56 // Chrome does not yet support silent push messages, and requires websites to |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 | 337 |
| 339 GURL PushMessagingServiceImpl::GetPushEndpoint() { | 338 GURL PushMessagingServiceImpl::GetPushEndpoint() { |
| 340 return GURL(std::string(kPushMessagingEndpoint)); | 339 return GURL(std::string(kPushMessagingEndpoint)); |
| 341 } | 340 } |
| 342 | 341 |
| 343 // Subscribe and GetPermissionStatus methods ----------------------------------- | 342 // Subscribe and GetPermissionStatus methods ----------------------------------- |
| 344 | 343 |
| 345 void PushMessagingServiceImpl::SubscribeFromDocument( | 344 void PushMessagingServiceImpl::SubscribeFromDocument( |
| 346 const GURL& requesting_origin, | 345 const GURL& requesting_origin, |
| 347 int64_t service_worker_registration_id, | 346 int64_t service_worker_registration_id, |
| 347 const std::string& sender_id, |
| 348 int renderer_id, | 348 int renderer_id, |
| 349 int render_frame_id, | 349 int render_frame_id, |
| 350 const content::PushSubscriptionOptions& options, | 350 bool user_visible, |
| 351 const content::PushMessagingService::RegisterCallback& callback) { | 351 const content::PushMessagingService::RegisterCallback& callback) { |
| 352 PushMessagingAppIdentifier app_identifier = | 352 PushMessagingAppIdentifier app_identifier = |
| 353 PushMessagingAppIdentifier::Generate(requesting_origin, | 353 PushMessagingAppIdentifier::Generate(requesting_origin, |
| 354 service_worker_registration_id); | 354 service_worker_registration_id); |
| 355 | 355 |
| 356 if (push_subscription_count_ + pending_push_subscription_count_ >= | 356 if (push_subscription_count_ + pending_push_subscription_count_ >= |
| 357 kMaxRegistrations) { | 357 kMaxRegistrations) { |
| 358 SubscribeEndWithError(callback, | 358 SubscribeEndWithError(callback, |
| 359 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 359 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); |
| 360 return; | 360 return; |
| 361 } | 361 } |
| 362 | 362 |
| 363 content::RenderFrameHost* render_frame_host = | 363 content::RenderFrameHost* render_frame_host = |
| 364 content::RenderFrameHost::FromID(renderer_id, render_frame_id); | 364 content::RenderFrameHost::FromID(renderer_id, render_frame_id); |
| 365 content::WebContents* web_contents = | 365 content::WebContents* web_contents = |
| 366 content::WebContents::FromRenderFrameHost(render_frame_host); | 366 content::WebContents::FromRenderFrameHost(render_frame_host); |
| 367 if (!web_contents) | 367 if (!web_contents) |
| 368 return; | 368 return; |
| 369 | 369 |
| 370 if (!options.user_visible_only) { | 370 if (!user_visible) { |
| 371 web_contents->GetMainFrame()->AddMessageToConsole( | 371 web_contents->GetMainFrame()->AddMessageToConsole( |
| 372 content::CONSOLE_MESSAGE_LEVEL_ERROR, kSilentPushUnsupportedMessage); | 372 content::CONSOLE_MESSAGE_LEVEL_ERROR, kSilentPushUnsupportedMessage); |
| 373 | 373 |
| 374 SubscribeEndWithError(callback, | 374 SubscribeEndWithError(callback, |
| 375 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 375 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
| 376 return; | 376 return; |
| 377 } | 377 } |
| 378 | 378 |
| 379 // Push does not allow permission requests from iframes. | 379 // Push does not allow permission requests from iframes. |
| 380 profile_->GetPermissionManager()->RequestPermission( | 380 profile_->GetPermissionManager()->RequestPermission( |
| 381 content::PermissionType::PUSH_MESSAGING, web_contents->GetMainFrame(), | 381 content::PermissionType::PUSH_MESSAGING, web_contents->GetMainFrame(), |
| 382 requesting_origin, | 382 requesting_origin, |
| 383 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, | 383 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, |
| 384 weak_factory_.GetWeakPtr(), app_identifier, options, | 384 weak_factory_.GetWeakPtr(), app_identifier, sender_id, |
| 385 callback)); | 385 callback)); |
| 386 } | 386 } |
| 387 | 387 |
| 388 void PushMessagingServiceImpl::SubscribeFromWorker( | 388 void PushMessagingServiceImpl::SubscribeFromWorker( |
| 389 const GURL& requesting_origin, | 389 const GURL& requesting_origin, |
| 390 int64_t service_worker_registration_id, | 390 int64_t service_worker_registration_id, |
| 391 const content::PushSubscriptionOptions& options, | 391 const std::string& sender_id, |
| 392 bool user_visible, |
| 392 const content::PushMessagingService::RegisterCallback& register_callback) { | 393 const content::PushMessagingService::RegisterCallback& register_callback) { |
| 393 PushMessagingAppIdentifier app_identifier = | 394 PushMessagingAppIdentifier app_identifier = |
| 394 PushMessagingAppIdentifier::Generate(requesting_origin, | 395 PushMessagingAppIdentifier::Generate(requesting_origin, |
| 395 service_worker_registration_id); | 396 service_worker_registration_id); |
| 396 | 397 |
| 397 if (push_subscription_count_ + pending_push_subscription_count_ >= | 398 if (push_subscription_count_ + pending_push_subscription_count_ >= |
| 398 kMaxRegistrations) { | 399 kMaxRegistrations) { |
| 399 SubscribeEndWithError(register_callback, | 400 SubscribeEndWithError(register_callback, |
| 400 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 401 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); |
| 401 return; | 402 return; |
| 402 } | 403 } |
| 403 | 404 |
| 404 blink::WebPushPermissionStatus permission_status = | 405 blink::WebPushPermissionStatus permission_status = |
| 405 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin, | 406 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin, |
| 406 options.user_visible_only); | 407 user_visible); |
| 407 | 408 |
| 408 if (permission_status != blink::WebPushPermissionStatusGranted) { | 409 if (permission_status != blink::WebPushPermissionStatusGranted) { |
| 409 SubscribeEndWithError(register_callback, | 410 SubscribeEndWithError(register_callback, |
| 410 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 411 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
| 411 return; | 412 return; |
| 412 } | 413 } |
| 413 | 414 |
| 414 IncreasePushSubscriptionCount(1, true /* is_pending */); | 415 IncreasePushSubscriptionCount(1, true /* is_pending */); |
| 415 std::vector<std::string> sender_ids(1, options.sender_info); | 416 std::vector<std::string> sender_ids(1, sender_id); |
| 416 GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, | 417 GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, |
| 417 base::Bind(&PushMessagingServiceImpl::DidSubscribe, | 418 base::Bind(&PushMessagingServiceImpl::DidSubscribe, |
| 418 weak_factory_.GetWeakPtr(), | 419 weak_factory_.GetWeakPtr(), |
| 419 app_identifier, register_callback)); | 420 app_identifier, register_callback)); |
| 420 } | 421 } |
| 421 | 422 |
| 422 blink::WebPushPermissionStatus PushMessagingServiceImpl::GetPermissionStatus( | 423 blink::WebPushPermissionStatus PushMessagingServiceImpl::GetPermissionStatus( |
| 423 const GURL& origin, | 424 const GURL& origin, |
| 424 bool user_visible) { | 425 bool user_visible) { |
| 425 if (!user_visible) | 426 if (!user_visible) |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 IncreasePushSubscriptionCount(1, false /* is_pending */); | 509 IncreasePushSubscriptionCount(1, false /* is_pending */); |
| 509 | 510 |
| 510 SubscribeEnd(callback, subscription_id, | 511 SubscribeEnd(callback, subscription_id, |
| 511 std::vector<uint8_t>(p256dh.begin(), p256dh.end()), | 512 std::vector<uint8_t>(p256dh.begin(), p256dh.end()), |
| 512 std::vector<uint8_t>(auth_secret.begin(), auth_secret.end()), | 513 std::vector<uint8_t>(auth_secret.begin(), auth_secret.end()), |
| 513 content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE); | 514 content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE); |
| 514 } | 515 } |
| 515 | 516 |
| 516 void PushMessagingServiceImpl::DidRequestPermission( | 517 void PushMessagingServiceImpl::DidRequestPermission( |
| 517 const PushMessagingAppIdentifier& app_identifier, | 518 const PushMessagingAppIdentifier& app_identifier, |
| 518 const content::PushSubscriptionOptions& options, | 519 const std::string& sender_id, |
| 519 const content::PushMessagingService::RegisterCallback& register_callback, | 520 const content::PushMessagingService::RegisterCallback& register_callback, |
| 520 content::PermissionStatus permission_status) { | 521 content::PermissionStatus permission_status) { |
| 521 if (permission_status != content::PermissionStatus::GRANTED) { | 522 if (permission_status != content::PermissionStatus::GRANTED) { |
| 522 SubscribeEndWithError(register_callback, | 523 SubscribeEndWithError(register_callback, |
| 523 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 524 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
| 524 return; | 525 return; |
| 525 } | 526 } |
| 526 | 527 |
| 527 IncreasePushSubscriptionCount(1, true /* is_pending */); | 528 IncreasePushSubscriptionCount(1, true /* is_pending */); |
| 528 std::vector<std::string> sender_ids(1, options.sender_info); | 529 std::vector<std::string> sender_ids(1, sender_id); |
| 529 GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, | 530 GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, |
| 530 base::Bind(&PushMessagingServiceImpl::DidSubscribe, | 531 base::Bind(&PushMessagingServiceImpl::DidSubscribe, |
| 531 weak_factory_.GetWeakPtr(), | 532 weak_factory_.GetWeakPtr(), |
| 532 app_identifier, register_callback)); | 533 app_identifier, register_callback)); |
| 533 } | 534 } |
| 534 | 535 |
| 535 // GetEncryptionInfo methods --------------------------------------------------- | 536 // GetEncryptionInfo methods --------------------------------------------------- |
| 536 | 537 |
| 537 void PushMessagingServiceImpl::GetEncryptionInfo( | 538 void PushMessagingServiceImpl::GetEncryptionInfo( |
| 538 const GURL& origin, | 539 const GURL& origin, |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 754 blink::WebPushPermissionStatusGranted; | 755 blink::WebPushPermissionStatusGranted; |
| 755 } | 756 } |
| 756 | 757 |
| 757 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { | 758 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { |
| 758 gcm::GCMProfileService* gcm_profile_service = | 759 gcm::GCMProfileService* gcm_profile_service = |
| 759 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 760 gcm::GCMProfileServiceFactory::GetForProfile(profile_); |
| 760 CHECK(gcm_profile_service); | 761 CHECK(gcm_profile_service); |
| 761 CHECK(gcm_profile_service->driver()); | 762 CHECK(gcm_profile_service->driver()); |
| 762 return gcm_profile_service->driver(); | 763 return gcm_profile_service->driver(); |
| 763 } | 764 } |
| OLD | NEW |