blob: 5a986e2102539b423f13e20c4d6de9d43d7e9407 [file] [log] [blame]
[email protected]21b77652014-05-31 01:21:091// Copyright 2014 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.
4
5#ifndef COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_
6#define COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_
7
8#include <map>
dchenga77e28eb2016-04-21 21:34:379#include <memory>
[email protected]21b77652014-05-31 01:21:0910#include <string>
tzikeaca44b2016-03-09 22:07:0211#include <tuple>
[email protected]21b77652014-05-31 01:21:0912#include <vector>
13
14#include "base/compiler_specific.h"
15#include "base/macros.h"
16#include "base/memory/ref_counted.h"
[email protected]21b77652014-05-31 01:21:0917#include "base/memory/weak_ptr.h"
fgorski0d5c00d2014-08-28 16:21:4518#include "base/observer_list.h"
jianli7a0c9b62015-05-26 23:24:4719#include "base/tuple.h"
jianli2dc910b02014-09-19 02:42:4620#include "components/gcm_driver/gcm_channel_status_syncer.h"
[email protected]cd57f372014-06-09 17:13:0621#include "components/gcm_driver/gcm_client.h"
fgorski0d5c00d2014-08-28 16:21:4522#include "components/gcm_driver/gcm_connection_observer.h"
[email protected]21b77652014-05-31 01:21:0923#include "components/gcm_driver/gcm_driver.h"
[email protected]21b77652014-05-31 01:21:0924
jianli2dc910b02014-09-19 02:42:4625class PrefService;
26
[email protected]21b77652014-05-31 01:21:0927namespace base {
28class FilePath;
29class SequencedTaskRunner;
30}
31
32namespace extensions {
33class ExtensionGCMAppHandlerTest;
34}
35
36namespace net {
37class URLRequestContextGetter;
38}
39
40namespace gcm {
41
fgorski58b9dfc2014-09-29 16:46:1842class GCMAccountMapper;
[email protected]21b77652014-05-31 01:21:0943class GCMAppHandler;
44class GCMClientFactory;
fgorski05d507c2014-09-10 17:28:1045class GCMDelayedTaskController;
[email protected]21b77652014-05-31 01:21:0946
47// GCMDriver implementation for desktop and Chrome OS, using GCMClient.
jianli10018b2d2015-05-11 21:14:1348class GCMDriverDesktop : public GCMDriver,
jianli7a0c9b62015-05-26 23:24:4749 public InstanceIDHandler {
[email protected]21b77652014-05-31 01:21:0950 public:
51 GCMDriverDesktop(
dchenga77e28eb2016-04-21 21:34:3752 std::unique_ptr<GCMClientFactory> gcm_client_factory,
[email protected]21b77652014-05-31 01:21:0953 const GCMClient::ChromeBuildInfo& chrome_build_info,
fgorski3f6b84cd2014-10-10 21:04:1854 const std::string& channel_status_request_url,
55 const std::string& user_agent,
jianli2dc910b02014-09-19 02:42:4656 PrefService* prefs,
[email protected]21b77652014-05-31 01:21:0957 const base::FilePath& store_path,
58 const scoped_refptr<net::URLRequestContextGetter>& request_context,
59 const scoped_refptr<base::SequencedTaskRunner>& ui_thread,
60 const scoped_refptr<base::SequencedTaskRunner>& io_thread,
61 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner);
dcheng00ea022b2014-10-21 11:24:5662 ~GCMDriverDesktop() override;
[email protected]21b77652014-05-31 01:21:0963
johnme54a3e1482016-03-11 19:13:2264 // GCMDriver implementation:
dcheng00ea022b2014-10-21 11:24:5665 void Shutdown() override;
66 void OnSignedIn() override;
67 void OnSignedOut() override;
dcheng00ea022b2014-10-21 11:24:5668 void AddAppHandler(const std::string& app_id,
69 GCMAppHandler* handler) override;
70 void RemoveAppHandler(const std::string& app_id) override;
71 void AddConnectionObserver(GCMConnectionObserver* observer) override;
72 void RemoveConnectionObserver(GCMConnectionObserver* observer) override;
73 void Enable() override;
74 void Disable() override;
75 GCMClient* GetGCMClientForTesting() const override;
76 bool IsStarted() const override;
77 bool IsConnected() const override;
78 void GetGCMStatistics(const GetGCMStatisticsCallback& callback,
peter1ed82982015-12-21 17:22:4979 ClearActivityLogs clear_logs) override;
dcheng00ea022b2014-10-21 11:24:5680 void SetGCMRecording(const GetGCMStatisticsCallback& callback,
81 bool recording) override;
82 void SetAccountTokens(
mostynbfe59f482014-10-06 15:04:4683 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) override;
dcheng00ea022b2014-10-21 11:24:5684 void UpdateAccountMapping(const AccountMapping& account_mapping) override;
85 void RemoveAccountMapping(const std::string& account_id) override;
fgorski5df101702014-10-28 02:09:3186 base::Time GetLastTokenFetchTime() override;
87 void SetLastTokenFetchTime(const base::Time& time) override;
chirantan192a9212014-12-06 03:30:4588 void WakeFromSuspendForHeartbeat(bool wake) override;
johnme54a3e1482016-03-11 19:13:2289 InstanceIDHandler* GetInstanceIDHandlerInternal() override;
fgorski22754462015-05-14 00:05:2290 void AddHeartbeatInterval(const std::string& scope, int interval_ms) override;
91 void RemoveHeartbeatInterval(const std::string& scope) override;
jianli10018b2d2015-05-11 21:14:1392
jianli2dc910b02014-09-19 02:42:4693 // Exposed for testing purpose.
94 bool gcm_enabled() const { return gcm_enabled_; }
95 GCMChannelStatusSyncer* gcm_channel_status_syncer_for_testing() {
96 return gcm_channel_status_syncer_.get();
97 }
98
[email protected]c27c10792014-06-05 15:27:2399 protected:
100 // GCMDriver implementation:
jianlif3e52af42015-01-21 23:18:47101 GCMClient::Result EnsureStarted(GCMClient::StartMode start_mode) override;
dcheng00ea022b2014-10-21 11:24:56102 void RegisterImpl(const std::string& app_id,
103 const std::vector<std::string>& sender_ids) override;
104 void UnregisterImpl(const std::string& app_id) override;
105 void SendImpl(const std::string& app_id,
106 const std::string& receiver_id,
mvanouwerkerkf8633deb2015-07-13 11:04:06107 const OutgoingMessage& message) override;
peteree284ba52016-02-01 11:53:28108 void RecordDecryptionFailure(const std::string& app_id,
peter266a2aa42016-02-19 18:51:39109 GCMEncryptionProvider::DecryptionResult result)
peteree284ba52016-02-01 11:53:28110 override;
[email protected]21b77652014-05-31 01:21:09111
johnme54a3e1482016-03-11 19:13:22112 // InstanceIDHandler implementation:
113 void GetToken(const std::string& app_id,
114 const std::string& authorized_entity,
115 const std::string& scope,
116 const std::map<std::string, std::string>& options,
117 const GetTokenCallback& callback) override;
118 void DeleteToken(const std::string& app_id,
119 const std::string& authorized_entity,
120 const std::string& scope,
121 const DeleteTokenCallback& callback) override;
122 void AddInstanceIDData(const std::string& app_id,
123 const std::string& instance_id,
124 const std::string& extra_data) override;
125 void RemoveInstanceIDData(const std::string& app_id) override;
126 void GetInstanceIDData(const std::string& app_id,
127 const GetInstanceIDDataCallback& callback) override;
128
[email protected]21b77652014-05-31 01:21:09129 private:
[email protected]21b77652014-05-31 01:21:09130 class IOWorker;
131
tzikeaca44b2016-03-09 22:07:02132 typedef std::tuple<std::string, std::string, std::string> TokenTuple;
jianli7a0c9b62015-05-26 23:24:47133 struct TokenTupleComparer {
134 bool operator()(const TokenTuple& a, const TokenTuple& b) const;
135 };
136
[email protected]21b77652014-05-31 01:21:09137 // Stops the GCM service. It can be restarted by calling EnsureStarted again.
138 void Stop();
139
140 // Remove cached data when GCM service is stopped.
141 void RemoveCachedData();
142
[email protected]21b77652014-05-31 01:21:09143 void DoRegister(const std::string& app_id,
144 const std::vector<std::string>& sender_ids);
145 void DoUnregister(const std::string& app_id);
146 void DoSend(const std::string& app_id,
147 const std::string& receiver_id,
mvanouwerkerkf8633deb2015-07-13 11:04:06148 const OutgoingMessage& message);
jianliea8534872015-06-22 21:06:22149 void DoAddInstanceIDData(const std::string& app_id,
150 const std::string& instance_id,
151 const std::string& extra_data);
152 void DoRemoveInstanceIDData(const std::string& app_id);
153 void DoGetInstanceIDData(const std::string& app_id);
jianli012b5c82015-05-28 01:41:29154 void DoGetToken(const std::string& app_id,
155 const std::string& authorized_entity,
156 const std::string& scope,
157 const std::map<std::string, std::string>& options);
158 void DoDeleteToken(const std::string& app_id,
159 const std::string& authorized_entity,
160 const std::string& scope);
[email protected]21b77652014-05-31 01:21:09161
162 // Callbacks posted from IO thread to UI thread.
[email protected]21b77652014-05-31 01:21:09163 void MessageReceived(const std::string& app_id,
mvanouwerkerkf8633deb2015-07-13 11:04:06164 const IncomingMessage& message);
[email protected]21b77652014-05-31 01:21:09165 void MessagesDeleted(const std::string& app_id);
166 void MessageSendError(const std::string& app_id,
167 const GCMClient::SendErrorDetails& send_error_details);
[email protected]292af2b22014-08-06 19:42:45168 void SendAcknowledged(const std::string& app_id,
169 const std::string& message_id);
fgorski5df101702014-10-28 02:09:31170 void GCMClientReady(const std::vector<AccountMapping>& account_mappings,
171 const base::Time& last_token_fetch_time);
[email protected]fc6078a2014-06-14 08:28:43172 void OnConnected(const net::IPEndPoint& ip_endpoint);
173 void OnDisconnected();
[email protected]21b77652014-05-31 01:21:09174
[email protected]25908352014-06-03 10:19:11175 void GetGCMStatisticsFinished(const GCMClient::GCMStatistics& stats);
jianli10018b2d2015-05-11 21:14:13176 void GetInstanceIDDataFinished(const std::string& app_id,
jianli7a0c9b62015-05-26 23:24:47177 const std::string& instance_id,
178 const std::string& extra_data);
179 void GetTokenFinished(const std::string& app_id,
180 const std::string& authorized_entity,
181 const std::string& scope,
182 const std::string& token,
183 GCMClient::Result result);
184 void DeleteTokenFinished(const std::string& app_id,
185 const std::string& authorized_entity,
186 const std::string& scope,
187 GCMClient::Result result);
[email protected]21b77652014-05-31 01:21:09188
dchenga77e28eb2016-04-21 21:34:37189 std::unique_ptr<GCMChannelStatusSyncer> gcm_channel_status_syncer_;
jianli2dc910b02014-09-19 02:42:46190
[email protected]da54623f2014-06-17 17:03:55191 // Flag to indicate whether the user is signed in to a GAIA account.
[email protected]da54623f2014-06-17 17:03:55192 bool signed_in_;
193
194 // Flag to indicate if GCM is started.
195 bool gcm_started_;
196
[email protected]21b77652014-05-31 01:21:09197 // Flag to indicate if GCM is enabled.
198 bool gcm_enabled_;
199
[email protected]fc6078a2014-06-14 08:28:43200 // Flag to indicate the last known state of the GCM client. Because this
201 // flag lives on the UI thread, while the GCM client lives on the IO thread,
202 // it may be out of date while connection changes are happening.
203 bool connected_;
204
fgorski0d5c00d2014-08-28 16:21:45205 // List of observers to notify when connection state changes.
brettw236d3172015-06-03 16:31:43206 base::ObserverList<GCMConnectionObserver, false> connection_observer_list_;
fgorski0d5c00d2014-08-28 16:21:45207
fgorski58b9dfc2014-09-29 16:46:18208 // Account mapper. Only works when user is signed in.
dchenga77e28eb2016-04-21 21:34:37209 std::unique_ptr<GCMAccountMapper> account_mapper_;
fgorski58b9dfc2014-09-29 16:46:18210
fgorski5df101702014-10-28 02:09:31211 // Time of last token fetching.
212 base::Time last_token_fetch_time_;
213
[email protected]21b77652014-05-31 01:21:09214 scoped_refptr<base::SequencedTaskRunner> ui_thread_;
215 scoped_refptr<base::SequencedTaskRunner> io_thread_;
216
dchenga77e28eb2016-04-21 21:34:37217 std::unique_ptr<GCMDelayedTaskController> delayed_task_controller_;
[email protected]21b77652014-05-31 01:21:09218
chirantan192a9212014-12-06 03:30:45219 // Whether the HeartbeatManager should try to wake the system from suspend for
220 // sending heartbeat messages.
221 bool wake_from_suspend_enabled_;
222
[email protected]21b77652014-05-31 01:21:09223 // For all the work occurring on the IO thread. Must be destroyed on the IO
224 // thread.
dchenga77e28eb2016-04-21 21:34:37225 std::unique_ptr<IOWorker> io_worker_;
[email protected]21b77652014-05-31 01:21:09226
[email protected]21b77652014-05-31 01:21:09227 // Callback for GetGCMStatistics.
228 GetGCMStatisticsCallback request_gcm_statistics_callback_;
229
jianli10018b2d2015-05-11 21:14:13230 // Callbacks for GetInstanceIDData.
231 std::map<std::string, GetInstanceIDDataCallback>
232 get_instance_id_data_callbacks_;
233
jianli7a0c9b62015-05-26 23:24:47234 // Callbacks for GetToken/DeleteToken.
235 std::map<TokenTuple, GetTokenCallback, TokenTupleComparer>
236 get_token_callbacks_;
237 std::map<TokenTuple, DeleteTokenCallback, TokenTupleComparer>
238 delete_token_callbacks_;
239
[email protected]21b77652014-05-31 01:21:09240 // Used to pass a weak pointer to the IO worker.
241 base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_;
242
243 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop);
244};
245
246} // namespace gcm
247
248#endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_