Home | History | Annotate | Download | only in drive_backend
      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_API_UTIL_H_
      6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_API_UTIL_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/memory/weak_ptr.h"
     12 #include "base/threading/non_thread_safe.h"
     13 #include "chrome/browser/drive/drive_service_interface.h"
     14 #include "chrome/browser/google_apis/drive_api_url_generator.h"
     15 #include "chrome/browser/google_apis/gdata_wapi_url_generator.h"
     16 #include "chrome/browser/sync_file_system/drive_backend/api_util_interface.h"
     17 #include "net/base/network_change_notifier.h"
     18 #include "webkit/common/blob/scoped_file.h"
     19 
     20 class GURL;
     21 class Profile;
     22 
     23 namespace drive { class DriveUploaderInterface; }
     24 
     25 namespace sync_file_system {
     26 namespace drive_backend {
     27 
     28 // This class is responsible for talking to the Drive service to get and put
     29 // Drive directories, files and metadata.
     30 // This class is owned by DriveFileSyncService.
     31 class APIUtil : public APIUtilInterface,
     32                 public drive::DriveServiceObserver,
     33                 public net::NetworkChangeNotifier::ConnectionTypeObserver,
     34                 public base::NonThreadSafe,
     35                 public base::SupportsWeakPtr<APIUtil> {
     36  public:
     37   // The resulting status of EnsureTitleUniqueness.
     38   enum EnsureUniquenessStatus {
     39     NO_DUPLICATES_FOUND,
     40     RESOLVED_DUPLICATES,
     41   };
     42 
     43   typedef base::Callback<void(google_apis::GDataErrorCode,
     44                               EnsureUniquenessStatus status,
     45                               scoped_ptr<google_apis::ResourceEntry> entry)>
     46       EnsureUniquenessCallback;
     47 
     48   APIUtil(Profile* profile, const base::FilePath& temp_dir_path);
     49   virtual ~APIUtil();
     50 
     51   virtual void AddObserver(APIUtilObserver* observer) OVERRIDE;
     52   virtual void RemoveObserver(APIUtilObserver* observer) OVERRIDE;
     53 
     54   static scoped_ptr<APIUtil> CreateForTesting(
     55       const base::FilePath& temp_dir_path,
     56       scoped_ptr<drive::DriveServiceInterface> drive_service,
     57       scoped_ptr<drive::DriveUploaderInterface> drive_uploader);
     58 
     59   // APIUtilInterface overrides.
     60   virtual void GetDriveDirectoryForSyncRoot(const ResourceIdCallback& callback)
     61       OVERRIDE;
     62   virtual void GetDriveDirectoryForOrigin(
     63       const std::string& sync_root_resource_id,
     64       const GURL& origin,
     65       const ResourceIdCallback& callback) OVERRIDE;
     66   virtual void GetLargestChangeStamp(const ChangeStampCallback& callback)
     67       OVERRIDE;
     68   virtual void GetResourceEntry(const std::string& resource_id,
     69                                 const ResourceEntryCallback& callback) OVERRIDE;
     70   virtual void ListFiles(const std::string& directory_resource_id,
     71                          const ResourceListCallback& callback) OVERRIDE;
     72   virtual void ListChanges(int64 start_changestamp,
     73                            const ResourceListCallback& callback) OVERRIDE;
     74   virtual void ContinueListing(const GURL& feed_url,
     75                                const ResourceListCallback& callback) OVERRIDE;
     76   virtual void DownloadFile(const std::string& resource_id,
     77                             const std::string& local_file_md5,
     78                             const DownloadFileCallback& callback) OVERRIDE;
     79   virtual void UploadNewFile(const std::string& directory_resource_id,
     80                              const base::FilePath& local_file_path,
     81                              const std::string& title,
     82                              const UploadFileCallback& callback) OVERRIDE;
     83   virtual void UploadExistingFile(const std::string& resource_id,
     84                                   const std::string& remote_file_md5,
     85                                   const base::FilePath& local_file_path,
     86                                   const UploadFileCallback& callback) OVERRIDE;
     87   virtual void CreateDirectory(const std::string& parent_resource_id,
     88                                const std::string& title,
     89                                const ResourceIdCallback& callback) OVERRIDE;
     90   virtual bool IsAuthenticated() const OVERRIDE;
     91   virtual void DeleteFile(const std::string& resource_id,
     92                           const std::string& remote_file_md5,
     93                           const GDataErrorCallback& callback) OVERRIDE;
     94   virtual GURL ResourceIdToResourceLink(const std::string& resource_id) const
     95       OVERRIDE;
     96   virtual void EnsureSyncRootIsNotInMyDrive(
     97       const std::string& sync_root_resource_id) OVERRIDE;
     98 
     99   static std::string GetSyncRootDirectoryName();
    100   static std::string OriginToDirectoryTitle(const GURL& origin);
    101   static GURL DirectoryTitleToOrigin(const std::string& title);
    102 
    103   // DriveServiceObserver overrides.
    104   virtual void OnReadyToSendRequests() OVERRIDE;
    105 
    106   // ConnectionTypeObserver overrides.
    107   virtual void OnConnectionTypeChanged(
    108       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
    109 
    110  private:
    111   typedef int64 UploadKey;
    112   typedef std::map<UploadKey, UploadFileCallback> UploadCallbackMap;
    113 
    114   friend class APIUtilTest;
    115 
    116   // Constructor for test use.
    117   APIUtil(const base::FilePath& temp_dir_path,
    118           const GURL& base_url,
    119           const GURL& base_download_url,
    120           scoped_ptr<drive::DriveServiceInterface> drive_service,
    121           scoped_ptr<drive::DriveUploaderInterface> drive_uploader);
    122 
    123   void GetDriveRootResourceId(const GDataErrorCallback& callback);
    124   void DidGetDriveRootResourceId(
    125       const GDataErrorCallback& callback,
    126       google_apis::GDataErrorCode error,
    127       scoped_ptr<google_apis::AboutResource> about_resource);
    128 
    129   void DidGetDriveRootResourceIdForGetSyncRoot(
    130       const ResourceIdCallback& callback,
    131       google_apis::GDataErrorCode error);
    132 
    133   void DidGetDirectory(const std::string& parent_resource_id,
    134                        const std::string& directory_name,
    135                        const ResourceIdCallback& callback,
    136                        google_apis::GDataErrorCode error,
    137                        scoped_ptr<google_apis::ResourceList> feed);
    138 
    139   void DidCreateDirectory(const std::string& parent_resource_id,
    140                           const std::string& title,
    141                           const ResourceIdCallback& callback,
    142                           google_apis::GDataErrorCode error,
    143                           scoped_ptr<google_apis::ResourceEntry> entry);
    144 
    145   void DidEnsureUniquenessForCreateDirectory(
    146       const ResourceIdCallback& callback,
    147       google_apis::GDataErrorCode error,
    148       EnsureUniquenessStatus status,
    149       scoped_ptr<google_apis::ResourceEntry> entry);
    150 
    151   void SearchByTitle(const std::string& title,
    152                      const std::string& directory_resource_id,
    153                      const ResourceListCallback& callback);
    154 
    155   void DidGetLargestChangeStamp(
    156       const ChangeStampCallback& callback,
    157       google_apis::GDataErrorCode error,
    158       scoped_ptr<google_apis::AboutResource> about_resource);
    159 
    160   void DidGetDriveRootResourceIdForEnsureSyncRoot(
    161       const std::string& sync_root_resource_id,
    162       google_apis::GDataErrorCode error);
    163 
    164   void DidGetResourceList(const ResourceListCallback& callback,
    165                           google_apis::GDataErrorCode error,
    166                           scoped_ptr<google_apis::ResourceList> resource_list);
    167 
    168   void DidGetResourceEntry(const ResourceEntryCallback& callback,
    169                            google_apis::GDataErrorCode error,
    170                            scoped_ptr<google_apis::ResourceEntry> entry);
    171 
    172   void DidGetTemporaryFileForDownload(
    173       const std::string& resource_id,
    174       const std::string& local_file_md5,
    175       scoped_ptr<webkit_blob::ScopedFile> local_file,
    176       const DownloadFileCallback& callback,
    177       bool success);
    178 
    179   void DownloadFileInternal(const std::string& local_file_md5,
    180                             scoped_ptr<webkit_blob::ScopedFile> local_file,
    181                             const DownloadFileCallback& callback,
    182                             google_apis::GDataErrorCode error,
    183                             scoped_ptr<google_apis::ResourceEntry> entry);
    184 
    185   void DidDownloadFile(scoped_ptr<google_apis::ResourceEntry> entry,
    186                        scoped_ptr<webkit_blob::ScopedFile> local_file,
    187                        const DownloadFileCallback& callback,
    188                        google_apis::GDataErrorCode error,
    189                        const base::FilePath& downloaded_file_path);
    190 
    191   void DidUploadNewFile(const std::string& parent_resource_id,
    192                         const std::string& title,
    193                         UploadKey upload_key,
    194                         google_apis::GDataErrorCode error,
    195                         scoped_ptr<google_apis::ResourceEntry> entry);
    196 
    197   void DidEnsureUniquenessForCreateFile(
    198       const std::string& expected_resource_id,
    199       const UploadFileCallback& callback,
    200       google_apis::GDataErrorCode error,
    201       EnsureUniquenessStatus status,
    202       scoped_ptr<google_apis::ResourceEntry> entry);
    203 
    204   void UploadExistingFileInternal(const std::string& remote_file_md5,
    205                                   const base::FilePath& local_file_path,
    206                                   const UploadFileCallback& callback,
    207                                   google_apis::GDataErrorCode error,
    208                                   scoped_ptr<google_apis::ResourceEntry> entry);
    209 
    210   void DidUploadExistingFile(UploadKey upload_key,
    211                              google_apis::GDataErrorCode error,
    212                              scoped_ptr<google_apis::ResourceEntry> entry);
    213 
    214   void DeleteFileInternal(const std::string& remote_file_md5,
    215                           const GDataErrorCallback& callback,
    216                           google_apis::GDataErrorCode error,
    217                           scoped_ptr<google_apis::ResourceEntry> entry);
    218 
    219   void DidDeleteFile(const GDataErrorCallback& callback,
    220                      google_apis::GDataErrorCode error);
    221 
    222   void EnsureTitleUniqueness(const std::string& parent_resource_id,
    223                              const std::string& expected_title,
    224                              const EnsureUniquenessCallback& callback);
    225   void DidListEntriesToEnsureUniqueness(
    226       const std::string& parent_resource_id,
    227       const std::string& expected_title,
    228       const EnsureUniquenessCallback& callback,
    229       google_apis::GDataErrorCode error,
    230       scoped_ptr<google_apis::ResourceList> feed);
    231   void DeleteEntriesForEnsuringTitleUniqueness(
    232       ScopedVector<google_apis::ResourceEntry> entries,
    233       const GDataErrorCallback& callback);
    234   void DidDeleteEntriesForEnsuringTitleUniqueness(
    235       ScopedVector<google_apis::ResourceEntry> entries,
    236       const GDataErrorCallback& callback,
    237       google_apis::GDataErrorCode error);
    238 
    239   UploadKey RegisterUploadCallback(const UploadFileCallback& callback);
    240   UploadFileCallback GetAndUnregisterUploadCallback(UploadKey key);
    241   void CancelAllUploads(google_apis::GDataErrorCode error);
    242 
    243   std::string GetRootResourceId() const;
    244 
    245   scoped_ptr<drive::DriveServiceInterface> drive_service_;
    246   scoped_ptr<drive::DriveUploaderInterface> drive_uploader_;
    247 
    248   google_apis::GDataWapiUrlGenerator wapi_url_generator_;
    249   google_apis::DriveApiUrlGenerator drive_api_url_generator_;
    250 
    251   UploadCallbackMap upload_callback_map_;
    252   UploadKey upload_next_key_;
    253 
    254   base::FilePath temp_dir_path_;
    255 
    256   std::string root_resource_id_;
    257 
    258   ObserverList<APIUtilObserver> observers_;
    259 
    260   DISALLOW_COPY_AND_ASSIGN(APIUtil);
    261 };
    262 
    263 }  // namespace drive_backend
    264 }  // namespace sync_file_system
    265 
    266 #endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_API_UTIL_H_
    267