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 // TODO(mvanouwerkerk): Use ScopedClosureRunner for this. |
251 in_flight_message_deliveries_.find(app_id)); | 250 base::Closure completion_closure = |
| 251 base::Bind(&PushMessagingServiceImpl::DidHandleMessage, |
| 252 weak_factory_.GetWeakPtr(), app_id, message_handled_closure); |
252 | 253 |
253 // TODO(mvanouwerkerk): Show a warning in the developer console of the | 254 // TODO(mvanouwerkerk): Show a warning in the developer console of the |
254 // Service Worker corresponding to app_id (and/or on an internals page). | 255 // Service Worker corresponding to app_id (and/or on an internals page). |
255 // See https://crbug.com/508516 for options. | 256 // See https://crbug.com/508516 for options. |
256 switch (status) { | 257 switch (status) { |
257 // Call EnforceUserVisibleOnlyRequirements if the message was delivered to | 258 // Call EnforceUserVisibleOnlyRequirements if the message was delivered to |
258 // the Service Worker JavaScript, even if the website's event handler failed | 259 // 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 | 260 // (to prevent sites deliberately failing in order to avoid having to show |
260 // notifications). | 261 // notifications). |
261 case content::PUSH_DELIVERY_STATUS_SUCCESS: | 262 case content::PUSH_DELIVERY_STATUS_SUCCESS: |
262 case content::PUSH_DELIVERY_STATUS_EVENT_WAITUNTIL_REJECTED: | 263 case content::PUSH_DELIVERY_STATUS_EVENT_WAITUNTIL_REJECTED: |
263 #if defined(ENABLE_NOTIFICATIONS) | 264 #if defined(ENABLE_NOTIFICATIONS) |
264 // Only enforce the user visible requirements after the entire queue of | 265 // Only enforce the user visible requirements if this is currently running |
265 // incoming messages for |app_id| has been flushed. | 266 // as the delivery callback for the last in-flight message. |
266 if (!in_flight_message_deliveries_.count(app_id)) { | 267 if (in_flight_message_deliveries_.count(app_id) == 1) { |
267 notification_manager_.EnforceUserVisibleOnlyRequirements( | 268 notification_manager_.EnforceUserVisibleOnlyRequirements( |
268 requesting_origin, service_worker_registration_id, | 269 requesting_origin, service_worker_registration_id, |
269 message_handled_closure); | 270 completion_closure); |
270 } else { | 271 } else { |
271 message_handled_closure.Run(); | 272 completion_closure.Run(); |
272 } | 273 } |
273 #else | 274 #else |
274 message_handled_closure.Run(); | 275 completion_closure.Run(); |
275 #endif | 276 #endif |
276 break; | 277 break; |
277 case content::PUSH_DELIVERY_STATUS_INVALID_MESSAGE: | 278 case content::PUSH_DELIVERY_STATUS_INVALID_MESSAGE: |
278 case content::PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR: | 279 case content::PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR: |
279 message_handled_closure.Run(); | 280 completion_closure.Run(); |
280 break; | 281 break; |
281 case content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID: | 282 case content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID: |
282 case content::PUSH_DELIVERY_STATUS_PERMISSION_DENIED: | 283 case content::PUSH_DELIVERY_STATUS_PERMISSION_DENIED: |
283 case content::PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER: | 284 case content::PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER: |
284 Unsubscribe( | 285 Unsubscribe(app_id, message.sender_id, |
285 app_id, message.sender_id, | 286 base::Bind(&UnregisterCallbackToClosure, completion_closure)); |
286 base::Bind(&UnregisterCallbackToClosure, message_handled_closure)); | |
287 break; | 287 break; |
288 } | 288 } |
289 | 289 |
290 RecordDeliveryStatus(status); | 290 RecordDeliveryStatus(status); |
291 } | 291 } |
292 | 292 |
| 293 void PushMessagingServiceImpl::DidHandleMessage( |
| 294 const std::string& app_id, |
| 295 const base::Closure& message_handled_closure) { |
| 296 auto in_flight_iterator = in_flight_message_deliveries_.find(app_id); |
| 297 DCHECK(in_flight_iterator != in_flight_message_deliveries_.end()); |
| 298 |
| 299 // Remove a single in-flight delivery for |app_id|. This has to be done using |
| 300 // an iterator rather than by value, as the latter removes all entries. |
| 301 in_flight_message_deliveries_.erase(in_flight_iterator); |
| 302 |
| 303 message_handled_closure.Run(); |
| 304 |
| 305 if (push_messaging_service_observer_) |
| 306 push_messaging_service_observer_->OnMessageHandled(); |
| 307 } |
| 308 |
293 void PushMessagingServiceImpl::SetMessageCallbackForTesting( | 309 void PushMessagingServiceImpl::SetMessageCallbackForTesting( |
294 const base::Closure& callback) { | 310 const base::Closure& callback) { |
295 message_callback_for_testing_ = callback; | 311 message_callback_for_testing_ = callback; |
296 } | 312 } |
297 | 313 |
298 // Other gcm::GCMAppHandler methods ------------------------------------------- | 314 // Other gcm::GCMAppHandler methods ------------------------------------------- |
299 | 315 |
300 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) { | 316 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) { |
301 // TODO(mvanouwerkerk): Fire push error event on the Service Worker | 317 // TODO(mvanouwerkerk): Fire push error event on the Service Worker |
302 // corresponding to app_id. | 318 // corresponding to app_id. |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
757 switches::kEnableExperimentalWebPlatformFeatures); | 773 switches::kEnableExperimentalWebPlatformFeatures); |
758 } | 774 } |
759 | 775 |
760 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { | 776 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { |
761 gcm::GCMProfileService* gcm_profile_service = | 777 gcm::GCMProfileService* gcm_profile_service = |
762 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 778 gcm::GCMProfileServiceFactory::GetForProfile(profile_); |
763 CHECK(gcm_profile_service); | 779 CHECK(gcm_profile_service); |
764 CHECK(gcm_profile_service->driver()); | 780 CHECK(gcm_profile_service->driver()); |
765 return gcm_profile_service->driver(); | 781 return gcm_profile_service->driver(); |
766 } | 782 } |
OLD | NEW |