Home | History | Annotate | Download | only in sync_file_system
      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_FILE_SYSTEM_REMOTE_FILE_SYNC_SERVICE_H_
      6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_FILE_SYNC_SERVICE_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/basictypes.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "chrome/browser/sync_file_system/conflict_resolution_policy.h"
     16 #include "chrome/browser/sync_file_system/sync_callbacks.h"
     17 #include "chrome/browser/sync_file_system/sync_file_metadata.h"
     18 #include "webkit/browser/fileapi/file_system_url.h"
     19 
     20 class BrowserContextKeyedServiceFactory;
     21 class GURL;
     22 
     23 namespace base {
     24 class ListValue;
     25 }
     26 
     27 namespace content {
     28 class BrowserContext;
     29 }
     30 
     31 namespace webkit_blob {
     32 class ScopedFile;
     33 }
     34 
     35 namespace sync_file_system {
     36 
     37 class FileStatusObserver;
     38 class LocalChangeProcessor;
     39 class RemoteChangeProcessor;
     40 class TaskLogger;
     41 
     42 enum RemoteServiceState {
     43   // Remote service is up and running, or has not seen any errors yet.
     44   // The consumer of this service can make new requests while the
     45   // service is in this state.
     46   REMOTE_SERVICE_OK = 0,
     47 
     48   // Remote service is temporarily unavailable due to network,
     49   // authentication or some other temporary failure.
     50   // This state may be automatically resolved when the underlying
     51   // network condition or service condition changes.
     52   // The consumer of this service can still make new requests but
     53   // they may fail (with recoverable error code).
     54   REMOTE_SERVICE_TEMPORARY_UNAVAILABLE,
     55 
     56   // Remote service is temporarily unavailable due to authentication failure.
     57   // This state may be automatically resolved when the authentication token
     58   // has been refreshed internally (e.g. when the user signed in etc).
     59   // The consumer of this service can still make new requests but
     60   // they may fail (with recoverable error code).
     61   REMOTE_SERVICE_AUTHENTICATION_REQUIRED,
     62 
     63   // Remote service is disabled by configuration change or due to some
     64   // unrecoverable errors, e.g. local database corruption.
     65   // Any new requests will immediately fail when the service is in
     66   // this state.
     67   REMOTE_SERVICE_DISABLED,
     68 
     69   REMOTE_SERVICE_STATE_MAX,
     70 };
     71 
     72 // This class represents a backing service of the sync filesystem.
     73 // This also maintains conflict information, i.e. a list of conflicting files
     74 // (at least in the current design).
     75 // Owned by SyncFileSystemService.
     76 class RemoteFileSyncService {
     77  public:
     78   enum BackendVersion {
     79     V1,
     80     V2,
     81   };
     82 
     83   class Observer {
     84    public:
     85     Observer() {}
     86     virtual ~Observer() {}
     87 
     88     // This is called when RemoteFileSyncService updates its internal queue
     89     // of pending remote changes.
     90     // |pending_changes_hint| indicates the pending queue length to help sync
     91     // scheduling but the value may not be accurately reflect the real-time
     92     // value.
     93     virtual void OnRemoteChangeQueueUpdated(int64 pending_changes_hint) = 0;
     94 
     95     // This is called when RemoteFileSyncService updates its state.
     96     virtual void OnRemoteServiceStateUpdated(
     97         RemoteServiceState state,
     98         const std::string& description) {}
     99 
    100    private:
    101     DISALLOW_COPY_AND_ASSIGN(Observer);
    102   };
    103 
    104   struct Version {
    105     std::string id;
    106     SyncFileMetadata metadata;
    107   };
    108 
    109   enum UninstallFlag {
    110     UNINSTALL_AND_PURGE_REMOTE,
    111     UNINSTALL_AND_KEEP_REMOTE,
    112   };
    113 
    114   // For GetOriginStatusMap.
    115   typedef std::map<GURL, std::string> OriginStatusMap;
    116   typedef base::Callback<void(scoped_ptr<OriginStatusMap> status_map)>
    117       StatusMapCallback;
    118 
    119   // For GetRemoteVersions.
    120   typedef base::Callback<void(SyncStatusCode status,
    121                               const std::vector<Version>& versions)>
    122       RemoteVersionsCallback;
    123   typedef base::Callback<void(SyncStatusCode status,
    124                               webkit_blob::ScopedFile downloaded)>
    125       DownloadVersionCallback;
    126 
    127   // For DumpFile.
    128   typedef base::Callback<void(scoped_ptr<base::ListValue> list)> ListCallback;
    129 
    130   // Creates an initialized RemoteFileSyncService for backend |version|
    131   // for |context|.
    132   static scoped_ptr<RemoteFileSyncService> CreateForBrowserContext(
    133       BackendVersion version,
    134       content::BrowserContext* context,
    135       TaskLogger* task_logger);
    136 
    137   // Returns BrowserContextKeyedServiceFactory's an instance of
    138   // RemoteFileSyncService for backend |version| depends on.
    139   static void AppendDependsOnFactories(
    140       BackendVersion version,
    141       std::set<BrowserContextKeyedServiceFactory*>* factories);
    142 
    143   RemoteFileSyncService() {}
    144   virtual ~RemoteFileSyncService() {}
    145 
    146   // Adds and removes observers.
    147   virtual void AddServiceObserver(Observer* observer) = 0;
    148   virtual void AddFileStatusObserver(FileStatusObserver* observer) = 0;
    149 
    150   // Registers |origin| to track remote side changes for the |origin|.
    151   // Upon completion, invokes |callback|.
    152   // The caller may call this method again when the remote service state
    153   // migrates to REMOTE_SERVICE_OK state if the error code returned via
    154   // |callback| was retriable ones.
    155   virtual void RegisterOrigin(
    156       const GURL& origin,
    157       const SyncStatusCallback& callback) = 0;
    158 
    159   // Re-enables |origin| that was previously disabled. If |origin| is not a
    160   // SyncFS app, then the origin is effectively ignored.
    161   virtual void EnableOrigin(
    162       const GURL& origin,
    163       const SyncStatusCallback& callback) = 0;
    164 
    165   virtual void DisableOrigin(
    166       const GURL& origin,
    167       const SyncStatusCallback& callback) = 0;
    168 
    169   // Uninstalls the |origin| by deleting its remote data copy and then removing
    170   // the origin from the metadata store.
    171   virtual void UninstallOrigin(
    172       const GURL& origin,
    173       UninstallFlag flag,
    174       const SyncStatusCallback& callback) = 0;
    175 
    176   // Called by the sync engine to process one remote change.
    177   // After a change is processed |callback| will be called (to return
    178   // the control to the sync engine).
    179   // It is invalid to call this before calling SetRemoteChangeProcessor().
    180   virtual void ProcessRemoteChange(const SyncFileCallback& callback) = 0;
    181 
    182   // Sets a remote change processor.  This must be called before any
    183   // ProcessRemoteChange().
    184   virtual void SetRemoteChangeProcessor(
    185       RemoteChangeProcessor* processor) = 0;
    186 
    187   // Returns a LocalChangeProcessor that applies a local change to the remote
    188   // storage backed by this service.
    189   virtual LocalChangeProcessor* GetLocalChangeProcessor() = 0;
    190 
    191   // Returns the current remote service state (should equal to the value
    192   // returned by the last OnRemoteServiceStateUpdated notification.
    193   virtual RemoteServiceState GetCurrentState() const = 0;
    194 
    195   // Returns all origins along with an arbitrary string description of their
    196   // corresponding sync statuses.
    197   virtual void GetOriginStatusMap(const StatusMapCallback& callback) = 0;
    198 
    199   // Returns file metadata for |origin| to call |callback|.
    200   virtual void DumpFiles(const GURL& origin,
    201                          const ListCallback& callback) = 0;
    202 
    203   // Returns the dump of internal database.
    204   virtual void DumpDatabase(const ListCallback& callback) = 0;
    205 
    206   // Enables or disables the background sync.
    207   // Setting this to false should disable the synchronization (and make
    208   // the service state to REMOTE_SERVICE_DISABLED), while setting this to
    209   // true does not necessarily mean the service is actually turned on
    210   // (for example if Chrome is offline the service state will become
    211   // REMOTE_SERVICE_TEMPORARY_UNAVAILABLE).
    212   virtual void SetSyncEnabled(bool enabled) = 0;
    213 
    214   virtual void PromoteDemotedChanges() = 0;
    215 
    216  private:
    217   DISALLOW_COPY_AND_ASSIGN(RemoteFileSyncService);
    218 };
    219 
    220 }  // namespace sync_file_system
    221 
    222 #endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_FILE_SYNC_SERVICE_H_
    223