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