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