| 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" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
| 15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 16 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
| 17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 18 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 18 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 19 #include "chrome/browser/permissions/permission_manager.h" | 19 #include "chrome/browser/permissions/permission_manager.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" | 21 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" |
| 22 #include "chrome/browser/push_messaging/push_messaging_constants.h" | 22 #include "chrome/browser/push_messaging/push_messaging_constants.h" |
| 23 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" | 23 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" |
| 24 #include "chrome/browser/push_messaging/push_messaging_service_observer.h" |
| 24 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 25 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 25 #include "chrome/browser/ui/chrome_pages.h" | 26 #include "chrome/browser/ui/chrome_pages.h" |
| 26 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
| 27 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
| 28 #include "chrome/grit/generated_resources.h" | 29 #include "chrome/grit/generated_resources.h" |
| 29 #include "components/content_settings/core/browser/host_content_settings_map.h" | 30 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 30 #include "components/gcm_driver/gcm_driver.h" | 31 #include "components/gcm_driver/gcm_driver.h" |
| 31 #include "components/gcm_driver/gcm_profile_service.h" | 32 #include "components/gcm_driver/gcm_profile_service.h" |
| 32 #include "components/pref_registry/pref_registry_syncable.h" | 33 #include "components/pref_registry/pref_registry_syncable.h" |
| 33 #include "components/rappor/rappor_utils.h" | 34 #include "components/rappor/rappor_utils.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 push_service->IncreasePushSubscriptionCount(count, false /* is_pending */); | 115 push_service->IncreasePushSubscriptionCount(count, false /* is_pending */); |
| 115 } | 116 } |
| 116 | 117 |
| 117 PushMessagingServiceImpl::PushMessagingServiceImpl(Profile* profile) | 118 PushMessagingServiceImpl::PushMessagingServiceImpl(Profile* profile) |
| 118 : profile_(profile), | 119 : profile_(profile), |
| 119 push_subscription_count_(0), | 120 push_subscription_count_(0), |
| 120 pending_push_subscription_count_(0), | 121 pending_push_subscription_count_(0), |
| 121 #if defined(ENABLE_NOTIFICATIONS) | 122 #if defined(ENABLE_NOTIFICATIONS) |
| 122 notification_manager_(profile), | 123 notification_manager_(profile), |
| 123 #endif | 124 #endif |
| 125 push_messaging_service_observer_(PushMessagingServiceObserver::Create()), |
| 124 weak_factory_(this) { | 126 weak_factory_(this) { |
| 125 DCHECK(profile); | 127 DCHECK(profile); |
| 126 HostContentSettingsMapFactory::GetForProfile(profile_)->AddObserver(this); | 128 HostContentSettingsMapFactory::GetForProfile(profile_)->AddObserver(this); |
| 127 } | 129 } |
| 128 | 130 |
| 129 PushMessagingServiceImpl::~PushMessagingServiceImpl() { | 131 PushMessagingServiceImpl::~PushMessagingServiceImpl() { |
| 130 HostContentSettingsMapFactory::GetForProfile(profile_)->RemoveObserver(this); | 132 HostContentSettingsMapFactory::GetForProfile(profile_)->RemoveObserver(this); |
| 131 } | 133 } |
| 132 | 134 |
| 133 void PushMessagingServiceImpl::IncreasePushSubscriptionCount(int add, | 135 void PushMessagingServiceImpl::IncreasePushSubscriptionCount(int add, |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 } | 237 } |
| 236 } | 238 } |
| 237 | 239 |
| 238 void PushMessagingServiceImpl::DeliverMessageCallback( | 240 void PushMessagingServiceImpl::DeliverMessageCallback( |
| 239 const std::string& app_id, | 241 const std::string& app_id, |
| 240 const GURL& requesting_origin, | 242 const GURL& requesting_origin, |
| 241 int64 service_worker_registration_id, | 243 int64 service_worker_registration_id, |
| 242 const gcm::IncomingMessage& message, | 244 const gcm::IncomingMessage& message, |
| 243 const base::Closure& message_handled_closure, | 245 const base::Closure& message_handled_closure, |
| 244 content::PushDeliveryStatus status) { | 246 content::PushDeliveryStatus status) { |
| 245 // Remove a single in-flight delivery for |app_id|. This has to be done using | 247 DCHECK_GE(in_flight_message_deliveries_.count(app_id), 1u); |
| 246 // an iterator rather than by value, as the latter removes all entries. | |
| 247 DCHECK(in_flight_message_deliveries_.find(app_id) != | |
| 248 in_flight_message_deliveries_.end()); | |
| 249 | 248 |
| 250 in_flight_message_deliveries_.erase( | 249 base::Closure completion_closure = |
| 251 in_flight_message_deliveries_.find(app_id)); | 250 base::Bind(&PushMessagingServiceImpl::DidHandleMessage, |
| 251 weak_factory_.GetWeakPtr(), app_id, message_handled_closure); |
| 252 | 252 |
| 253 // TODO(mvanouwerkerk): Show a warning in the developer console of the | 253 // TODO(mvanouwerkerk): Show a warning in the developer console of the |
| 254 // Service Worker corresponding to app_id (and/or on an internals page). | 254 // Service Worker corresponding to app_id (and/or on an internals page). |
| 255 // See https://crbug.com/508516 for options. | 255 // See https://crbug.com/508516 for options. |
| 256 switch (status) { | 256 switch (status) { |
| 257 // Call EnforceUserVisibleOnlyRequirements if the message was delivered to | 257 // Call EnforceUserVisibleOnlyRequirements if the message was delivered to |
| 258 // the Service Worker JavaScript, even if the website's event handler failed | 258 // the Service Worker JavaScript, even if the website's event handler failed |
| 259 // (to prevent sites deliberately failing in order to avoid having to show | 259 // (to prevent sites deliberately failing in order to avoid having to show |
| 260 // notifications). | 260 // notifications). |
| 261 case content::PUSH_DELIVERY_STATUS_SUCCESS: | 261 case content::PUSH_DELIVERY_STATUS_SUCCESS: |
| 262 case content::PUSH_DELIVERY_STATUS_EVENT_WAITUNTIL_REJECTED: | 262 case content::PUSH_DELIVERY_STATUS_EVENT_WAITUNTIL_REJECTED: |
| 263 #if defined(ENABLE_NOTIFICATIONS) | 263 #if defined(ENABLE_NOTIFICATIONS) |
| 264 // Only enforce the user visible requirements after the entire queue of | 264 // Only enforce the user visible requirements if this is currently running |
| 265 // incoming messages for |app_id| has been flushed. | 265 // as the delivery callback for the last in-flight message. |
| 266 if (!in_flight_message_deliveries_.count(app_id)) { | 266 if (in_flight_message_deliveries_.count(app_id) == 1) { |
| 267 notification_manager_.EnforceUserVisibleOnlyRequirements( | 267 notification_manager_.EnforceUserVisibleOnlyRequirements( |
| 268 requesting_origin, service_worker_registration_id, | 268 requesting_origin, service_worker_registration_id, |
| 269 message_handled_closure); | 269 completion_closure); |
| 270 } else { | 270 } else { |
| 271 message_handled_closure.Run(); | 271 completion_closure.Run(); |
| 272 } | 272 } |
| 273 #else | 273 #else |
| 274 message_handled_closure.Run(); | 274 completion_closure.Run(); |
| 275 #endif | 275 #endif |
| 276 break; | 276 break; |
| 277 case content::PUSH_DELIVERY_STATUS_INVALID_MESSAGE: | 277 case content::PUSH_DELIVERY_STATUS_INVALID_MESSAGE: |
| 278 case content::PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR: | 278 case content::PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR: |
| 279 message_handled_closure.Run(); | 279 completion_closure.Run(); |
| 280 break; | 280 break; |
| 281 case content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID: | 281 case content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID: |
| 282 case content::PUSH_DELIVERY_STATUS_PERMISSION_DENIED: | 282 case content::PUSH_DELIVERY_STATUS_PERMISSION_DENIED: |
| 283 case content::PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER: | 283 case content::PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER: |
| 284 Unsubscribe( | 284 Unsubscribe(app_id, message.sender_id, |
| 285 app_id, message.sender_id, | 285 base::Bind(&UnregisterCallbackToClosure, completion_closure)); |
| 286 base::Bind(&UnregisterCallbackToClosure, message_handled_closure)); | |
| 287 break; | 286 break; |
| 288 } | 287 } |
| 289 | 288 |
| 290 RecordDeliveryStatus(status); | 289 RecordDeliveryStatus(status); |
| 291 } | 290 } |
| 292 | 291 |
| 292 void PushMessagingServiceImpl::DidHandleMessage( |
| 293 const std::string& app_id, |
| 294 const base::Closure& message_handled_closure) { |
| 295 auto in_flight_iterator = in_flight_message_deliveries_.find(app_id); |
| 296 DCHECK(in_flight_iterator != in_flight_message_deliveries_.end()); |
| 297 |
| 298 // Remove a single in-flight delivery for |app_id|. This has to be done using |
| 299 // an iterator rather than by value, as the latter removes all entries. |
| 300 in_flight_message_deliveries_.erase(in_flight_iterator); |
| 301 |
| 302 message_handled_closure.Run(); |
| 303 |
| 304 if (push_messaging_service_observer_) |
| 305 push_messaging_service_observer_->OnMessageHandled(); |
| 306 } |
| 307 |
| 293 void PushMessagingServiceImpl::SetMessageCallbackForTesting( | 308 void PushMessagingServiceImpl::SetMessageCallbackForTesting( |
| 294 const base::Closure& callback) { | 309 const base::Closure& callback) { |
| 295 message_callback_for_testing_ = callback; | 310 message_callback_for_testing_ = callback; |
| 296 } | 311 } |
| 297 | 312 |
| 298 // Other gcm::GCMAppHandler methods ------------------------------------------- | 313 // Other gcm::GCMAppHandler methods ------------------------------------------- |
| 299 | 314 |
| 300 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) { | 315 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) { |
| 301 // TODO(mvanouwerkerk): Fire push error event on the Service Worker | 316 // TODO(mvanouwerkerk): Fire push error event on the Service Worker |
| 302 // corresponding to app_id. | 317 // corresponding to app_id. |
| (...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 757 switches::kEnableExperimentalWebPlatformFeatures); | 772 switches::kEnableExperimentalWebPlatformFeatures); |
| 758 } | 773 } |
| 759 | 774 |
| 760 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { | 775 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { |
| 761 gcm::GCMProfileService* gcm_profile_service = | 776 gcm::GCMProfileService* gcm_profile_service = |
| 762 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 777 gcm::GCMProfileServiceFactory::GetForProfile(profile_); |
| 763 CHECK(gcm_profile_service); | 778 CHECK(gcm_profile_service); |
| 764 CHECK(gcm_profile_service->driver()); | 779 CHECK(gcm_profile_service->driver()); |
| 765 return gcm_profile_service->driver(); | 780 return gcm_profile_service->driver(); |
| 766 } | 781 } |
| OLD | NEW |