Home | History | Annotate | Download | only in tools
      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/model_type.h"
     31 #include "sync/internal_api/public/base_node.h"
     32 #include "sync/internal_api/public/engine/passive_model_worker.h"
     33 #include "sync/internal_api/public/http_bridge.h"
     34 #include "sync/internal_api/public/internal_components_factory_impl.h"
     35 #include "sync/internal_api/public/read_node.h"
     36 #include "sync/internal_api/public/sync_manager.h"
     37 #include "sync/internal_api/public/sync_manager_factory.h"
     38 #include "sync/internal_api/public/util/report_unrecoverable_error_function.h"
     39 #include "sync/internal_api/public/util/unrecoverable_error_handler.h"
     40 #include "sync/internal_api/public/util/weak_handle.h"
     41 #include "sync/js/js_event_details.h"
     42 #include "sync/js/js_event_handler.h"
     43 #include "sync/notifier/invalidation_state_tracker.h"
     44 #include "sync/notifier/non_blocking_invalidator.h"
     45 #include "sync/test/fake_encryptor.h"
     46 #include "sync/tools/null_invalidation_state_tracker.h"
     47 
     48 #if defined(OS_MACOSX)
     49 #include "base/mac/scoped_nsautorelease_pool.h"
     50 #endif
     51 
     52 // This is a simple utility that initializes a sync client and
     53 // prints out any events.
     54 
     55 // TODO(akalin): Refactor to combine shared code with
     56 // sync_listen_notifications.
     57 namespace syncer {
     58 namespace {
     59 
     60 const char kEmailSwitch[] = "email";
     61 const char kTokenSwitch[] = "token";
     62 const char kXmppHostPortSwitch[] = "xmpp-host-port";
     63 const char kXmppTrySslTcpFirstSwitch[] = "xmpp-try-ssltcp-first";
     64 const char kXmppAllowInsecureConnectionSwitch[] =
     65     "xmpp-allow-insecure-connection";
     66 const char kNotificationMethodSwitch[] = "notification-method";
     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 stack_trace;
    190   stack_trace.PrintBacktrace();
    191 }
    192 
    193 notifier::NotifierOptions ParseNotifierOptions(
    194     const CommandLine& command_line,
    195     const scoped_refptr<net::URLRequestContextGetter>&
    196         request_context_getter) {
    197   notifier::NotifierOptions notifier_options;
    198   notifier_options.request_context_getter = request_context_getter;
    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.GetAllInvalidationStates(),
    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   scoped_ptr<HttpPostProviderFactory> post_factory(
    339       new HttpBridgeFactory(context_getter.get(),
    340                             kUserAgent,
    341                             base::Bind(&StubNetworkTimeUpdateCallback)));
    342   // Used only when committing bookmarks, so it's okay to leave this
    343   // as NULL.
    344   ExtensionsActivity* extensions_activity = NULL;
    345   LoggingChangeDelegate change_delegate;
    346   const char kRestoredKeyForBootstrapping[] = "";
    347   const char kRestoredKeystoreKeyForBootstrapping[] = "";
    348   NullEncryptor null_encryptor;
    349   InternalComponentsFactoryImpl::Switches factory_switches = {
    350       InternalComponentsFactory::ENCRYPTION_KEYSTORE,
    351       InternalComponentsFactory::BACKOFF_NORMAL
    352   };
    353 
    354   sync_manager->Init(database_dir.path(),
    355                     WeakHandle<JsEventHandler>(
    356                         js_event_handler.AsWeakPtr()),
    357                     kSyncServerAndPath,
    358                     kSyncServerPort,
    359                     kUseSsl,
    360                     post_factory.Pass(),
    361                     workers,
    362                     extensions_activity,
    363                     &change_delegate,
    364                     credentials,
    365                     invalidator_id,
    366                     kRestoredKeyForBootstrapping,
    367                     kRestoredKeystoreKeyForBootstrapping,
    368                     new InternalComponentsFactoryImpl(factory_switches),
    369                     &null_encryptor,
    370                     scoped_ptr<UnrecoverableErrorHandler>(
    371                         new LoggingUnrecoverableErrorHandler).Pass(),
    372                     &LogUnrecoverableErrorContext, false);
    373   // TODO(akalin): Avoid passing in model parameters multiple times by
    374   // organizing handling of model types.
    375   invalidator->UpdateCredentials(credentials.email, credentials.sync_token);
    376   invalidator->RegisterHandler(sync_manager.get());
    377   invalidator->UpdateRegisteredIds(
    378       sync_manager.get(), ModelTypeSetToObjectIdSet(model_types));
    379   sync_manager->StartSyncingNormally(routing_info);
    380 
    381   sync_loop.Run();
    382 
    383   io_thread.Stop();
    384   return 0;
    385 }
    386 
    387 }  // namespace
    388 }  // namespace syncer
    389 
    390 int main(int argc, char* argv[]) {
    391   return syncer::SyncClientMain(argc, argv);
    392 }
    393