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.
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_
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_v1/api_util_interface.h"
17 #include "net/base/network_change_notifier.h"
18 #include "webkit/common/blob/scoped_file.h"
23 namespace drive { class DriveUploaderInterface; }
25 namespace sync_file_system {
26 namespace drive_backend {
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> {
37 // The resulting status of EnsureTitleUniqueness.
38 enum EnsureUniquenessStatus {
43 typedef base::Callback<void(google_apis::GDataErrorCode,
44 EnsureUniquenessStatus status,
45 scoped_ptr<google_apis::ResourceEntry> entry)>
46 EnsureUniquenessCallback;
48 APIUtil(Profile* profile, const base::FilePath& temp_dir_path);
51 virtual void AddObserver(APIUtilObserver* observer) OVERRIDE;
52 virtual void RemoveObserver(APIUtilObserver* observer) OVERRIDE;
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);
59 // APIUtilInterface overrides.
60 virtual void GetDriveDirectoryForSyncRoot(const ResourceIdCallback& callback)
62 virtual void GetDriveDirectoryForOrigin(
63 const std::string& sync_root_resource_id,
65 const ResourceIdCallback& callback) OVERRIDE;
66 virtual void GetLargestChangeStamp(const ChangeStampCallback& callback)
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& next_link,
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
96 virtual void EnsureSyncRootIsNotInMyDrive(
97 const std::string& sync_root_resource_id) OVERRIDE;
99 static std::string GetSyncRootDirectoryName();
100 static std::string OriginToDirectoryTitle(const GURL& origin);
101 static GURL DirectoryTitleToOrigin(const std::string& title);
103 // DriveServiceObserver overrides.
104 virtual void OnReadyToSendRequests() OVERRIDE;
106 // ConnectionTypeObserver overrides.
107 virtual void OnConnectionTypeChanged(
108 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
111 typedef int64 UploadKey;
112 typedef std::map<UploadKey, UploadFileCallback> UploadCallbackMap;
114 friend class APIUtilTest;
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 const std::string& account_id);
124 void GetDriveRootResourceId(const GDataErrorCallback& callback);
125 void DidGetDriveRootResourceId(
126 const GDataErrorCallback& callback,
127 google_apis::GDataErrorCode error,
128 scoped_ptr<google_apis::AboutResource> about_resource);
130 void DidGetDriveRootResourceIdForGetSyncRoot(
131 const ResourceIdCallback& callback,
132 google_apis::GDataErrorCode error);
134 void DidGetDirectory(const std::string& parent_resource_id,
135 const std::string& directory_name,
136 const ResourceIdCallback& callback,
137 google_apis::GDataErrorCode error,
138 scoped_ptr<google_apis::ResourceList> feed);
140 void DidCreateDirectory(const std::string& parent_resource_id,
141 const std::string& title,
142 const ResourceIdCallback& callback,
143 google_apis::GDataErrorCode error,
144 scoped_ptr<google_apis::ResourceEntry> entry);
146 void DidEnsureUniquenessForCreateDirectory(
147 const ResourceIdCallback& callback,
148 google_apis::GDataErrorCode error,
149 EnsureUniquenessStatus status,
150 scoped_ptr<google_apis::ResourceEntry> entry);
152 void SearchByTitle(const std::string& title,
153 const std::string& directory_resource_id,
154 const ResourceListCallback& callback);
156 void DidGetLargestChangeStamp(
157 const ChangeStampCallback& callback,
158 google_apis::GDataErrorCode error,
159 scoped_ptr<google_apis::AboutResource> about_resource);
161 void DidGetDriveRootResourceIdForEnsureSyncRoot(
162 const std::string& sync_root_resource_id,
163 google_apis::GDataErrorCode error);
165 void DidGetResourceList(const ResourceListCallback& callback,
166 google_apis::GDataErrorCode error,
167 scoped_ptr<google_apis::ResourceList> resource_list);
169 void DidGetResourceEntry(const ResourceEntryCallback& callback,
170 google_apis::GDataErrorCode error,
171 scoped_ptr<google_apis::ResourceEntry> entry);
173 void DidGetTemporaryFileForDownload(
174 const std::string& resource_id,
175 const std::string& local_file_md5,
176 scoped_ptr<webkit_blob::ScopedFile> local_file,
177 const DownloadFileCallback& callback,
180 void DownloadFileInternal(const std::string& local_file_md5,
181 scoped_ptr<webkit_blob::ScopedFile> local_file,
182 const DownloadFileCallback& callback,
183 google_apis::GDataErrorCode error,
184 scoped_ptr<google_apis::ResourceEntry> entry);
186 void DidDownloadFile(scoped_ptr<google_apis::ResourceEntry> entry,
187 scoped_ptr<webkit_blob::ScopedFile> local_file,
188 const DownloadFileCallback& callback,
189 google_apis::GDataErrorCode error,
190 const base::FilePath& downloaded_file_path);
192 void DidUploadNewFile(const std::string& parent_resource_id,
193 const std::string& title,
194 UploadKey upload_key,
195 google_apis::GDataErrorCode error,
196 scoped_ptr<google_apis::ResourceEntry> entry);
198 void DidEnsureUniquenessForCreateFile(
199 const std::string& expected_resource_id,
200 const UploadFileCallback& callback,
201 google_apis::GDataErrorCode error,
202 EnsureUniquenessStatus status,
203 scoped_ptr<google_apis::ResourceEntry> entry);
205 void UploadExistingFileInternal(const std::string& remote_file_md5,
206 const base::FilePath& local_file_path,
207 const UploadFileCallback& callback,
208 google_apis::GDataErrorCode error,
209 scoped_ptr<google_apis::ResourceEntry> entry);
211 void DidUploadExistingFile(UploadKey upload_key,
212 google_apis::GDataErrorCode error,
213 scoped_ptr<google_apis::ResourceEntry> entry);
215 void DeleteFileInternal(const std::string& remote_file_md5,
216 const GDataErrorCallback& callback,
217 google_apis::GDataErrorCode error,
218 scoped_ptr<google_apis::ResourceEntry> entry);
220 void DidDeleteFile(const GDataErrorCallback& callback,
221 google_apis::GDataErrorCode error);
223 void EnsureTitleUniqueness(const std::string& parent_resource_id,
224 const std::string& expected_title,
225 const EnsureUniquenessCallback& callback);
226 void DidListEntriesToEnsureUniqueness(
227 const std::string& parent_resource_id,
228 const std::string& expected_title,
229 const EnsureUniquenessCallback& callback,
230 google_apis::GDataErrorCode error,
231 scoped_ptr<google_apis::ResourceList> feed);
232 void DeleteEntriesForEnsuringTitleUniqueness(
233 ScopedVector<google_apis::ResourceEntry> entries,
234 const GDataErrorCallback& callback);
235 void DidDeleteEntriesForEnsuringTitleUniqueness(
236 ScopedVector<google_apis::ResourceEntry> entries,
237 const GDataErrorCallback& callback,
238 google_apis::GDataErrorCode error);
240 UploadKey RegisterUploadCallback(const UploadFileCallback& callback);
241 UploadFileCallback GetAndUnregisterUploadCallback(UploadKey key);
242 void CancelAllUploads(google_apis::GDataErrorCode error);
244 std::string GetRootResourceId() const;
246 scoped_ptr<drive::DriveServiceInterface> drive_service_;
247 scoped_ptr<drive::DriveUploaderInterface> drive_uploader_;
249 google_apis::GDataWapiUrlGenerator wapi_url_generator_;
250 google_apis::DriveApiUrlGenerator drive_api_url_generator_;
252 UploadCallbackMap upload_callback_map_;
253 UploadKey upload_next_key_;
255 base::FilePath temp_dir_path_;
257 std::string root_resource_id_;
259 ObserverList<APIUtilObserver> observers_;
261 DISALLOW_COPY_AND_ASSIGN(APIUtil);
264 } // namespace drive_backend
265 } // namespace sync_file_system
267 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_V1_API_UTIL_H_