OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/gcm/fake_gcm_profile_service.h" | 5 #include "chrome/browser/gcm/fake_gcm_profile_service.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" |
13 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "base/time/time.h" |
17 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
18 #include "components/gcm_driver/fake_gcm_client_factory.h" | 20 #include "components/gcm_driver/fake_gcm_client_factory.h" |
| 21 #include "components/gcm_driver/gcm_driver.h" |
19 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h" | 22 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h" |
20 #include "content/public/browser/browser_context.h" | 23 #include "content/public/browser/browser_context.h" |
21 | 24 |
22 namespace gcm { | 25 namespace gcm { |
23 | 26 |
24 namespace { | 27 class FakeGCMProfileService::CustomFakeGCMDriver |
25 | 28 : public instance_id::FakeGCMDriverForInstanceID { |
26 class CustomFakeGCMDriver : public instance_id::FakeGCMDriverForInstanceID { | |
27 public: | 29 public: |
28 explicit CustomFakeGCMDriver(FakeGCMProfileService* service); | 30 explicit CustomFakeGCMDriver(FakeGCMProfileService* service); |
29 ~CustomFakeGCMDriver() override; | 31 ~CustomFakeGCMDriver() override; |
30 | 32 |
31 void OnRegisterFinished(const std::string& app_id, | 33 void OnRegisterFinished(const std::string& app_id, |
32 const std::string& registration_id, | 34 const std::string& registration_id, |
33 GCMClient::Result result); | 35 GCMClient::Result result); |
34 void OnUnregisterFinished(const std::string& app_id, | |
35 GCMClient::Result result); | |
36 void OnSendFinished(const std::string& app_id, | 36 void OnSendFinished(const std::string& app_id, |
37 const std::string& message_id, | 37 const std::string& message_id, |
38 GCMClient::Result result); | 38 GCMClient::Result result); |
39 | 39 |
40 void OnDispatchMessage(const std::string& app_id, | 40 void OnDispatchMessage(const std::string& app_id, |
41 const IncomingMessage& message); | 41 const IncomingMessage& message); |
42 | 42 |
43 protected: | 43 protected: |
44 // FakeGCMDriverForInstanceID overrides: | 44 // FakeGCMDriver overrides: |
45 void RegisterImpl(const std::string& app_id, | 45 void RegisterImpl(const std::string& app_id, |
46 const std::vector<std::string>& sender_ids) override; | 46 const std::vector<std::string>& sender_ids) override; |
47 void UnregisterImpl(const std::string& app_id) override; | 47 void UnregisterImpl(const std::string& app_id) override; |
48 void UnregisterWithSenderIdImpl(const std::string& app_id, | 48 void UnregisterWithSenderIdImpl(const std::string& app_id, |
49 const std::string& sender_id) override; | 49 const std::string& sender_id) override; |
50 void SendImpl(const std::string& app_id, | 50 void SendImpl(const std::string& app_id, |
51 const std::string& receiver_id, | 51 const std::string& receiver_id, |
52 const OutgoingMessage& message) override; | 52 const OutgoingMessage& message) override; |
53 | 53 |
| 54 // FakeGCMDriverForInstanceID overrides: |
| 55 void GetToken(const std::string& app_id, |
| 56 const std::string& authorized_entity, |
| 57 const std::string& scope, |
| 58 const std::map<std::string, std::string>& options, |
| 59 const GetTokenCallback& callback) override; |
| 60 void DeleteToken(const std::string& app_id, |
| 61 const std::string& authorized_entity, |
| 62 const std::string& scope, |
| 63 const DeleteTokenCallback& callback) override; |
| 64 |
54 private: | 65 private: |
| 66 void DoRegister(const std::string& app_id, |
| 67 const std::vector<std::string>& sender_ids, |
| 68 const std::string& registration_id); |
| 69 void DoSend(const std::string& app_id, |
| 70 const std::string& receiver_id, |
| 71 const OutgoingMessage& message); |
| 72 |
55 FakeGCMProfileService* service_; | 73 FakeGCMProfileService* service_; |
56 | 74 |
| 75 // Used to give each registration a unique registration id. Does not decrease |
| 76 // when unregister is called. |
| 77 int registration_count_ = 0; |
| 78 |
| 79 base::WeakPtrFactory<CustomFakeGCMDriver> weak_factory_; // Must be last. |
| 80 |
57 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver); | 81 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver); |
58 }; | 82 }; |
59 | 83 |
60 CustomFakeGCMDriver::CustomFakeGCMDriver(FakeGCMProfileService* service) | 84 FakeGCMProfileService::CustomFakeGCMDriver::CustomFakeGCMDriver( |
| 85 FakeGCMProfileService* service) |
61 : instance_id::FakeGCMDriverForInstanceID( | 86 : instance_id::FakeGCMDriverForInstanceID( |
62 base::ThreadTaskRunnerHandle::Get()), | 87 base::ThreadTaskRunnerHandle::Get()), |
63 service_(service) {} | 88 service_(service), |
| 89 weak_factory_(this) {} |
64 | 90 |
65 CustomFakeGCMDriver::~CustomFakeGCMDriver() { | 91 FakeGCMProfileService::CustomFakeGCMDriver::~CustomFakeGCMDriver() {} |
| 92 |
| 93 void FakeGCMProfileService::CustomFakeGCMDriver::RegisterImpl( |
| 94 const std::string& app_id, |
| 95 const std::vector<std::string>& sender_ids) { |
| 96 if (service_->is_offline_) |
| 97 return; // Drop request. |
| 98 |
| 99 // Generate fake registration IDs, encoding the number of sender IDs (used by |
| 100 // GcmApiTest.RegisterValidation), then an incrementing count (even for the |
| 101 // same app_id - there's no caching) so tests can distinguish registrations. |
| 102 std::string registration_id = base::StringPrintf( |
| 103 "%" PRIuS "-%d", sender_ids.size(), registration_count_); |
| 104 ++registration_count_; |
| 105 |
| 106 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 107 FROM_HERE, |
| 108 base::Bind(&CustomFakeGCMDriver::DoRegister, weak_factory_.GetWeakPtr(), |
| 109 app_id, sender_ids, registration_id)); |
66 } | 110 } |
67 | 111 |
68 void CustomFakeGCMDriver::RegisterImpl( | 112 void FakeGCMProfileService::CustomFakeGCMDriver::DoRegister( |
69 const std::string& app_id, | 113 const std::string& app_id, |
70 const std::vector<std::string>& sender_ids) { | 114 const std::vector<std::string>& sender_ids, |
71 base::ThreadTaskRunnerHandle::Get()->PostTask( | 115 const std::string& registration_id) { |
72 FROM_HERE, base::Bind(&FakeGCMProfileService::RegisterFinished, | 116 if (service_->collect_) { |
73 base::Unretained(service_), app_id, sender_ids)); | 117 service_->last_registered_app_id_ = app_id; |
| 118 service_->last_registered_sender_ids_ = sender_ids; |
| 119 } |
| 120 RegisterFinished(app_id, registration_id, GCMClient::SUCCESS); |
74 } | 121 } |
75 | 122 |
76 void CustomFakeGCMDriver::UnregisterImpl(const std::string& app_id) { | 123 void FakeGCMProfileService::CustomFakeGCMDriver::UnregisterImpl( |
| 124 const std::string& app_id) { |
| 125 if (service_->is_offline_) |
| 126 return; // Drop request. |
| 127 |
| 128 GCMClient::Result result = GCMClient::SUCCESS; |
| 129 if (!service_->unregister_responses_.empty()) { |
| 130 result = service_->unregister_responses_.front(); |
| 131 service_->unregister_responses_.pop_front(); |
| 132 } |
77 base::ThreadTaskRunnerHandle::Get()->PostTask( | 133 base::ThreadTaskRunnerHandle::Get()->PostTask( |
78 FROM_HERE, base::Bind(&FakeGCMProfileService::UnregisterFinished, | 134 FROM_HERE, base::Bind(&CustomFakeGCMDriver::UnregisterFinished, |
79 base::Unretained(service_), app_id)); | 135 weak_factory_.GetWeakPtr(), app_id, result)); |
80 } | 136 } |
81 | 137 |
82 void CustomFakeGCMDriver::UnregisterWithSenderIdImpl( | 138 void FakeGCMProfileService::CustomFakeGCMDriver::UnregisterWithSenderIdImpl( |
83 const std::string& app_id, | 139 const std::string& app_id, |
84 const std::string& sender_id) {} | 140 const std::string& sender_id) { |
| 141 NOTREACHED() << "This Android-specific method is not yet faked."; |
| 142 } |
85 | 143 |
86 void CustomFakeGCMDriver::SendImpl(const std::string& app_id, | 144 void FakeGCMProfileService::CustomFakeGCMDriver::SendImpl( |
87 const std::string& receiver_id, | 145 const std::string& app_id, |
88 const OutgoingMessage& message) { | 146 const std::string& receiver_id, |
| 147 const OutgoingMessage& message) { |
| 148 if (service_->is_offline_) |
| 149 return; // Drop request. |
| 150 |
89 base::ThreadTaskRunnerHandle::Get()->PostTask( | 151 base::ThreadTaskRunnerHandle::Get()->PostTask( |
90 FROM_HERE, | 152 FROM_HERE, |
91 base::Bind(&FakeGCMProfileService::SendFinished, | 153 base::Bind(&CustomFakeGCMDriver::DoSend, weak_factory_.GetWeakPtr(), |
92 base::Unretained(service_), app_id, receiver_id, message)); | 154 app_id, receiver_id, message)); |
93 } | 155 } |
94 | 156 |
95 void CustomFakeGCMDriver::OnRegisterFinished( | 157 void FakeGCMProfileService::CustomFakeGCMDriver::DoSend( |
96 const std::string& app_id, | 158 const std::string& app_id, |
97 const std::string& registration_id, | 159 const std::string& receiver_id, |
98 GCMClient::Result result) { | 160 const OutgoingMessage& message) { |
99 RegisterFinished(app_id, registration_id, result); | 161 if (service_->collect_) { |
| 162 service_->last_sent_message_ = message; |
| 163 service_->last_receiver_id_ = receiver_id; |
| 164 } |
| 165 SendFinished(app_id, message.id, GCMClient::SUCCESS); |
100 } | 166 } |
101 | 167 |
102 void CustomFakeGCMDriver::OnUnregisterFinished(const std::string& app_id, | 168 void FakeGCMProfileService::CustomFakeGCMDriver::GetToken( |
103 GCMClient::Result result) { | 169 const std::string& app_id, |
104 UnregisterFinished(app_id, result); | 170 const std::string& authorized_entity, |
| 171 const std::string& scope, |
| 172 const std::map<std::string, std::string>& options, |
| 173 const GetTokenCallback& callback) { |
| 174 if (service_->is_offline_) |
| 175 return; // Drop request. |
| 176 |
| 177 instance_id::FakeGCMDriverForInstanceID::GetToken(app_id, authorized_entity, |
| 178 scope, options, callback); |
105 } | 179 } |
106 | 180 |
107 void CustomFakeGCMDriver::OnSendFinished(const std::string& app_id, | 181 void FakeGCMProfileService::CustomFakeGCMDriver::DeleteToken( |
108 const std::string& message_id, | 182 const std::string& app_id, |
109 GCMClient::Result result) { | 183 const std::string& authorized_entity, |
110 SendFinished(app_id, message_id, result); | 184 const std::string& scope, |
| 185 const DeleteTokenCallback& callback) { |
| 186 if (service_->is_offline_) |
| 187 return; // Drop request. |
| 188 |
| 189 instance_id::FakeGCMDriverForInstanceID::DeleteToken( |
| 190 app_id, authorized_entity, scope, callback); |
111 } | 191 } |
112 | 192 |
113 void CustomFakeGCMDriver::OnDispatchMessage(const std::string& app_id, | 193 void FakeGCMProfileService::CustomFakeGCMDriver::OnDispatchMessage( |
114 const IncomingMessage& message) { | 194 const std::string& app_id, |
| 195 const IncomingMessage& message) { |
115 DispatchMessage(app_id, message); | 196 DispatchMessage(app_id, message); |
116 } | 197 } |
117 | 198 |
118 } // namespace | |
119 | |
120 // static | 199 // static |
121 std::unique_ptr<KeyedService> FakeGCMProfileService::Build( | 200 std::unique_ptr<KeyedService> FakeGCMProfileService::Build( |
122 content::BrowserContext* context) { | 201 content::BrowserContext* context) { |
123 Profile* profile = static_cast<Profile*>(context); | 202 Profile* profile = static_cast<Profile*>(context); |
124 std::unique_ptr<FakeGCMProfileService> service( | 203 std::unique_ptr<FakeGCMProfileService> service( |
125 new FakeGCMProfileService(profile)); | 204 new FakeGCMProfileService(profile)); |
126 service->SetDriverForTesting(new CustomFakeGCMDriver(service.get())); | 205 service->SetDriverForTesting(new CustomFakeGCMDriver(service.get())); |
127 return std::move(service); | 206 return std::move(service); |
128 } | 207 } |
129 | 208 |
130 FakeGCMProfileService::FakeGCMProfileService(Profile* profile) | 209 FakeGCMProfileService::FakeGCMProfileService(Profile* profile) {} |
131 : collect_(false), | |
132 registration_count_(0) { | |
133 } | |
134 | 210 |
135 FakeGCMProfileService::~FakeGCMProfileService() {} | 211 FakeGCMProfileService::~FakeGCMProfileService() {} |
136 | 212 |
137 void FakeGCMProfileService::RegisterFinished( | |
138 const std::string& app_id, | |
139 const std::vector<std::string>& sender_ids) { | |
140 if (collect_) { | |
141 last_registered_app_id_ = app_id; | |
142 last_registered_sender_ids_ = sender_ids; | |
143 } | |
144 | |
145 // Generate fake registration IDs, encoding the number of sender IDs (used by | |
146 // GcmApiTest.RegisterValidation), then an incrementing count (even for the | |
147 // same app_id - there's no caching) so tests can distinguish registrations. | |
148 std::string registration_id = base::StringPrintf("%" PRIuS "-%d", | |
149 sender_ids.size(), | |
150 registration_count_); | |
151 ++registration_count_; | |
152 | |
153 CustomFakeGCMDriver* custom_driver = | |
154 static_cast<CustomFakeGCMDriver*>(driver()); | |
155 custom_driver->OnRegisterFinished( | |
156 app_id, registration_id, GCMClient::SUCCESS); | |
157 } | |
158 | |
159 void FakeGCMProfileService::UnregisterFinished(const std::string& app_id) { | |
160 GCMClient::Result result = GCMClient::SUCCESS; | |
161 if (!unregister_responses_.empty()) { | |
162 result = unregister_responses_.front(); | |
163 unregister_responses_.pop_front(); | |
164 } | |
165 | |
166 CustomFakeGCMDriver* custom_driver = | |
167 static_cast<CustomFakeGCMDriver*>(driver()); | |
168 custom_driver->OnUnregisterFinished(app_id, result); | |
169 | |
170 if (!unregister_callback_.is_null()) | |
171 unregister_callback_.Run(app_id); | |
172 } | |
173 | |
174 void FakeGCMProfileService::SendFinished(const std::string& app_id, | |
175 const std::string& receiver_id, | |
176 const OutgoingMessage& message) { | |
177 if (collect_) { | |
178 last_sent_message_ = message; | |
179 last_receiver_id_ = receiver_id; | |
180 } | |
181 | |
182 CustomFakeGCMDriver* custom_driver = | |
183 static_cast<CustomFakeGCMDriver*>(driver()); | |
184 custom_driver->OnSendFinished(app_id, message.id, GCMClient::SUCCESS); | |
185 } | |
186 | |
187 void FakeGCMProfileService::AddExpectedUnregisterResponse( | 213 void FakeGCMProfileService::AddExpectedUnregisterResponse( |
188 GCMClient::Result result) { | 214 GCMClient::Result result) { |
189 unregister_responses_.push_back(result); | 215 unregister_responses_.push_back(result); |
190 } | 216 } |
191 | 217 |
192 void FakeGCMProfileService::SetUnregisterCallback( | |
193 const UnregisterCallback& callback) { | |
194 unregister_callback_ = callback; | |
195 } | |
196 | |
197 void FakeGCMProfileService::DispatchMessage(const std::string& app_id, | 218 void FakeGCMProfileService::DispatchMessage(const std::string& app_id, |
198 const IncomingMessage& message) { | 219 const IncomingMessage& message) { |
199 CustomFakeGCMDriver* custom_driver = | 220 CustomFakeGCMDriver* custom_driver = |
200 static_cast<CustomFakeGCMDriver*>(driver()); | 221 static_cast<CustomFakeGCMDriver*>(driver()); |
201 custom_driver->OnDispatchMessage(app_id, message); | 222 custom_driver->OnDispatchMessage(app_id, message); |
202 } | 223 } |
203 | 224 |
204 } // namespace gcm | 225 } // namespace gcm |
OLD | NEW |