OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_GCM_PROFILE_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_ |
6 #define CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_ | 6 #define CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 // Acts as a bridge between GCM API and GCMClient layer. It is profile based. | 41 // Acts as a bridge between GCM API and GCMClient layer. It is profile based. |
42 class GCMProfileService : public BrowserContextKeyedService, | 42 class GCMProfileService : public BrowserContextKeyedService, |
43 public content::NotificationObserver { | 43 public content::NotificationObserver { |
44 public: | 44 public: |
45 typedef base::Callback<void(const std::string& registration_id, | 45 typedef base::Callback<void(const std::string& registration_id, |
46 GCMClient::Result result)> RegisterCallback; | 46 GCMClient::Result result)> RegisterCallback; |
47 typedef base::Callback<void(const std::string& message_id, | 47 typedef base::Callback<void(const std::string& message_id, |
48 GCMClient::Result result)> SendCallback; | 48 GCMClient::Result result)> SendCallback; |
49 | 49 |
| 50 enum GCMEnabledState { |
| 51 // GCM is always enabled. GCMClient will always load and connect with GCM. |
| 52 ALWAYS_ENABLED, |
| 53 // GCM is only enabled for apps. GCMClient will start to load and connect |
| 54 // with GCM only when GCM API is used. |
| 55 ENABLED_FOR_APPS, |
| 56 // GCM is always disabled. GCMClient will never load and connect with GCM. |
| 57 ALWAYS_DISABLED |
| 58 }; |
| 59 |
50 // For testing purpose. | 60 // For testing purpose. |
51 class TestingDelegate { | 61 class TestingDelegate { |
52 public: | 62 public: |
53 virtual GCMEventRouter* GetEventRouter() const = 0; | 63 virtual GCMEventRouter* GetEventRouter() const = 0; |
54 }; | 64 }; |
55 | 65 |
56 // Returns true if the GCM support is enabled. | 66 // Returns the GCM enabled state. |
57 static bool IsGCMEnabled(Profile* profile); | 67 static GCMEnabledState GetGCMEnabledState(Profile* profile); |
58 | 68 |
59 // Register profile-specific prefs for GCM. | 69 // Register profile-specific prefs for GCM. |
60 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 70 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
61 | 71 |
62 explicit GCMProfileService(Profile* profile); | 72 explicit GCMProfileService(Profile* profile); |
63 virtual ~GCMProfileService(); | 73 virtual ~GCMProfileService(); |
64 | 74 |
65 void Initialize(scoped_ptr<GCMClientFactory> gcm_client_factory); | 75 void Initialize(scoped_ptr<GCMClientFactory> gcm_client_factory); |
66 | 76 |
67 // Registers |sender_id| for an app. A registration ID will be returned by | 77 // Registers |sender_id| for an app. A registration ID will be returned by |
(...skipping 13 matching lines...) Expand all Loading... |
81 // |app_id|: application ID. | 91 // |app_id|: application ID. |
82 // |receiver_id|: registration ID of the receiver party. | 92 // |receiver_id|: registration ID of the receiver party. |
83 // |message|: message to be sent. | 93 // |message|: message to be sent. |
84 // |callback|: to be called once the asynchronous operation is done. | 94 // |callback|: to be called once the asynchronous operation is done. |
85 virtual void Send(const std::string& app_id, | 95 virtual void Send(const std::string& app_id, |
86 const std::string& receiver_id, | 96 const std::string& receiver_id, |
87 const GCMClient::OutgoingMessage& message, | 97 const GCMClient::OutgoingMessage& message, |
88 SendCallback callback); | 98 SendCallback callback); |
89 | 99 |
90 // For testing purpose. | 100 // For testing purpose. |
| 101 GCMClient* GetGCMClientForTesting() const; |
| 102 |
91 void set_testing_delegate(TestingDelegate* testing_delegate) { | 103 void set_testing_delegate(TestingDelegate* testing_delegate) { |
92 testing_delegate_ = testing_delegate; | 104 testing_delegate_ = testing_delegate; |
93 } | 105 } |
94 | 106 |
95 protected: | |
96 // Flag that could be set by the testing code to enable GCM. Otherwise, | |
97 // tests from official build will fail. | |
98 static bool enable_gcm_for_testing_; | |
99 | |
100 private: | 107 private: |
101 friend class GCMProfileServiceTestConsumer; | 108 friend class GCMProfileServiceTestConsumer; |
102 | 109 |
103 class DelayedTaskController; | 110 class DelayedTaskController; |
104 class IOWorker; | 111 class IOWorker; |
105 | 112 |
106 struct RegistrationInfo { | 113 struct RegistrationInfo { |
107 RegistrationInfo(); | 114 RegistrationInfo(); |
108 ~RegistrationInfo(); | 115 ~RegistrationInfo(); |
109 bool IsValid() const; | 116 bool IsValid() const; |
110 | 117 |
111 std::vector<std::string> sender_ids; | 118 std::vector<std::string> sender_ids; |
112 std::string registration_id; | 119 std::string registration_id; |
113 }; | 120 }; |
114 | 121 |
115 // Overridden from content::NotificationObserver: | 122 // Overridden from content::NotificationObserver: |
116 virtual void Observe(int type, | 123 virtual void Observe(int type, |
117 const content::NotificationSource& source, | 124 const content::NotificationSource& source, |
118 const content::NotificationDetails& details) OVERRIDE; | 125 const content::NotificationDetails& details) OVERRIDE; |
119 | 126 |
120 // Checks in with GCM by creating and initializing GCMClient when the profile | 127 // Ensures that the GCMClient is loaded and the GCM check-in is done when |
121 // has been signed in. | 128 // the profile was signed in. |
122 void CheckIn(const std::string& username); | 129 void EnsureLoaded(); |
123 | 130 |
124 // Checks out of GCM when the profile has been signed out. This will erase | 131 // Checks out of GCM when the profile has been signed out. This will erase |
125 // all the cached and persisted data. | 132 // all the cached and persisted data. |
126 void CheckOut(); | 133 void CheckOut(); |
127 | 134 |
128 // Resets the GCMClient instance. This is called when the profile is being | 135 // Resets the GCMClient instance. This is called when the profile is being |
129 // destroyed. | 136 // destroyed. |
130 void ResetGCMClient(); | 137 void ResetGCMClient(); |
131 | 138 |
132 // Ensures that the app is ready for GCM functions and events. | 139 // Ensures that the app is ready for GCM functions and events. |
(...skipping 15 matching lines...) Expand all Loading... |
148 GCMClient::Result result); | 155 GCMClient::Result result); |
149 void SendFinished(const std::string& app_id, | 156 void SendFinished(const std::string& app_id, |
150 const std::string& message_id, | 157 const std::string& message_id, |
151 GCMClient::Result result); | 158 GCMClient::Result result); |
152 void MessageReceived(const std::string& app_id, | 159 void MessageReceived(const std::string& app_id, |
153 GCMClient::IncomingMessage message); | 160 GCMClient::IncomingMessage message); |
154 void MessagesDeleted(const std::string& app_id); | 161 void MessagesDeleted(const std::string& app_id); |
155 void MessageSendError(const std::string& app_id, | 162 void MessageSendError(const std::string& app_id, |
156 const std::string& message_id, | 163 const std::string& message_id, |
157 GCMClient::Result result); | 164 GCMClient::Result result); |
158 void FinishInitializationOnUI(bool ready); | |
159 void GCMClientReady(); | 165 void GCMClientReady(); |
160 | 166 |
161 // Returns the event router to fire the event for the given app. | 167 // Returns the event router to fire the event for the given app. |
162 GCMEventRouter* GetEventRouter(const std::string& app_id) const; | 168 GCMEventRouter* GetEventRouter(const std::string& app_id) const; |
163 | 169 |
164 // Used to persist the IDs of registered apps. | 170 // Used to persist the IDs of registered apps. |
165 void ReadRegisteredAppIDs(); | 171 void ReadRegisteredAppIDs(); |
166 void WriteRegisteredAppIDs(); | 172 void WriteRegisteredAppIDs(); |
167 | 173 |
168 // Used to persist registration info into the app's state store. | 174 // Used to persist registration info into the app's state store. |
169 void DeleteRegistrationInfo(const std::string& app_id); | 175 void DeleteRegistrationInfo(const std::string& app_id); |
170 void WriteRegistrationInfo(const std::string& app_id); | 176 void WriteRegistrationInfo(const std::string& app_id); |
171 void ReadRegistrationInfo(const std::string& app_id); | 177 void ReadRegistrationInfo(const std::string& app_id); |
172 void ReadRegistrationInfoFinished(const std::string& app_id, | 178 void ReadRegistrationInfoFinished(const std::string& app_id, |
173 scoped_ptr<base::Value> value); | 179 scoped_ptr<base::Value> value); |
174 bool ParsePersistedRegistrationInfo(scoped_ptr<base::Value> value, | 180 bool ParsePersistedRegistrationInfo(scoped_ptr<base::Value> value, |
175 RegistrationInfo* registration_info); | 181 RegistrationInfo* registration_info); |
176 | 182 |
177 // Returns the key used to identify the registration info saved into the | 183 // Returns the key used to identify the registration info saved into the |
178 // app's state store. Used for testing purpose. | 184 // app's state store. Used for testing purpose. |
179 static const char* GetPersistentRegisterKeyForTesting(); | 185 static const char* GetPersistentRegisterKeyForTesting(); |
180 | 186 |
181 // The profile which owns this object. | 187 // The profile which owns this object. |
182 Profile* profile_; | 188 Profile* profile_; |
183 | 189 |
184 // Used to creat the GCMClient instance. | |
185 scoped_ptr<GCMClientFactory> gcm_client_factory_; | |
186 | |
187 // Flag to indicate if GCMClient is ready. | 190 // Flag to indicate if GCMClient is ready. |
188 bool gcm_client_ready_; | 191 bool gcm_client_ready_; |
189 | 192 |
190 // The username of the signed-in profile. | 193 // The username of the signed-in profile. |
191 std::string username_; | 194 std::string username_; |
192 | 195 |
193 content::NotificationRegistrar registrar_; | 196 content::NotificationRegistrar registrar_; |
194 | 197 |
195 scoped_ptr<DelayedTaskController> delayed_task_controller_; | 198 scoped_ptr<DelayedTaskController> delayed_task_controller_; |
196 | 199 |
(...skipping 20 matching lines...) Expand all Loading... |
217 | 220 |
218 // Used to pass a weak pointer to the IO worker. | 221 // Used to pass a weak pointer to the IO worker. |
219 base::WeakPtrFactory<GCMProfileService> weak_ptr_factory_; | 222 base::WeakPtrFactory<GCMProfileService> weak_ptr_factory_; |
220 | 223 |
221 DISALLOW_COPY_AND_ASSIGN(GCMProfileService); | 224 DISALLOW_COPY_AND_ASSIGN(GCMProfileService); |
222 }; | 225 }; |
223 | 226 |
224 } // namespace gcm | 227 } // namespace gcm |
225 | 228 |
226 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_ | 229 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_ |
OLD | NEW |