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/services/gcm/push_messaging_service_impl.h" | 5 #include "chrome/browser/services/gcm/push_messaging_service_impl.h" |
6 | 6 |
7 #include <bitset> | 7 #include <bitset> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/notifications/notification_ui_manager.h" | 18 #include "chrome/browser/notifications/notification_ui_manager.h" |
19 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 19 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 21 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
22 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 22 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
23 #include "chrome/browser/services/gcm/push_messaging_application_id.h" | 23 #include "chrome/browser/services/gcm/push_messaging_application_id.h" |
24 #include "chrome/browser/services/gcm/push_messaging_constants.h" | 24 #include "chrome/browser/services/gcm/push_messaging_constants.h" |
25 #include "chrome/browser/services/gcm/push_messaging_permission_context.h" | 25 #include "chrome/browser/services/gcm/push_messaging_permission_context.h" |
26 #include "chrome/browser/services/gcm/push_messaging_permission_context_factory. h" | 26 #include "chrome/browser/services/gcm/push_messaging_permission_context_factory. h" |
27 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
28 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
29 #include "chrome/grit/generated_resources.h" | 29 #include "chrome/grit/generated_resources.h" |
30 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
30 #include "components/content_settings/core/common/permission_request_id.h" | 31 #include "components/content_settings/core/common/permission_request_id.h" |
31 #include "components/gcm_driver/gcm_driver.h" | 32 #include "components/gcm_driver/gcm_driver.h" |
32 #include "components/pref_registry/pref_registry_syncable.h" | 33 #include "components/pref_registry/pref_registry_syncable.h" |
33 #include "content/public/browser/browser_context.h" | 34 #include "content/public/browser/browser_context.h" |
35 #include "content/public/browser/browser_thread.h" | |
34 #include "content/public/browser/render_frame_host.h" | 36 #include "content/public/browser/render_frame_host.h" |
35 #include "content/public/browser/service_worker_context.h" | 37 #include "content/public/browser/service_worker_context.h" |
36 #include "content/public/browser/storage_partition.h" | 38 #include "content/public/browser/storage_partition.h" |
37 #include "content/public/browser/web_contents.h" | 39 #include "content/public/browser/web_contents.h" |
38 #include "content/public/common/child_process_host.h" | 40 #include "content/public/common/child_process_host.h" |
39 #include "content/public/common/content_switches.h" | 41 #include "content/public/common/content_switches.h" |
40 #include "content/public/common/platform_notification_data.h" | 42 #include "content/public/common/platform_notification_data.h" |
41 #include "content/public/common/push_messaging_status.h" | 43 #include "content/public/common/push_messaging_status.h" |
42 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 44 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
43 #include "third_party/skia/include/core/SkBitmap.h" | 45 #include "third_party/skia/include/core/SkBitmap.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 } | 125 } |
124 | 126 |
125 PushMessagingServiceImpl::PushMessagingServiceImpl( | 127 PushMessagingServiceImpl::PushMessagingServiceImpl( |
126 GCMProfileService* gcm_profile_service, | 128 GCMProfileService* gcm_profile_service, |
127 Profile* profile) | 129 Profile* profile) |
128 : gcm_profile_service_(gcm_profile_service), | 130 : gcm_profile_service_(gcm_profile_service), |
129 profile_(profile), | 131 profile_(profile), |
130 push_registration_count_(0), | 132 push_registration_count_(0), |
131 pending_push_registration_count_(0), | 133 pending_push_registration_count_(0), |
132 weak_factory_(this) { | 134 weak_factory_(this) { |
135 // In some tests, we might end up with |profile_| being null at that point. | |
johnme
2015/02/16 20:27:44
that -> this
mlamouri (slow - plz ping)
2015/02/16 20:46:03
Done.
| |
136 // When that is the case |profile_| will be set in SetProfileForTesting(), at | |
137 // which point the service will start to observe HostContentSettingsMap. | |
138 if (profile_) | |
139 profile_->GetHostContentSettingsMap()->AddObserver(this); | |
133 } | 140 } |
134 | 141 |
135 PushMessagingServiceImpl::~PushMessagingServiceImpl() { | 142 PushMessagingServiceImpl::~PushMessagingServiceImpl() { |
136 // TODO(johnme): If it's possible for this to be destroyed before GCMDriver, | 143 // TODO(johnme): If it's possible for this to be destroyed before GCMDriver, |
137 // then we should call RemoveAppHandler. | 144 // then we should call RemoveAppHandler. |
145 profile_->GetHostContentSettingsMap()->RemoveObserver(this); | |
138 } | 146 } |
139 | 147 |
140 void PushMessagingServiceImpl::IncreasePushRegistrationCount(int add, | 148 void PushMessagingServiceImpl::IncreasePushRegistrationCount(int add, |
141 bool is_pending) { | 149 bool is_pending) { |
142 DCHECK(add > 0); | 150 DCHECK(add > 0); |
143 if (push_registration_count_ + pending_push_registration_count_ == 0) { | 151 if (push_registration_count_ + pending_push_registration_count_ == 0) { |
144 gcm_profile_service_->driver()->AddAppHandler( | 152 gcm_profile_service_->driver()->AddAppHandler( |
145 kPushMessagingApplicationIdPrefix, this); | 153 kPushMessagingApplicationIdPrefix, this); |
146 } | 154 } |
147 if (is_pending) { | 155 if (is_pending) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 profile_, | 235 profile_, |
228 application_id.origin, | 236 application_id.origin, |
229 application_id.service_worker_registration_id, | 237 application_id.service_worker_registration_id, |
230 data, | 238 data, |
231 base::Bind(&PushMessagingServiceImpl::DeliverMessageCallback, | 239 base::Bind(&PushMessagingServiceImpl::DeliverMessageCallback, |
232 weak_factory_.GetWeakPtr(), | 240 weak_factory_.GetWeakPtr(), |
233 application_id.app_id_guid, application_id.origin, | 241 application_id.app_id_guid, application_id.origin, |
234 application_id.service_worker_registration_id, message)); | 242 application_id.service_worker_registration_id, message)); |
235 } | 243 } |
236 | 244 |
245 void PushMessagingServiceImpl::OnContentSettingChanged( | |
246 const ContentSettingsPattern& primary_pattern, | |
247 const ContentSettingsPattern& secondary_pattern, | |
248 ContentSettingsType content_type, | |
249 std::string resource_identifier) { | |
250 if (content_type != CONTENT_SETTINGS_TYPE_PUSH_MESSAGING && | |
251 content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS) { | |
252 return; | |
253 } | |
254 | |
255 for (const auto& id : PushMessagingApplicationId::GetAll(profile_)) { | |
256 if (primary_pattern.IsValid() && !primary_pattern.Matches(id.origin)) | |
257 continue; | |
258 | |
259 // |primary_pattern| might be invalid or it does matches the origin, thus | |
260 // the origin permission status might have changed. | |
261 if (HasPermission(id.origin)) | |
262 continue; | |
263 | |
264 // Unregister the PushMessagingApplicationId with the push service. | |
265 Unregister(id.app_id_guid, true /* retry */, UnregisterCallback());; | |
johnme
2015/02/16 20:27:45
;; -> ;
mlamouri (slow - plz ping)
2015/02/16 20:46:03
Surprised this didn't trigger a warning.
| |
266 | |
267 // Clear the associated service worker push registration id. | |
268 PushMessagingService::ClearPushRegistrationID( | |
johnme
2015/02/16 20:27:45
We should probably also call this from the PUSH_DE
mlamouri (slow - plz ping)
2015/02/16 20:46:03
I think we should consider having the service doin
| |
269 profile_, id.origin, id.service_worker_registration_id); | |
270 } | |
271 } | |
272 | |
237 void PushMessagingServiceImpl::SetProfileForTesting(Profile* profile) { | 273 void PushMessagingServiceImpl::SetProfileForTesting(Profile* profile) { |
238 profile_ = profile; | 274 profile_ = profile; |
275 profile_->GetHostContentSettingsMap()->AddObserver(this); | |
johnme
2015/02/16 20:27:45
Maybe add a DCHECK(profile) to emphasize that only
mlamouri (slow - plz ping)
2015/02/16 20:46:03
Not very useful given that it will crash anyway an
| |
239 } | 276 } |
240 | 277 |
241 void PushMessagingServiceImpl::DeliverMessageCallback( | 278 void PushMessagingServiceImpl::DeliverMessageCallback( |
242 const std::string& app_id_guid, | 279 const std::string& app_id_guid, |
243 const GURL& requesting_origin, | 280 const GURL& requesting_origin, |
244 int64 service_worker_registration_id, | 281 int64 service_worker_registration_id, |
245 const GCMClient::IncomingMessage& message, | 282 const GCMClient::IncomingMessage& message, |
246 content::PushDeliveryStatus status) { | 283 content::PushDeliveryStatus status) { |
247 // TODO(mvanouwerkerk): UMA logging. | 284 // TODO(mvanouwerkerk): UMA logging. |
248 // TODO(mvanouwerkerk): Show a warning in the developer console of the | 285 // TODO(mvanouwerkerk): Show a warning in the developer console of the |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
617 void PushMessagingServiceImpl::Unregister( | 654 void PushMessagingServiceImpl::Unregister( |
618 const GURL& requesting_origin, | 655 const GURL& requesting_origin, |
619 int64 service_worker_registration_id, | 656 int64 service_worker_registration_id, |
620 bool retry_on_failure, | 657 bool retry_on_failure, |
621 const content::PushMessagingService::UnregisterCallback& callback) { | 658 const content::PushMessagingService::UnregisterCallback& callback) { |
622 DCHECK(gcm_profile_service_->driver()); | 659 DCHECK(gcm_profile_service_->driver()); |
623 | 660 |
624 PushMessagingApplicationId application_id = PushMessagingApplicationId::Get( | 661 PushMessagingApplicationId application_id = PushMessagingApplicationId::Get( |
625 profile_, requesting_origin, service_worker_registration_id); | 662 profile_, requesting_origin, service_worker_registration_id); |
626 if (!application_id.IsValid()) { | 663 if (!application_id.IsValid()) { |
627 callback.Run( | 664 if (!callback.is_null()) { |
628 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); | 665 callback.Run( |
johnme
2015/02/16 20:27:45
2-space indent within if please
mlamouri (slow - plz ping)
2015/02/16 20:46:03
Done.
| |
666 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); | |
667 } | |
629 return; | 668 return; |
630 } | 669 } |
631 | 670 |
632 Unregister(application_id.app_id_guid, retry_on_failure, callback); | 671 Unregister(application_id.app_id_guid, retry_on_failure, callback); |
633 } | 672 } |
634 | 673 |
635 void PushMessagingServiceImpl::Unregister( | 674 void PushMessagingServiceImpl::Unregister( |
636 const std::string& app_id_guid, | 675 const std::string& app_id_guid, |
637 bool retry_on_failure, | 676 bool retry_on_failure, |
638 const content::PushMessagingService::UnregisterCallback& callback) { | 677 const content::PushMessagingService::UnregisterCallback& callback) { |
(...skipping 18 matching lines...) Expand all Loading... | |
657 } | 696 } |
658 | 697 |
659 void PushMessagingServiceImpl::DidUnregister( | 698 void PushMessagingServiceImpl::DidUnregister( |
660 const std::string& app_id_guid, | 699 const std::string& app_id_guid, |
661 bool retry_on_failure, | 700 bool retry_on_failure, |
662 const content::PushMessagingService::UnregisterCallback& callback, | 701 const content::PushMessagingService::UnregisterCallback& callback, |
663 GCMClient::Result result) { | 702 GCMClient::Result result) { |
664 if (result == GCMClient::SUCCESS) { | 703 if (result == GCMClient::SUCCESS) { |
665 PushMessagingApplicationId application_id = | 704 PushMessagingApplicationId application_id = |
666 PushMessagingApplicationId::Get(profile_, app_id_guid); | 705 PushMessagingApplicationId::Get(profile_, app_id_guid); |
667 if (application_id.IsValid()) | 706 if (!application_id.IsValid()) { |
668 application_id.DeleteFromDisk(profile_); | 707 if (!callback.is_null()) { |
708 callback.Run( | |
709 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); | |
johnme
2015/02/16 20:27:45
4-space indent for wrap please
mlamouri (slow - plz ping)
2015/02/16 20:46:03
Done.
| |
710 } | |
711 return; | |
712 } | |
713 | |
714 application_id.DeleteFromDisk(profile_); | |
669 DecreasePushRegistrationCount(1, false /* was_pending */); | 715 DecreasePushRegistrationCount(1, false /* was_pending */); |
670 } | 716 } |
671 | 717 |
672 // Internal calls pass a null callback. | 718 // Internal calls pass a null callback. |
673 if (!callback.is_null()) { | 719 if (!callback.is_null()) { |
674 switch (result) { | 720 switch (result) { |
675 case GCMClient::SUCCESS: | 721 case GCMClient::SUCCESS: |
676 callback.Run(content::PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTER); | 722 callback.Run(content::PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTER); |
677 break; | 723 break; |
678 case GCMClient::NETWORK_ERROR: | 724 case GCMClient::NETWORK_ERROR: |
(...skipping 22 matching lines...) Expand all Loading... | |
701 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { | 747 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { |
702 gcm::PushMessagingPermissionContext* permission_context = | 748 gcm::PushMessagingPermissionContext* permission_context = |
703 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); | 749 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); |
704 DCHECK(permission_context); | 750 DCHECK(permission_context); |
705 | 751 |
706 return permission_context->GetPermissionStatus(origin, origin) == | 752 return permission_context->GetPermissionStatus(origin, origin) == |
707 CONTENT_SETTING_ALLOW; | 753 CONTENT_SETTING_ALLOW; |
708 } | 754 } |
709 | 755 |
710 } // namespace gcm | 756 } // namespace gcm |
OLD | NEW |