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.
5 #ifndef CHROME_BROWSER_DRIVE_DRIVE_SERVICE_INTERFACE_H_
6 #define CHROME_BROWSER_DRIVE_DRIVE_SERVICE_INTERFACE_H_
10 #include "base/time/time.h"
11 #include "google_apis/drive/auth_service_interface.h"
12 #include "google_apis/drive/base_requests.h"
13 #include "google_apis/drive/drive_api_requests.h"
14 #include "google_apis/drive/drive_common_callbacks.h"
22 // Observer interface for DriveServiceInterface.
23 class DriveServiceObserver
{
25 // Triggered when the service gets ready to send requests.
26 virtual void OnReadyToSendRequests() {}
28 // Called when the refresh token was found to be invalid.
29 virtual void OnRefreshTokenInvalid() {}
32 virtual ~DriveServiceObserver() {}
35 // This defines an interface for sharing by DriveService and MockDriveService
36 // so that we can do testing of clients of DriveService.
38 // All functions must be called on UI thread. DriveService is built on top of
39 // URLFetcher that runs on UI thread.
40 class DriveServiceInterface
{
42 // Optional parameters for AddNewDirectory().
43 struct AddNewDirectoryOptions
{
44 AddNewDirectoryOptions();
45 ~AddNewDirectoryOptions();
47 // modified_date of the directory.
48 // Pass the null Time if you are not interested in setting this property.
49 base::Time modified_date
;
51 // last_viewed_by_me_date of the directory.
52 // Pass the null Time if you are not interested in setting this property.
53 base::Time last_viewed_by_me_date
;
55 // List of properties for a new directory.
56 google_apis::drive::Properties properties
;
59 // Optional parameters for InitiateUploadNewFile() and
60 // MultipartUploadNewFile().
61 struct UploadNewFileOptions
{
62 UploadNewFileOptions();
63 ~UploadNewFileOptions();
65 // modified_date of the file.
66 // Pass the null Time if you are not interested in setting this property.
67 base::Time modified_date
;
69 // last_viewed_by_me_date of the file.
70 // Pass the null Time if you are not interested in setting this property.
71 base::Time last_viewed_by_me_date
;
73 // List of properties for a new file.
74 google_apis::drive::Properties properties
;
77 // Optional parameters for InitiateUploadExistingFile() and
78 // MultipartUploadExistingFile().
79 struct UploadExistingFileOptions
{
80 UploadExistingFileOptions();
81 ~UploadExistingFileOptions();
83 // Expected ETag of the file. UPLOAD_ERROR_CONFLICT error is generated when
85 // Pass the empty string to disable this behavior.
88 // New parent of the file.
89 // Pass the empty string to keep the property unchanged.
90 std::string parent_resource_id
;
92 // New title of the file.
93 // Pass the empty string to keep the property unchanged.
96 // New modified_date of the file.
97 // Pass the null Time if you are not interested in setting this property.
98 base::Time modified_date
;
100 // New last_viewed_by_me_date of the file.
101 // Pass the null Time if you are not interested in setting this property.
102 base::Time last_viewed_by_me_date
;
104 // List of new properties for an existing file (it will be merged with
105 // existing properties).
106 google_apis::drive::Properties properties
;
109 virtual ~DriveServiceInterface() {}
113 // Initializes the documents service with |account_id|.
114 virtual void Initialize(const std::string
& account_id
) = 0;
117 virtual void AddObserver(DriveServiceObserver
* observer
) = 0;
119 // Removes an observer.
120 virtual void RemoveObserver(DriveServiceObserver
* observer
) = 0;
122 // True if ready to send requests.
123 virtual bool CanSendRequest() const = 0;
125 // Authentication service:
127 // True if OAuth2 access token is retrieved and believed to be fresh.
128 virtual bool HasAccessToken() const = 0;
130 // Gets the cached OAuth2 access token or if empty, then fetches a new one.
131 virtual void RequestAccessToken(
132 const google_apis::AuthStatusCallback
& callback
) = 0;
134 // True if OAuth2 refresh token is present.
135 virtual bool HasRefreshToken() const = 0;
137 // Clears OAuth2 access token.
138 virtual void ClearAccessToken() = 0;
140 // Clears OAuth2 refresh token.
141 virtual void ClearRefreshToken() = 0;
145 // Returns the resource id for the root directory.
146 virtual std::string
GetRootResourceId() const = 0;
148 // Fetches a file list of the account. |callback| will be called upon
150 // If the list is too long, it may be paged. In such a case, a URL to fetch
151 // remaining results will be included in the returned result. See also
152 // GetRemainingFileList.
154 // |callback| must not be null.
155 virtual google_apis::CancelCallback
GetAllFileList(
156 const google_apis::FileListCallback
& callback
) = 0;
158 // Fetches a file list in the directory with |directory_resource_id|.
159 // |callback| will be called upon completion.
160 // If the list is too long, it may be paged. In such a case, a URL to fetch
161 // remaining results will be included in the returned result. See also
162 // GetRemainingFileList.
164 // |directory_resource_id| must not be empty.
165 // |callback| must not be null.
166 virtual google_apis::CancelCallback
GetFileListInDirectory(
167 const std::string
& directory_resource_id
,
168 const google_apis::FileListCallback
& callback
) = 0;
170 // Searches the resources for the |search_query| from all the user's
171 // resources. |callback| will be called upon completion.
172 // If the list is too long, it may be paged. In such a case, a URL to fetch
173 // remaining results will be included in the returned result. See also
174 // GetRemainingFileList.
176 // |search_query| must not be empty.
177 // |callback| must not be null.
178 virtual google_apis::CancelCallback
Search(
179 const std::string
& search_query
,
180 const google_apis::FileListCallback
& callback
) = 0;
182 // Searches the resources with the |title|.
183 // |directory_resource_id| is an optional parameter. If it is empty,
184 // the search target is all the existing resources. Otherwise, it is
185 // the resources directly under the directory with |directory_resource_id|.
186 // If the list is too long, it may be paged. In such a case, a URL to fetch
187 // remaining results will be included in the returned result. See also
188 // GetRemainingFileList.
190 // |title| must not be empty, and |callback| must not be null.
191 virtual google_apis::CancelCallback
SearchByTitle(
192 const std::string
& title
,
193 const std::string
& directory_resource_id
,
194 const google_apis::FileListCallback
& callback
) = 0;
196 // Fetches change list since |start_changestamp|. |callback| will be
197 // called upon completion.
198 // If the list is too long, it may be paged. In such a case, a URL to fetch
199 // remaining results will be included in the returned result. See also
200 // GetRemainingChangeList.
202 // |callback| must not be null.
203 virtual google_apis::CancelCallback
GetChangeList(
204 int64 start_changestamp
,
205 const google_apis::ChangeListCallback
& callback
) = 0;
207 // The result of GetChangeList() may be paged.
208 // In such a case, a next link to fetch remaining result is returned.
209 // The page token can be used for this method. |callback| will be called upon
212 // |next_link| must not be empty. |callback| must not be null.
213 virtual google_apis::CancelCallback
GetRemainingChangeList(
214 const GURL
& next_link
,
215 const google_apis::ChangeListCallback
& callback
) = 0;
217 // The result of GetAllFileList(), GetFileListInDirectory(), Search()
218 // and SearchByTitle() may be paged. In such a case, a next link to fetch
219 // remaining result is returned. The page token can be used for this method.
220 // |callback| will be called upon completion.
222 // |next_link| must not be empty. |callback| must not be null.
223 virtual google_apis::CancelCallback
GetRemainingFileList(
224 const GURL
& next_link
,
225 const google_apis::FileListCallback
& callback
) = 0;
227 // Fetches single entry metadata from server. The entry's file id equals
229 // Upon completion, invokes |callback| with results on the calling thread.
230 // |callback| must not be null.
231 virtual google_apis::CancelCallback
GetFileResource(
232 const std::string
& resource_id
,
233 const google_apis::FileResourceCallback
& callback
) = 0;
235 // Fetches an url for the sharing dialog for a single entry with id
236 // |resource_id|, to be embedded in a webview or an iframe with origin
237 // |embed_origin|. The url is returned via |callback| with results on the
238 // calling thread. |callback| must not be null.
239 virtual google_apis::CancelCallback
GetShareUrl(
240 const std::string
& resource_id
,
241 const GURL
& embed_origin
,
242 const google_apis::GetShareUrlCallback
& callback
) = 0;
244 // Gets the about resource information from the server.
245 // Upon completion, invokes |callback| with results on the calling thread.
246 // |callback| must not be null.
247 virtual google_apis::CancelCallback
GetAboutResource(
248 const google_apis::AboutResourceCallback
& callback
) = 0;
250 // Gets the application information from the server.
251 // Upon completion, invokes |callback| with results on the calling thread.
252 // |callback| must not be null.
253 virtual google_apis::CancelCallback
GetAppList(
254 const google_apis::AppListCallback
& callback
) = 0;
256 // Permanently deletes a resource identified by its |resource_id|.
257 // If |etag| is not empty and did not match, the deletion fails with
258 // HTTP_PRECONDITION error.
259 // Upon completion, invokes |callback| with results on the calling thread.
260 // |callback| must not be null.
261 virtual google_apis::CancelCallback
DeleteResource(
262 const std::string
& resource_id
,
263 const std::string
& etag
,
264 const google_apis::EntryActionCallback
& callback
) = 0;
266 // Trashes a resource identified by its |resource_id|.
267 // Upon completion, invokes |callback| with results on the calling thread.
268 // |callback| must not be null.
269 virtual google_apis::CancelCallback
TrashResource(
270 const std::string
& resource_id
,
271 const google_apis::EntryActionCallback
& callback
) = 0;
273 // Makes a copy of a resource with |resource_id|.
274 // The new resource will be put under a directory with |parent_resource_id|,
275 // and it'll be named |new_title|.
276 // If |last_modified| is not null, the modified date of the resource on the
277 // server will be set to the date.
278 // Upon completion, invokes |callback| with results on the calling thread.
279 // |callback| must not be null.
280 virtual google_apis::CancelCallback
CopyResource(
281 const std::string
& resource_id
,
282 const std::string
& parent_resource_id
,
283 const std::string
& new_title
,
284 const base::Time
& last_modified
,
285 const google_apis::FileResourceCallback
& callback
) = 0;
287 // Updates a resource with |resource_id| to the directory of
288 // |parent_resource_id| with renaming to |new_title|.
289 // If |last_modified| or |last_accessed| is not null, the modified/accessed
290 // date of the resource on the server will be set to the date.
291 // If |properties| are specified, then they will be set on |resource_id|.
292 // Upon completion, invokes |callback| with results on the calling thread.
293 // |callback| must not be null.
294 virtual google_apis::CancelCallback
UpdateResource(
295 const std::string
& resource_id
,
296 const std::string
& parent_resource_id
,
297 const std::string
& new_title
,
298 const base::Time
& last_modified
,
299 const base::Time
& last_viewed_by_me
,
300 const google_apis::drive::Properties
& properties
,
301 const google_apis::FileResourceCallback
& callback
) = 0;
303 // Adds a resource (document, file, or collection) identified by its
304 // |resource_id| to a collection represented by the |parent_resource_id|.
305 // Upon completion, invokes |callback| with results on the calling thread.
306 // |callback| must not be null.
307 virtual google_apis::CancelCallback
AddResourceToDirectory(
308 const std::string
& parent_resource_id
,
309 const std::string
& resource_id
,
310 const google_apis::EntryActionCallback
& callback
) = 0;
312 // Removes a resource (document, file, collection) identified by its
313 // |resource_id| from a collection represented by the |parent_resource_id|.
314 // Upon completion, invokes |callback| with results on the calling thread.
315 // |callback| must not be null.
316 virtual google_apis::CancelCallback
RemoveResourceFromDirectory(
317 const std::string
& parent_resource_id
,
318 const std::string
& resource_id
,
319 const google_apis::EntryActionCallback
& callback
) = 0;
321 // Adds new collection with |directory_title| under parent directory
322 // identified with |parent_resource_id|. |parent_resource_id| can be the
323 // value returned by GetRootResourceId to represent the root directory.
324 // Upon completion, invokes |callback| and passes newly created entry on
325 // the calling thread.
326 // This function cannot be named as "CreateDirectory" as it conflicts with
327 // a macro on Windows.
328 // |callback| must not be null.
329 virtual google_apis::CancelCallback
AddNewDirectory(
330 const std::string
& parent_resource_id
,
331 const std::string
& directory_title
,
332 const AddNewDirectoryOptions
& options
,
333 const google_apis::FileResourceCallback
& callback
) = 0;
335 // Downloads a file with |resourced_id|. The downloaded file will
336 // be stored at |local_cache_path| location. Upon completion, invokes
337 // |download_action_callback| with results on the calling thread.
338 // If |get_content_callback| is not empty,
339 // URLFetcherDelegate::OnURLFetchDownloadData will be called, which will in
340 // turn invoke |get_content_callback| on the calling thread.
341 // If |progress_callback| is not empty, it is invoked periodically when
342 // the download made some progress.
344 // |download_action_callback| must not be null.
345 // |get_content_callback| and |progress_callback| may be null.
346 virtual google_apis::CancelCallback
DownloadFile(
347 const base::FilePath
& local_cache_path
,
348 const std::string
& resource_id
,
349 const google_apis::DownloadActionCallback
& download_action_callback
,
350 const google_apis::GetContentCallback
& get_content_callback
,
351 const google_apis::ProgressCallback
& progress_callback
) = 0;
353 // Initiates uploading of a new document/file.
354 // |content_type| and |content_length| should be the ones of the file to be
356 // |callback| must not be null.
357 virtual google_apis::CancelCallback
InitiateUploadNewFile(
358 const std::string
& content_type
,
359 int64 content_length
,
360 const std::string
& parent_resource_id
,
361 const std::string
& title
,
362 const UploadNewFileOptions
& options
,
363 const google_apis::InitiateUploadCallback
& callback
) = 0;
365 // Initiates uploading of an existing document/file.
366 // |content_type| and |content_length| should be the ones of the file to be
368 // |callback| must not be null.
369 virtual google_apis::CancelCallback
InitiateUploadExistingFile(
370 const std::string
& content_type
,
371 int64 content_length
,
372 const std::string
& resource_id
,
373 const UploadExistingFileOptions
& options
,
374 const google_apis::InitiateUploadCallback
& callback
) = 0;
376 // Resumes uploading of a document/file on the calling thread.
377 // |callback| must not be null. |progress_callback| may be null.
378 virtual google_apis::CancelCallback
ResumeUpload(
379 const GURL
& upload_url
,
380 int64 start_position
,
382 int64 content_length
,
383 const std::string
& content_type
,
384 const base::FilePath
& local_file_path
,
385 const google_apis::drive::UploadRangeCallback
& callback
,
386 const google_apis::ProgressCallback
& progress_callback
) = 0;
388 // Gets the current status of the uploading to |upload_url| from the server.
389 // |drive_file_path| and |content_length| should be set to the same value
390 // which is used for ResumeUpload.
391 // |callback| must not be null.
392 virtual google_apis::CancelCallback
GetUploadStatus(
393 const GURL
& upload_url
,
394 int64 content_length
,
395 const google_apis::drive::UploadRangeCallback
& callback
) = 0;
397 // Uploads a file by a single request with multipart body. It's more efficient
398 // for small files than using |InitiateUploadNewFile| and |ResumeUpload|.
399 // |content_type| and |content_length| should be the ones of the file to be
400 // uploaded. |callback| must not be null. |progress_callback| may be null.
401 virtual google_apis::CancelCallback
MultipartUploadNewFile(
402 const std::string
& content_type
,
403 int64 content_length
,
404 const std::string
& parent_resource_id
,
405 const std::string
& title
,
406 const base::FilePath
& local_file_path
,
407 const UploadNewFileOptions
& options
,
408 const google_apis::FileResourceCallback
& callback
,
409 const google_apis::ProgressCallback
& progress_callback
) = 0;
411 // Uploads a file by a single request with multipart body. It's more efficient
412 // for small files than using |InitiateUploadExistingFile| and |ResumeUpload|.
413 // |content_type| and |content_length| should be the ones of the file to be
414 // uploaded. |callback| must not be null. |progress_callback| may be null.
415 virtual google_apis::CancelCallback
MultipartUploadExistingFile(
416 const std::string
& content_type
,
417 int64 content_length
,
418 const std::string
& resource_id
,
419 const base::FilePath
& local_file_path
,
420 const UploadExistingFileOptions
& options
,
421 const google_apis::FileResourceCallback
& callback
,
422 const google_apis::ProgressCallback
& progress_callback
) = 0;
424 // Authorizes a Drive app with the id |app_id| to open the given file.
425 // Upon completion, invokes |callback| with the link to open the file with
426 // the provided app. |callback| must not be null.
427 virtual google_apis::CancelCallback
AuthorizeApp(
428 const std::string
& resource_id
,
429 const std::string
& app_id
,
430 const google_apis::AuthorizeAppCallback
& callback
) = 0;
432 // Uninstalls a Drive app with the id |app_id|. |callback| must not be null.
433 virtual google_apis::CancelCallback
UninstallApp(
434 const std::string
& app_id
,
435 const google_apis::EntryActionCallback
& callback
) = 0;
437 // Authorizes the account |email| to access |resource_id| as a |role|.
438 // |callback| must not be null.
439 virtual google_apis::CancelCallback
AddPermission(
440 const std::string
& resource_id
,
441 const std::string
& email
,
442 google_apis::drive::PermissionRole role
,
443 const google_apis::EntryActionCallback
& callback
) = 0;
448 #endif // CHROME_BROWSER_DRIVE_DRIVE_SERVICE_INTERFACE_H_