[email protected] | df84c53 | 2014-04-25 15:36:54 | [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 | |
[email protected] | 8d41e5a | 2014-05-28 03:18:49 | [diff] [blame] | 5 | #include "components/gcm_driver/gcm_driver.h" |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 6 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
jianli | 753282a3 | 2015-01-09 02:07:40 | [diff] [blame] | 11 | #include "base/bind.h" |
peter | bfa736e | 2015-07-28 16:19:55 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 13 | #include "base/logging.h" |
peter | 266a2aa4 | 2016-02-19 18:51:39 | [diff] [blame] | 14 | #include "base/metrics/histogram_macros.h" |
[email protected] | 446f73c2 | 2014-05-14 20:47:18 | [diff] [blame] | 15 | #include "components/gcm_driver/gcm_app_handler.h" |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 16 | |
| 17 | namespace gcm { |
| 18 | |
peter | add31f6 | 2015-10-09 10:18:52 | [diff] [blame] | 19 | namespace { |
| 20 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 21 | const size_t kMaxSenders = 100; |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 22 | |
peter | add31f6 | 2015-10-09 10:18:52 | [diff] [blame] | 23 | } // namespace |
| 24 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 25 | InstanceIDHandler::InstanceIDHandler() { |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 26 | } |
| 27 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 28 | InstanceIDHandler::~InstanceIDHandler() { |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 29 | } |
| 30 | |
jianli | c5f6bd9 | 2015-05-28 02:23:45 | [diff] [blame] | 31 | void InstanceIDHandler::DeleteAllTokensForApp( |
| 32 | const std::string& app_id, const DeleteTokenCallback& callback) { |
| 33 | DeleteToken(app_id, "*", "*", callback); |
| 34 | } |
| 35 | |
peter | bfa736e | 2015-07-28 16:19:55 | [diff] [blame] | 36 | GCMDriver::GCMDriver( |
| 37 | const base::FilePath& store_path, |
| 38 | const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) |
| 39 | : weak_ptr_factory_(this) { |
| 40 | // The |blocking_task_runner| can be NULL for tests that do not need the |
| 41 | // encryption capabilities of the GCMDriver class. |
| 42 | if (blocking_task_runner) |
| 43 | encryption_provider_.Init(store_path, blocking_task_runner); |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | GCMDriver::~GCMDriver() { |
| 47 | } |
| 48 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 49 | void GCMDriver::Register(const std::string& app_id, |
| 50 | const std::vector<std::string>& sender_ids, |
| 51 | const RegisterCallback& callback) { |
| 52 | DCHECK(!app_id.empty()); |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 53 | DCHECK(!sender_ids.empty() && sender_ids.size() <= kMaxSenders); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 54 | DCHECK(!callback.is_null()); |
| 55 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 56 | GCMClient::Result result = EnsureStarted(GCMClient::IMMEDIATE_START); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 57 | if (result != GCMClient::SUCCESS) { |
| 58 | callback.Run(std::string(), result); |
| 59 | return; |
| 60 | } |
| 61 | |
jianli | f17b85a | 2014-10-29 21:42:30 | [diff] [blame] | 62 | // If previous register operation is still in progress, bail out. |
| 63 | if (register_callbacks_.find(app_id) != register_callbacks_.end()) { |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 64 | callback.Run(std::string(), GCMClient::ASYNC_OPERATION_PENDING); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | // Normalize the sender IDs by making them sorted. |
| 69 | std::vector<std::string> normalized_sender_ids = sender_ids; |
| 70 | std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end()); |
| 71 | |
| 72 | register_callbacks_[app_id] = callback; |
| 73 | |
jianli | f17b85a | 2014-10-29 21:42:30 | [diff] [blame] | 74 | // If previous unregister operation is still in progress, wait until it |
| 75 | // finishes. We don't want to throw ASYNC_OPERATION_PENDING when the user |
| 76 | // uninstalls an app (ungistering) and then reinstalls the app again |
| 77 | // (registering). |
| 78 | std::map<std::string, UnregisterCallback>::iterator unregister_iter = |
| 79 | unregister_callbacks_.find(app_id); |
| 80 | if (unregister_iter != unregister_callbacks_.end()) { |
| 81 | // Replace the original unregister callback with an intermediate callback |
| 82 | // that will invoke the original unregister callback and trigger the pending |
| 83 | // registration after the unregistration finishes. |
| 84 | // Note that some parameters to RegisterAfterUnregister are specified here |
| 85 | // when the callback is created (base::Bind supports the partial binding |
| 86 | // of parameters). |
| 87 | unregister_iter->second = base::Bind( |
| 88 | &GCMDriver::RegisterAfterUnregister, |
| 89 | weak_ptr_factory_.GetWeakPtr(), |
| 90 | app_id, |
| 91 | normalized_sender_ids, |
| 92 | unregister_iter->second); |
| 93 | return; |
| 94 | } |
| 95 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 96 | RegisterImpl(app_id, normalized_sender_ids); |
| 97 | } |
| 98 | |
| 99 | void GCMDriver::Unregister(const std::string& app_id, |
| 100 | const UnregisterCallback& callback) { |
johnme | 07b355a | 2015-02-19 17:00:51 | [diff] [blame] | 101 | UnregisterInternal(app_id, nullptr /* sender_id */, callback); |
| 102 | } |
| 103 | |
| 104 | void GCMDriver::UnregisterWithSenderId( |
| 105 | const std::string& app_id, |
| 106 | const std::string& sender_id, |
| 107 | const UnregisterCallback& callback) { |
| 108 | DCHECK(!sender_id.empty()); |
| 109 | UnregisterInternal(app_id, &sender_id, callback); |
| 110 | } |
| 111 | |
| 112 | void GCMDriver::UnregisterInternal(const std::string& app_id, |
| 113 | const std::string* sender_id, |
| 114 | const UnregisterCallback& callback) { |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 115 | DCHECK(!app_id.empty()); |
| 116 | DCHECK(!callback.is_null()); |
| 117 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 118 | GCMClient::Result result = EnsureStarted(GCMClient::IMMEDIATE_START); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 119 | if (result != GCMClient::SUCCESS) { |
| 120 | callback.Run(result); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | // If previous un/register operation is still in progress, bail out. |
jianli | f17b85a | 2014-10-29 21:42:30 | [diff] [blame] | 125 | if (register_callbacks_.find(app_id) != register_callbacks_.end() || |
| 126 | unregister_callbacks_.find(app_id) != unregister_callbacks_.end()) { |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 127 | callback.Run(GCMClient::ASYNC_OPERATION_PENDING); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | unregister_callbacks_[app_id] = callback; |
| 132 | |
johnme | 07b355a | 2015-02-19 17:00:51 | [diff] [blame] | 133 | if (sender_id) |
| 134 | UnregisterWithSenderIdImpl(app_id, *sender_id); |
| 135 | else |
| 136 | UnregisterImpl(app_id); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void GCMDriver::Send(const std::string& app_id, |
| 140 | const std::string& receiver_id, |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 141 | const OutgoingMessage& message, |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 142 | const SendCallback& callback) { |
| 143 | DCHECK(!app_id.empty()); |
| 144 | DCHECK(!receiver_id.empty()); |
| 145 | DCHECK(!callback.is_null()); |
| 146 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 147 | GCMClient::Result result = EnsureStarted(GCMClient::IMMEDIATE_START); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 148 | if (result != GCMClient::SUCCESS) { |
| 149 | callback.Run(std::string(), result); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | // If the message with send ID is still in progress, bail out. |
| 154 | std::pair<std::string, std::string> key(app_id, message.id); |
| 155 | if (send_callbacks_.find(key) != send_callbacks_.end()) { |
| 156 | callback.Run(message.id, GCMClient::INVALID_PARAMETER); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | send_callbacks_[key] = callback; |
| 161 | |
| 162 | SendImpl(app_id, receiver_id, message); |
| 163 | } |
| 164 | |
peter | 903cde43 | 2015-12-21 15:39:50 | [diff] [blame] | 165 | void GCMDriver::GetEncryptionInfo( |
peter | bfa736e | 2015-07-28 16:19:55 | [diff] [blame] | 166 | const std::string& app_id, |
peter | 903cde43 | 2015-12-21 15:39:50 | [diff] [blame] | 167 | const GetEncryptionInfoCallback& callback) { |
johnme | 36ae580 | 2016-05-10 15:46:24 | [diff] [blame^] | 168 | encryption_provider_.GetEncryptionInfo(app_id, "" /* authorized_entity */, |
| 169 | callback); |
peter | bfa736e | 2015-07-28 16:19:55 | [diff] [blame] | 170 | } |
| 171 | |
johnme | 07b355a | 2015-02-19 17:00:51 | [diff] [blame] | 172 | void GCMDriver::UnregisterWithSenderIdImpl(const std::string& app_id, |
| 173 | const std::string& sender_id) { |
| 174 | NOTREACHED(); |
| 175 | } |
| 176 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 177 | void GCMDriver::RegisterFinished(const std::string& app_id, |
| 178 | const std::string& registration_id, |
| 179 | GCMClient::Result result) { |
| 180 | std::map<std::string, RegisterCallback>::iterator callback_iter = |
| 181 | register_callbacks_.find(app_id); |
| 182 | if (callback_iter == register_callbacks_.end()) { |
| 183 | // The callback could have been removed when the app is uninstalled. |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | RegisterCallback callback = callback_iter->second; |
| 188 | register_callbacks_.erase(callback_iter); |
| 189 | callback.Run(registration_id, result); |
| 190 | } |
| 191 | |
peter | a4795ae83 | 2016-02-17 11:35:27 | [diff] [blame] | 192 | void GCMDriver::RemoveEncryptionInfoAfterUnregister(const std::string& app_id, |
| 193 | GCMClient::Result result) { |
| 194 | encryption_provider_.RemoveEncryptionInfo( |
johnme | 36ae580 | 2016-05-10 15:46:24 | [diff] [blame^] | 195 | app_id, "" /* authorized_entity */, |
| 196 | base::Bind(&GCMDriver::UnregisterFinished, weak_ptr_factory_.GetWeakPtr(), |
| 197 | app_id, result)); |
peter | a4795ae83 | 2016-02-17 11:35:27 | [diff] [blame] | 198 | } |
| 199 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 200 | void GCMDriver::UnregisterFinished(const std::string& app_id, |
| 201 | GCMClient::Result result) { |
| 202 | std::map<std::string, UnregisterCallback>::iterator callback_iter = |
| 203 | unregister_callbacks_.find(app_id); |
| 204 | if (callback_iter == unregister_callbacks_.end()) |
| 205 | return; |
| 206 | |
| 207 | UnregisterCallback callback = callback_iter->second; |
| 208 | unregister_callbacks_.erase(callback_iter); |
| 209 | callback.Run(result); |
| 210 | } |
| 211 | |
| 212 | void GCMDriver::SendFinished(const std::string& app_id, |
| 213 | const std::string& message_id, |
| 214 | GCMClient::Result result) { |
| 215 | std::map<std::pair<std::string, std::string>, SendCallback>::iterator |
| 216 | callback_iter = send_callbacks_.find( |
| 217 | std::pair<std::string, std::string>(app_id, message_id)); |
| 218 | if (callback_iter == send_callbacks_.end()) { |
| 219 | // The callback could have been removed when the app is uninstalled. |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | SendCallback callback = callback_iter->second; |
| 224 | send_callbacks_.erase(callback_iter); |
| 225 | callback.Run(message_id, result); |
| 226 | } |
| 227 | |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 228 | void GCMDriver::Shutdown() { |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 229 | for (GCMAppHandlerMap::const_iterator iter = app_handlers_.begin(); |
| 230 | iter != app_handlers_.end(); ++iter) { |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 231 | DVLOG(1) << "Calling ShutdownHandler for: " << iter->first; |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 232 | iter->second->ShutdownHandler(); |
| 233 | } |
| 234 | app_handlers_.clear(); |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 235 | } |
| 236 | |
[email protected] | 530f216 | 2014-05-15 01:05:04 | [diff] [blame] | 237 | void GCMDriver::AddAppHandler(const std::string& app_id, |
| 238 | GCMAppHandler* handler) { |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 239 | DCHECK(!app_id.empty()); |
| 240 | DCHECK(handler); |
[email protected] | b717626 | 2014-06-18 15:26:29 | [diff] [blame] | 241 | DCHECK_EQ(app_handlers_.count(app_id), 0u); |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 242 | app_handlers_[app_id] = handler; |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 243 | DVLOG(1) << "App handler added for: " << app_id; |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 244 | } |
| 245 | |
[email protected] | 530f216 | 2014-05-15 01:05:04 | [diff] [blame] | 246 | void GCMDriver::RemoveAppHandler(const std::string& app_id) { |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 247 | DCHECK(!app_id.empty()); |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 248 | app_handlers_.erase(app_id); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 249 | DVLOG(1) << "App handler removed for: " << app_id; |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 250 | } |
| 251 | |
[email protected] | 530f216 | 2014-05-15 01:05:04 | [diff] [blame] | 252 | GCMAppHandler* GCMDriver::GetAppHandler(const std::string& app_id) { |
[email protected] | b717626 | 2014-06-18 15:26:29 | [diff] [blame] | 253 | // Look for exact match. |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 254 | GCMAppHandlerMap::const_iterator iter = app_handlers_.find(app_id); |
[email protected] | b717626 | 2014-06-18 15:26:29 | [diff] [blame] | 255 | if (iter != app_handlers_.end()) |
| 256 | return iter->second; |
| 257 | |
| 258 | // Ask the handlers whether they know how to handle it. |
| 259 | for (iter = app_handlers_.begin(); iter != app_handlers_.end(); ++iter) { |
| 260 | if (iter->second->CanHandle(app_id)) |
| 261 | return iter->second; |
| 262 | } |
| 263 | |
| 264 | return &default_app_handler_; |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 267 | bool GCMDriver::HasRegisterCallback(const std::string& app_id) { |
| 268 | return register_callbacks_.find(app_id) != register_callbacks_.end(); |
| 269 | } |
| 270 | |
| 271 | void GCMDriver::ClearCallbacks() { |
| 272 | register_callbacks_.clear(); |
| 273 | unregister_callbacks_.clear(); |
| 274 | send_callbacks_.clear(); |
| 275 | } |
| 276 | |
peter | add31f6 | 2015-10-09 10:18:52 | [diff] [blame] | 277 | void GCMDriver::DispatchMessage(const std::string& app_id, |
| 278 | const IncomingMessage& message) { |
peter | 266a2aa4 | 2016-02-19 18:51:39 | [diff] [blame] | 279 | encryption_provider_.DecryptMessage( |
| 280 | app_id, message, base::Bind(&GCMDriver::DispatchMessageInternal, |
| 281 | weak_ptr_factory_.GetWeakPtr(), app_id)); |
| 282 | } |
| 283 | |
| 284 | void GCMDriver::DispatchMessageInternal( |
| 285 | const std::string& app_id, |
| 286 | GCMEncryptionProvider::DecryptionResult result, |
| 287 | const IncomingMessage& message) { |
| 288 | UMA_HISTOGRAM_ENUMERATION("GCM.Crypto.DecryptMessageResult", result, |
| 289 | GCMEncryptionProvider::DECRYPTION_RESULT_LAST + 1); |
| 290 | |
| 291 | switch (result) { |
| 292 | case GCMEncryptionProvider::DECRYPTION_RESULT_UNENCRYPTED: |
| 293 | case GCMEncryptionProvider::DECRYPTION_RESULT_DECRYPTED: |
| 294 | GetAppHandler(app_id)->OnMessage(app_id, message); |
| 295 | return; |
| 296 | case GCMEncryptionProvider::DECRYPTION_RESULT_INVALID_ENCRYPTION_HEADER: |
| 297 | case GCMEncryptionProvider::DECRYPTION_RESULT_INVALID_CRYPTO_KEY_HEADER: |
| 298 | case GCMEncryptionProvider::DECRYPTION_RESULT_NO_KEYS: |
| 299 | case GCMEncryptionProvider::DECRYPTION_RESULT_INVALID_SHARED_SECRET: |
| 300 | case GCMEncryptionProvider::DECRYPTION_RESULT_INVALID_PAYLOAD: |
| 301 | RecordDecryptionFailure(app_id, result); |
| 302 | return; |
peter | add31f6 | 2015-10-09 10:18:52 | [diff] [blame] | 303 | } |
| 304 | |
peter | 266a2aa4 | 2016-02-19 18:51:39 | [diff] [blame] | 305 | NOTREACHED(); |
peter | add31f6 | 2015-10-09 10:18:52 | [diff] [blame] | 306 | } |
| 307 | |
jianli | f17b85a | 2014-10-29 21:42:30 | [diff] [blame] | 308 | void GCMDriver::RegisterAfterUnregister( |
| 309 | const std::string& app_id, |
| 310 | const std::vector<std::string>& normalized_sender_ids, |
| 311 | const UnregisterCallback& unregister_callback, |
| 312 | GCMClient::Result result) { |
| 313 | // Invoke the original unregister callback. |
| 314 | unregister_callback.Run(result); |
| 315 | |
| 316 | // Trigger the pending registration. |
| 317 | DCHECK(register_callbacks_.find(app_id) != register_callbacks_.end()); |
| 318 | RegisterImpl(app_id, normalized_sender_ids); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 319 | } |
| 320 | |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 321 | } // namespace gcm |