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 3 // found in the LICENSE file. 4 5 #include <cstddef> 6 #include <cstdio> 7 #include <string> 8 9 #include "base/at_exit.h" 10 #include "base/command_line.h" 11 #include "base/compiler_specific.h" 12 #include "base/debug/stack_trace.h" 13 #include "base/files/scoped_temp_dir.h" 14 #include "base/json/json_writer.h" 15 #include "base/logging.h" 16 #include "base/memory/ref_counted.h" 17 #include "base/memory/scoped_ptr.h" 18 #include "base/memory/weak_ptr.h" 19 #include "base/message_loop/message_loop.h" 20 #include "base/rand_util.h" 21 #include "base/task_runner.h" 22 #include "base/threading/thread.h" 23 #include "jingle/notifier/base/notification_method.h" 24 #include "jingle/notifier/base/notifier_options.h" 25 #include "net/base/host_port_pair.h" 26 #include "net/base/network_change_notifier.h" 27 #include "net/dns/host_resolver.h" 28 #include "net/http/transport_security_state.h" 29 #include "net/url_request/url_request_test_util.h" 30 #include "sync/internal_api/public/base/cancelation_signal.h" 31 #include "sync/internal_api/public/base/model_type.h" 32 #include "sync/internal_api/public/base_node.h" 33 #include "sync/internal_api/public/engine/passive_model_worker.h" 34 #include "sync/internal_api/public/http_bridge.h" 35 #include "sync/internal_api/public/internal_components_factory_impl.h" 36 #include "sync/internal_api/public/read_node.h" 37 #include "sync/internal_api/public/sync_manager.h" 38 #include "sync/internal_api/public/sync_manager_factory.h" 39 #include "sync/internal_api/public/util/report_unrecoverable_error_function.h" 40 #include "sync/internal_api/public/util/unrecoverable_error_handler.h" 41 #include "sync/internal_api/public/util/weak_handle.h" 42 #include "sync/js/js_event_details.h" 43 #include "sync/js/js_event_handler.h" 44 #include "sync/notifier/invalidation_state_tracker.h" 45 #include "sync/notifier/non_blocking_invalidator.h" 46 #include "sync/test/fake_encryptor.h" 47 #include "sync/tools/null_invalidation_state_tracker.h" 48 49 #if defined(OS_MACOSX) 50 #include "base/mac/scoped_nsautorelease_pool.h" 51 #endif 52 53 // This is a simple utility that initializes a sync client and 54 // prints out any events. 55 56 // TODO(akalin): Refactor to combine shared code with 57 // sync_listen_notifications. 58 namespace syncer { 59 namespace { 60 61 const char kEmailSwitch[] = "email"; 62 const char kTokenSwitch[] = "token"; 63 const char kXmppHostPortSwitch[] = "xmpp-host-port"; 64 const char kXmppTrySslTcpFirstSwitch[] = "xmpp-try-ssltcp-first"; 65 const char kXmppAllowInsecureConnectionSwitch[] = 66 "xmpp-allow-insecure-connection"; 67 68 // Needed to use a real host resolver. 69 class MyTestURLRequestContext : public net::TestURLRequestContext { 70 public: 71 MyTestURLRequestContext() : TestURLRequestContext(true) { 72 context_storage_.set_host_resolver( 73 net::HostResolver::CreateDefaultResolver(NULL)); 74 context_storage_.set_transport_security_state( 75 new net::TransportSecurityState()); 76 Init(); 77 } 78 79 virtual ~MyTestURLRequestContext() {} 80 }; 81 82 class MyTestURLRequestContextGetter : public net::TestURLRequestContextGetter { 83 public: 84 explicit MyTestURLRequestContextGetter( 85 const scoped_refptr<base::MessageLoopProxy>& io_message_loop_proxy) 86 : TestURLRequestContextGetter(io_message_loop_proxy) {} 87 88 virtual net::TestURLRequestContext* GetURLRequestContext() OVERRIDE { 89 // Construct |context_| lazily so it gets constructed on the right 90 // thread (the IO thread). 91 if (!context_) 92 context_.reset(new MyTestURLRequestContext()); 93 return context_.get(); 94 } 95 96 private: 97 virtual ~MyTestURLRequestContextGetter() {} 98 99 scoped_ptr<MyTestURLRequestContext> context_; 100 }; 101 102 // TODO(akalin): Use system encryptor once it's moved to sync/. 103 class NullEncryptor : public Encryptor { 104 public: 105 virtual ~NullEncryptor() {} 106 107 virtual bool EncryptString(const std::string& plaintext, 108 std::string* ciphertext) OVERRIDE { 109 *ciphertext = plaintext; 110 return true; 111 } 112 113 virtual bool DecryptString(const std::string& ciphertext, 114 std::string* plaintext) OVERRIDE { 115 *plaintext = ciphertext; 116 return true; 117 } 118 }; 119 120 std::string ValueToString(const Value& value) { 121 std::string str; 122 base::JSONWriter::Write(&value, &str); 123 return str; 124 } 125 126 class LoggingChangeDelegate : public SyncManager::ChangeDelegate { 127 public: 128 virtual ~LoggingChangeDelegate() {} 129 130 virtual void OnChangesApplied( 131 ModelType model_type, 132 int64 model_version, 133 const BaseTransaction* trans, 134 const ImmutableChangeRecordList& changes) OVERRIDE { 135 LOG(INFO) << "Changes applied for " 136 << ModelTypeToString(model_type); 137 size_t i = 1; 138 size_t change_count = changes.Get().size(); 139 for (ChangeRecordList::const_iterator it = 140 changes.Get().begin(); it != changes.Get().end(); ++it) { 141 scoped_ptr<base::DictionaryValue> change_value(it->ToValue()); 142 LOG(INFO) << "Change (" << i << "/" << change_count << "): " 143 << ValueToString(*change_value); 144 if (it->action != ChangeRecord::ACTION_DELETE) { 145 ReadNode node(trans); 146 CHECK_EQ(node.InitByIdLookup(it->id), BaseNode::INIT_OK); 147 scoped_ptr<base::DictionaryValue> details(node.GetDetailsAsValue()); 148 VLOG(1) << "Details: " << ValueToString(*details); 149 } 150 ++i; 151 } 152 } 153 154 virtual void OnChangesComplete(ModelType model_type) OVERRIDE { 155 LOG(INFO) << "Changes complete for " 156 << ModelTypeToString(model_type); 157 } 158 }; 159 160 class LoggingUnrecoverableErrorHandler 161 : public UnrecoverableErrorHandler { 162 public: 163 virtual ~LoggingUnrecoverableErrorHandler() {} 164 165 virtual void OnUnrecoverableError(const tracked_objects::Location& from_here, 166 const std::string& message) OVERRIDE { 167 if (LOG_IS_ON(ERROR)) { 168 logging::LogMessage(from_here.file_name(), from_here.line_number(), 169 logging::LOG_ERROR).stream() 170 << message; 171 } 172 } 173 }; 174 175 class LoggingJsEventHandler 176 : public JsEventHandler, 177 public base::SupportsWeakPtr<LoggingJsEventHandler> { 178 public: 179 virtual ~LoggingJsEventHandler() {} 180 181 virtual void HandleJsEvent( 182 const std::string& name, 183 const JsEventDetails& details) OVERRIDE { 184 VLOG(1) << name << ": " << details.ToString(); 185 } 186 }; 187 188 void LogUnrecoverableErrorContext() { 189 base::debug::StackTrace().Print(); 190 } 191 192 notifier::NotifierOptions ParseNotifierOptions( 193 const CommandLine& command_line, 194 const scoped_refptr<net::URLRequestContextGetter>& 195 request_context_getter) { 196 notifier::NotifierOptions notifier_options; 197 notifier_options.request_context_getter = request_context_getter; 198 notifier_options.auth_mechanism = "X-OAUTH2"; 199 200 if (command_line.HasSwitch(kXmppHostPortSwitch)) { 201 notifier_options.xmpp_host_port = 202 net::HostPortPair::FromString( 203 command_line.GetSwitchValueASCII(kXmppHostPortSwitch)); 204 LOG(INFO) << "Using " << notifier_options.xmpp_host_port.ToString() 205 << " for test sync notification server."; 206 } 207 208 notifier_options.try_ssltcp_first = 209 command_line.HasSwitch(kXmppTrySslTcpFirstSwitch); 210 LOG_IF(INFO, notifier_options.try_ssltcp_first) 211 << "Trying SSL/TCP port before XMPP port for notifications."; 212 213 notifier_options.allow_insecure_connection = 214 command_line.HasSwitch(kXmppAllowInsecureConnectionSwitch); 215 LOG_IF(INFO, notifier_options.allow_insecure_connection) 216 << "Allowing insecure XMPP connections."; 217 218 return notifier_options; 219 } 220 221 void StubNetworkTimeUpdateCallback(const base::Time&, 222 const base::TimeDelta&, 223 const base::TimeDelta&) { 224 } 225 226 int SyncClientMain(int argc, char* argv[]) { 227 #if defined(OS_MACOSX) 228 base::mac::ScopedNSAutoreleasePool pool; 229 #endif 230 base::AtExitManager exit_manager; 231 CommandLine::Init(argc, argv); 232 logging::LoggingSettings settings; 233 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; 234 logging::InitLogging(settings); 235 236 base::MessageLoop sync_loop; 237 base::Thread io_thread("IO thread"); 238 base::Thread::Options options; 239 options.message_loop_type = base::MessageLoop::TYPE_IO; 240 io_thread.StartWithOptions(options); 241 242 // Parse command line. 243 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 244 SyncCredentials credentials; 245 credentials.email = command_line.GetSwitchValueASCII(kEmailSwitch); 246 credentials.sync_token = command_line.GetSwitchValueASCII(kTokenSwitch); 247 // TODO(akalin): Write a wrapper script that gets a token for an 248 // email and password and passes that in to this utility. 249 if (credentials.email.empty() || credentials.sync_token.empty()) { 250 std::printf("Usage: %s --%s=foo (at) bar.com --%s=token\n" 251 "[--%s=host:port] [--%s] [--%s]\n" 252 "Run chrome and set a breakpoint on\n" 253 "syncer::SyncManagerImpl::UpdateCredentials() " 254 "after logging into\n" 255 "sync to get the token to pass into this utility.\n", 256 argv[0], 257 kEmailSwitch, kTokenSwitch, kXmppHostPortSwitch, 258 kXmppTrySslTcpFirstSwitch, 259 kXmppAllowInsecureConnectionSwitch); 260 return -1; 261 } 262 263 // Set up objects that monitor the network. 264 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier( 265 net::NetworkChangeNotifier::Create()); 266 267 // Set up sync notifier factory. 268 const scoped_refptr<MyTestURLRequestContextGetter> context_getter = 269 new MyTestURLRequestContextGetter(io_thread.message_loop_proxy()); 270 const notifier::NotifierOptions& notifier_options = 271 ParseNotifierOptions(command_line, context_getter); 272 const char kClientInfo[] = "standalone_sync_client"; 273 std::string invalidator_id = base::RandBytesAsString(8); 274 NullInvalidationStateTracker null_invalidation_state_tracker; 275 scoped_ptr<Invalidator> invalidator(new NonBlockingInvalidator( 276 notifier_options, 277 invalidator_id, 278 null_invalidation_state_tracker.GetSavedInvalidations(), 279 null_invalidation_state_tracker.GetBootstrapData(), 280 WeakHandle<InvalidationStateTracker>( 281 null_invalidation_state_tracker.AsWeakPtr()), 282 kClientInfo)); 283 284 // Set up database directory for the syncer. 285 base::ScopedTempDir database_dir; 286 CHECK(database_dir.CreateUniqueTempDir()); 287 288 // Developers often add types to ModelTypeSet::All() before the server 289 // supports them. We need to be explicit about which types we want here. 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; 316 for (ModelTypeSet::Iterator it = model_types.First(); 317 it.Good(); it.Inc()) { 318 routing_info[it.Get()] = GROUP_PASSIVE; 319 } 320 scoped_refptr<PassiveModelWorker> passive_model_safe_worker = 321 new PassiveModelWorker(&sync_loop, NULL); 322 std::vector<ModelSafeWorker*> workers; 323 workers.push_back(passive_model_safe_worker.get()); 324 325 // Set up sync manager. 326 SyncManagerFactory sync_manager_factory; 327 scoped_ptr<SyncManager> sync_manager = 328 sync_manager_factory.CreateSyncManager("sync_client manager"); 329 LoggingJsEventHandler js_event_handler; 330 const char kSyncServerAndPath[] = "clients4.google.com/chrome-sync/dev"; 331 int kSyncServerPort = 443; 332 bool kUseSsl = true; 333 // Used only by InitialProcessMetadata(), so it's okay to leave this as NULL. 334 const scoped_refptr<base::TaskRunner> blocking_task_runner = NULL; 335 const char kUserAgent[] = "sync_client"; 336 // TODO(akalin): Replace this with just the context getter once 337 // HttpPostProviderFactory is removed. 338 CancelationSignal factory_cancelation_signal; 339 scoped_ptr<HttpPostProviderFactory> post_factory( 340 new HttpBridgeFactory(context_getter.get(), 341 base::Bind(&StubNetworkTimeUpdateCallback), 342 &factory_cancelation_signal)); 343 post_factory->Init(kUserAgent); 344 // Used only when committing bookmarks, so it's okay to leave this 345 // as NULL. 346 ExtensionsActivity* extensions_activity = NULL; 347 LoggingChangeDelegate change_delegate; 348 const char kRestoredKeyForBootstrapping[] = ""; 349 const char kRestoredKeystoreKeyForBootstrapping[] = ""; 350 NullEncryptor null_encryptor; 351 InternalComponentsFactoryImpl::Switches factory_switches = { 352 InternalComponentsFactory::ENCRYPTION_KEYSTORE, 353 InternalComponentsFactory::BACKOFF_NORMAL 354 }; 355 CancelationSignal scm_cancelation_signal; 356 357 sync_manager->Init(database_dir.path(), 358 WeakHandle<JsEventHandler>( 359 js_event_handler.AsWeakPtr()), 360 kSyncServerAndPath, 361 kSyncServerPort, 362 kUseSsl, 363 post_factory.Pass(), 364 workers, 365 extensions_activity, 366 &change_delegate, 367 credentials, 368 invalidator_id, 369 kRestoredKeyForBootstrapping, 370 kRestoredKeystoreKeyForBootstrapping, 371 new InternalComponentsFactoryImpl(factory_switches), 372 &null_encryptor, 373 scoped_ptr<UnrecoverableErrorHandler>( 374 new LoggingUnrecoverableErrorHandler).Pass(), 375 &LogUnrecoverableErrorContext, 376 &scm_cancelation_signal); 377 // TODO(akalin): Avoid passing in model parameters multiple times by 378 // organizing handling of model types. 379 invalidator->UpdateCredentials(credentials.email, credentials.sync_token); 380 invalidator->RegisterHandler(sync_manager.get()); 381 invalidator->UpdateRegisteredIds( 382 sync_manager.get(), ModelTypeSetToObjectIdSet(model_types)); 383 sync_manager->StartSyncingNormally(routing_info); 384 385 sync_loop.Run(); 386 387 io_thread.Stop(); 388 return 0; 389 } 390 391 } // namespace 392 } // namespace syncer 393 394 int main(int argc, char* argv[]) { 395 return syncer::SyncClientMain(argc, argv); 396 } 397