| 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 // A standalone tool for testing MCS connections and the MCS client on their | 5 // A standalone tool for testing MCS connections and the MCS client on their |
| 6 // own. | 6 // own. |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 |
| 9 #include <cstddef> | 10 #include <cstddef> |
| 10 #include <cstdio> | 11 #include <cstdio> |
| 12 #include <memory> |
| 11 #include <string> | 13 #include <string> |
| 12 #include <utility> | 14 #include <utility> |
| 13 #include <vector> | 15 #include <vector> |
| 14 | 16 |
| 15 #include "base/at_exit.h" | 17 #include "base/at_exit.h" |
| 16 #include "base/command_line.h" | 18 #include "base/command_line.h" |
| 17 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
| 18 #include "base/files/scoped_file.h" | 20 #include "base/files/scoped_file.h" |
| 19 #include "base/logging.h" | 21 #include "base/logging.h" |
| 22 #include "base/memory/ptr_util.h" |
| 20 #include "base/memory/ref_counted.h" | 23 #include "base/memory/ref_counted.h" |
| 21 #include "base/memory/scoped_ptr.h" | |
| 22 #include "base/message_loop/message_loop.h" | 24 #include "base/message_loop/message_loop.h" |
| 23 #include "base/run_loop.h" | 25 #include "base/run_loop.h" |
| 24 #include "base/strings/string_number_conversions.h" | 26 #include "base/strings/string_number_conversions.h" |
| 25 #include "base/thread_task_runner_handle.h" | 27 #include "base/thread_task_runner_handle.h" |
| 26 #include "base/threading/thread.h" | 28 #include "base/threading/thread.h" |
| 27 #include "base/threading/worker_pool.h" | 29 #include "base/threading/worker_pool.h" |
| 28 #include "base/time/default_clock.h" | 30 #include "base/time/default_clock.h" |
| 29 #include "base/values.h" | 31 #include "base/values.h" |
| 30 #include "build/build_config.h" | 32 #include "build/build_config.h" |
| 31 #include "google_apis/gcm/base/fake_encryptor.h" | 33 #include "google_apis/gcm/base/fake_encryptor.h" |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 << " Message send status: " << status; | 137 << " Message send status: " << status; |
| 136 } | 138 } |
| 137 | 139 |
| 138 // Needed to use a real host resolver. | 140 // Needed to use a real host resolver. |
| 139 class MyTestURLRequestContext : public net::TestURLRequestContext { | 141 class MyTestURLRequestContext : public net::TestURLRequestContext { |
| 140 public: | 142 public: |
| 141 MyTestURLRequestContext() : TestURLRequestContext(true) { | 143 MyTestURLRequestContext() : TestURLRequestContext(true) { |
| 142 context_storage_.set_host_resolver( | 144 context_storage_.set_host_resolver( |
| 143 net::HostResolver::CreateDefaultResolver(NULL)); | 145 net::HostResolver::CreateDefaultResolver(NULL)); |
| 144 context_storage_.set_transport_security_state( | 146 context_storage_.set_transport_security_state( |
| 145 make_scoped_ptr(new net::TransportSecurityState())); | 147 base::WrapUnique(new net::TransportSecurityState())); |
| 146 Init(); | 148 Init(); |
| 147 } | 149 } |
| 148 | 150 |
| 149 ~MyTestURLRequestContext() override {} | 151 ~MyTestURLRequestContext() override {} |
| 150 }; | 152 }; |
| 151 | 153 |
| 152 class MyTestURLRequestContextGetter : public net::TestURLRequestContextGetter { | 154 class MyTestURLRequestContextGetter : public net::TestURLRequestContextGetter { |
| 153 public: | 155 public: |
| 154 explicit MyTestURLRequestContextGetter( | 156 explicit MyTestURLRequestContextGetter( |
| 155 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 157 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 156 : TestURLRequestContextGetter(io_task_runner) {} | 158 : TestURLRequestContextGetter(io_task_runner) {} |
| 157 | 159 |
| 158 net::TestURLRequestContext* GetURLRequestContext() override { | 160 net::TestURLRequestContext* GetURLRequestContext() override { |
| 159 // Construct |context_| lazily so it gets constructed on the right | 161 // Construct |context_| lazily so it gets constructed on the right |
| 160 // thread (the IO thread). | 162 // thread (the IO thread). |
| 161 if (!context_) | 163 if (!context_) |
| 162 context_.reset(new MyTestURLRequestContext()); | 164 context_.reset(new MyTestURLRequestContext()); |
| 163 return context_.get(); | 165 return context_.get(); |
| 164 } | 166 } |
| 165 | 167 |
| 166 private: | 168 private: |
| 167 ~MyTestURLRequestContextGetter() override {} | 169 ~MyTestURLRequestContextGetter() override {} |
| 168 | 170 |
| 169 scoped_ptr<MyTestURLRequestContext> context_; | 171 std::unique_ptr<MyTestURLRequestContext> context_; |
| 170 }; | 172 }; |
| 171 | 173 |
| 172 // A cert verifier that access all certificates. | 174 // A cert verifier that access all certificates. |
| 173 class MyTestCertVerifier : public net::CertVerifier { | 175 class MyTestCertVerifier : public net::CertVerifier { |
| 174 public: | 176 public: |
| 175 MyTestCertVerifier() {} | 177 MyTestCertVerifier() {} |
| 176 ~MyTestCertVerifier() override {} | 178 ~MyTestCertVerifier() override {} |
| 177 | 179 |
| 178 int Verify(net::X509Certificate* cert, | 180 int Verify(net::X509Certificate* cert, |
| 179 const std::string& hostname, | 181 const std::string& hostname, |
| 180 const std::string& ocsp_response, | 182 const std::string& ocsp_response, |
| 181 int flags, | 183 int flags, |
| 182 net::CRLSet* crl_set, | 184 net::CRLSet* crl_set, |
| 183 net::CertVerifyResult* verify_result, | 185 net::CertVerifyResult* verify_result, |
| 184 const net::CompletionCallback& callback, | 186 const net::CompletionCallback& callback, |
| 185 scoped_ptr<Request>* out_req, | 187 std::unique_ptr<Request>* out_req, |
| 186 const net::BoundNetLog& net_log) override { | 188 const net::BoundNetLog& net_log) override { |
| 187 return net::OK; | 189 return net::OK; |
| 188 } | 190 } |
| 189 }; | 191 }; |
| 190 | 192 |
| 191 class MCSProbeAuthPreferences : public net::HttpAuthPreferences { | 193 class MCSProbeAuthPreferences : public net::HttpAuthPreferences { |
| 192 public: | 194 public: |
| 193 MCSProbeAuthPreferences() | 195 MCSProbeAuthPreferences() |
| 194 : HttpAuthPreferences(std::vector<std::string>() | 196 : HttpAuthPreferences(std::vector<std::string>() |
| 195 #if defined(OS_POSIX) && !defined(OS_ANDROID) | 197 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 219 void Start(); | 221 void Start(); |
| 220 | 222 |
| 221 uint64_t android_id() const { return android_id_; } | 223 uint64_t android_id() const { return android_id_; } |
| 222 uint64_t secret() const { return secret_; } | 224 uint64_t secret() const { return secret_; } |
| 223 | 225 |
| 224 private: | 226 private: |
| 225 void CheckIn(); | 227 void CheckIn(); |
| 226 void InitializeNetworkState(); | 228 void InitializeNetworkState(); |
| 227 void BuildNetworkSession(); | 229 void BuildNetworkSession(); |
| 228 | 230 |
| 229 void LoadCallback(scoped_ptr<GCMStore::LoadResult> load_result); | 231 void LoadCallback(std::unique_ptr<GCMStore::LoadResult> load_result); |
| 230 void UpdateCallback(bool success); | 232 void UpdateCallback(bool success); |
| 231 void ErrorCallback(); | 233 void ErrorCallback(); |
| 232 void OnCheckInCompleted( | 234 void OnCheckInCompleted( |
| 233 const checkin_proto::AndroidCheckinResponse& checkin_response); | 235 const checkin_proto::AndroidCheckinResponse& checkin_response); |
| 234 void StartMCSLogin(); | 236 void StartMCSLogin(); |
| 235 | 237 |
| 236 base::DefaultClock clock_; | 238 base::DefaultClock clock_; |
| 237 | 239 |
| 238 base::CommandLine command_line_; | 240 base::CommandLine command_line_; |
| 239 | 241 |
| 240 base::FilePath gcm_store_path_; | 242 base::FilePath gcm_store_path_; |
| 241 uint64_t android_id_; | 243 uint64_t android_id_; |
| 242 uint64_t secret_; | 244 uint64_t secret_; |
| 243 std::string server_host_; | 245 std::string server_host_; |
| 244 int server_port_; | 246 int server_port_; |
| 245 | 247 |
| 246 // Network state. | 248 // Network state. |
| 247 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 249 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 248 net::NetLog net_log_; | 250 net::NetLog net_log_; |
| 249 scoped_ptr<net::WriteToFileNetLogObserver> logger_; | 251 std::unique_ptr<net::WriteToFileNetLogObserver> logger_; |
| 250 scoped_ptr<net::HostResolver> host_resolver_; | 252 std::unique_ptr<net::HostResolver> host_resolver_; |
| 251 scoped_ptr<net::CertVerifier> cert_verifier_; | 253 std::unique_ptr<net::CertVerifier> cert_verifier_; |
| 252 scoped_ptr<net::ChannelIDService> system_channel_id_service_; | 254 std::unique_ptr<net::ChannelIDService> system_channel_id_service_; |
| 253 scoped_ptr<net::TransportSecurityState> transport_security_state_; | 255 std::unique_ptr<net::TransportSecurityState> transport_security_state_; |
| 254 MCSProbeAuthPreferences http_auth_preferences_; | 256 MCSProbeAuthPreferences http_auth_preferences_; |
| 255 scoped_ptr<net::HttpAuthHandlerFactory> http_auth_handler_factory_; | 257 std::unique_ptr<net::HttpAuthHandlerFactory> http_auth_handler_factory_; |
| 256 scoped_ptr<net::HttpServerPropertiesImpl> http_server_properties_; | 258 std::unique_ptr<net::HttpServerPropertiesImpl> http_server_properties_; |
| 257 scoped_ptr<net::HostMappingRules> host_mapping_rules_; | 259 std::unique_ptr<net::HostMappingRules> host_mapping_rules_; |
| 258 scoped_ptr<net::HttpNetworkSession> network_session_; | 260 std::unique_ptr<net::HttpNetworkSession> network_session_; |
| 259 scoped_ptr<net::ProxyService> proxy_service_; | 261 std::unique_ptr<net::ProxyService> proxy_service_; |
| 260 | 262 |
| 261 FakeGCMStatsRecorder recorder_; | 263 FakeGCMStatsRecorder recorder_; |
| 262 scoped_ptr<GCMStore> gcm_store_; | 264 std::unique_ptr<GCMStore> gcm_store_; |
| 263 scoped_ptr<MCSClient> mcs_client_; | 265 std::unique_ptr<MCSClient> mcs_client_; |
| 264 scoped_ptr<CheckinRequest> checkin_request_; | 266 std::unique_ptr<CheckinRequest> checkin_request_; |
| 265 | 267 |
| 266 scoped_ptr<ConnectionFactoryImpl> connection_factory_; | 268 std::unique_ptr<ConnectionFactoryImpl> connection_factory_; |
| 267 | 269 |
| 268 base::Thread file_thread_; | 270 base::Thread file_thread_; |
| 269 | 271 |
| 270 scoped_ptr<base::RunLoop> run_loop_; | 272 std::unique_ptr<base::RunLoop> run_loop_; |
| 271 }; | 273 }; |
| 272 | 274 |
| 273 MCSProbe::MCSProbe( | 275 MCSProbe::MCSProbe( |
| 274 const base::CommandLine& command_line, | 276 const base::CommandLine& command_line, |
| 275 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) | 277 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) |
| 276 : command_line_(command_line), | 278 : command_line_(command_line), |
| 277 gcm_store_path_(base::FilePath(FILE_PATH_LITERAL("gcm_store"))), | 279 gcm_store_path_(base::FilePath(FILE_PATH_LITERAL("gcm_store"))), |
| 278 android_id_(0), | 280 android_id_(0), |
| 279 secret_(0), | 281 secret_(0), |
| 280 server_port_(0), | 282 server_port_(0), |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 net::HostPortPair(server_host_, | 319 net::HostPortPair(server_host_, |
| 318 server_port_).ToString())); | 320 server_port_).ToString())); |
| 319 connection_factory_.reset( | 321 connection_factory_.reset( |
| 320 new ConnectionFactoryImpl(endpoints, | 322 new ConnectionFactoryImpl(endpoints, |
| 321 kDefaultBackoffPolicy, | 323 kDefaultBackoffPolicy, |
| 322 network_session_.get(), | 324 network_session_.get(), |
| 323 NULL, | 325 NULL, |
| 324 &net_log_, | 326 &net_log_, |
| 325 &recorder_)); | 327 &recorder_)); |
| 326 gcm_store_.reset( | 328 gcm_store_.reset( |
| 327 new GCMStoreImpl(gcm_store_path_, | 329 new GCMStoreImpl(gcm_store_path_, file_thread_.task_runner(), |
| 328 file_thread_.task_runner(), | 330 base::WrapUnique<Encryptor>(new FakeEncryptor))); |
| 329 make_scoped_ptr<Encryptor>(new FakeEncryptor))); | |
| 330 mcs_client_.reset(new MCSClient("probe", | 331 mcs_client_.reset(new MCSClient("probe", |
| 331 &clock_, | 332 &clock_, |
| 332 connection_factory_.get(), | 333 connection_factory_.get(), |
| 333 gcm_store_.get(), | 334 gcm_store_.get(), |
| 334 &recorder_)); | 335 &recorder_)); |
| 335 run_loop_.reset(new base::RunLoop()); | 336 run_loop_.reset(new base::RunLoop()); |
| 336 gcm_store_->Load(GCMStore::CREATE_IF_MISSING, | 337 gcm_store_->Load(GCMStore::CREATE_IF_MISSING, |
| 337 base::Bind(&MCSProbe::LoadCallback, | 338 base::Bind(&MCSProbe::LoadCallback, |
| 338 base::Unretained(this))); | 339 base::Unretained(this))); |
| 339 run_loop_->Run(); | 340 run_loop_->Run(); |
| 340 } | 341 } |
| 341 | 342 |
| 342 void MCSProbe::LoadCallback(scoped_ptr<GCMStore::LoadResult> load_result) { | 343 void MCSProbe::LoadCallback(std::unique_ptr<GCMStore::LoadResult> load_result) { |
| 343 DCHECK(load_result->success); | 344 DCHECK(load_result->success); |
| 344 if (android_id_ != 0 && secret_ != 0) { | 345 if (android_id_ != 0 && secret_ != 0) { |
| 345 DVLOG(1) << "Presetting MCS id " << android_id_; | 346 DVLOG(1) << "Presetting MCS id " << android_id_; |
| 346 load_result->device_android_id = android_id_; | 347 load_result->device_android_id = android_id_; |
| 347 load_result->device_security_token = secret_; | 348 load_result->device_security_token = secret_; |
| 348 gcm_store_->SetDeviceCredentials(android_id_, | 349 gcm_store_->SetDeviceCredentials(android_id_, |
| 349 secret_, | 350 secret_, |
| 350 base::Bind(&MCSProbe::UpdateCallback, | 351 base::Bind(&MCSProbe::UpdateCallback, |
| 351 base::Unretained(this))); | 352 base::Unretained(this))); |
| 352 } else { | 353 } else { |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 | 513 |
| 513 return 0; | 514 return 0; |
| 514 } | 515 } |
| 515 | 516 |
| 516 } // namespace | 517 } // namespace |
| 517 } // namespace gcm | 518 } // namespace gcm |
| 518 | 519 |
| 519 int main(int argc, char* argv[]) { | 520 int main(int argc, char* argv[]) { |
| 520 return gcm::MCSProbeMain(argc, argv); | 521 return gcm::MCSProbeMain(argc, argv); |
| 521 } | 522 } |
| OLD | NEW |