Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(185)

Side by Side Diff: chrome/browser/push_messaging/push_messaging_service_impl.cc

Issue 1485743002: Push API: test that default notification is shown on Android when needed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@PushMessagingInstrumentationTest
Patch Set: Address peter's comments. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
124 weak_factory_(this) { 125 weak_factory_(this) {
125 DCHECK(profile); 126 DCHECK(profile);
126 HostContentSettingsMapFactory::GetForProfile(profile_)->AddObserver(this); 127 HostContentSettingsMapFactory::GetForProfile(profile_)->AddObserver(this);
128 push_messaging_service_observer_.reset(
129 PushMessagingServiceObserver::Create());
127 } 130 }
128 131
129 PushMessagingServiceImpl::~PushMessagingServiceImpl() { 132 PushMessagingServiceImpl::~PushMessagingServiceImpl() {
130 HostContentSettingsMapFactory::GetForProfile(profile_)->RemoveObserver(this); 133 HostContentSettingsMapFactory::GetForProfile(profile_)->RemoveObserver(this);
131 } 134 }
132 135
133 void PushMessagingServiceImpl::IncreasePushSubscriptionCount(int add, 136 void PushMessagingServiceImpl::IncreasePushSubscriptionCount(int add,
134 bool is_pending) { 137 bool is_pending) {
135 DCHECK(add > 0); 138 DCHECK(add > 0);
136 if (push_subscription_count_ + pending_push_subscription_count_ == 0) { 139 if (push_subscription_count_ + pending_push_subscription_count_ == 0) {
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 } 238 }
236 } 239 }
237 240
238 void PushMessagingServiceImpl::DeliverMessageCallback( 241 void PushMessagingServiceImpl::DeliverMessageCallback(
239 const std::string& app_id, 242 const std::string& app_id,
240 const GURL& requesting_origin, 243 const GURL& requesting_origin,
241 int64 service_worker_registration_id, 244 int64 service_worker_registration_id,
242 const gcm::IncomingMessage& message, 245 const gcm::IncomingMessage& message,
243 const base::Closure& message_handled_closure, 246 const base::Closure& message_handled_closure,
244 content::PushDeliveryStatus status) { 247 content::PushDeliveryStatus status) {
245 // Remove a single in-flight delivery for |app_id|. This has to be done using 248 DCHECK(in_flight_message_deliveries_.count(app_id) >= 1);
Peter Beverloo 2015/12/10 18:47:51 DCHECK_GE(in_flight_message_deliveries_.count(app_
Michael van Ouwerkerk 2015/12/11 11:23:54 Done.
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 249
250 in_flight_message_deliveries_.erase( 250 base::Closure completion_closure =
251 in_flight_message_deliveries_.find(app_id)); 251 base::Bind(&PushMessagingServiceImpl::OnMessageHandled,
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::OnMessageHandled(
294 const std::string& app_id,
295 const base::Closure& message_handled_closure) {
296 DCHECK(in_flight_message_deliveries_.find(app_id) !=
297 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(
302 in_flight_message_deliveries_.find(app_id));
303
304 message_handled_closure.Run();
305
306 if (push_messaging_service_observer_)
307 push_messaging_service_observer_->OnMessageHandled();
308 }
309
293 void PushMessagingServiceImpl::SetMessageCallbackForTesting( 310 void PushMessagingServiceImpl::SetMessageCallbackForTesting(
294 const base::Closure& callback) { 311 const base::Closure& callback) {
295 message_callback_for_testing_ = callback; 312 message_callback_for_testing_ = callback;
296 } 313 }
297 314
298 // Other gcm::GCMAppHandler methods ------------------------------------------- 315 // Other gcm::GCMAppHandler methods -------------------------------------------
299 316
300 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) { 317 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) {
301 // TODO(mvanouwerkerk): Fire push error event on the Service Worker 318 // TODO(mvanouwerkerk): Fire push error event on the Service Worker
302 // corresponding to app_id. 319 // corresponding to app_id.
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 switches::kEnableExperimentalWebPlatformFeatures); 774 switches::kEnableExperimentalWebPlatformFeatures);
758 } 775 }
759 776
760 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { 777 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const {
761 gcm::GCMProfileService* gcm_profile_service = 778 gcm::GCMProfileService* gcm_profile_service =
762 gcm::GCMProfileServiceFactory::GetForProfile(profile_); 779 gcm::GCMProfileServiceFactory::GetForProfile(profile_);
763 CHECK(gcm_profile_service); 780 CHECK(gcm_profile_service);
764 CHECK(gcm_profile_service->driver()); 781 CHECK(gcm_profile_service->driver());
765 return gcm_profile_service->driver(); 782 return gcm_profile_service->driver();
766 } 783 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698