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 #ifndef CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_SERVICE_IMPL_H_ | 5 #ifndef CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_SERVICE_IMPL_H_ |
6 #define CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_SERVICE_IMPL_H_ | 6 #define CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_SERVICE_IMPL_H_ |
7 | 7 |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "components/content_settings/core/browser/content_settings_observer.h" | 10 #include "components/content_settings/core/browser/content_settings_observer.h" |
11 #include "components/gcm_driver/gcm_app_handler.h" | 11 #include "components/gcm_driver/gcm_app_handler.h" |
12 #include "components/gcm_driver/gcm_client.h" | 12 #include "components/gcm_driver/gcm_client.h" |
| 13 #include "components/keyed_service/core/keyed_service.h" |
13 #include "content/public/browser/push_messaging_service.h" | 14 #include "content/public/browser/push_messaging_service.h" |
14 #include "content/public/common/push_messaging_status.h" | 15 #include "content/public/common/push_messaging_status.h" |
15 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushPermi
ssionStatus.h" | 16 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushPermi
ssionStatus.h" |
16 | 17 |
17 class Profile; | 18 class Profile; |
| 19 class PushMessagingApplicationId; |
18 | 20 |
19 namespace user_prefs { | 21 namespace user_prefs { |
20 class PrefRegistrySyncable; | 22 class PrefRegistrySyncable; |
21 } | 23 } |
22 | 24 |
23 namespace gcm { | 25 namespace gcm { |
24 | 26 class GCMDriver; |
25 class GCMProfileService; | 27 class GCMProfileService; |
26 class PushMessagingApplicationId; | 28 } |
27 | 29 |
28 class PushMessagingServiceImpl : public content::PushMessagingService, | 30 class PushMessagingServiceImpl : public content::PushMessagingService, |
29 public GCMAppHandler, | 31 public gcm::GCMAppHandler, |
30 public content_settings::Observer { | 32 public content_settings::Observer, |
| 33 public KeyedService { |
31 public: | 34 public: |
32 // Register profile-specific prefs for GCM. | 35 // Register profile-specific prefs for GCM. |
33 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 36 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
34 | 37 |
35 // If any Service Workers are using push, starts GCM and adds an app handler. | 38 // If any Service Workers are using push, starts GCM and adds an app handler. |
36 static void InitializeForProfile(Profile* profile); | 39 static void InitializeForProfile(Profile* profile); |
37 | 40 |
38 PushMessagingServiceImpl(GCMProfileService* gcm_profile_service, | 41 explicit PushMessagingServiceImpl(Profile* profile); |
39 Profile* profile); | |
40 ~PushMessagingServiceImpl() override; | 42 ~PushMessagingServiceImpl() override; |
41 | 43 |
42 // GCMAppHandler implementation. | 44 // gcm::GCMAppHandler implementation. |
43 void ShutdownHandler() override; | 45 void ShutdownHandler() override; |
44 void OnMessage(const std::string& app_id, | 46 void OnMessage(const std::string& app_id, |
45 const GCMClient::IncomingMessage& message) override; | 47 const gcm::GCMClient::IncomingMessage& message) override; |
46 void OnMessagesDeleted(const std::string& app_id) override; | 48 void OnMessagesDeleted(const std::string& app_id) override; |
47 void OnSendError( | 49 void OnSendError( |
48 const std::string& app_id, | 50 const std::string& app_id, |
49 const GCMClient::SendErrorDetails& send_error_details) override; | 51 const gcm::GCMClient::SendErrorDetails& send_error_details) override; |
50 void OnSendAcknowledged(const std::string& app_id, | 52 void OnSendAcknowledged(const std::string& app_id, |
51 const std::string& message_id) override; | 53 const std::string& message_id) override; |
52 bool CanHandle(const std::string& app_id) const override; | 54 bool CanHandle(const std::string& app_id) const override; |
53 | 55 |
54 // content::PushMessagingService implementation: | 56 // content::PushMessagingService implementation: |
55 GURL GetPushEndpoint() override; | 57 GURL GetPushEndpoint() override; |
56 void RegisterFromDocument( | 58 void RegisterFromDocument( |
57 const GURL& requesting_origin, | 59 const GURL& requesting_origin, |
58 int64 service_worker_registration_id, | 60 int64 service_worker_registration_id, |
59 const std::string& sender_id, | 61 const std::string& sender_id, |
(...skipping 15 matching lines...) Expand all Loading... |
75 blink::WebPushPermissionStatus GetPermissionStatus( | 77 blink::WebPushPermissionStatus GetPermissionStatus( |
76 const GURL& requesting_origin, | 78 const GURL& requesting_origin, |
77 const GURL& embedding_origin) override; | 79 const GURL& embedding_origin) override; |
78 | 80 |
79 // content_settings::Observer implementation. | 81 // content_settings::Observer implementation. |
80 void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern, | 82 void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern, |
81 const ContentSettingsPattern& secondary_pattern, | 83 const ContentSettingsPattern& secondary_pattern, |
82 ContentSettingsType content_type, | 84 ContentSettingsType content_type, |
83 std::string resource_identifier) override; | 85 std::string resource_identifier) override; |
84 | 86 |
85 void SetProfileForTesting(Profile* profile); | 87 // KeyedService implementation. |
| 88 void Shutdown() override; |
86 | 89 |
87 private: | 90 private: |
88 // A registration is pending until it has succeeded or failed. | 91 // A registration is pending until it has succeeded or failed. |
89 void IncreasePushRegistrationCount(int add, bool is_pending); | 92 void IncreasePushRegistrationCount(int add, bool is_pending); |
90 void DecreasePushRegistrationCount(int subtract, bool was_pending); | 93 void DecreasePushRegistrationCount(int subtract, bool was_pending); |
91 | 94 |
92 // OnMessage methods --------------------------------------------------------- | 95 // OnMessage methods --------------------------------------------------------- |
93 | 96 |
94 void DeliverMessageCallback(const std::string& app_id_guid, | 97 void DeliverMessageCallback(const std::string& app_id_guid, |
95 const GURL& requesting_origin, | 98 const GURL& requesting_origin, |
96 int64 service_worker_registration_id, | 99 int64 service_worker_registration_id, |
97 const GCMClient::IncomingMessage& message, | 100 const gcm::GCMClient::IncomingMessage& message, |
98 content::PushDeliveryStatus status); | 101 content::PushDeliveryStatus status); |
99 | 102 |
100 // Developers are required to display a Web Notification in response to an | 103 // Developers are required to display a Web Notification in response to an |
101 // incoming push message in order to clarify to the user that something has | 104 // incoming push message in order to clarify to the user that something has |
102 // happened in the background. When they forget to do so, display a default | 105 // happened in the background. When they forget to do so, display a default |
103 // notification on their behalf. | 106 // notification on their behalf. |
104 void RequireUserVisibleUX(const GURL& requesting_origin, | 107 void RequireUserVisibleUX(const GURL& requesting_origin, |
105 int64 service_worker_registration_id); | 108 int64 service_worker_registration_id); |
106 void DidGetNotificationsShown( | 109 void DidGetNotificationsShown( |
107 const GURL& requesting_origin, | 110 const GURL& requesting_origin, |
108 int64 service_worker_registration_id, | 111 int64 service_worker_registration_id, |
109 bool notification_shown, | 112 bool notification_shown, |
110 bool notification_needed, | 113 bool notification_needed, |
111 const std::string& data, | 114 const std::string& data, |
112 bool success, | 115 bool success, |
113 bool not_found); | 116 bool not_found); |
114 | 117 |
115 // Register methods ---------------------------------------------------------- | 118 // Register methods ---------------------------------------------------------- |
116 | 119 |
117 void RegisterEnd( | 120 void RegisterEnd( |
118 const content::PushMessagingService::RegisterCallback& callback, | 121 const content::PushMessagingService::RegisterCallback& callback, |
119 const std::string& registration_id, | 122 const std::string& registration_id, |
120 content::PushRegistrationStatus status); | 123 content::PushRegistrationStatus status); |
121 | 124 |
122 void DidRegister( | 125 void DidRegister( |
123 const PushMessagingApplicationId& application_id, | 126 const PushMessagingApplicationId& application_id, |
124 const content::PushMessagingService::RegisterCallback& callback, | 127 const content::PushMessagingService::RegisterCallback& callback, |
125 const std::string& registration_id, | 128 const std::string& registration_id, |
126 GCMClient::Result result); | 129 gcm::GCMClient::Result result); |
127 | 130 |
128 void DidRequestPermission( | 131 void DidRequestPermission( |
129 const PushMessagingApplicationId& application_id, | 132 const PushMessagingApplicationId& application_id, |
130 const std::string& sender_id, | 133 const std::string& sender_id, |
131 const content::PushMessagingService::RegisterCallback& callback, | 134 const content::PushMessagingService::RegisterCallback& callback, |
132 bool allow); | 135 bool allow); |
133 | 136 |
134 // Unregister methods -------------------------------------------------------- | 137 // Unregister methods -------------------------------------------------------- |
135 | 138 |
136 void Unregister(const std::string& app_id_guid, | 139 void Unregister(const std::string& app_id_guid, |
137 const std::string& sender_id, | 140 const std::string& sender_id, |
138 bool retry_on_failure, | 141 bool retry_on_failure, |
139 const content::PushMessagingService::UnregisterCallback&); | 142 const content::PushMessagingService::UnregisterCallback&); |
140 | 143 |
141 void DidUnregister(const std::string& app_id_guid, | 144 void DidUnregister(const std::string& app_id_guid, |
142 bool retry_on_failure, | 145 bool retry_on_failure, |
143 const content::PushMessagingService::UnregisterCallback&, | 146 const content::PushMessagingService::UnregisterCallback&, |
144 GCMClient::Result result); | 147 gcm::GCMClient::Result result); |
145 | 148 |
146 // OnContentSettingChanged methods ------------------------------------------- | 149 // OnContentSettingChanged methods ------------------------------------------- |
147 | 150 |
148 void UnregisterBecausePermissionRevoked(const PushMessagingApplicationId& id, | 151 void UnregisterBecausePermissionRevoked(const PushMessagingApplicationId& id, |
149 const std::string& sender_id, | 152 const std::string& sender_id, |
150 bool success, bool not_found); | 153 bool success, bool not_found); |
151 | 154 |
152 // Helper methods ------------------------------------------------------------ | 155 // Helper methods ------------------------------------------------------------ |
153 | 156 |
154 // Checks if a given origin is allowed to use Push. | 157 // Checks if a given origin is allowed to use Push. |
155 bool HasPermission(const GURL& origin); | 158 bool HasPermission(const GURL& origin); |
156 | 159 |
157 GCMProfileService* gcm_profile_service_; // It owns us. | 160 gcm::GCMDriver* gcm_driver() const; |
158 | 161 |
159 Profile* profile_; // It owns our owner. | 162 // PushMessagingServiceFactory defines a dependency on the GCMProfileService, |
| 163 // guaranteeing that GCMProfileService will outlive this class. |
| 164 gcm::GCMProfileService* gcm_profile_service_; |
| 165 |
| 166 Profile* profile_; |
160 | 167 |
161 int push_registration_count_; | 168 int push_registration_count_; |
162 int pending_push_registration_count_; | 169 int pending_push_registration_count_; |
163 | 170 |
164 base::WeakPtrFactory<PushMessagingServiceImpl> weak_factory_; | 171 base::WeakPtrFactory<PushMessagingServiceImpl> weak_factory_; |
165 | 172 |
166 DISALLOW_COPY_AND_ASSIGN(PushMessagingServiceImpl); | 173 DISALLOW_COPY_AND_ASSIGN(PushMessagingServiceImpl); |
167 }; | 174 }; |
168 | 175 |
169 } // namespace gcm | 176 #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_SERVICE_IMPL_H_ |
170 | |
171 #endif // CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_SERVICE_IMPL_H_ | |
OLD | NEW |