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