[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 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/fake_gcm_client.h" |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 6 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 11 | #include "base/bind.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 12 | #include "base/location.h" |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 13 | #include "base/logging.h" |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 14 | #include "base/sequenced_task_runner.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 15 | #include "base/single_thread_task_runner.h" |
jianli | 15aecc15 | 2014-10-07 03:03:53 | [diff] [blame] | 16 | #include "base/strings/string_number_conversions.h" |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 17 | #include "base/sys_byteorder.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 18 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 19 | #include "base/time/time.h" |
chirantan | 192a921 | 2014-12-06 03:30:45 | [diff] [blame] | 20 | #include "base/timer/timer.h" |
[email protected] | 446f73c2 | 2014-05-14 20:47:18 | [diff] [blame] | 21 | #include "google_apis/gcm/base/encryptor.h" |
fgorski | d578c18b | 2014-09-24 23:40:17 | [diff] [blame] | 22 | #include "google_apis/gcm/engine/account_mapping.h" |
[email protected] | fc6078a | 2014-06-14 08:28:43 | [diff] [blame] | 23 | #include "net/base/ip_endpoint.h" |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 24 | |
| 25 | namespace gcm { |
| 26 | |
jianli | 012b5c8 | 2015-05-28 01:41:29 | [diff] [blame] | 27 | // static |
| 28 | std::string FakeGCMClient::GenerateGCMRegistrationID( |
| 29 | const std::vector<std::string>& sender_ids) { |
| 30 | // GCMService normalizes the sender IDs by making them sorted. |
| 31 | std::vector<std::string> normalized_sender_ids = sender_ids; |
| 32 | std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end()); |
| 33 | |
| 34 | // Simulate the registration_id by concaternating all sender IDs. |
| 35 | // Set registration_id to empty to denote an error if sender_ids contains a |
| 36 | // hint. |
| 37 | std::string registration_id; |
| 38 | if (sender_ids.size() != 1 || |
| 39 | sender_ids[0].find("error") == std::string::npos) { |
| 40 | for (size_t i = 0; i < normalized_sender_ids.size(); ++i) { |
| 41 | if (i > 0) |
| 42 | registration_id += ","; |
| 43 | registration_id += normalized_sender_ids[i]; |
| 44 | } |
| 45 | } |
| 46 | return registration_id; |
| 47 | } |
| 48 | |
| 49 | // static |
| 50 | std::string FakeGCMClient::GenerateInstanceIDToken( |
| 51 | const std::string& authorized_entity, const std::string& scope) { |
| 52 | if (authorized_entity.find("error") != std::string::npos) |
| 53 | return ""; |
| 54 | std::string token(authorized_entity); |
| 55 | token += ","; |
| 56 | token += scope; |
| 57 | return token; |
| 58 | } |
| 59 | |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 60 | FakeGCMClient::FakeGCMClient( |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 61 | const scoped_refptr<base::SequencedTaskRunner>& ui_thread, |
| 62 | const scoped_refptr<base::SequencedTaskRunner>& io_thread) |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 63 | : delegate_(NULL), |
jianli | 1956259 | 2015-01-12 20:16:30 | [diff] [blame] | 64 | started_(false), |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 65 | start_mode_(DELAYED_START), |
| 66 | start_mode_overridding_(RESPECT_START_MODE), |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 67 | ui_thread_(ui_thread), |
| 68 | io_thread_(io_thread), |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 69 | weak_ptr_factory_(this) { |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 72 | FakeGCMClient::~FakeGCMClient() { |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 75 | void FakeGCMClient::Initialize( |
[email protected] | 8ad8051 | 2014-05-23 09:40:47 | [diff] [blame] | 76 | const ChromeBuildInfo& chrome_build_info, |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 77 | const base::FilePath& store_path, |
| 78 | const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, |
| 79 | const scoped_refptr<net::URLRequestContextGetter>& |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 80 | url_request_context_getter, |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 81 | std::unique_ptr<Encryptor> encryptor, |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 82 | Delegate* delegate) { |
johnme | 627dc8c7 | 2016-08-19 21:49:39 | [diff] [blame] | 83 | product_category_for_subtypes_ = |
| 84 | chrome_build_info.product_category_for_subtypes; |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 85 | delegate_ = delegate; |
[email protected] | e2a4a801 | 2014-02-07 22:32:52 | [diff] [blame] | 86 | } |
| 87 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 88 | void FakeGCMClient::Start(StartMode start_mode) { |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 89 | DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 90 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 91 | if (started_) |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 92 | return; |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 93 | |
| 94 | if (start_mode == IMMEDIATE_START) |
| 95 | start_mode_ = IMMEDIATE_START; |
| 96 | if (start_mode_ == DELAYED_START || |
| 97 | start_mode_overridding_ == FORCE_TO_ALWAYS_DELAY_START_GCM) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | DoStart(); |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 102 | } |
| 103 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 104 | void FakeGCMClient::DoStart() { |
jianli | 1956259 | 2015-01-12 20:16:30 | [diff] [blame] | 105 | started_ = true; |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 106 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 107 | FROM_HERE, |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 108 | base::Bind(&FakeGCMClient::Started, weak_ptr_factory_.GetWeakPtr())); |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 111 | void FakeGCMClient::Stop() { |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 112 | DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
jianli | 1956259 | 2015-01-12 20:16:30 | [diff] [blame] | 113 | started_ = false; |
[email protected] | fc6078a | 2014-06-14 08:28:43 | [diff] [blame] | 114 | delegate_->OnDisconnected(); |
[email protected] | 21fee548 | 2014-03-05 00:57:15 | [diff] [blame] | 115 | } |
| 116 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 117 | void FakeGCMClient::Register( |
| 118 | const linked_ptr<RegistrationInfo>& registration_info) { |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 119 | DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 120 | |
jianli | 012b5c8 | 2015-05-28 01:41:29 | [diff] [blame] | 121 | std::string registration_id; |
| 122 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 123 | GCMRegistrationInfo* gcm_registration_info = |
| 124 | GCMRegistrationInfo::FromRegistrationInfo(registration_info.get()); |
jianli | 012b5c8 | 2015-05-28 01:41:29 | [diff] [blame] | 125 | if (gcm_registration_info) { |
| 126 | registration_id = GenerateGCMRegistrationID( |
| 127 | gcm_registration_info->sender_ids); |
| 128 | } |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 129 | |
jianli | 012b5c8 | 2015-05-28 01:41:29 | [diff] [blame] | 130 | InstanceIDTokenInfo* instance_id_token_info = |
| 131 | InstanceIDTokenInfo::FromRegistrationInfo(registration_info.get()); |
| 132 | if (instance_id_token_info) { |
| 133 | registration_id = GenerateInstanceIDToken( |
| 134 | instance_id_token_info->authorized_entity, |
| 135 | instance_id_token_info->scope); |
| 136 | } |
| 137 | |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 138 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 139 | FROM_HERE, base::Bind(&FakeGCMClient::RegisterFinished, |
| 140 | weak_ptr_factory_.GetWeakPtr(), registration_info, |
| 141 | registration_id)); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 142 | } |
| 143 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 144 | void FakeGCMClient::Unregister( |
| 145 | const linked_ptr<RegistrationInfo>& registration_info) { |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 146 | DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
[email protected] | 0e88e1d1 | 2014-03-19 06:53:08 | [diff] [blame] | 147 | |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 148 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 149 | FROM_HERE, base::Bind(&FakeGCMClient::UnregisterFinished, |
| 150 | weak_ptr_factory_.GetWeakPtr(), registration_info)); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 151 | } |
| 152 | |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 153 | void FakeGCMClient::Send(const std::string& app_id, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 154 | const std::string& receiver_id, |
| 155 | const OutgoingMessage& message) { |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 156 | DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 157 | |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 158 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 159 | FROM_HERE, base::Bind(&FakeGCMClient::SendFinished, |
| 160 | weak_ptr_factory_.GetWeakPtr(), app_id, message)); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 161 | } |
| 162 | |
peter | ee284ba5 | 2016-02-01 11:53:28 | [diff] [blame] | 163 | void FakeGCMClient::RecordDecryptionFailure( |
| 164 | const std::string& app_id, |
peter | 266a2aa4 | 2016-02-19 18:51:39 | [diff] [blame] | 165 | GCMEncryptionProvider::DecryptionResult result) { |
| 166 | recorder_.RecordDecryptionFailure(app_id, result); |
peter | ee284ba5 | 2016-02-01 11:53:28 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 169 | void FakeGCMClient::SetRecording(bool recording) { |
peter | ee284ba5 | 2016-02-01 11:53:28 | [diff] [blame] | 170 | recorder_.set_is_recording(recording); |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 173 | void FakeGCMClient::ClearActivityLogs() { |
peter | ee284ba5 | 2016-02-01 11:53:28 | [diff] [blame] | 174 | recorder_.Clear(); |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 177 | GCMClient::GCMStatistics FakeGCMClient::GetStatistics() const { |
peter | ee284ba5 | 2016-02-01 11:53:28 | [diff] [blame] | 178 | GCMClient::GCMStatistics statistics; |
| 179 | statistics.is_recording = recorder_.is_recording(); |
| 180 | |
| 181 | recorder_.CollectActivities(&statistics.recorded_activities); |
| 182 | return statistics; |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 183 | } |
| 184 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 185 | void FakeGCMClient::SetAccountTokens( |
| 186 | const std::vector<AccountTokenInfo>& account_tokens) { |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | 72d4f25 | 2014-08-20 22:34:28 | [diff] [blame] | 189 | void FakeGCMClient::UpdateAccountMapping( |
| 190 | const AccountMapping& account_mapping) { |
| 191 | } |
| 192 | |
| 193 | void FakeGCMClient::RemoveAccountMapping(const std::string& account_id) { |
| 194 | } |
| 195 | |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 196 | void FakeGCMClient::SetLastTokenFetchTime(const base::Time& time) { |
| 197 | } |
| 198 | |
dcheng | a77e28eb | 2016-04-21 21:34:37 | [diff] [blame] | 199 | void FakeGCMClient::UpdateHeartbeatTimer(std::unique_ptr<base::Timer> timer) {} |
chirantan | 192a921 | 2014-12-06 03:30:45 | [diff] [blame] | 200 | |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 201 | void FakeGCMClient::AddInstanceIDData(const std::string& app_id, |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 202 | const std::string& instance_id, |
| 203 | const std::string& extra_data) { |
jianli | ea853487 | 2015-06-22 21:06:22 | [diff] [blame] | 204 | instance_id_data_[app_id] = make_pair(instance_id, extra_data); |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void FakeGCMClient::RemoveInstanceIDData(const std::string& app_id) { |
jianli | ea853487 | 2015-06-22 21:06:22 | [diff] [blame] | 208 | instance_id_data_.erase(app_id); |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 209 | } |
| 210 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 211 | void FakeGCMClient::GetInstanceIDData(const std::string& app_id, |
| 212 | std::string* instance_id, |
| 213 | std::string* extra_data) { |
jianli | ea853487 | 2015-06-22 21:06:22 | [diff] [blame] | 214 | auto iter = instance_id_data_.find(app_id); |
| 215 | if (iter == instance_id_data_.end()) { |
| 216 | instance_id->clear(); |
| 217 | extra_data->clear(); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | *instance_id = iter->second.first; |
| 222 | *extra_data = iter->second.second; |
jianli | 10018b2d | 2015-05-11 21:14:13 | [diff] [blame] | 223 | } |
| 224 | |
fgorski | 2275446 | 2015-05-14 00:05:22 | [diff] [blame] | 225 | void FakeGCMClient::AddHeartbeatInterval(const std::string& scope, |
| 226 | int interval_ms) { |
| 227 | } |
| 228 | |
| 229 | void FakeGCMClient::RemoveHeartbeatInterval(const std::string& scope) { |
| 230 | } |
| 231 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 232 | void FakeGCMClient::PerformDelayedStart() { |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 233 | DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 234 | |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 235 | io_thread_->PostTask( |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 236 | FROM_HERE, |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 237 | base::Bind(&FakeGCMClient::DoStart, weak_ptr_factory_.GetWeakPtr())); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 238 | } |
| 239 | |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 240 | void FakeGCMClient::ReceiveMessage(const std::string& app_id, |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 241 | const IncomingMessage& message) { |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 242 | DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 243 | |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 244 | io_thread_->PostTask( |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 245 | FROM_HERE, |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 246 | base::Bind(&FakeGCMClient::MessageReceived, |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 247 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 248 | app_id, |
| 249 | message)); |
| 250 | } |
| 251 | |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 252 | void FakeGCMClient::DeleteMessages(const std::string& app_id) { |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 253 | DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 254 | |
[email protected] | 1795df51a | 2014-05-22 20:18:04 | [diff] [blame] | 255 | io_thread_->PostTask( |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 256 | FROM_HERE, |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 257 | base::Bind(&FakeGCMClient::MessagesDeleted, |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 258 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 259 | app_id)); |
| 260 | } |
| 261 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 262 | void FakeGCMClient::Started() { |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 263 | delegate_->OnGCMReady(std::vector<AccountMapping>(), base::Time()); |
[email protected] | fc6078a | 2014-06-14 08:28:43 | [diff] [blame] | 264 | delegate_->OnConnected(net::IPEndPoint()); |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 265 | } |
| 266 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 267 | void FakeGCMClient::RegisterFinished( |
| 268 | const linked_ptr<RegistrationInfo>& registration_info, |
| 269 | const std::string& registrion_id) { |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 270 | delegate_->OnRegisterFinished( |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 271 | registration_info, |
| 272 | registrion_id, |
| 273 | registrion_id.empty() ? SERVER_ERROR : SUCCESS); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 274 | } |
| 275 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 276 | void FakeGCMClient::UnregisterFinished( |
| 277 | const linked_ptr<RegistrationInfo>& registration_info) { |
| 278 | delegate_->OnUnregisterFinished(registration_info, GCMClient::SUCCESS); |
[email protected] | 0e88e1d1 | 2014-03-19 06:53:08 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 281 | void FakeGCMClient::SendFinished(const std::string& app_id, |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 282 | const OutgoingMessage& message) { |
| 283 | delegate_->OnSendFinished(app_id, message.id, SUCCESS); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 284 | |
| 285 | // Simulate send error if message id contains a hint. |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 286 | if (message.id.find("error") != std::string::npos) { |
| 287 | SendErrorDetails send_error_details; |
| 288 | send_error_details.message_id = message.id; |
| 289 | send_error_details.result = NETWORK_ERROR; |
| 290 | send_error_details.additional_data = message.data; |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 291 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 292 | FROM_HERE, |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 293 | base::Bind(&FakeGCMClient::MessageSendError, |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 294 | weak_ptr_factory_.GetWeakPtr(), app_id, send_error_details), |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 295 | base::TimeDelta::FromMilliseconds(200)); |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 296 | } else if(message.id.find("ack") != std::string::npos) { |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 297 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 298 | FROM_HERE, |
| 299 | base::Bind(&FakeGCMClient::SendAcknowledgement, |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 300 | weak_ptr_factory_.GetWeakPtr(), app_id, message.id), |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 301 | base::TimeDelta::FromMilliseconds(200)); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 305 | void FakeGCMClient::MessageReceived(const std::string& app_id, |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 306 | const IncomingMessage& message) { |
| 307 | if (delegate_) |
| 308 | delegate_->OnMessageReceived(app_id, message); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 309 | } |
| 310 | |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 311 | void FakeGCMClient::MessagesDeleted(const std::string& app_id) { |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 312 | if (delegate_) |
| 313 | delegate_->OnMessagesDeleted(app_id); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 314 | } |
| 315 | |
[email protected] | accbbc9 | 2014-05-15 21:28:59 | [diff] [blame] | 316 | void FakeGCMClient::MessageSendError( |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 317 | const std::string& app_id, |
| 318 | const GCMClient::SendErrorDetails& send_error_details) { |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 319 | if (delegate_) |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 320 | delegate_->OnMessageSendError(app_id, send_error_details); |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 321 | } |
| 322 | |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 323 | void FakeGCMClient::SendAcknowledgement(const std::string& app_id, |
| 324 | const std::string& message_id) { |
| 325 | if (delegate_) |
| 326 | delegate_->OnSendAcknowledged(app_id, message_id); |
| 327 | } |
| 328 | |
[email protected] | b16a7c5 | 2013-11-20 01:18:59 | [diff] [blame] | 329 | } // namespace gcm |