Home | History | Annotate | Download | only in drive_backend_v1
      1 // Copyright 2013 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_DRIVE_BACKEND_V1_API_UTIL_INTERFACE_H_
      6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_V1_API_UTIL_INTERFACE_H_
      7 
      8 #include <string>
      9 
     10 #include "base/callback_forward.h"
     11 #include "google_apis/drive/gdata_errorcode.h"
     12 #include "google_apis/drive/gdata_wapi_parser.h"
     13 
     14 class GURL;
     15 class Profile;
     16 
     17 namespace base {
     18 class Time;
     19 }
     20 
     21 namespace google_apis {
     22 class DriveUploaderInterface;
     23 }
     24 
     25 namespace webkit_blob {
     26 class ScopedFile;
     27 }
     28 
     29 namespace sync_file_system {
     30 namespace drive_backend {
     31 
     32 class APIUtilObserver {
     33  public:
     34   APIUtilObserver() {}
     35   virtual ~APIUtilObserver() {}
     36   virtual void OnAuthenticated() = 0;
     37   virtual void OnNetworkConnected() = 0;
     38 
     39  private:
     40   DISALLOW_COPY_AND_ASSIGN(APIUtilObserver);
     41 };
     42 
     43 // The implementation of this class is responsible for talking to the Drive
     44 // service to get and put Drive directories, files and metadata.
     45 // This class is owned by DriveFileSyncService.
     46 class APIUtilInterface {
     47  public:
     48   typedef base::Callback<void(google_apis::GDataErrorCode error)>
     49       GDataErrorCallback;
     50   typedef base::Callback<void(google_apis::GDataErrorCode error,
     51                               const std::string& file_md5,
     52                               int64 file_size,
     53                               const base::Time& last_updated,
     54                               webkit_blob::ScopedFile downloaded)>
     55       DownloadFileCallback;
     56   typedef base::Callback<void(google_apis::GDataErrorCode error,
     57                               const std::string& resource_id,
     58                               const std::string& file_md5)> UploadFileCallback;
     59   typedef base::Callback<
     60       void(google_apis::GDataErrorCode error, const std::string& resource_id)>
     61       ResourceIdCallback;
     62   typedef base::Callback<void(google_apis::GDataErrorCode error,
     63                               int64 changestamp)> ChangeStampCallback;
     64   typedef base::Callback<void(google_apis::GDataErrorCode error,
     65                               scoped_ptr<google_apis::ResourceList> feed)>
     66       ResourceListCallback;
     67   typedef base::Callback<void(google_apis::GDataErrorCode error,
     68                               scoped_ptr<google_apis::ResourceEntry> entry)>
     69       ResourceEntryCallback;
     70 
     71   APIUtilInterface() {}
     72   virtual ~APIUtilInterface() {}
     73 
     74   virtual void AddObserver(APIUtilObserver* observer) = 0;
     75   virtual void RemoveObserver(APIUtilObserver* observer) = 0;
     76 
     77   // Fetches Resource ID of the directory where we should place all files to
     78   // sync.  Upon completion, invokes |callback|.
     79   // If the directory does not exist on the server this also creates
     80   // the directory.
     81   //
     82   // Returns HTTP_SUCCESS if the directory already exists.
     83   // Returns HTTP_CREATED if the directory was not found and created.
     84   virtual void GetDriveDirectoryForSyncRoot(
     85       const ResourceIdCallback& callback) = 0;
     86 
     87   // Fetches Resource ID of the directory for the |origin|.
     88   // Upon completion, invokes |callback|.
     89   // If the directory does not exist on the server this also creates
     90   // the directory.
     91   //
     92   // Returns HTTP_SUCCESS if the directory already exists.
     93   // Returns HTTP_CREATED if the directory was not found and created.
     94   virtual void GetDriveDirectoryForOrigin(
     95       const std::string& sync_root_resource_id,
     96       const GURL& origin,
     97       const ResourceIdCallback& callback) = 0;
     98 
     99   // Fetches the largest changestamp for the signed-in account.
    100   // Upon completion, invokes |callback|.
    101   virtual void GetLargestChangeStamp(const ChangeStampCallback& callback) = 0;
    102 
    103   // Fetches the resource entry for the file identified by |resource_id|.
    104   // Upon completion, invokes |callback|.
    105   virtual void GetResourceEntry(const std::string& resource_id,
    106                                 const ResourceEntryCallback& callback) = 0;
    107 
    108   // Lists files in the directory identified by |resource_id|.
    109   // Upon completion, invokes |callback|.
    110   // The result may be chunked and may have successive results. The caller needs
    111   // to call ContunueListing with the result of GetNextFeedURL to get complete
    112   // list of files.
    113   virtual void ListFiles(const std::string& directory_resource_id,
    114                          const ResourceListCallback& callback) = 0;
    115 
    116   // Lists changes that happened after |start_changestamp|.
    117   // Upon completion, invokes |callback|.
    118   // The result may be chunked and may have successive results. The caller needs
    119   // to call ContunueListing with the result of GetNextFeedURL to get complete
    120   // list of changes.
    121   virtual void ListChanges(int64 start_changestamp,
    122                            const ResourceListCallback& callback) = 0;
    123 
    124   // Fetches the next chunk of ResourceList identified by |next_link|.
    125   // Upon completion, invokes |callback|.
    126   virtual void ContinueListing(const GURL& next_link,
    127                                const ResourceListCallback& callback) = 0;
    128 
    129   // Downloads the file identified by |resource_id| from Drive to
    130   // |local_file_path|.
    131   // |local_file_md5| represents the hash value of the local file to be updated.
    132   // If |local_file_md5| is equal to remote file's value, cancels the download
    133   // and invokes |callback| with GDataErrorCode::HTTP_NOT_MODIFIED immediately.
    134   // When there is no local file to be updated, |local_file_md5| should be
    135   // empty.
    136   virtual void DownloadFile(const std::string& resource_id,
    137                             const std::string& local_file_md5,
    138                             const DownloadFileCallback& callback) = 0;
    139 
    140   // Uploads the new file |local_file_path| with specified |title| into the
    141   // directory identified by |directory_resource_id|.
    142   // Upon completion, invokes |callback| and returns HTTP_CREATED if the file
    143   // is created.
    144   virtual void UploadNewFile(const std::string& directory_resource_id,
    145                              const base::FilePath& local_file_path,
    146                              const std::string& title,
    147                              const UploadFileCallback& callback) = 0;
    148 
    149   // Uploads the existing file identified by |local_file_path|.
    150   // |remote_file_md5| represents the expected hash value of the file to be
    151   // updated on Drive. If |remote_file_md5| is different from the actual value,
    152   // cancels the upload and invokes |callback| with
    153   // GDataErrorCode::HTTP_CONFLICT immediately.
    154   // Returns HTTP_SUCCESS if the file uploaded successfully.
    155   virtual void UploadExistingFile(const std::string& resource_id,
    156                                   const std::string& remote_file_md5,
    157                                   const base::FilePath& local_file_path,
    158                                   const UploadFileCallback& callback) = 0;
    159 
    160   // Creates a new directory with specified |title| into the directory
    161   // identified by |parent_resource_id|.
    162   // Upon completion, invokes |callback| and returns HTTP_CREATED if
    163   // the directory is created.
    164   virtual void CreateDirectory(const std::string& parent_resource_id,
    165                                const std::string& title,
    166                                const ResourceIdCallback& callback) = 0;
    167 
    168   // Returns true if the user is authenticated.
    169   virtual bool IsAuthenticated() const = 0;
    170 
    171   // Deletes the file identified by |resource_id|.
    172   // A directory is considered a file and will cause a recursive delete if
    173   // given as the |resource_id|.
    174   // TODO(tzik): Rename this function to DeleteResource.
    175   //
    176   // |remote_file_md5| represents the expected hash value of the file to be
    177   // deleted from Drive. If |remote_file_md5| is empty, then it's implied that
    178   // the file should be deleted on the remote side regardless. If
    179   // |remote_file_md5| is not empty and is different from the actual value,
    180   // the deletion operation is canceled and the |callback| with
    181   // GDataErrorCode::HTTP_CONFLICT is invoked immediately.
    182   virtual void DeleteFile(const std::string& resource_id,
    183                           const std::string& remote_file_md5,
    184                           const GDataErrorCallback& callback) = 0;
    185 
    186   // Ensures the sync root directory is not in 'My Drive'. Even if the directory
    187   // is in directories other than 'My Drive', it will not be removed from there.
    188   virtual void EnsureSyncRootIsNotInMyDrive(
    189       const std::string& sync_root_resource_id) = 0;
    190 
    191  private:
    192   DISALLOW_COPY_AND_ASSIGN(APIUtilInterface);
    193 };
    194 
    195 }  // namespace drive_backend
    196 }  // namespace sync_file_system
    197 
    198 #endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_V1_API_UTIL_INTERFACE_H_
    199