Home | History | Annotate | Download | only in glue
      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 #ifndef CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_
      6 #define CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/callback.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/threading/thread.h"
     15 #include "components/sync_driver/backend_data_type_configurer.h"
     16 #include "sync/internal_api/public/base/model_type.h"
     17 #include "sync/internal_api/public/configure_reason.h"
     18 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
     19 #include "sync/internal_api/public/sync_core_proxy.h"
     20 #include "sync/internal_api/public/sync_manager.h"
     21 #include "sync/internal_api/public/sync_manager_factory.h"
     22 #include "sync/internal_api/public/util/report_unrecoverable_error_function.h"
     23 #include "sync/internal_api/public/util/unrecoverable_error_handler.h"
     24 #include "sync/internal_api/public/util/weak_handle.h"
     25 
     26 class GURL;
     27 
     28 namespace base {
     29 class MessageLoop;
     30 }
     31 
     32 namespace syncer {
     33 class NetworkResources;
     34 class SyncManagerFactory;
     35 }
     36 
     37 namespace browser_sync {
     38 
     39 class ChangeProcessor;
     40 class SyncFrontend;
     41 class SyncedDeviceTracker;
     42 
     43 // An API to "host" the top level SyncAPI element.
     44 //
     45 // This class handles dispatch of potentially blocking calls to appropriate
     46 // threads and ensures that the SyncFrontend is only accessed on the UI loop.
     47 class SyncBackendHost : public BackendDataTypeConfigurer {
     48  public:
     49   typedef syncer::SyncStatus Status;
     50 
     51   // Stubs used by implementing classes.
     52   SyncBackendHost();
     53   virtual ~SyncBackendHost();
     54 
     55   // Called on the frontend's thread to kick off asynchronous initialization.
     56   // Optionally deletes the "Sync Data" folder during init in order to make
     57   // sure we're starting fresh.
     58   //
     59   // |report_unrecoverable_error_function| can be NULL.  Note:
     60   // |unrecoverable_error_handler| may be invoked from any thread.
     61   virtual void Initialize(
     62       SyncFrontend* frontend,
     63       scoped_ptr<base::Thread> sync_thread,
     64       const syncer::WeakHandle<syncer::JsEventHandler>& event_handler,
     65       const GURL& service_url,
     66       const syncer::SyncCredentials& credentials,
     67       bool delete_sync_data_folder,
     68       scoped_ptr<syncer::SyncManagerFactory> sync_manager_factory,
     69       scoped_ptr<syncer::UnrecoverableErrorHandler> unrecoverable_error_handler,
     70       syncer::ReportUnrecoverableErrorFunction
     71           report_unrecoverable_error_function,
     72       syncer::NetworkResources* network_resources) = 0;
     73 
     74   // Called on the frontend's thread to update SyncCredentials.
     75   virtual void UpdateCredentials(
     76       const syncer::SyncCredentials& credentials) = 0;
     77 
     78   // This starts the SyncerThread running a Syncer object to communicate with
     79   // sync servers.  Until this is called, no changes will leave or enter this
     80   // browser from the cloud / sync servers.
     81   // Called on |frontend_loop_|.
     82   virtual void StartSyncingWithServer() = 0;
     83 
     84   // Called on |frontend_loop_| to asynchronously set a new passphrase for
     85   // encryption. Note that it is an error to call SetEncryptionPassphrase under
     86   // the following circumstances:
     87   // - An explicit passphrase has already been set
     88   // - |is_explicit| is true and we have pending keys.
     89   // When |is_explicit| is false, a couple of things could happen:
     90   // - If there are pending keys, we try to decrypt them. If decryption works,
     91   //   this acts like a call to SetDecryptionPassphrase. If not, the GAIA
     92   //   passphrase passed in is cached so we can re-encrypt with it in future.
     93   // - If there are no pending keys, data is encrypted with |passphrase| (this
     94   //   is a no-op if data was already encrypted with |passphrase|.)
     95   virtual void SetEncryptionPassphrase(
     96       const std::string& passphrase,
     97       bool is_explicit) = 0;
     98 
     99   // Called on |frontend_loop_| to use the provided passphrase to asynchronously
    100   // attempt decryption. Returns false immediately if the passphrase could not
    101   // be used to decrypt a locally cached copy of encrypted keys; returns true
    102   // otherwise. If new encrypted keys arrive during the asynchronous call,
    103   // OnPassphraseRequired may be triggered at a later time. It is an error to
    104   // call this when there are no pending keys.
    105   virtual bool SetDecryptionPassphrase(const std::string& passphrase)
    106       WARN_UNUSED_RESULT = 0;
    107 
    108   // Called on |frontend_loop_| to kick off shutdown procedure.  Attempts to cut
    109   // short any long-lived or blocking sync thread tasks so that the shutdown on
    110   // sync thread task that we're about to post won't have to wait very long.
    111   virtual void StopSyncingForShutdown() = 0;
    112 
    113   // Called on |frontend_loop_| to kick off shutdown.
    114   // See the implementation and Core::DoShutdown for details.
    115   // Must be called *after* StopSyncingForShutdown. Caller should claim sync
    116   // thread using STOP_AND_CLAIM_THREAD or DISABLE_AND_CLAIM_THREAD if sync
    117   // backend might be recreated later because otherwise:
    118   // * sync loop may be stopped on main loop and cause it to be blocked.
    119   // * new/old backend may interfere with each other if new backend is created
    120   //   before old one finishes cleanup.
    121   enum ShutdownOption {
    122     STOP,                      // Stop syncing and let backend stop sync thread.
    123     STOP_AND_CLAIM_THREAD,     // Stop syncing and return sync thread.
    124     DISABLE_AND_CLAIM_THREAD,  // Disable sync and return sync thread.
    125   };
    126   virtual scoped_ptr<base::Thread> Shutdown(ShutdownOption option) = 0;
    127 
    128   // Removes all current registrations from the backend on the
    129   // InvalidationService.
    130   virtual void UnregisterInvalidationIds() = 0;
    131 
    132   // Changes the set of data types that are currently being synced.
    133   // The ready_task will be run when configuration is done with the
    134   // set of all types that failed configuration (i.e., if its argument
    135   // is non-empty, then an error was encountered).
    136   virtual void ConfigureDataTypes(
    137       syncer::ConfigureReason reason,
    138       const DataTypeConfigStateMap& config_state_map,
    139       const base::Callback<void(syncer::ModelTypeSet,
    140                                 syncer::ModelTypeSet)>& ready_task,
    141       const base::Callback<void()>& retry_callback) OVERRIDE = 0;
    142 
    143   // Turns on encryption of all present and future sync data.
    144   virtual void EnableEncryptEverything() = 0;
    145 
    146   // Called on |frontend_loop_| to obtain a handle to the UserShare needed for
    147   // creating transactions.  Should not be called before we signal
    148   // initialization is complete with OnBackendInitialized().
    149   virtual syncer::UserShare* GetUserShare() const = 0;
    150 
    151   // Called on |frontend_loop_| to obtain a handle to the SyncCore needed by
    152   // the non-blocking sync types to communicate with the server.
    153   //
    154   // Should be called only when the backend is initialized.
    155   virtual scoped_ptr<syncer::SyncCoreProxy> GetSyncCoreProxy() = 0;
    156 
    157   // Called from any thread to obtain current status information in detailed or
    158   // summarized form.
    159   virtual Status GetDetailedStatus() = 0;
    160   virtual syncer::sessions::SyncSessionSnapshot
    161       GetLastSessionSnapshot() const = 0;
    162 
    163   // Determines if the underlying sync engine has made any local changes to
    164   // items that have not yet been synced with the server.
    165   // ONLY CALL THIS IF OnInitializationComplete was called!
    166   virtual bool HasUnsyncedItems() const = 0;
    167 
    168   // Whether or not we are syncing encryption keys.
    169   virtual bool IsNigoriEnabled() const = 0;
    170 
    171   // Returns the type of passphrase being used to encrypt data. See
    172   // sync_encryption_handler.h.
    173   virtual syncer::PassphraseType GetPassphraseType() const = 0;
    174 
    175   // If an explicit passphrase is in use, returns the time at which that
    176   // passphrase was set (if available).
    177   virtual base::Time GetExplicitPassphraseTime() const = 0;
    178 
    179   // True if the cryptographer has any keys available to attempt decryption.
    180   // Could mean we've downloaded and loaded Nigori objects, or we bootstrapped
    181   // using a token previously received.
    182   virtual bool IsCryptographerReady(
    183       const syncer::BaseTransaction* trans) const = 0;
    184 
    185   virtual void GetModelSafeRoutingInfo(
    186       syncer::ModelSafeRoutingInfo* out) const = 0;
    187 
    188   // Fetches the DeviceInfo tracker.
    189   virtual SyncedDeviceTracker* GetSyncedDeviceTracker() const = 0;
    190 
    191   // Requests that the backend forward to the fronent any protocol events in
    192   // its buffer and begin forwarding automatically from now on.  Repeated calls
    193   // to this function may result in the same events being emitted several
    194   // times.
    195   virtual void RequestBufferedProtocolEventsAndEnableForwarding() = 0;
    196 
    197   // Disables protocol event forwarding.
    198   virtual void DisableProtocolEventForwarding() = 0;
    199 
    200   // Returns a ListValue representing all nodes for the specified types through
    201   // |callback| on this thread.
    202   virtual void GetAllNodesForTypes(
    203       syncer::ModelTypeSet types,
    204       base::Callback<void(const std::vector<syncer::ModelType>&,
    205                           ScopedVector<base::ListValue>)> type) = 0;
    206 
    207   // Enables the sending of directory type debug counters.  Also, for every
    208   // time it is called, it makes an explicit request that updates to an update
    209   // for all counters be emitted.
    210   virtual void EnableDirectoryTypeDebugInfoForwarding() = 0;
    211 
    212   // Disables the sending of directory type debug counters.
    213   virtual void DisableDirectoryTypeDebugInfoForwarding() = 0;
    214 
    215   virtual base::MessageLoop* GetSyncLoopForTesting() = 0;
    216 
    217   DISALLOW_COPY_AND_ASSIGN(SyncBackendHost);
    218 };
    219 
    220 }  // namespace browser_sync
    221 
    222 #endif  // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_
    223