[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" |
Peter Beverloo | a376e98c | 2017-06-27 15:55:37 | [diff] [blame] | 15 | #include "components/gcm_driver/crypto/gcm_decryption_result.h" |
Alex Chau | 67605b9 | 2019-06-05 10:48:29 | [diff] [blame] | 16 | #include "components/gcm_driver/crypto/gcm_encryption_result.h" |
[email protected] | 446f73c2 | 2014-05-14 20:47:18 | [diff] [blame] | 17 | #include "components/gcm_driver/gcm_app_handler.h" |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 18 | |
| 19 | namespace gcm { |
| 20 | |
Rayan Kanso | 9e5adf21 | 2019-05-09 16:07:53 | [diff] [blame] | 21 | InstanceIDHandler::InstanceIDHandler() = default; |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 22 | |
Rayan Kanso | 9e5adf21 | 2019-05-09 16:07:53 | [diff] [blame] | 23 | InstanceIDHandler::~InstanceIDHandler() = default; |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 24 | |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 25 | void InstanceIDHandler::DeleteAllTokensForApp(const std::string& app_id, |
| 26 | DeleteTokenCallback callback) { |
| 27 | DeleteToken(app_id, "*", "*", std::move(callback)); |
jianli | c5f6bd9 | 2015-05-28 02:23:45 | [diff] [blame] | 28 | } |
| 29 | |
peter | bfa736e | 2015-07-28 16:19:55 | [diff] [blame] | 30 | GCMDriver::GCMDriver( |
| 31 | const base::FilePath& store_path, |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 32 | const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) { |
Alex Chau | a76a6e3 | 2019-06-26 16:20:01 | [diff] [blame] | 33 | // The |blocking_task_runner| can be nullptr for tests that do not need the |
peter | bfa736e | 2015-07-28 16:19:55 | [diff] [blame] | 34 | // encryption capabilities of the GCMDriver class. |
| 35 | if (blocking_task_runner) |
| 36 | encryption_provider_.Init(store_path, blocking_task_runner); |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 37 | } |
| 38 | |
Rayan Kanso | 9e5adf21 | 2019-05-09 16:07:53 | [diff] [blame] | 39 | GCMDriver::~GCMDriver() = default; |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 40 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 41 | void GCMDriver::Register(const std::string& app_id, |
| 42 | const std::vector<std::string>& sender_ids, |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 43 | RegisterCallback callback) { |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 44 | DCHECK(!app_id.empty()); |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 45 | DCHECK(!sender_ids.empty() && sender_ids.size() <= kMaxSenders); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 46 | DCHECK(!callback.is_null()); |
| 47 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 48 | GCMClient::Result result = EnsureStarted(GCMClient::IMMEDIATE_START); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 49 | if (result != GCMClient::SUCCESS) { |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 50 | std::move(callback).Run(std::string(), result); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | |
jianli | f17b85a | 2014-10-29 21:42:30 | [diff] [blame] | 54 | // If previous register operation is still in progress, bail out. |
| 55 | if (register_callbacks_.find(app_id) != register_callbacks_.end()) { |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 56 | std::move(callback).Run(std::string(), GCMClient::ASYNC_OPERATION_PENDING); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
| 60 | // Normalize the sender IDs by making them sorted. |
| 61 | std::vector<std::string> normalized_sender_ids = sender_ids; |
| 62 | std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end()); |
| 63 | |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 64 | register_callbacks_[app_id] = std::move(callback); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 65 | |
jianli | f17b85a | 2014-10-29 21:42:30 | [diff] [blame] | 66 | // If previous unregister operation is still in progress, wait until it |
| 67 | // finishes. We don't want to throw ASYNC_OPERATION_PENDING when the user |
| 68 | // uninstalls an app (ungistering) and then reinstalls the app again |
| 69 | // (registering). |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 70 | auto unregister_iter = unregister_callbacks_.find(app_id); |
jianli | f17b85a | 2014-10-29 21:42:30 | [diff] [blame] | 71 | if (unregister_iter != unregister_callbacks_.end()) { |
| 72 | // Replace the original unregister callback with an intermediate callback |
| 73 | // that will invoke the original unregister callback and trigger the pending |
| 74 | // registration after the unregistration finishes. |
| 75 | // Note that some parameters to RegisterAfterUnregister are specified here |
| 76 | // when the callback is created (base::Bind supports the partial binding |
| 77 | // of parameters). |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 78 | unregister_iter->second = base::BindOnce( |
| 79 | &GCMDriver::RegisterAfterUnregister, weak_ptr_factory_.GetWeakPtr(), |
| 80 | app_id, normalized_sender_ids, std::move(unregister_iter->second)); |
jianli | f17b85a | 2014-10-29 21:42:30 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 84 | RegisterImpl(app_id, normalized_sender_ids); |
| 85 | } |
| 86 | |
| 87 | void GCMDriver::Unregister(const std::string& app_id, |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 88 | UnregisterCallback callback) { |
| 89 | UnregisterInternal(app_id, nullptr /* sender_id */, std::move(callback)); |
johnme | 07b355a | 2015-02-19 17:00:51 | [diff] [blame] | 90 | } |
| 91 | |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 92 | void GCMDriver::UnregisterWithSenderId(const std::string& app_id, |
| 93 | const std::string& sender_id, |
| 94 | UnregisterCallback callback) { |
johnme | 07b355a | 2015-02-19 17:00:51 | [diff] [blame] | 95 | DCHECK(!sender_id.empty()); |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 96 | UnregisterInternal(app_id, &sender_id, std::move(callback)); |
johnme | 07b355a | 2015-02-19 17:00:51 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void GCMDriver::UnregisterInternal(const std::string& app_id, |
| 100 | const std::string* sender_id, |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 101 | UnregisterCallback callback) { |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 102 | DCHECK(!app_id.empty()); |
| 103 | DCHECK(!callback.is_null()); |
| 104 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 105 | GCMClient::Result result = EnsureStarted(GCMClient::IMMEDIATE_START); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 106 | if (result != GCMClient::SUCCESS) { |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 107 | std::move(callback).Run(result); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
| 111 | // If previous un/register operation is still in progress, bail out. |
jianli | f17b85a | 2014-10-29 21:42:30 | [diff] [blame] | 112 | if (register_callbacks_.find(app_id) != register_callbacks_.end() || |
| 113 | unregister_callbacks_.find(app_id) != unregister_callbacks_.end()) { |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 114 | std::move(callback).Run(GCMClient::ASYNC_OPERATION_PENDING); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 118 | unregister_callbacks_[app_id] = std::move(callback); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 119 | |
johnme | 07b355a | 2015-02-19 17:00:51 | [diff] [blame] | 120 | if (sender_id) |
| 121 | UnregisterWithSenderIdImpl(app_id, *sender_id); |
| 122 | else |
| 123 | UnregisterImpl(app_id); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void GCMDriver::Send(const std::string& app_id, |
| 127 | const std::string& receiver_id, |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 128 | const OutgoingMessage& message, |
Reilly Grant | aa7bc2f | 2020-01-31 11:49:29 | [diff] [blame] | 129 | SendCallback callback) { |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 130 | DCHECK(!app_id.empty()); |
| 131 | DCHECK(!receiver_id.empty()); |
| 132 | DCHECK(!callback.is_null()); |
| 133 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 134 | GCMClient::Result result = EnsureStarted(GCMClient::IMMEDIATE_START); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 135 | if (result != GCMClient::SUCCESS) { |
Reilly Grant | aa7bc2f | 2020-01-31 11:49:29 | [diff] [blame] | 136 | std::move(callback).Run(std::string(), result); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
| 140 | // If the message with send ID is still in progress, bail out. |
| 141 | std::pair<std::string, std::string> key(app_id, message.id); |
| 142 | if (send_callbacks_.find(key) != send_callbacks_.end()) { |
Reilly Grant | aa7bc2f | 2020-01-31 11:49:29 | [diff] [blame] | 143 | std::move(callback).Run(message.id, GCMClient::INVALID_PARAMETER); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 144 | return; |
| 145 | } |
| 146 | |
Reilly Grant | aa7bc2f | 2020-01-31 11:49:29 | [diff] [blame] | 147 | send_callbacks_[key] = std::move(callback); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 148 | |
| 149 | SendImpl(app_id, receiver_id, message); |
| 150 | } |
| 151 | |
Peter Beverloo | 9635b66 | 2019-07-10 19:05:18 | [diff] [blame] | 152 | void GCMDriver::GetEncryptionInfo(const std::string& app_id, |
| 153 | GetEncryptionInfoCallback callback) { |
johnme | 36ae580 | 2016-05-10 15:46:24 | [diff] [blame] | 154 | encryption_provider_.GetEncryptionInfo(app_id, "" /* authorized_entity */, |
Peter Beverloo | 9635b66 | 2019-07-10 19:05:18 | [diff] [blame] | 155 | std::move(callback)); |
peter | bfa736e | 2015-07-28 16:19:55 | [diff] [blame] | 156 | } |
| 157 | |
johnme | 07b355a | 2015-02-19 17:00:51 | [diff] [blame] | 158 | void GCMDriver::UnregisterWithSenderIdImpl(const std::string& app_id, |
| 159 | const std::string& sender_id) { |
| 160 | NOTREACHED(); |
| 161 | } |
| 162 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 163 | void GCMDriver::RegisterFinished(const std::string& app_id, |
| 164 | const std::string& registration_id, |
| 165 | GCMClient::Result result) { |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 166 | auto callback_iter = register_callbacks_.find(app_id); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 167 | if (callback_iter == register_callbacks_.end()) { |
| 168 | // The callback could have been removed when the app is uninstalled. |
| 169 | return; |
| 170 | } |
| 171 | |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 172 | RegisterCallback callback = std::move(callback_iter->second); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 173 | register_callbacks_.erase(callback_iter); |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 174 | std::move(callback).Run(registration_id, result); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 175 | } |
| 176 | |
peter | a4795ae83 | 2016-02-17 11:35:27 | [diff] [blame] | 177 | void GCMDriver::RemoveEncryptionInfoAfterUnregister(const std::string& app_id, |
| 178 | GCMClient::Result result) { |
| 179 | encryption_provider_.RemoveEncryptionInfo( |
johnme | 36ae580 | 2016-05-10 15:46:24 | [diff] [blame] | 180 | app_id, "" /* authorized_entity */, |
Reilly Grant | aa7bc2f | 2020-01-31 11:49:29 | [diff] [blame] | 181 | base::BindOnce(&GCMDriver::UnregisterFinished, |
| 182 | weak_ptr_factory_.GetWeakPtr(), app_id, result)); |
peter | a4795ae83 | 2016-02-17 11:35:27 | [diff] [blame] | 183 | } |
| 184 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 185 | void GCMDriver::UnregisterFinished(const std::string& app_id, |
| 186 | GCMClient::Result result) { |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 187 | auto callback_iter = unregister_callbacks_.find(app_id); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 188 | if (callback_iter == unregister_callbacks_.end()) |
| 189 | return; |
| 190 | |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 191 | UnregisterCallback callback = std::move(callback_iter->second); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 192 | unregister_callbacks_.erase(callback_iter); |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 193 | std::move(callback).Run(result); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void GCMDriver::SendFinished(const std::string& app_id, |
| 197 | const std::string& message_id, |
| 198 | GCMClient::Result result) { |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 199 | auto callback_iter = send_callbacks_.find( |
| 200 | std::pair<std::string, std::string>(app_id, message_id)); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 201 | if (callback_iter == send_callbacks_.end()) { |
| 202 | // The callback could have been removed when the app is uninstalled. |
| 203 | return; |
| 204 | } |
| 205 | |
Reilly Grant | aa7bc2f | 2020-01-31 11:49:29 | [diff] [blame] | 206 | SendCallback callback = std::move(callback_iter->second); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 207 | send_callbacks_.erase(callback_iter); |
Reilly Grant | aa7bc2f | 2020-01-31 11:49:29 | [diff] [blame] | 208 | std::move(callback).Run(message_id, result); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 211 | void GCMDriver::Shutdown() { |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 212 | for (GCMAppHandlerMap::const_iterator iter = app_handlers_.begin(); |
| 213 | iter != app_handlers_.end(); ++iter) { |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 214 | DVLOG(1) << "Calling ShutdownHandler for: " << iter->first; |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 215 | iter->second->ShutdownHandler(); |
| 216 | } |
| 217 | app_handlers_.clear(); |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 218 | } |
| 219 | |
[email protected] | 530f216 | 2014-05-15 01:05:04 | [diff] [blame] | 220 | void GCMDriver::AddAppHandler(const std::string& app_id, |
| 221 | GCMAppHandler* handler) { |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 222 | DCHECK(!app_id.empty()); |
| 223 | DCHECK(handler); |
[email protected] | b717626 | 2014-06-18 15:26:29 | [diff] [blame] | 224 | DCHECK_EQ(app_handlers_.count(app_id), 0u); |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 225 | app_handlers_[app_id] = handler; |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 226 | DVLOG(1) << "App handler added for: " << app_id; |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 227 | } |
| 228 | |
[email protected] | 530f216 | 2014-05-15 01:05:04 | [diff] [blame] | 229 | void GCMDriver::RemoveAppHandler(const std::string& app_id) { |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 230 | DCHECK(!app_id.empty()); |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 231 | app_handlers_.erase(app_id); |
fgorski | 83afd87 | 2014-10-16 01:11:52 | [diff] [blame] | 232 | DVLOG(1) << "App handler removed for: " << app_id; |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 233 | } |
| 234 | |
[email protected] | 530f216 | 2014-05-15 01:05:04 | [diff] [blame] | 235 | GCMAppHandler* GCMDriver::GetAppHandler(const std::string& app_id) { |
[email protected] | b717626 | 2014-06-18 15:26:29 | [diff] [blame] | 236 | // Look for exact match. |
[email protected] | 21b7765 | 2014-05-31 01:21:09 | [diff] [blame] | 237 | GCMAppHandlerMap::const_iterator iter = app_handlers_.find(app_id); |
[email protected] | b717626 | 2014-06-18 15:26:29 | [diff] [blame] | 238 | if (iter != app_handlers_.end()) |
| 239 | return iter->second; |
| 240 | |
| 241 | // Ask the handlers whether they know how to handle it. |
| 242 | for (iter = app_handlers_.begin(); iter != app_handlers_.end(); ++iter) { |
| 243 | if (iter->second->CanHandle(app_id)) |
| 244 | return iter->second; |
| 245 | } |
| 246 | |
peter | bef2a02 | 2017-02-23 14:41:15 | [diff] [blame] | 247 | return nullptr; |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 248 | } |
| 249 | |
johnme | a00cc8ae | 2016-06-02 13:58:04 | [diff] [blame] | 250 | GCMEncryptionProvider* GCMDriver::GetEncryptionProviderInternal() { |
| 251 | return &encryption_provider_; |
| 252 | } |
| 253 | |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 254 | bool GCMDriver::HasRegisterCallback(const std::string& app_id) { |
| 255 | return register_callbacks_.find(app_id) != register_callbacks_.end(); |
| 256 | } |
| 257 | |
| 258 | void GCMDriver::ClearCallbacks() { |
| 259 | register_callbacks_.clear(); |
| 260 | unregister_callbacks_.clear(); |
| 261 | send_callbacks_.clear(); |
| 262 | } |
| 263 | |
peter | add31f6 | 2015-10-09 10:18:52 | [diff] [blame] | 264 | void GCMDriver::DispatchMessage(const std::string& app_id, |
| 265 | const IncomingMessage& message) { |
peter | 266a2aa4 | 2016-02-19 18:51:39 | [diff] [blame] | 266 | encryption_provider_.DecryptMessage( |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 267 | app_id, message, |
| 268 | base::BindOnce(&GCMDriver::DispatchMessageInternal, |
| 269 | weak_ptr_factory_.GetWeakPtr(), app_id)); |
peter | 266a2aa4 | 2016-02-19 18:51:39 | [diff] [blame] | 270 | } |
| 271 | |
Peter Beverloo | a376e98c | 2017-06-27 15:55:37 | [diff] [blame] | 272 | void GCMDriver::DispatchMessageInternal(const std::string& app_id, |
| 273 | GCMDecryptionResult result, |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 274 | IncomingMessage message) { |
peter | 266a2aa4 | 2016-02-19 18:51:39 | [diff] [blame] | 275 | UMA_HISTOGRAM_ENUMERATION("GCM.Crypto.DecryptMessageResult", result, |
Peter Beverloo | a376e98c | 2017-06-27 15:55:37 | [diff] [blame] | 276 | GCMDecryptionResult::ENUM_SIZE); |
peter | 266a2aa4 | 2016-02-19 18:51:39 | [diff] [blame] | 277 | |
| 278 | switch (result) { |
Peter Beverloo | a376e98c | 2017-06-27 15:55:37 | [diff] [blame] | 279 | case GCMDecryptionResult::UNENCRYPTED: |
| 280 | case GCMDecryptionResult::DECRYPTED_DRAFT_03: |
| 281 | case GCMDecryptionResult::DECRYPTED_DRAFT_08: { |
peter | bef2a02 | 2017-02-23 14:41:15 | [diff] [blame] | 282 | GCMAppHandler* handler = GetAppHandler(app_id); |
Peter Beverloo | f6403f3 | 2019-09-05 12:33:10 | [diff] [blame] | 283 | UMA_HISTOGRAM_BOOLEAN("GCM.DeliveredToAppHandler", !!handler); |
| 284 | |
peter | bef2a02 | 2017-02-23 14:41:15 | [diff] [blame] | 285 | if (handler) |
| 286 | handler->OnMessage(app_id, message); |
| 287 | |
| 288 | // TODO(peter/harkness): Surface unavailable app handlers on |
| 289 | // chrome://gcm-internals and send a delivery receipt. |
peter | 266a2aa4 | 2016-02-19 18:51:39 | [diff] [blame] | 290 | return; |
peter | bef2a02 | 2017-02-23 14:41:15 | [diff] [blame] | 291 | } |
Peter Beverloo | a376e98c | 2017-06-27 15:55:37 | [diff] [blame] | 292 | case GCMDecryptionResult::INVALID_ENCRYPTION_HEADER: |
| 293 | case GCMDecryptionResult::INVALID_CRYPTO_KEY_HEADER: |
| 294 | case GCMDecryptionResult::NO_KEYS: |
| 295 | case GCMDecryptionResult::INVALID_SHARED_SECRET: |
| 296 | case GCMDecryptionResult::INVALID_PAYLOAD: |
Peter Beverloo | e5eef7173 | 2017-06-27 15:51:29 | [diff] [blame] | 297 | case GCMDecryptionResult::INVALID_BINARY_HEADER_PAYLOAD_LENGTH: |
| 298 | case GCMDecryptionResult::INVALID_BINARY_HEADER_RECORD_SIZE: |
| 299 | case GCMDecryptionResult::INVALID_BINARY_HEADER_PUBLIC_KEY_LENGTH: |
Rayan Kanso | 9e5adf21 | 2019-05-09 16:07:53 | [diff] [blame] | 300 | case GCMDecryptionResult::INVALID_BINARY_HEADER_PUBLIC_KEY_FORMAT: { |
peter | 266a2aa4 | 2016-02-19 18:51:39 | [diff] [blame] | 301 | RecordDecryptionFailure(app_id, result); |
Rayan Kanso | 9e5adf21 | 2019-05-09 16:07:53 | [diff] [blame] | 302 | GCMAppHandler* handler = GetAppHandler(app_id); |
| 303 | if (handler) { |
| 304 | handler->OnMessageDecryptionFailed( |
Rayan Kanso | 7bde0799 | 2019-05-09 17:59:22 | [diff] [blame] | 305 | app_id, message.message_id, |
| 306 | ToGCMDecryptionResultDetailsString(result)); |
Rayan Kanso | 9e5adf21 | 2019-05-09 16:07:53 | [diff] [blame] | 307 | } |
peter | 266a2aa4 | 2016-02-19 18:51:39 | [diff] [blame] | 308 | return; |
Rayan Kanso | 9e5adf21 | 2019-05-09 16:07:53 | [diff] [blame] | 309 | } |
Peter Beverloo | a376e98c | 2017-06-27 15:55:37 | [diff] [blame] | 310 | case GCMDecryptionResult::ENUM_SIZE: |
| 311 | break; // deliberate fall-through |
peter | add31f6 | 2015-10-09 10:18:52 | [diff] [blame] | 312 | } |
| 313 | |
peter | 266a2aa4 | 2016-02-19 18:51:39 | [diff] [blame] | 314 | NOTREACHED(); |
peter | add31f6 | 2015-10-09 10:18:52 | [diff] [blame] | 315 | } |
| 316 | |
jianli | f17b85a | 2014-10-29 21:42:30 | [diff] [blame] | 317 | void GCMDriver::RegisterAfterUnregister( |
| 318 | const std::string& app_id, |
| 319 | const std::vector<std::string>& normalized_sender_ids, |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 320 | UnregisterCallback unregister_callback, |
jianli | f17b85a | 2014-10-29 21:42:30 | [diff] [blame] | 321 | GCMClient::Result result) { |
| 322 | // Invoke the original unregister callback. |
danakj | b534bf7 | 2019-05-02 17:10:14 | [diff] [blame] | 323 | std::move(unregister_callback).Run(result); |
jianli | f17b85a | 2014-10-29 21:42:30 | [diff] [blame] | 324 | |
| 325 | // Trigger the pending registration. |
| 326 | DCHECK(register_callbacks_.find(app_id) != register_callbacks_.end()); |
| 327 | RegisterImpl(app_id, normalized_sender_ids); |
[email protected] | c27c1079 | 2014-06-05 15:27:23 | [diff] [blame] | 328 | } |
| 329 | |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 330 | void GCMDriver::EncryptMessage(const std::string& app_id, |
| 331 | const std::string& authorized_entity, |
| 332 | const std::string& p256dh, |
| 333 | const std::string& auth_secret, |
| 334 | const std::string& message, |
| 335 | EncryptMessageCallback callback) { |
Alex Chau | 67605b9 | 2019-06-05 10:48:29 | [diff] [blame] | 336 | encryption_provider_.EncryptMessage( |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 337 | app_id, authorized_entity, p256dh, auth_secret, message, |
Alex Chau | a76a6e3 | 2019-06-26 16:20:01 | [diff] [blame] | 338 | base::BindOnce(&GCMDriver::OnMessageEncrypted, |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 339 | weak_ptr_factory_.GetWeakPtr(), std::move(callback))); |
Alex Chau | 67605b9 | 2019-06-05 10:48:29 | [diff] [blame] | 340 | } |
| 341 | |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 342 | void GCMDriver::OnMessageEncrypted(EncryptMessageCallback callback, |
Alex Chau | 67605b9 | 2019-06-05 10:48:29 | [diff] [blame] | 343 | GCMEncryptionResult result, |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 344 | std::string message) { |
Alex Chau | 67605b9 | 2019-06-05 10:48:29 | [diff] [blame] | 345 | UMA_HISTOGRAM_ENUMERATION("GCM.Crypto.EncryptMessageResult", result, |
| 346 | GCMEncryptionResult::ENUM_SIZE); |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 347 | std::move(callback).Run(result, std::move(message)); |
| 348 | } |
Alex Chau | 67605b9 | 2019-06-05 10:48:29 | [diff] [blame] | 349 | |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 350 | void GCMDriver::DecryptMessage(const std::string& app_id, |
| 351 | const std::string& authorized_entity, |
| 352 | const std::string& message, |
| 353 | DecryptMessageCallback callback) { |
| 354 | IncomingMessage incoming_message; |
| 355 | incoming_message.sender_id = authorized_entity; |
| 356 | incoming_message.raw_data = message; |
| 357 | incoming_message.data[GCMEncryptionProvider::kContentEncodingProperty] = |
| 358 | GCMEncryptionProvider::kContentCodingAes128Gcm; |
| 359 | encryption_provider_.DecryptMessage( |
| 360 | app_id, incoming_message, |
| 361 | base::BindOnce(&GCMDriver::OnMessageDecrypted, |
| 362 | weak_ptr_factory_.GetWeakPtr(), std::move(callback))); |
| 363 | } |
Alex Chau | 67605b9 | 2019-06-05 10:48:29 | [diff] [blame] | 364 | |
Alex Chau | da2073d6 | 2020-01-29 11:26:08 | [diff] [blame] | 365 | void GCMDriver::OnMessageDecrypted(DecryptMessageCallback callback, |
| 366 | GCMDecryptionResult result, |
| 367 | IncomingMessage message) { |
| 368 | UMA_HISTOGRAM_ENUMERATION("GCM.Crypto.DecryptMessageResult", result, |
| 369 | GCMDecryptionResult::ENUM_SIZE); |
| 370 | std::move(callback).Run(result, std::move(message.raw_data)); |
Alex Chau | 67605b9 | 2019-06-05 10:48:29 | [diff] [blame] | 371 | } |
| 372 | |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 373 | } // namespace gcm |