Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: sync/tools/sync_client.cc

Issue 15580002: Make use of InvalidationService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update with OAuth2 support Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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::InitLogging( 232 logging::InitLogging(
234 NULL, 233 NULL,
(...skipping 11 matching lines...) Expand all
246 // Parse command line. 245 // Parse command line.
247 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 246 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
248 SyncCredentials credentials; 247 SyncCredentials credentials;
249 credentials.email = command_line.GetSwitchValueASCII(kEmailSwitch); 248 credentials.email = command_line.GetSwitchValueASCII(kEmailSwitch);
250 credentials.sync_token = command_line.GetSwitchValueASCII(kTokenSwitch); 249 credentials.sync_token = command_line.GetSwitchValueASCII(kTokenSwitch);
251 // TODO(akalin): Write a wrapper script that gets a token for an 250 // TODO(akalin): Write a wrapper script that gets a token for an
252 // email and password and passes that in to this utility. 251 // email and password and passes that in to this utility.
253 if (credentials.email.empty() || credentials.sync_token.empty()) { 252 if (credentials.email.empty() || credentials.sync_token.empty()) {
254 std::printf("Usage: %s --%s=foo@bar.com --%s=token\n" 253 std::printf("Usage: %s --%s=foo@bar.com --%s=token\n"
255 "[--%s=host:port] [--%s] [--%s]\n" 254 "[--%s=host:port] [--%s] [--%s]\n"
256 "[--%s=(server|p2p)]\n\n"
257 "Run chrome and set a breakpoint on\n" 255 "Run chrome and set a breakpoint on\n"
258 "syncer::SyncManagerImpl::UpdateCredentials() " 256 "syncer::SyncManagerImpl::UpdateCredentials() "
259 "after logging into\n" 257 "after logging into\n"
260 "sync to get the token to pass into this utility.\n", 258 "sync to get the token to pass into this utility.\n",
261 argv[0], 259 argv[0],
262 kEmailSwitch, kTokenSwitch, kXmppHostPortSwitch, 260 kEmailSwitch, kTokenSwitch, kXmppHostPortSwitch,
263 kXmppTrySslTcpFirstSwitch, 261 kXmppTrySslTcpFirstSwitch,
264 kXmppAllowInsecureConnectionSwitch, 262 kXmppAllowInsecureConnectionSwitch);
265 kNotificationMethodSwitch);
266 return -1; 263 return -1;
267 } 264 }
268 265
269 // Set up objects that monitor the network. 266 // Set up objects that monitor the network.
270 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier( 267 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier(
271 net::NetworkChangeNotifier::Create()); 268 net::NetworkChangeNotifier::Create());
272 269
273 // Set up sync notifier factory. 270 // Set up sync notifier factory.
274 const scoped_refptr<MyTestURLRequestContextGetter> context_getter = 271 const scoped_refptr<MyTestURLRequestContextGetter> context_getter =
275 new MyTestURLRequestContextGetter(io_thread.message_loop_proxy()); 272 new MyTestURLRequestContextGetter(io_thread.message_loop_proxy());
276 const notifier::NotifierOptions& notifier_options = 273 const notifier::NotifierOptions& notifier_options =
277 ParseNotifierOptions(command_line, context_getter); 274 ParseNotifierOptions(command_line, context_getter);
278 const char kClientInfo[] = "sync_listen_notifications"; 275 const char kClientInfo[] = "standalone_sync_client";
276 std::string invalidator_id = base::RandBytesAsString(8);
279 NullInvalidationStateTracker null_invalidation_state_tracker; 277 NullInvalidationStateTracker null_invalidation_state_tracker;
280 InvalidatorFactory invalidator_factory( 278 scoped_ptr<Invalidator> invalidator(new NonBlockingInvalidator(
281 notifier_options, kClientInfo, 279 notifier_options,
282 null_invalidation_state_tracker.AsWeakPtr()); 280 invalidator_id,
281 null_invalidation_state_tracker.GetAllInvalidationStates(),
282 null_invalidation_state_tracker.GetBootstrapData(),
283 WeakHandle<InvalidationStateTracker>(
284 null_invalidation_state_tracker.AsWeakPtr()),
285 kClientInfo));
283 286
284 // Set up database directory for the syncer. 287 // Set up database directory for the syncer.
285 base::ScopedTempDir database_dir; 288 base::ScopedTempDir database_dir;
286 CHECK(database_dir.CreateUniqueTempDir()); 289 CHECK(database_dir.CreateUniqueTempDir());
287 290
288 // Set up model type parameters. 291 // Developers often add types to ModelTypeSet::All() before the server
289 const ModelTypeSet model_types = ModelTypeSet::All(); 292 // supports them. We need to be explicit about which types we want here.
293 ModelTypeSet model_types;
294 model_types.Put(BOOKMARKS);
295 model_types.Put(PREFERENCES);
296 model_types.Put(PASSWORDS);
297 model_types.Put(AUTOFILL);
298 model_types.Put(THEMES);
299 model_types.Put(TYPED_URLS);
300 model_types.Put(EXTENSIONS);
301 model_types.Put(NIGORI);
302 model_types.Put(SEARCH_ENGINES);
303 model_types.Put(SESSIONS);
304 model_types.Put(APPS);
305 model_types.Put(AUTOFILL_PROFILE);
306 model_types.Put(APP_SETTINGS);
307 model_types.Put(EXTENSION_SETTINGS);
308 model_types.Put(APP_NOTIFICATIONS);
309 model_types.Put(HISTORY_DELETE_DIRECTIVES);
310 model_types.Put(SYNCED_NOTIFICATIONS);
311 model_types.Put(DEVICE_INFO);
312 model_types.Put(EXPERIMENTS);
313 model_types.Put(PRIORITY_PREFERENCES);
314 model_types.Put(DICTIONARY);
315 model_types.Put(FAVICON_IMAGES);
316 model_types.Put(FAVICON_TRACKING);
317
290 ModelSafeRoutingInfo routing_info; 318 ModelSafeRoutingInfo routing_info;
291 for (ModelTypeSet::Iterator it = model_types.First(); 319 for (ModelTypeSet::Iterator it = model_types.First();
292 it.Good(); it.Inc()) { 320 it.Good(); it.Inc()) {
293 routing_info[it.Get()] = GROUP_PASSIVE; 321 routing_info[it.Get()] = GROUP_PASSIVE;
294 } 322 }
295 scoped_refptr<PassiveModelWorker> passive_model_safe_worker = 323 scoped_refptr<PassiveModelWorker> passive_model_safe_worker =
296 new PassiveModelWorker(&sync_loop, NULL); 324 new PassiveModelWorker(&sync_loop, NULL);
297 std::vector<ModelSafeWorker*> workers; 325 std::vector<ModelSafeWorker*> workers;
298 workers.push_back(passive_model_safe_worker.get()); 326 workers.push_back(passive_model_safe_worker.get());
299 327
300 // Set up sync manager. 328 // Set up sync manager.
301 SyncManagerFactory sync_manager_factory; 329 SyncManagerFactory sync_manager_factory;
302 scoped_ptr<SyncManager> sync_manager = 330 scoped_ptr<SyncManager> sync_manager =
303 sync_manager_factory.CreateSyncManager("sync_client manager"); 331 sync_manager_factory.CreateSyncManager("sync_client manager");
304 LoggingJsEventHandler js_event_handler; 332 LoggingJsEventHandler js_event_handler;
305 const char kSyncServerAndPath[] = "clients4.google.com/chrome-sync/dev"; 333 const char kSyncServerAndPath[] = "clients4.google.com/chrome-sync/dev";
306 int kSyncServerPort = 443; 334 int kSyncServerPort = 443;
307 bool kUseSsl = true; 335 bool kUseSsl = true;
308 // Used only by InitialProcessMetadata(), so it's okay to leave this as NULL. 336 // Used only by InitialProcessMetadata(), so it's okay to leave this as NULL.
309 const scoped_refptr<base::TaskRunner> blocking_task_runner = NULL; 337 const scoped_refptr<base::TaskRunner> blocking_task_runner = NULL;
310 const char kUserAgent[] = "sync_client"; 338 const char kUserAgent[] = "sync_client";
311 // TODO(akalin): Replace this with just the context getter once 339 // TODO(akalin): Replace this with just the context getter once
312 // HttpPostProviderFactory is removed. 340 // HttpPostProviderFactory is removed.
313 scoped_ptr<HttpPostProviderFactory> post_factory(new HttpBridgeFactory( 341 scoped_ptr<HttpPostProviderFactory> post_factory(
314 context_getter.get(), kUserAgent, NetworkTimeUpdateCallback())); 342 new HttpBridgeFactory(context_getter,
343 kUserAgent,
344 base::Bind(&StubNetworkTimeUpdateCallback)));
315 // Used only when committing bookmarks, so it's okay to leave this 345 // Used only when committing bookmarks, so it's okay to leave this
316 // as NULL. 346 // as NULL.
317 ExtensionsActivityMonitor* extensions_activity_monitor = NULL; 347 ExtensionsActivityMonitor* extensions_activity_monitor = NULL;
318 LoggingChangeDelegate change_delegate; 348 LoggingChangeDelegate change_delegate;
319 const char kRestoredKeyForBootstrapping[] = ""; 349 const char kRestoredKeyForBootstrapping[] = "";
320 const char kRestoredKeystoreKeyForBootstrapping[] = ""; 350 const char kRestoredKeystoreKeyForBootstrapping[] = "";
321 NullEncryptor null_encryptor; 351 NullEncryptor null_encryptor;
322 LoggingUnrecoverableErrorHandler unrecoverable_error_handler; 352 LoggingUnrecoverableErrorHandler unrecoverable_error_handler;
323 InternalComponentsFactoryImpl::Switches factory_switches = { 353 InternalComponentsFactoryImpl::Switches factory_switches = {
324 InternalComponentsFactory::ENCRYPTION_KEYSTORE, 354 InternalComponentsFactory::ENCRYPTION_KEYSTORE,
325 InternalComponentsFactory::BACKOFF_NORMAL 355 InternalComponentsFactory::BACKOFF_NORMAL
326 }; 356 };
327 357
328 sync_manager->Init(database_dir.path(), 358 sync_manager->Init(database_dir.path(),
329 WeakHandle<JsEventHandler>( 359 WeakHandle<JsEventHandler>(
330 js_event_handler.AsWeakPtr()), 360 js_event_handler.AsWeakPtr()),
331 kSyncServerAndPath, 361 kSyncServerAndPath,
332 kSyncServerPort, 362 kSyncServerPort,
333 kUseSsl, 363 kUseSsl,
334 post_factory.Pass(), 364 post_factory.Pass(),
335 workers, 365 workers,
336 extensions_activity_monitor, 366 extensions_activity_monitor,
337 &change_delegate, 367 &change_delegate,
338 credentials, 368 credentials,
339 scoped_ptr<Invalidator>( 369 invalidator_id,
340 invalidator_factory.CreateInvalidator()),
341 invalidator_factory.GetInvalidatorClientId(),
342 kRestoredKeyForBootstrapping, 370 kRestoredKeyForBootstrapping,
343 kRestoredKeystoreKeyForBootstrapping, 371 kRestoredKeystoreKeyForBootstrapping,
344 scoped_ptr<InternalComponentsFactory>( 372 scoped_ptr<InternalComponentsFactory>(
345 new InternalComponentsFactoryImpl(factory_switches)), 373 new InternalComponentsFactoryImpl(factory_switches)),
346 &null_encryptor, 374 &null_encryptor,
347 &unrecoverable_error_handler, 375 &unrecoverable_error_handler,
348 &LogUnrecoverableErrorContext, false); 376 &LogUnrecoverableErrorContext, false);
349 // TODO(akalin): Avoid passing in model parameters multiple times by 377 // TODO(akalin): Avoid passing in model parameters multiple times by
350 // organizing handling of model types. 378 // organizing handling of model types.
351 sync_manager->UpdateEnabledTypes(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));
352 sync_manager->StartSyncingNormally(routing_info); 383 sync_manager->StartSyncingNormally(routing_info);
353 384
354 sync_loop.Run(); 385 sync_loop.Run();
355 386
356 io_thread.Stop(); 387 io_thread.Stop();
357 return 0; 388 return 0;
358 } 389 }
359 390
360 } // namespace 391 } // namespace
361 } // namespace syncer 392 } // namespace syncer
362 393
363 int main(int argc, char* argv[]) { 394 int main(int argc, char* argv[]) {
364 return syncer::SyncClientMain(argc, argv); 395 return syncer::SyncClientMain(argc, argv);
365 } 396 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698