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 <cstddef> | 8 #include <cstddef> |
9 #include <cstdio> | 9 #include <cstdio> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/at_exit.h" | 12 #include "base/at_exit.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
19 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
20 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
21 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
22 #include "base/threading/worker_pool.h" | 22 #include "base/threading/worker_pool.h" |
23 #include "base/time/default_clock.h" | 23 #include "base/time/default_clock.h" |
24 #include "base/values.h" | 24 #include "base/values.h" |
25 #include "google_apis/gcm/base/mcs_message.h" | 25 #include "google_apis/gcm/base/mcs_message.h" |
26 #include "google_apis/gcm/base/mcs_util.h" | 26 #include "google_apis/gcm/base/mcs_util.h" |
27 #include "google_apis/gcm/engine/checkin_request.h" | |
27 #include "google_apis/gcm/engine/connection_factory_impl.h" | 28 #include "google_apis/gcm/engine/connection_factory_impl.h" |
28 #include "google_apis/gcm/engine/gcm_store_impl.h" | 29 #include "google_apis/gcm/engine/gcm_store_impl.h" |
29 #include "google_apis/gcm/engine/mcs_client.h" | 30 #include "google_apis/gcm/engine/mcs_client.h" |
30 #include "net/base/host_mapping_rules.h" | 31 #include "net/base/host_mapping_rules.h" |
31 #include "net/base/net_log_logger.h" | 32 #include "net/base/net_log_logger.h" |
32 #include "net/cert/cert_verifier.h" | 33 #include "net/cert/cert_verifier.h" |
33 #include "net/dns/host_resolver.h" | 34 #include "net/dns/host_resolver.h" |
34 #include "net/http/http_auth_handler_factory.h" | 35 #include "net/http/http_auth_handler_factory.h" |
35 #include "net/http/http_network_session.h" | 36 #include "net/http/http_network_session.h" |
36 #include "net/http/http_server_properties_impl.h" | 37 #include "net/http/http_server_properties_impl.h" |
37 #include "net/http/transport_security_state.h" | 38 #include "net/http/transport_security_state.h" |
38 #include "net/socket/client_socket_factory.h" | 39 #include "net/socket/client_socket_factory.h" |
39 #include "net/socket/ssl_client_socket.h" | 40 #include "net/socket/ssl_client_socket.h" |
40 #include "net/ssl/default_server_bound_cert_store.h" | 41 #include "net/ssl/default_server_bound_cert_store.h" |
41 #include "net/ssl/server_bound_cert_service.h" | 42 #include "net/ssl/server_bound_cert_service.h" |
42 #include "net/url_request/url_request_test_util.h" | 43 #include "net/url_request/url_request_test_util.h" |
43 | 44 |
44 #if defined(OS_MACOSX) | 45 #if defined(OS_MACOSX) |
45 #include "base/mac/scoped_nsautorelease_pool.h" | 46 #include "base/mac/scoped_nsautorelease_pool.h" |
46 #endif | 47 #endif |
47 | 48 |
48 // This is a simple utility that initializes an mcs client and | 49 // This is a simple utility that initializes an mcs client and |
49 // prints out any events. | 50 // prints out any events. |
50 namespace gcm { | 51 namespace gcm { |
51 namespace { | 52 namespace { |
52 | 53 |
54 // Default values used to communicate with the check-in server. | |
55 const char kChromeVersion[] = "Test Chrome Version"; | |
fgorski
2014/01/09 22:23:47
Should we call it "Chrome MCS Probe"?
It would be
jianli
2014/01/09 23:37:05
Done.
| |
56 const int64 kUserSerialNumber = 1; | |
fgorski
2014/01/09 22:23:47
This should be unique within the device, so should
jianli
2014/01/09 23:37:05
For now, mcs_probe only supports single user scena
| |
57 | |
53 // The default server to communicate with. | 58 // The default server to communicate with. |
54 const char kMCSServerHost[] = "mtalk.google.com"; | 59 const char kMCSServerHost[] = "mtalk.google.com"; |
55 const uint16 kMCSServerPort = 5228; | 60 const uint16 kMCSServerPort = 5228; |
56 | 61 |
57 // Command line switches. | 62 // Command line switches. |
58 const char kRMQFileName[] = "rmq_file"; | 63 const char kRMQFileName[] = "rmq_file"; |
59 const char kAndroidIdSwitch[] = "android_id"; | 64 const char kAndroidIdSwitch[] = "android_id"; |
60 const char kSecretSwitch[] = "secret"; | 65 const char kSecretSwitch[] = "secret"; |
61 const char kLogFileSwitch[] = "log-file"; | 66 const char kLogFileSwitch[] = "log-file"; |
62 const char kIgnoreCertSwitch[] = "ignore-certs"; | 67 const char kIgnoreCertSwitch[] = "ignore-certs"; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 const CommandLine& command_line, | 164 const CommandLine& command_line, |
160 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter); | 165 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter); |
161 ~MCSProbe(); | 166 ~MCSProbe(); |
162 | 167 |
163 void Start(); | 168 void Start(); |
164 | 169 |
165 uint64 android_id() const { return android_id_; } | 170 uint64 android_id() const { return android_id_; } |
166 uint64 secret() const { return secret_; } | 171 uint64 secret() const { return secret_; } |
167 | 172 |
168 private: | 173 private: |
174 void CheckIn(); | |
169 void InitializeNetworkState(); | 175 void InitializeNetworkState(); |
170 void BuildNetworkSession(); | 176 void BuildNetworkSession(); |
177 void LoginMCSClient(); | |
171 | 178 |
172 void InitializationCallback(bool success, | 179 void InitializationCallback(bool success, |
173 uint64 restored_android_id, | 180 uint64 restored_android_id, |
174 uint64 restored_security_token); | 181 uint64 restored_security_token); |
182 void OnCheckInCompleted(uint64 android_id, uint64 secret); | |
175 | 183 |
176 base::DefaultClock clock_; | 184 base::DefaultClock clock_; |
177 | 185 |
178 CommandLine command_line_; | 186 CommandLine command_line_; |
179 | 187 |
180 base::FilePath gcm_store_path_; | 188 base::FilePath gcm_store_path_; |
181 uint64 android_id_; | 189 uint64 android_id_; |
182 uint64 secret_; | 190 uint64 secret_; |
183 std::string server_host_; | 191 std::string server_host_; |
184 int server_port_; | 192 int server_port_; |
185 | 193 |
186 // Network state. | 194 // Network state. |
187 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 195 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
188 MyTestNetLog net_log_; | 196 MyTestNetLog net_log_; |
189 scoped_ptr<net::NetLogLogger> logger_; | 197 scoped_ptr<net::NetLogLogger> logger_; |
190 scoped_ptr<base::Value> net_constants_; | 198 scoped_ptr<base::Value> net_constants_; |
191 scoped_ptr<net::HostResolver> host_resolver_; | 199 scoped_ptr<net::HostResolver> host_resolver_; |
192 scoped_ptr<net::CertVerifier> cert_verifier_; | 200 scoped_ptr<net::CertVerifier> cert_verifier_; |
193 scoped_ptr<net::ServerBoundCertService> system_server_bound_cert_service_; | 201 scoped_ptr<net::ServerBoundCertService> system_server_bound_cert_service_; |
194 scoped_ptr<net::TransportSecurityState> transport_security_state_; | 202 scoped_ptr<net::TransportSecurityState> transport_security_state_; |
195 scoped_ptr<net::URLSecurityManager> url_security_manager_; | 203 scoped_ptr<net::URLSecurityManager> url_security_manager_; |
196 scoped_ptr<net::HttpAuthHandlerFactory> http_auth_handler_factory_; | 204 scoped_ptr<net::HttpAuthHandlerFactory> http_auth_handler_factory_; |
197 scoped_ptr<net::HttpServerPropertiesImpl> http_server_properties_; | 205 scoped_ptr<net::HttpServerPropertiesImpl> http_server_properties_; |
198 scoped_ptr<net::HostMappingRules> host_mapping_rules_; | 206 scoped_ptr<net::HostMappingRules> host_mapping_rules_; |
199 scoped_refptr<net::HttpNetworkSession> network_session_; | 207 scoped_refptr<net::HttpNetworkSession> network_session_; |
200 scoped_ptr<net::ProxyService> proxy_service_; | 208 scoped_ptr<net::ProxyService> proxy_service_; |
201 | 209 |
202 scoped_ptr<GCMStore> gcm_store_; | 210 scoped_ptr<GCMStore> gcm_store_; |
203 scoped_ptr<MCSClient> mcs_client_; | 211 scoped_ptr<MCSClient> mcs_client_; |
212 scoped_ptr<CheckinRequest> checkin_request_; | |
204 | 213 |
205 scoped_ptr<ConnectionFactoryImpl> connection_factory_; | 214 scoped_ptr<ConnectionFactoryImpl> connection_factory_; |
206 | 215 |
207 base::Thread file_thread_; | 216 base::Thread file_thread_; |
208 | 217 |
209 scoped_ptr<base::RunLoop> run_loop_; | 218 scoped_ptr<base::RunLoop> run_loop_; |
210 }; | 219 }; |
211 | 220 |
212 MCSProbe::MCSProbe( | 221 MCSProbe::MCSProbe( |
213 const CommandLine& command_line, | 222 const CommandLine& command_line, |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
333 session_params.proxy_service = proxy_service_.get(); | 342 session_params.proxy_service = proxy_service_.get(); |
334 | 343 |
335 network_session_ = new net::HttpNetworkSession(session_params); | 344 network_session_ = new net::HttpNetworkSession(session_params); |
336 } | 345 } |
337 | 346 |
338 void MCSProbe::InitializationCallback(bool success, | 347 void MCSProbe::InitializationCallback(bool success, |
339 uint64 restored_android_id, | 348 uint64 restored_android_id, |
340 uint64 restored_security_token) { | 349 uint64 restored_security_token) { |
341 LOG(INFO) << "Initialization " << (success ? "success!" : "failure!"); | 350 LOG(INFO) << "Initialization " << (success ? "success!" : "failure!"); |
342 if (restored_android_id && restored_security_token) { | 351 if (restored_android_id && restored_security_token) { |
352 LOG(INFO) << "Restored device check-in info."; | |
343 android_id_ = restored_android_id; | 353 android_id_ = restored_android_id; |
344 secret_ = restored_security_token; | 354 secret_ = restored_security_token; |
345 } | 355 } |
346 if (success) | 356 if (!success) |
347 mcs_client_->Login(android_id_, secret_); | 357 return; |
358 | |
359 if (!android_id_ || !secret_) { | |
360 CheckIn(); | |
361 return; | |
362 } | |
363 | |
364 LoginMCSClient(); | |
365 } | |
366 | |
367 void MCSProbe::CheckIn() { | |
368 LOG(INFO) << "Check-in request initiated."; | |
369 checkin_proto::ChromeBuildProto chrome_build_proto; | |
370 chrome_build_proto.set_platform( | |
371 checkin_proto::ChromeBuildProto::PLATFORM_LINUX); | |
372 chrome_build_proto.set_channel( | |
373 checkin_proto::ChromeBuildProto::CHANNEL_CANARY); | |
374 chrome_build_proto.set_chrome_version(kChromeVersion); | |
375 checkin_request_.reset(new CheckinRequest( | |
376 base::Bind(&MCSProbe::OnCheckInCompleted, base::Unretained(this)), | |
377 chrome_build_proto, | |
378 kUserSerialNumber, | |
379 0, | |
380 0, | |
381 url_request_context_getter_.get())); | |
382 checkin_request_->Start(); | |
383 } | |
384 | |
385 void MCSProbe::OnCheckInCompleted(uint64 android_id, uint64 secret) { | |
386 LOG(INFO) << "Check-in request completion " | |
387 << (android_id ? "success!" : "failure!"); | |
388 if (!android_id || !secret) | |
389 return; | |
390 android_id_ = android_id; | |
391 secret_ = secret; | |
fgorski
2014/01/09 22:23:47
I think you should persist android_id and secret,
jianli
2014/01/09 23:37:05
This has already been taken care of in MCSClient::
| |
392 | |
393 // We have to wait a while to talk with MCS server due to potential | |
394 // propagation delay. | |
395 const int kDelayMS = 10000; | |
396 LOG(INFO) << "Delaying MCS login for " << kDelayMS << "MS ..."; | |
397 base::MessageLoop::current()->PostDelayedTask( | |
398 FROM_HERE, | |
399 base::Bind(&MCSProbe::LoginMCSClient, base::Unretained(this)), | |
400 base::TimeDelta::FromMilliseconds(kDelayMS)); | |
401 } | |
402 | |
403 void MCSProbe::LoginMCSClient() { | |
404 LOG(INFO) << "MCS login initiated."; | |
405 mcs_client_->Login(android_id_, secret_); | |
348 } | 406 } |
349 | 407 |
350 int MCSProbeMain(int argc, char* argv[]) { | 408 int MCSProbeMain(int argc, char* argv[]) { |
351 base::AtExitManager exit_manager; | 409 base::AtExitManager exit_manager; |
352 | 410 |
353 CommandLine::Init(argc, argv); | 411 CommandLine::Init(argc, argv); |
354 logging::LoggingSettings settings; | 412 logging::LoggingSettings settings; |
355 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | 413 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; |
356 logging::InitLogging(settings); | 414 logging::InitLogging(settings); |
357 | 415 |
(...skipping 14 matching lines...) Expand all Loading... | |
372 | 430 |
373 return 0; | 431 return 0; |
374 } | 432 } |
375 | 433 |
376 } // namespace | 434 } // namespace |
377 } // namespace gcm | 435 } // namespace gcm |
378 | 436 |
379 int main(int argc, char* argv[]) { | 437 int main(int argc, char* argv[]) { |
380 return gcm::MCSProbeMain(argc, argv); | 438 return gcm::MCSProbeMain(argc, argv); |
381 } | 439 } |
OLD | NEW |