| 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 25 matching lines...) Expand all  Loading... | 
| 36 #include "components/rappor/rappor_utils.h" | 36 #include "components/rappor/rappor_utils.h" | 
| 37 #include "content/public/browser/browser_context.h" | 37 #include "content/public/browser/browser_context.h" | 
| 38 #include "content/public/browser/permission_type.h" | 38 #include "content/public/browser/permission_type.h" | 
| 39 #include "content/public/browser/render_frame_host.h" | 39 #include "content/public/browser/render_frame_host.h" | 
| 40 #include "content/public/browser/service_worker_context.h" | 40 #include "content/public/browser/service_worker_context.h" | 
| 41 #include "content/public/browser/storage_partition.h" | 41 #include "content/public/browser/storage_partition.h" | 
| 42 #include "content/public/browser/web_contents.h" | 42 #include "content/public/browser/web_contents.h" | 
| 43 #include "content/public/common/child_process_host.h" | 43 #include "content/public/common/child_process_host.h" | 
| 44 #include "content/public/common/content_switches.h" | 44 #include "content/public/common/content_switches.h" | 
| 45 #include "content/public/common/push_messaging_status.h" | 45 #include "content/public/common/push_messaging_status.h" | 
|  | 46 #include "content/public/common/push_subscription_options.h" | 
| 46 #include "ui/base/l10n/l10n_util.h" | 47 #include "ui/base/l10n/l10n_util.h" | 
| 47 | 48 | 
| 48 #if defined(ENABLE_BACKGROUND) | 49 #if defined(ENABLE_BACKGROUND) | 
| 49 #include "chrome/browser/background/background_mode_manager.h" | 50 #include "chrome/browser/background/background_mode_manager.h" | 
| 50 #endif | 51 #endif | 
| 51 | 52 | 
| 52 namespace { | 53 namespace { | 
| 53 const int kMaxRegistrations = 1000000; | 54 const int kMaxRegistrations = 1000000; | 
| 54 | 55 | 
| 55 // 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... | 
| 336 | 337 | 
| 337 GURL PushMessagingServiceImpl::GetPushEndpoint() { | 338 GURL PushMessagingServiceImpl::GetPushEndpoint() { | 
| 338   return GURL(std::string(kPushMessagingEndpoint)); | 339   return GURL(std::string(kPushMessagingEndpoint)); | 
| 339 } | 340 } | 
| 340 | 341 | 
| 341 // Subscribe and GetPermissionStatus methods ----------------------------------- | 342 // Subscribe and GetPermissionStatus methods ----------------------------------- | 
| 342 | 343 | 
| 343 void PushMessagingServiceImpl::SubscribeFromDocument( | 344 void PushMessagingServiceImpl::SubscribeFromDocument( | 
| 344     const GURL& requesting_origin, | 345     const GURL& requesting_origin, | 
| 345     int64_t service_worker_registration_id, | 346     int64_t service_worker_registration_id, | 
| 346     const std::string& sender_id, |  | 
| 347     int renderer_id, | 347     int renderer_id, | 
| 348     int render_frame_id, | 348     int render_frame_id, | 
| 349     bool user_visible, | 349     const content::PushSubscriptionOptions& options, | 
| 350     const content::PushMessagingService::RegisterCallback& callback) { | 350     const content::PushMessagingService::RegisterCallback& callback) { | 
| 351   PushMessagingAppIdentifier app_identifier = | 351   PushMessagingAppIdentifier app_identifier = | 
| 352       PushMessagingAppIdentifier::Generate(requesting_origin, | 352       PushMessagingAppIdentifier::Generate(requesting_origin, | 
| 353                                            service_worker_registration_id); | 353                                            service_worker_registration_id); | 
| 354 | 354 | 
| 355   if (push_subscription_count_ + pending_push_subscription_count_ >= | 355   if (push_subscription_count_ + pending_push_subscription_count_ >= | 
| 356       kMaxRegistrations) { | 356       kMaxRegistrations) { | 
| 357     SubscribeEndWithError(callback, | 357     SubscribeEndWithError(callback, | 
| 358                           content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 358                           content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 
| 359     return; | 359     return; | 
| 360   } | 360   } | 
| 361 | 361 | 
| 362   content::RenderFrameHost* render_frame_host = | 362   content::RenderFrameHost* render_frame_host = | 
| 363       content::RenderFrameHost::FromID(renderer_id, render_frame_id); | 363       content::RenderFrameHost::FromID(renderer_id, render_frame_id); | 
| 364   content::WebContents* web_contents = | 364   content::WebContents* web_contents = | 
| 365       content::WebContents::FromRenderFrameHost(render_frame_host); | 365       content::WebContents::FromRenderFrameHost(render_frame_host); | 
| 366   if (!web_contents) | 366   if (!web_contents) | 
| 367     return; | 367     return; | 
| 368 | 368 | 
| 369   if (!user_visible) { | 369   if (!options.user_visible_only) { | 
| 370     web_contents->GetMainFrame()->AddMessageToConsole( | 370     web_contents->GetMainFrame()->AddMessageToConsole( | 
| 371         content::CONSOLE_MESSAGE_LEVEL_ERROR, kSilentPushUnsupportedMessage); | 371         content::CONSOLE_MESSAGE_LEVEL_ERROR, kSilentPushUnsupportedMessage); | 
| 372 | 372 | 
| 373     SubscribeEndWithError(callback, | 373     SubscribeEndWithError(callback, | 
| 374                           content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 374                           content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 
| 375     return; | 375     return; | 
| 376   } | 376   } | 
| 377 | 377 | 
|  | 378   // TODO(harkness) Check the validity of the options.sender_info field. | 
|  | 379 | 
| 378   // Push does not allow permission requests from iframes. | 380   // Push does not allow permission requests from iframes. | 
| 379   profile_->GetPermissionManager()->RequestPermission( | 381   profile_->GetPermissionManager()->RequestPermission( | 
| 380       content::PermissionType::PUSH_MESSAGING, web_contents->GetMainFrame(), | 382       content::PermissionType::PUSH_MESSAGING, web_contents->GetMainFrame(), | 
| 381       requesting_origin, true /* user_gesture */, | 383       requesting_origin, true /* user_gesture */, | 
| 382       base::Bind(&PushMessagingServiceImpl::DidRequestPermission, | 384       base::Bind(&PushMessagingServiceImpl::DidRequestPermission, | 
| 383                  weak_factory_.GetWeakPtr(), app_identifier, sender_id, | 385                  weak_factory_.GetWeakPtr(), app_identifier, options, | 
| 384                  callback)); | 386                  callback)); | 
| 385 } | 387 } | 
| 386 | 388 | 
| 387 void PushMessagingServiceImpl::SubscribeFromWorker( | 389 void PushMessagingServiceImpl::SubscribeFromWorker( | 
| 388     const GURL& requesting_origin, | 390     const GURL& requesting_origin, | 
| 389     int64_t service_worker_registration_id, | 391     int64_t service_worker_registration_id, | 
| 390     const std::string& sender_id, | 392     const content::PushSubscriptionOptions& options, | 
| 391     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   GURL embedding_origin = requesting_origin; | 405   GURL embedding_origin = requesting_origin; | 
| 405   blink::WebPushPermissionStatus permission_status = | 406   blink::WebPushPermissionStatus permission_status = | 
| 406       PushMessagingServiceImpl::GetPermissionStatus( | 407       PushMessagingServiceImpl::GetPermissionStatus( | 
| 407           requesting_origin, embedding_origin, user_visible); | 408           requesting_origin, embedding_origin, options.user_visible_only); | 
| 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, sender_id); | 416   std::vector<std::string> sender_ids(1, options.sender_info); | 
| 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& requesting_origin, | 424     const GURL& requesting_origin, | 
| 424     const GURL& embedding_origin, | 425     const GURL& embedding_origin, | 
| 425     bool user_visible) { | 426     bool user_visible) { | 
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 515   IncreasePushSubscriptionCount(1, false /* is_pending */); | 516   IncreasePushSubscriptionCount(1, false /* is_pending */); | 
| 516 | 517 | 
| 517   SubscribeEnd(callback, subscription_id, | 518   SubscribeEnd(callback, subscription_id, | 
| 518                std::vector<uint8_t>(p256dh.begin(), p256dh.end()), | 519                std::vector<uint8_t>(p256dh.begin(), p256dh.end()), | 
| 519                std::vector<uint8_t>(auth_secret.begin(), auth_secret.end()), | 520                std::vector<uint8_t>(auth_secret.begin(), auth_secret.end()), | 
| 520                content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE); | 521                content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE); | 
| 521 } | 522 } | 
| 522 | 523 | 
| 523 void PushMessagingServiceImpl::DidRequestPermission( | 524 void PushMessagingServiceImpl::DidRequestPermission( | 
| 524     const PushMessagingAppIdentifier& app_identifier, | 525     const PushMessagingAppIdentifier& app_identifier, | 
| 525     const std::string& sender_id, | 526     const content::PushSubscriptionOptions& options, | 
| 526     const content::PushMessagingService::RegisterCallback& register_callback, | 527     const content::PushMessagingService::RegisterCallback& register_callback, | 
| 527     content::PermissionStatus permission_status) { | 528     content::PermissionStatus permission_status) { | 
| 528   if (permission_status != content::PermissionStatus::GRANTED) { | 529   if (permission_status != content::PermissionStatus::GRANTED) { | 
| 529     SubscribeEndWithError(register_callback, | 530     SubscribeEndWithError(register_callback, | 
| 530                           content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 531                           content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 
| 531     return; | 532     return; | 
| 532   } | 533   } | 
| 533 | 534 | 
| 534   IncreasePushSubscriptionCount(1, true /* is_pending */); | 535   IncreasePushSubscriptionCount(1, true /* is_pending */); | 
| 535   std::vector<std::string> sender_ids(1, sender_id); | 536   std::vector<std::string> sender_ids(1, options.sender_info); | 
| 536   GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, | 537   GetGCMDriver()->Register(app_identifier.app_id(), sender_ids, | 
| 537                            base::Bind(&PushMessagingServiceImpl::DidSubscribe, | 538                            base::Bind(&PushMessagingServiceImpl::DidSubscribe, | 
| 538                                       weak_factory_.GetWeakPtr(), | 539                                       weak_factory_.GetWeakPtr(), | 
| 539                                       app_identifier, register_callback)); | 540                                       app_identifier, register_callback)); | 
| 540 } | 541 } | 
| 541 | 542 | 
| 542 // GetEncryptionInfo methods --------------------------------------------------- | 543 // GetEncryptionInfo methods --------------------------------------------------- | 
| 543 | 544 | 
| 544 void PushMessagingServiceImpl::GetEncryptionInfo( | 545 void PushMessagingServiceImpl::GetEncryptionInfo( | 
| 545     const GURL& origin, | 546     const GURL& origin, | 
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 773       switches::kEnableExperimentalWebPlatformFeatures); | 774       switches::kEnableExperimentalWebPlatformFeatures); | 
| 774 } | 775 } | 
| 775 | 776 | 
| 776 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { | 777 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { | 
| 777   gcm::GCMProfileService* gcm_profile_service = | 778   gcm::GCMProfileService* gcm_profile_service = | 
| 778       gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 779       gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 
| 779   CHECK(gcm_profile_service); | 780   CHECK(gcm_profile_service); | 
| 780   CHECK(gcm_profile_service->driver()); | 781   CHECK(gcm_profile_service->driver()); | 
| 781   return gcm_profile_service->driver(); | 782   return gcm_profile_service->driver(); | 
| 782 } | 783 } | 
| OLD | NEW | 
|---|