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