[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 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 |
| 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> |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 10 | #include <string> |
tzik | eaca44b | 2016-03-09 22:07:02 | [diff] [blame] | 11 | #include <tuple> |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
| 14 | #include "base/compiler_specific.h" |
| 15 | #include "base/macros.h" |
| 16 | #include "base/memory/ref_counted.h" |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 17 | #include "base/memory/weak_ptr.h" |
fgorski | 0d5c00d | 2014-08-28 16:21:45 | [diff] [blame] | 18 | #include "base/observer_list.h" |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 19 | #include "base/tuple.h" |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 20 | #include "components/gcm_driver/gcm_channel_status_syncer.h" |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 21 | #include "components/gcm_driver/gcm_client.h" |
fgorski | 0d5c00d | 2014-08-28 16:21:45 | [diff] [blame] | 22 | #include "components/gcm_driver/gcm_connection_observer.h" |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 23 | #include "components/gcm_driver/gcm_driver.h" |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 24 | |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 25 | class PrefService; |
| 26 | |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 27 | namespace base { |
| 28 | class FilePath; |
| 29 | class SequencedTaskRunner; |
| 30 | } |
| 31 | |
| 32 | namespace extensions { |
| 33 | class ExtensionGCMAppHandlerTest; |
| 34 | } |
| 35 | |
| 36 | namespace net { |
| 37 | class URLRequestContextGetter; |
| 38 | } |
| 39 | |
| 40 | namespace gcm { |
| 41 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 42 | class GCMAccountMapper; |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 43 | class GCMAppHandler; |
| 44 | class GCMClientFactory; |
fgorski | 05d507c | 2014-09-10 17:28:10 | [diff] [blame] | 45 | class GCMDelayedTaskController; |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 46 | |
| 47 | // GCMDriver implementation for desktop and Chrome OS, using GCMClient. |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 48 | class GCMDriverDesktop : public GCMDriver, |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 49 | public InstanceIDHandler { |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 50 | public: |
| 51 | GCMDriverDesktop( |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 52 | std::unique_ptr<GCMClientFactory> gcm_client_factory, |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 53 | const GCMClient::ChromeBuildInfo& chrome_build_info, |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 54 | const std::string& channel_status_request_url, |
| 55 | const std::string& user_agent, |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 56 | PrefService* prefs, |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 57 | 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); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 62 | ~GCMDriverDesktop() override; |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 63 | |
johnme | 54a3e148 | 2016-03-11 19:13:22 | [diff] [blame] | 64 | // GCMDriver implementation: |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 65 | void Shutdown() override; |
| 66 | void OnSignedIn() override; |
| 67 | void OnSignedOut() override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 68 | 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, |
peter | 1ed8298 | 2015-12-21 17:22:49 | [diff] [blame] | 79 | ClearActivityLogs clear_logs) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 80 | void SetGCMRecording(const GetGCMStatisticsCallback& callback, |
| 81 | bool recording) override; |
| 82 | void SetAccountTokens( |
mostynb | fe59f48 | 2014-10-06 15:04:46 | [diff] [blame] | 83 | const std::vector<GCMClient::AccountTokenInfo>& account_tokens) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 84 | void UpdateAccountMapping(const AccountMapping& account_mapping) override; |
| 85 | void RemoveAccountMapping(const std::string& account_id) override; |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 86 | base::Time GetLastTokenFetchTime() override; |
| 87 | void SetLastTokenFetchTime(const base::Time& time) override; |
chirantan | 192a921 | 2014-12-06 03:30:45 | [diff] [blame] | 88 | void WakeFromSuspendForHeartbeat(bool wake) override; |
johnme | 54a3e148 | 2016-03-11 19:13:22 | [diff] [blame] | 89 | InstanceIDHandler* GetInstanceIDHandlerInternal() override; |
fgorski | 2275446 | 2015-05-14 00:05:22 | [diff] [blame] | 90 | void AddHeartbeatInterval(const std::string& scope, int interval_ms) override; |
| 91 | void RemoveHeartbeatInterval(const std::string& scope) override; |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 92 | |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 93 | // 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] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 99 | protected: |
| 100 | // GCMDriver implementation: |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 101 | GCMClient::Result EnsureStarted(GCMClient::StartMode start_mode) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 102 | 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, |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 107 | const OutgoingMessage& message) override; |
peter | ee284ba5 | 2016-02-01 11:53:28 | [diff] [blame] | 108 | void RecordDecryptionFailure(const std::string& app_id, |
peter | 266a2aa4 | 2016-02-19 18:51:39 | [diff] [blame] | 109 | GCMEncryptionProvider::DecryptionResult result) |
peter | ee284ba5 | 2016-02-01 11:53:28 | [diff] [blame] | 110 | override; |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 111 | |
johnme | 54a3e148 | 2016-03-11 19:13:22 | [diff] [blame] | 112 | // 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] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 129 | private: |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 130 | class IOWorker; |
| 131 | |
tzik | eaca44b | 2016-03-09 22:07:02 | [diff] [blame] | 132 | typedef std::tuple<std::string, std::string, std::string> TokenTuple; |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 133 | struct TokenTupleComparer { |
| 134 | bool operator()(const TokenTuple& a, const TokenTuple& b) const; |
| 135 | }; |
| 136 | |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 137 | // 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] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 143 | 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, |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 148 | const OutgoingMessage& message); |
jianli | ea853487 | 2015-06-22 21:06:22 | [diff] [blame] | 149 | 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); |
jianli | 012b5c8 | 2015-05-28 01:41:29 | [diff] [blame] | 154 | 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] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 161 | |
| 162 | // Callbacks posted from IO thread to UI thread. |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 163 | void MessageReceived(const std::string& app_id, |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 164 | const IncomingMessage& message); |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 165 | void MessagesDeleted(const std::string& app_id); |
| 166 | void MessageSendError(const std::string& app_id, |
| 167 | const GCMClient::SendErrorDetails& send_error_details); |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 168 | void SendAcknowledged(const std::string& app_id, |
| 169 | const std::string& message_id); |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 170 | void GCMClientReady(const std::vector<AccountMapping>& account_mappings, |
| 171 | const base::Time& last_token_fetch_time); |
[email protected] | fc6078a | 2014-06-14 08:28:43 | [diff] [blame] | 172 | void OnConnected(const net::IPEndPoint& ip_endpoint); |
| 173 | void OnDisconnected(); |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 174 | |
[email protected] | 2590835 | 2014-06-03 10:19:11 | [diff] [blame] | 175 | void GetGCMStatisticsFinished(const GCMClient::GCMStatistics& stats); |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 176 | void GetInstanceIDDataFinished(const std::string& app_id, |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 177 | 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] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 188 | |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 189 | std::unique_ptr<GCMChannelStatusSyncer> gcm_channel_status_syncer_; |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 190 | |
[email protected] | da54623f | 2014-06-17 17:03:55 | [diff] [blame] | 191 | // Flag to indicate whether the user is signed in to a GAIA account. |
[email protected] | da54623f | 2014-06-17 17:03:55 | [diff] [blame] | 192 | bool signed_in_; |
| 193 | |
| 194 | // Flag to indicate if GCM is started. |
| 195 | bool gcm_started_; |
| 196 | |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 197 | // Flag to indicate if GCM is enabled. |
| 198 | bool gcm_enabled_; |
| 199 | |
[email protected] | fc6078a | 2014-06-14 08:28:43 | [diff] [blame] | 200 | // 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 | |
fgorski | 0d5c00d | 2014-08-28 16:21:45 | [diff] [blame] | 205 | // List of observers to notify when connection state changes. |
brettw | 236d317 | 2015-06-03 16:31:43 | [diff] [blame] | 206 | base::ObserverList<GCMConnectionObserver, false> connection_observer_list_; |
fgorski | 0d5c00d | 2014-08-28 16:21:45 | [diff] [blame] | 207 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 208 | // Account mapper. Only works when user is signed in. |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 209 | std::unique_ptr<GCMAccountMapper> account_mapper_; |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 210 | |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 211 | // Time of last token fetching. |
| 212 | base::Time last_token_fetch_time_; |
| 213 | |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 214 | scoped_refptr<base::SequencedTaskRunner> ui_thread_; |
| 215 | scoped_refptr<base::SequencedTaskRunner> io_thread_; |
| 216 | |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 217 | std::unique_ptr<GCMDelayedTaskController> delayed_task_controller_; |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 218 | |
chirantan | 192a921 | 2014-12-06 03:30:45 | [diff] [blame] | 219 | // 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] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 223 | // For all the work occurring on the IO thread. Must be destroyed on the IO |
| 224 | // thread. |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 225 | std::unique_ptr<IOWorker> io_worker_; |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 226 | |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 227 | // Callback for GetGCMStatistics. |
| 228 | GetGCMStatisticsCallback request_gcm_statistics_callback_; |
| 229 | |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 230 | // Callbacks for GetInstanceIDData. |
| 231 | std::map<std::string, GetInstanceIDDataCallback> |
| 232 | get_instance_id_data_callbacks_; |
| 233 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 234 | // 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] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 240 | // 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_ |