Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / services / gcm / gcm_profile_service.h
blobbf268a5ea83af2b0a663d531d42dac07915bba4f
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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_
6 #define CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_
8 #include <map>
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "google_apis/gcm/gcm_client.h"
21 class Profile;
23 namespace base {
24 class Value;
27 namespace extensions {
28 class Extension;
31 namespace user_prefs {
32 class PrefRegistrySyncable;
35 namespace gcm {
37 class GCMEventRouter;
38 class GCMProfileServiceTest;
40 // Acts as a bridge between GCM API and GCMClient layer. It is profile based.
41 class GCMProfileService : public BrowserContextKeyedService,
42 public content::NotificationObserver {
43 public:
44 typedef base::Callback<void(const std::string& registration_id,
45 GCMClient::Result result)> RegisterCallback;
46 typedef base::Callback<void(const std::string& message_id,
47 GCMClient::Result result)> SendCallback;
49 // For testing purpose.
50 class TestingDelegate {
51 public:
52 virtual GCMEventRouter* GetEventRouter() const = 0;
53 virtual void CheckInFinished(const GCMClient::CheckinInfo& checkin_info,
54 GCMClient::Result result) = 0;
57 // Returns true if the GCM support is enabled.
58 static bool IsGCMEnabled(Profile* profile);
60 // Register profile-specific prefs for GCM.
61 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
63 explicit GCMProfileService(Profile* profile);
64 // Constructor for testing purpose.
65 GCMProfileService(Profile* profile, TestingDelegate* testing_delegate);
66 virtual ~GCMProfileService();
68 // Registers |sender_id| for an app. A registration ID will be returned by
69 // the GCM server.
70 // |app_id|: application ID.
71 // |cert|: SHA-1 of public key of the application, in base16 format.
72 // |sender_ids|: list of IDs of the servers that are allowed to send the
73 // messages to the application. These IDs are assigned by the
74 // Google API Console.
75 // |callback|: to be called once the asynchronous operation is done.
76 virtual void Register(const std::string& app_id,
77 const std::vector<std::string>& sender_ids,
78 const std::string& cert,
79 RegisterCallback callback);
81 // Sends a message to a given receiver.
82 // |app_id|: application ID.
83 // |receiver_id|: registration ID of the receiver party.
84 // |message|: message to be sent.
85 // |callback|: to be called once the asynchronous operation is done.
86 virtual void Send(const std::string& app_id,
87 const std::string& receiver_id,
88 const GCMClient::OutgoingMessage& message,
89 SendCallback callback);
91 protected:
92 // Flag that could be set by the testing code to enable GCM. Otherwise,
93 // tests from official build will fail.
94 static bool enable_gcm_for_testing_;
96 private:
97 friend class GCMProfileServiceTest;
98 friend class GCMProfileServiceRegisterTest;
99 FRIEND_TEST_ALL_PREFIXES(GCMProfileServiceTest, CheckInFromPrefsStore);
100 FRIEND_TEST_ALL_PREFIXES(GCMProfileServiceTest, CheckOut);
101 FRIEND_TEST_ALL_PREFIXES(GCMProfileServiceRegisterTest, Unregister);
103 class DelayedTaskController;
104 class IOWorker;
106 struct RegistrationInfo {
107 RegistrationInfo();
108 ~RegistrationInfo();
109 bool IsValid() const;
111 std::vector<std::string> sender_ids;
112 std::string registration_id;
115 // Overridden from content::NotificationObserver:
116 virtual void Observe(int type,
117 const content::NotificationSource& source,
118 const content::NotificationDetails& details) OVERRIDE;
120 void Init();
122 // Allows a signed-in user to use the GCM. If the check-in info can be found
123 // in the prefs store, use it directly. Otherwise, a check-in communication
124 // will be made with the GCM.
125 void AddUser(const std::string& username);
127 // Stops the user from using the GCM after the user signs out. This simply
128 // removes the cached and persisted check-in info.
129 void RemoveUser();
131 // Unregisters an app from using the GCM after it has been uninstalled.
132 void Unregister(const std::string& app_id);
134 void DoRegister(const std::string& app_id,
135 const std::vector<std::string>& sender_ids,
136 const std::string& cert,
137 RegisterCallback callback);
138 void DoSend(const std::string& app_id,
139 const std::string& receiver_id,
140 const GCMClient::OutgoingMessage& message);
142 // Callbacks posted from IO thread to UI thread.
143 // TODO(fgorski): Update parameters to be passed by const ref.
144 void CheckInFinished(GCMClient::CheckinInfo checkin_info,
145 GCMClient::Result result);
146 void RegisterFinished(std::string app_id,
147 std::string registration_id,
148 GCMClient::Result result);
149 void SendFinished(std::string app_id,
150 std::string message_id,
151 GCMClient::Result result);
152 void MessageReceived(std::string app_id,
153 GCMClient::IncomingMessage message);
154 void MessagesDeleted(std::string app_id);
155 void MessageSendError(std::string app_id,
156 std::string message_id,
157 GCMClient::Result result);
158 void CheckGCMClientLoadingFinished(bool is_loading);
159 void GCMClientLoadingFinished();
161 // Returns the event router to fire the event for the given app.
162 GCMEventRouter* GetEventRouter(const std::string& app_id);
164 // Used to persist registration info into the app's state store.
165 void DeleteRegistrationInfo(const std::string& app_id);
166 void WriteRegistrationInfo(const std::string& app_id);
167 void ReadRegistrationInfo(const std::string& app_id);
168 void ReadRegistrationInfoFinished(std::string app_id,
169 scoped_ptr<base::Value> value);
170 bool ParsePersistedRegistrationInfo(scoped_ptr<base::Value> value,
171 RegistrationInfo* registration_info);
173 // Returns the key used to identify the registration info saved into the
174 // app's state store. Used for testing purpose.
175 static const char* GetPersistentRegisterKeyForTesting();
177 // The profile which owns this object.
178 Profile* profile_;
180 // The username of the signed-in profile.
181 std::string username_;
183 content::NotificationRegistrar registrar_;
185 scoped_ptr<DelayedTaskController> delayed_task_controller_;
187 // For all the work occured in IO thread.
188 scoped_refptr<IOWorker> io_worker_;
190 // Callback map (from app_id to callback) for Register.
191 std::map<std::string, RegisterCallback> register_callbacks_;
193 // Callback map (from <app_id, message_id> to callback) for Send.
194 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_;
196 // Map from app_id to registration info (sender ids & registration ID).
197 typedef std::map<std::string, RegistrationInfo> RegistrationInfoMap;
198 RegistrationInfoMap registration_info_map_;
200 // Event router to talk with JS API.
201 scoped_ptr<GCMEventRouter> js_event_router_;
203 // For testing purpose.
204 TestingDelegate* testing_delegate_;
206 // Used to pass a weak pointer to the IO worker.
207 base::WeakPtrFactory<GCMProfileService> weak_ptr_factory_;
209 DISALLOW_COPY_AND_ASSIGN(GCMProfileService);
212 } // namespace gcm
214 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_PROFILE_SERVICE_H_