Chromium Code Reviews| 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 <bitset> | 7 #include <bitset> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/barrier_closure.h" | |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | |
| 11 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 12 #include "base/logging.h" | 14 #include "base/logging.h" |
| 13 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 14 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
| 15 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 17 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
| 18 #include "chrome/browser/notifications/notification_ui_manager.h" | 20 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 19 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 21 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 } | 322 } |
| 321 #endif | 323 #endif |
| 322 | 324 |
| 323 // Don't track push messages that didn't show a notification but were exempt | 325 // Don't track push messages that didn't show a notification but were exempt |
| 324 // from needing to do so. | 326 // from needing to do so. |
| 325 if (notification_shown || notification_needed) { | 327 if (notification_shown || notification_needed) { |
| 326 content::ServiceWorkerContext* service_worker_context = | 328 content::ServiceWorkerContext* service_worker_context = |
| 327 content::BrowserContext::GetStoragePartitionForSite( | 329 content::BrowserContext::GetStoragePartitionForSite( |
| 328 profile_, requesting_origin)->GetServiceWorkerContext(); | 330 profile_, requesting_origin)->GetServiceWorkerContext(); |
| 329 | 331 |
| 330 PushMessagingService::GetNotificationsShownByLastFewPushes( | 332 GetNotificationsShownByLastFewPushes( |
| 331 service_worker_context, service_worker_registration_id, | 333 service_worker_context, service_worker_registration_id, |
| 332 base::Bind(&PushMessagingServiceImpl::DidGetNotificationsShown, | 334 base::Bind(&PushMessagingServiceImpl::DidGetNotificationsShown, |
| 333 weak_factory_.GetWeakPtr(), | 335 weak_factory_.GetWeakPtr(), |
| 334 requesting_origin, service_worker_registration_id, | 336 requesting_origin, service_worker_registration_id, |
| 335 notification_shown, notification_needed)); | 337 notification_shown, notification_needed)); |
| 336 } else { | 338 } else { |
| 337 RecordUserVisibleStatus( | 339 RecordUserVisibleStatus( |
| 338 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); | 340 content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); |
| 339 } | 341 } |
| 340 #endif // defined(ENABLE_NOTIFICATIONS) | 342 #endif // defined(ENABLE_NOTIFICATIONS) |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 358 std::bitset<MISSED_NOTIFICATIONS_LENGTH> missed_notifications(data); | 360 std::bitset<MISSED_NOTIFICATIONS_LENGTH> missed_notifications(data); |
| 359 | 361 |
| 360 bool needed_but_not_shown = notification_needed && !notification_shown; | 362 bool needed_but_not_shown = notification_needed && !notification_shown; |
| 361 | 363 |
| 362 // New entries go at the end, and old ones are shifted off the beginning once | 364 // New entries go at the end, and old ones are shifted off the beginning once |
| 363 // the history length is exceeded. | 365 // the history length is exceeded. |
| 364 missed_notifications <<= 1; | 366 missed_notifications <<= 1; |
| 365 missed_notifications[0] = needed_but_not_shown; | 367 missed_notifications[0] = needed_but_not_shown; |
| 366 std::string updated_data(missed_notifications. | 368 std::string updated_data(missed_notifications. |
| 367 to_string<char, std::string::traits_type, std::string::allocator_type>()); | 369 to_string<char, std::string::traits_type, std::string::allocator_type>()); |
| 368 PushMessagingService::SetNotificationsShownByLastFewPushes( | 370 SetNotificationsShownByLastFewPushes( |
| 369 service_worker_context, service_worker_registration_id, | 371 service_worker_context, service_worker_registration_id, |
| 370 requesting_origin, updated_data, | 372 requesting_origin, updated_data, |
| 371 base::Bind(&IgnoreResult)); // This is a heuristic; ignore failure. | 373 base::Bind(&IgnoreResult)); // This is a heuristic; ignore failure. |
| 372 | 374 |
| 373 if (notification_shown) { | 375 if (notification_shown) { |
| 374 RecordUserVisibleStatus( | 376 RecordUserVisibleStatus( |
| 375 notification_needed | 377 notification_needed |
| 376 ? content::PUSH_USER_VISIBLE_STATUS_REQUIRED_AND_SHOWN | 378 ? content::PUSH_USER_VISIBLE_STATUS_REQUIRED_AND_SHOWN |
| 377 : content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_BUT_SHOWN); | 379 : content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_BUT_SHOWN); |
| 378 return; | 380 return; |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 688 void PushMessagingServiceImpl::OnContentSettingChanged( | 690 void PushMessagingServiceImpl::OnContentSettingChanged( |
| 689 const ContentSettingsPattern& primary_pattern, | 691 const ContentSettingsPattern& primary_pattern, |
| 690 const ContentSettingsPattern& secondary_pattern, | 692 const ContentSettingsPattern& secondary_pattern, |
| 691 ContentSettingsType content_type, | 693 ContentSettingsType content_type, |
| 692 std::string resource_identifier) { | 694 std::string resource_identifier) { |
| 693 if (content_type != CONTENT_SETTINGS_TYPE_PUSH_MESSAGING && | 695 if (content_type != CONTENT_SETTINGS_TYPE_PUSH_MESSAGING && |
| 694 content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS) { | 696 content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS) { |
| 695 return; | 697 return; |
| 696 } | 698 } |
| 697 | 699 |
| 698 for (const auto& id : PushMessagingApplicationId::GetAll(profile_)) { | 700 std::vector<PushMessagingApplicationId> all_app_ids = |
| 701 PushMessagingApplicationId::GetAll(profile_); | |
| 702 | |
| 703 base::Closure barrier_closure = base::BarrierClosure( | |
| 704 all_app_ids.size(), | |
| 705 content_setting_changed_callback_for_testing_.is_null() | |
| 706 ? base::Bind(&base::DoNothing) | |
|
Bernhard Bauer
2015/03/12 17:46:51
I think I would indent this four more spaces?
johnme
2015/03/13 11:56:13
Done.
| |
| 707 : content_setting_changed_callback_for_testing_); | |
| 708 content_setting_changed_callback_for_testing_.Reset(); | |
| 709 | |
| 710 for (const auto& id : all_app_ids) { | |
| 699 // If |primary_pattern| is not valid, we should always check for a | 711 // If |primary_pattern| is not valid, we should always check for a |
| 700 // permission change because it can happen for example when the entire | 712 // permission change because it can happen for example when the entire |
| 701 // Push or Notifications permissions are cleared. | 713 // Push or Notifications permissions are cleared. |
| 702 // Otherwise, the permission should be checked if the pattern matches the | 714 // Otherwise, the permission should be checked if the pattern matches the |
| 703 // origin. | 715 // origin. |
| 704 if (primary_pattern.IsValid() && !primary_pattern.Matches(id.origin())) | 716 if (primary_pattern.IsValid() && !primary_pattern.Matches(id.origin())) { |
| 717 barrier_closure.Run(); | |
| 705 continue; | 718 continue; |
| 719 } | |
| 706 | 720 |
| 707 if (HasPermission(id.origin())) | 721 if (HasPermission(id.origin())) { |
| 722 barrier_closure.Run(); | |
| 708 continue; | 723 continue; |
| 724 } | |
| 709 | 725 |
| 710 PushMessagingService::GetSenderId( | 726 GetSenderId( |
| 711 profile_, id.origin(), id.service_worker_registration_id(), | 727 profile_, id.origin(), id.service_worker_registration_id(), |
| 712 base::Bind( | 728 base::Bind( |
| 713 &PushMessagingServiceImpl::UnregisterBecausePermissionRevoked, | 729 &PushMessagingServiceImpl::UnregisterBecausePermissionRevoked, |
| 714 weak_factory_.GetWeakPtr(), id)); | 730 weak_factory_.GetWeakPtr(), id, barrier_closure)); |
| 715 } | 731 } |
| 716 } | 732 } |
| 717 | 733 |
| 734 static void UnregisterCallbackToClosure( | |
|
Bernhard Bauer
2015/03/12 17:46:51
Move this to an anonymous namespace instead of mak
johnme
2015/03/13 11:56:13
Done.
| |
| 735 const base::Closure& closure, content::PushUnregistrationStatus status) { | |
| 736 closure.Run(); | |
| 737 } | |
| 738 | |
| 718 void PushMessagingServiceImpl::UnregisterBecausePermissionRevoked( | 739 void PushMessagingServiceImpl::UnregisterBecausePermissionRevoked( |
| 719 const PushMessagingApplicationId& id, | 740 const PushMessagingApplicationId& id, const base::Closure& closure, |
| 720 const std::string& sender_id, bool success, bool not_found) { | 741 const std::string& sender_id, bool success, bool not_found) { |
| 742 base::Closure barrier_closure = base::BarrierClosure(2, closure); | |
| 743 | |
| 721 // Unregister the PushMessagingApplicationId with the push service. | 744 // Unregister the PushMessagingApplicationId with the push service. |
| 722 Unregister(id.app_id_guid(), sender_id, UnregisterCallback()); | 745 Unregister(id.app_id_guid(), sender_id, |
| 746 base::Bind(&UnregisterCallbackToClosure, barrier_closure)); | |
| 723 | 747 |
| 724 // Clear the associated service worker push registration id. | 748 // Clear the associated service worker push registration id. |
| 725 PushMessagingService::ClearPushRegistrationID( | 749 ClearPushRegistrationID(profile_, id.origin(), |
| 726 profile_, id.origin(), id.service_worker_registration_id()); | 750 id.service_worker_registration_id(), barrier_closure); |
| 751 } | |
| 752 | |
| 753 void PushMessagingServiceImpl::SetContentSettingChangedCallbackForTesting( | |
| 754 const base::Closure& callback) { | |
| 755 content_setting_changed_callback_for_testing_ = callback; | |
| 727 } | 756 } |
| 728 | 757 |
| 729 // KeyedService methods ------------------------------------------------------- | 758 // KeyedService methods ------------------------------------------------------- |
| 730 | 759 |
| 731 void PushMessagingServiceImpl::Shutdown() { | 760 void PushMessagingServiceImpl::Shutdown() { |
| 732 GetGCMDriver()->RemoveAppHandler(kPushMessagingApplicationIdPrefix); | 761 GetGCMDriver()->RemoveAppHandler(kPushMessagingApplicationIdPrefix); |
| 733 } | 762 } |
| 734 | 763 |
| 735 // Helper methods -------------------------------------------------------------- | 764 // Helper methods -------------------------------------------------------------- |
| 736 | 765 |
| 737 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { | 766 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { |
| 738 PushMessagingPermissionContext* permission_context = | 767 PushMessagingPermissionContext* permission_context = |
| 739 PushMessagingPermissionContextFactory::GetForProfile(profile_); | 768 PushMessagingPermissionContextFactory::GetForProfile(profile_); |
| 740 DCHECK(permission_context); | 769 DCHECK(permission_context); |
| 741 | 770 |
| 742 return permission_context->GetPermissionStatus(origin, origin) == | 771 return permission_context->GetPermissionStatus(origin, origin) == |
| 743 CONTENT_SETTING_ALLOW; | 772 CONTENT_SETTING_ALLOW; |
| 744 } | 773 } |
| 745 | 774 |
| 746 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { | 775 gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { |
| 747 gcm::GCMProfileService* gcm_profile_service = | 776 gcm::GCMProfileService* gcm_profile_service = |
| 748 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 777 gcm::GCMProfileServiceFactory::GetForProfile(profile_); |
| 749 CHECK(gcm_profile_service); | 778 CHECK(gcm_profile_service); |
| 750 CHECK(gcm_profile_service->driver()); | 779 CHECK(gcm_profile_service->driver()); |
| 751 return gcm_profile_service->driver(); | 780 return gcm_profile_service->driver(); |
| 752 } | 781 } |
| OLD | NEW |