| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <cstddef> | 5 #include <cstddef> |
| 6 #include <cstdio> | 6 #include <cstdio> |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/debug/stack_trace.h" | 12 #include "base/debug/stack_trace.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.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/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/message_loop.h" | 19 #include "base/message_loop.h" |
| 20 #include "base/rand_util.h" | |
| 21 #include "base/task_runner.h" | 20 #include "base/task_runner.h" |
| 22 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
| 23 #include "jingle/notifier/base/notification_method.h" | 22 #include "jingle/notifier/base/notification_method.h" |
| 24 #include "jingle/notifier/base/notifier_options.h" | 23 #include "jingle/notifier/base/notifier_options.h" |
| 25 #include "net/base/host_port_pair.h" | 24 #include "net/base/host_port_pair.h" |
| 26 #include "net/base/network_change_notifier.h" | 25 #include "net/base/network_change_notifier.h" |
| 27 #include "net/dns/host_resolver.h" | 26 #include "net/dns/host_resolver.h" |
| 28 #include "net/http/transport_security_state.h" | 27 #include "net/http/transport_security_state.h" |
| 29 #include "net/url_request/url_request_test_util.h" | 28 #include "net/url_request/url_request_test_util.h" |
| 30 #include "sync/internal_api/public/base/model_type.h" | 29 #include "sync/internal_api/public/base/model_type.h" |
| 31 #include "sync/internal_api/public/base_node.h" | 30 #include "sync/internal_api/public/base_node.h" |
| 32 #include "sync/internal_api/public/engine/passive_model_worker.h" | 31 #include "sync/internal_api/public/engine/passive_model_worker.h" |
| 33 #include "sync/internal_api/public/http_bridge.h" | 32 #include "sync/internal_api/public/http_bridge.h" |
| 34 #include "sync/internal_api/public/internal_components_factory_impl.h" | 33 #include "sync/internal_api/public/internal_components_factory_impl.h" |
| 35 #include "sync/internal_api/public/read_node.h" | 34 #include "sync/internal_api/public/read_node.h" |
| 36 #include "sync/internal_api/public/sync_manager.h" | 35 #include "sync/internal_api/public/sync_manager.h" |
| 37 #include "sync/internal_api/public/sync_manager_factory.h" | 36 #include "sync/internal_api/public/sync_manager_factory.h" |
| 38 #include "sync/internal_api/public/util/report_unrecoverable_error_function.h" | 37 #include "sync/internal_api/public/util/report_unrecoverable_error_function.h" |
| 39 #include "sync/internal_api/public/util/unrecoverable_error_handler.h" | 38 #include "sync/internal_api/public/util/unrecoverable_error_handler.h" |
| 40 #include "sync/internal_api/public/util/weak_handle.h" | 39 #include "sync/internal_api/public/util/weak_handle.h" |
| 41 #include "sync/js/js_event_details.h" | 40 #include "sync/js/js_event_details.h" |
| 42 #include "sync/js/js_event_handler.h" | 41 #include "sync/js/js_event_handler.h" |
| 43 #include "sync/notifier/invalidation_state_tracker.h" | 42 #include "sync/notifier/invalidation_state_tracker.h" |
| 44 #include "sync/notifier/non_blocking_invalidator.h" | 43 #include "sync/notifier/invalidator.h" |
| 44 #include "sync/notifier/invalidator_factory.h" |
| 45 #include "sync/test/fake_encryptor.h" | 45 #include "sync/test/fake_encryptor.h" |
| 46 #include "sync/tools/null_invalidation_state_tracker.h" | 46 #include "sync/tools/null_invalidation_state_tracker.h" |
| 47 | 47 |
| 48 #if defined(OS_MACOSX) | 48 #if defined(OS_MACOSX) |
| 49 #include "base/mac/scoped_nsautorelease_pool.h" | 49 #include "base/mac/scoped_nsautorelease_pool.h" |
| 50 #endif | 50 #endif |
| 51 | 51 |
| 52 // This is a simple utility that initializes a sync client and | 52 // This is a simple utility that initializes a sync client and |
| 53 // prints out any events. | 53 // prints out any events. |
| 54 | 54 |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 notifier_options.try_ssltcp_first = | 208 notifier_options.try_ssltcp_first = |
| 209 command_line.HasSwitch(kXmppTrySslTcpFirstSwitch); | 209 command_line.HasSwitch(kXmppTrySslTcpFirstSwitch); |
| 210 LOG_IF(INFO, notifier_options.try_ssltcp_first) | 210 LOG_IF(INFO, notifier_options.try_ssltcp_first) |
| 211 << "Trying SSL/TCP port before XMPP port for notifications."; | 211 << "Trying SSL/TCP port before XMPP port for notifications."; |
| 212 | 212 |
| 213 notifier_options.allow_insecure_connection = | 213 notifier_options.allow_insecure_connection = |
| 214 command_line.HasSwitch(kXmppAllowInsecureConnectionSwitch); | 214 command_line.HasSwitch(kXmppAllowInsecureConnectionSwitch); |
| 215 LOG_IF(INFO, notifier_options.allow_insecure_connection) | 215 LOG_IF(INFO, notifier_options.allow_insecure_connection) |
| 216 << "Allowing insecure XMPP connections."; | 216 << "Allowing insecure XMPP connections."; |
| 217 | 217 |
| 218 if (command_line.HasSwitch(kNotificationMethodSwitch)) { |
| 219 notifier_options.notification_method = |
| 220 notifier::StringToNotificationMethod( |
| 221 command_line.GetSwitchValueASCII(kNotificationMethodSwitch)); |
| 222 } |
| 223 |
| 218 return notifier_options; | 224 return notifier_options; |
| 219 } | 225 } |
| 220 | 226 |
| 221 void StubNetworkTimeUpdateCallback(const base::Time&, | |
| 222 const base::TimeDelta&, | |
| 223 const base::TimeDelta&) { | |
| 224 } | |
| 225 | |
| 226 int SyncClientMain(int argc, char* argv[]) { | 227 int SyncClientMain(int argc, char* argv[]) { |
| 227 #if defined(OS_MACOSX) | 228 #if defined(OS_MACOSX) |
| 228 base::mac::ScopedNSAutoreleasePool pool; | 229 base::mac::ScopedNSAutoreleasePool pool; |
| 229 #endif | 230 #endif |
| 230 base::AtExitManager exit_manager; | 231 base::AtExitManager exit_manager; |
| 231 CommandLine::Init(argc, argv); | 232 CommandLine::Init(argc, argv); |
| 232 logging::LoggingSettings settings; | 233 logging::LoggingSettings settings; |
| 233 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | 234 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; |
| 234 logging::InitLogging(settings); | 235 logging::InitLogging(settings); |
| 235 | 236 |
| 236 base::MessageLoop sync_loop; | 237 base::MessageLoop sync_loop; |
| 237 base::Thread io_thread("IO thread"); | 238 base::Thread io_thread("IO thread"); |
| 238 base::Thread::Options options; | 239 base::Thread::Options options; |
| 239 options.message_loop_type = base::MessageLoop::TYPE_IO; | 240 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 240 io_thread.StartWithOptions(options); | 241 io_thread.StartWithOptions(options); |
| 241 | 242 |
| 242 // Parse command line. | 243 // Parse command line. |
| 243 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 244 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 244 SyncCredentials credentials; | 245 SyncCredentials credentials; |
| 245 credentials.email = command_line.GetSwitchValueASCII(kEmailSwitch); | 246 credentials.email = command_line.GetSwitchValueASCII(kEmailSwitch); |
| 246 credentials.sync_token = command_line.GetSwitchValueASCII(kTokenSwitch); | 247 credentials.sync_token = command_line.GetSwitchValueASCII(kTokenSwitch); |
| 247 // TODO(akalin): Write a wrapper script that gets a token for an | 248 // TODO(akalin): Write a wrapper script that gets a token for an |
| 248 // email and password and passes that in to this utility. | 249 // email and password and passes that in to this utility. |
| 249 if (credentials.email.empty() || credentials.sync_token.empty()) { | 250 if (credentials.email.empty() || credentials.sync_token.empty()) { |
| 250 std::printf("Usage: %s --%s=foo@bar.com --%s=token\n" | 251 std::printf("Usage: %s --%s=foo@bar.com --%s=token\n" |
| 251 "[--%s=host:port] [--%s] [--%s]\n" | 252 "[--%s=host:port] [--%s] [--%s]\n" |
| 253 "[--%s=(server|p2p)]\n\n" |
| 252 "Run chrome and set a breakpoint on\n" | 254 "Run chrome and set a breakpoint on\n" |
| 253 "syncer::SyncManagerImpl::UpdateCredentials() " | 255 "syncer::SyncManagerImpl::UpdateCredentials() " |
| 254 "after logging into\n" | 256 "after logging into\n" |
| 255 "sync to get the token to pass into this utility.\n", | 257 "sync to get the token to pass into this utility.\n", |
| 256 argv[0], | 258 argv[0], |
| 257 kEmailSwitch, kTokenSwitch, kXmppHostPortSwitch, | 259 kEmailSwitch, kTokenSwitch, kXmppHostPortSwitch, |
| 258 kXmppTrySslTcpFirstSwitch, | 260 kXmppTrySslTcpFirstSwitch, |
| 259 kXmppAllowInsecureConnectionSwitch); | 261 kXmppAllowInsecureConnectionSwitch, |
| 262 kNotificationMethodSwitch); |
| 260 return -1; | 263 return -1; |
| 261 } | 264 } |
| 262 | 265 |
| 263 // Set up objects that monitor the network. | 266 // Set up objects that monitor the network. |
| 264 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier( | 267 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier( |
| 265 net::NetworkChangeNotifier::Create()); | 268 net::NetworkChangeNotifier::Create()); |
| 266 | 269 |
| 267 // Set up sync notifier factory. | 270 // Set up sync notifier factory. |
| 268 const scoped_refptr<MyTestURLRequestContextGetter> context_getter = | 271 const scoped_refptr<MyTestURLRequestContextGetter> context_getter = |
| 269 new MyTestURLRequestContextGetter(io_thread.message_loop_proxy()); | 272 new MyTestURLRequestContextGetter(io_thread.message_loop_proxy()); |
| 270 const notifier::NotifierOptions& notifier_options = | 273 const notifier::NotifierOptions& notifier_options = |
| 271 ParseNotifierOptions(command_line, context_getter); | 274 ParseNotifierOptions(command_line, context_getter); |
| 272 const char kClientInfo[] = "standalone_sync_client"; | 275 const char kClientInfo[] = "sync_listen_notifications"; |
| 273 std::string invalidator_id = base::RandBytesAsString(8); | |
| 274 NullInvalidationStateTracker null_invalidation_state_tracker; | 276 NullInvalidationStateTracker null_invalidation_state_tracker; |
| 275 scoped_ptr<Invalidator> invalidator(new NonBlockingInvalidator( | 277 InvalidatorFactory invalidator_factory( |
| 276 notifier_options, | 278 notifier_options, kClientInfo, |
| 277 invalidator_id, | 279 null_invalidation_state_tracker.AsWeakPtr()); |
| 278 null_invalidation_state_tracker.GetAllInvalidationStates(), | |
| 279 null_invalidation_state_tracker.GetBootstrapData(), | |
| 280 WeakHandle<InvalidationStateTracker>( | |
| 281 null_invalidation_state_tracker.AsWeakPtr()), | |
| 282 kClientInfo)); | |
| 283 | 280 |
| 284 // Set up database directory for the syncer. | 281 // Set up database directory for the syncer. |
| 285 base::ScopedTempDir database_dir; | 282 base::ScopedTempDir database_dir; |
| 286 CHECK(database_dir.CreateUniqueTempDir()); | 283 CHECK(database_dir.CreateUniqueTempDir()); |
| 287 | 284 |
| 288 // Developers often add types to ModelTypeSet::All() before the server | 285 // Set up model type parameters. |
| 289 // supports them. We need to be explicit about which types we want here. | 286 const ModelTypeSet model_types = ModelTypeSet::All(); |
| 290 ModelTypeSet model_types; | |
| 291 model_types.Put(BOOKMARKS); | |
| 292 model_types.Put(PREFERENCES); | |
| 293 model_types.Put(PASSWORDS); | |
| 294 model_types.Put(AUTOFILL); | |
| 295 model_types.Put(THEMES); | |
| 296 model_types.Put(TYPED_URLS); | |
| 297 model_types.Put(EXTENSIONS); | |
| 298 model_types.Put(NIGORI); | |
| 299 model_types.Put(SEARCH_ENGINES); | |
| 300 model_types.Put(SESSIONS); | |
| 301 model_types.Put(APPS); | |
| 302 model_types.Put(AUTOFILL_PROFILE); | |
| 303 model_types.Put(APP_SETTINGS); | |
| 304 model_types.Put(EXTENSION_SETTINGS); | |
| 305 model_types.Put(APP_NOTIFICATIONS); | |
| 306 model_types.Put(HISTORY_DELETE_DIRECTIVES); | |
| 307 model_types.Put(SYNCED_NOTIFICATIONS); | |
| 308 model_types.Put(DEVICE_INFO); | |
| 309 model_types.Put(EXPERIMENTS); | |
| 310 model_types.Put(PRIORITY_PREFERENCES); | |
| 311 model_types.Put(DICTIONARY); | |
| 312 model_types.Put(FAVICON_IMAGES); | |
| 313 model_types.Put(FAVICON_TRACKING); | |
| 314 | |
| 315 ModelSafeRoutingInfo routing_info; | 287 ModelSafeRoutingInfo routing_info; |
| 316 for (ModelTypeSet::Iterator it = model_types.First(); | 288 for (ModelTypeSet::Iterator it = model_types.First(); |
| 317 it.Good(); it.Inc()) { | 289 it.Good(); it.Inc()) { |
| 318 routing_info[it.Get()] = GROUP_PASSIVE; | 290 routing_info[it.Get()] = GROUP_PASSIVE; |
| 319 } | 291 } |
| 320 scoped_refptr<PassiveModelWorker> passive_model_safe_worker = | 292 scoped_refptr<PassiveModelWorker> passive_model_safe_worker = |
| 321 new PassiveModelWorker(&sync_loop, NULL); | 293 new PassiveModelWorker(&sync_loop, NULL); |
| 322 std::vector<ModelSafeWorker*> workers; | 294 std::vector<ModelSafeWorker*> workers; |
| 323 workers.push_back(passive_model_safe_worker.get()); | 295 workers.push_back(passive_model_safe_worker.get()); |
| 324 | 296 |
| 325 // Set up sync manager. | 297 // Set up sync manager. |
| 326 SyncManagerFactory sync_manager_factory; | 298 SyncManagerFactory sync_manager_factory; |
| 327 scoped_ptr<SyncManager> sync_manager = | 299 scoped_ptr<SyncManager> sync_manager = |
| 328 sync_manager_factory.CreateSyncManager("sync_client manager"); | 300 sync_manager_factory.CreateSyncManager("sync_client manager"); |
| 329 LoggingJsEventHandler js_event_handler; | 301 LoggingJsEventHandler js_event_handler; |
| 330 const char kSyncServerAndPath[] = "clients4.google.com/chrome-sync/dev"; | 302 const char kSyncServerAndPath[] = "clients4.google.com/chrome-sync/dev"; |
| 331 int kSyncServerPort = 443; | 303 int kSyncServerPort = 443; |
| 332 bool kUseSsl = true; | 304 bool kUseSsl = true; |
| 333 // Used only by InitialProcessMetadata(), so it's okay to leave this as NULL. | 305 // Used only by InitialProcessMetadata(), so it's okay to leave this as NULL. |
| 334 const scoped_refptr<base::TaskRunner> blocking_task_runner = NULL; | 306 const scoped_refptr<base::TaskRunner> blocking_task_runner = NULL; |
| 335 const char kUserAgent[] = "sync_client"; | 307 const char kUserAgent[] = "sync_client"; |
| 336 // TODO(akalin): Replace this with just the context getter once | 308 // TODO(akalin): Replace this with just the context getter once |
| 337 // HttpPostProviderFactory is removed. | 309 // HttpPostProviderFactory is removed. |
| 338 scoped_ptr<HttpPostProviderFactory> post_factory( | 310 scoped_ptr<HttpPostProviderFactory> post_factory(new HttpBridgeFactory( |
| 339 new HttpBridgeFactory(context_getter, | 311 context_getter.get(), kUserAgent, NetworkTimeUpdateCallback())); |
| 340 kUserAgent, | |
| 341 base::Bind(&StubNetworkTimeUpdateCallback))); | |
| 342 // Used only when committing bookmarks, so it's okay to leave this | 312 // Used only when committing bookmarks, so it's okay to leave this |
| 343 // as NULL. | 313 // as NULL. |
| 344 ExtensionsActivityMonitor* extensions_activity_monitor = NULL; | 314 ExtensionsActivityMonitor* extensions_activity_monitor = NULL; |
| 345 LoggingChangeDelegate change_delegate; | 315 LoggingChangeDelegate change_delegate; |
| 346 const char kRestoredKeyForBootstrapping[] = ""; | 316 const char kRestoredKeyForBootstrapping[] = ""; |
| 347 const char kRestoredKeystoreKeyForBootstrapping[] = ""; | 317 const char kRestoredKeystoreKeyForBootstrapping[] = ""; |
| 348 NullEncryptor null_encryptor; | 318 NullEncryptor null_encryptor; |
| 349 LoggingUnrecoverableErrorHandler unrecoverable_error_handler; | 319 LoggingUnrecoverableErrorHandler unrecoverable_error_handler; |
| 350 InternalComponentsFactoryImpl::Switches factory_switches = { | 320 InternalComponentsFactoryImpl::Switches factory_switches = { |
| 351 InternalComponentsFactory::ENCRYPTION_KEYSTORE, | 321 InternalComponentsFactory::ENCRYPTION_KEYSTORE, |
| 352 InternalComponentsFactory::BACKOFF_NORMAL | 322 InternalComponentsFactory::BACKOFF_NORMAL |
| 353 }; | 323 }; |
| 354 | 324 |
| 355 sync_manager->Init(database_dir.path(), | 325 sync_manager->Init(database_dir.path(), |
| 356 WeakHandle<JsEventHandler>( | 326 WeakHandle<JsEventHandler>( |
| 357 js_event_handler.AsWeakPtr()), | 327 js_event_handler.AsWeakPtr()), |
| 358 kSyncServerAndPath, | 328 kSyncServerAndPath, |
| 359 kSyncServerPort, | 329 kSyncServerPort, |
| 360 kUseSsl, | 330 kUseSsl, |
| 361 post_factory.Pass(), | 331 post_factory.Pass(), |
| 362 workers, | 332 workers, |
| 363 extensions_activity_monitor, | 333 extensions_activity_monitor, |
| 364 &change_delegate, | 334 &change_delegate, |
| 365 credentials, | 335 credentials, |
| 366 invalidator_id, | 336 scoped_ptr<Invalidator>( |
| 337 invalidator_factory.CreateInvalidator()), |
| 338 invalidator_factory.GetInvalidatorClientId(), |
| 367 kRestoredKeyForBootstrapping, | 339 kRestoredKeyForBootstrapping, |
| 368 kRestoredKeystoreKeyForBootstrapping, | 340 kRestoredKeystoreKeyForBootstrapping, |
| 369 scoped_ptr<InternalComponentsFactory>( | 341 scoped_ptr<InternalComponentsFactory>( |
| 370 new InternalComponentsFactoryImpl(factory_switches)), | 342 new InternalComponentsFactoryImpl(factory_switches)), |
| 371 &null_encryptor, | 343 &null_encryptor, |
| 372 &unrecoverable_error_handler, | 344 &unrecoverable_error_handler, |
| 373 &LogUnrecoverableErrorContext, false); | 345 &LogUnrecoverableErrorContext, false); |
| 374 // TODO(akalin): Avoid passing in model parameters multiple times by | 346 // TODO(akalin): Avoid passing in model parameters multiple times by |
| 375 // organizing handling of model types. | 347 // organizing handling of model types. |
| 376 invalidator->UpdateCredentials(credentials.email, credentials.sync_token); | 348 sync_manager->UpdateEnabledTypes(model_types); |
| 377 invalidator->RegisterHandler(sync_manager.get()); | |
| 378 invalidator->UpdateRegisteredIds( | |
| 379 sync_manager.get(), ModelTypeSetToObjectIdSet(model_types)); | |
| 380 sync_manager->StartSyncingNormally(routing_info); | 349 sync_manager->StartSyncingNormally(routing_info); |
| 381 | 350 |
| 382 sync_loop.Run(); | 351 sync_loop.Run(); |
| 383 | 352 |
| 384 io_thread.Stop(); | 353 io_thread.Stop(); |
| 385 return 0; | 354 return 0; |
| 386 } | 355 } |
| 387 | 356 |
| 388 } // namespace | 357 } // namespace |
| 389 } // namespace syncer | 358 } // namespace syncer |
| 390 | 359 |
| 391 int main(int argc, char* argv[]) { | 360 int main(int argc, char* argv[]) { |
| 392 return syncer::SyncClientMain(argc, argv); | 361 return syncer::SyncClientMain(argc, argv); |
| 393 } | 362 } |
| OLD | NEW |