drive_service_interface.h revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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.
4
5#ifndef CHROME_BROWSER_DRIVE_DRIVE_SERVICE_INTERFACE_H_
6#define CHROME_BROWSER_DRIVE_DRIVE_SERVICE_INTERFACE_H_
7
8#include <string>
9
10#include "chrome/browser/google_apis/auth_service_interface.h"
11#include "chrome/browser/google_apis/base_requests.h"
12#include "chrome/browser/google_apis/drive_common_callbacks.h"
13
14class Profile;
15
16namespace drive {
17
18// Observer interface for DriveServiceInterface.
19class DriveServiceObserver {
20 public:
21  // Triggered when the service gets ready to send requests.
22  virtual void OnReadyToSendRequests() {}
23
24  // Called when the refresh token was found to be invalid.
25  virtual void OnRefreshTokenInvalid() {}
26
27 protected:
28  virtual ~DriveServiceObserver() {}
29};
30
31// This defines an interface for sharing by DriveService and MockDriveService
32// so that we can do testing of clients of DriveService.
33//
34// All functions must be called on UI thread. DriveService is built on top of
35// URLFetcher that runs on UI thread.
36class DriveServiceInterface {
37 public:
38  virtual ~DriveServiceInterface() {}
39
40  // Common service:
41
42  // Initializes the documents service tied with |profile|.
43  virtual void Initialize(Profile* profile) = 0;
44
45  // Adds an observer.
46  virtual void AddObserver(DriveServiceObserver* observer) = 0;
47
48  // Removes an observer.
49  virtual void RemoveObserver(DriveServiceObserver* observer) = 0;
50
51  // True if ready to send requests.
52  virtual bool CanSendRequest() const = 0;
53
54  // Converts the given resource ID into the desired format.
55  virtual std::string CanonicalizeResourceId(
56      const std::string& resource_id) const = 0;
57
58  // Authentication service:
59
60  // True if OAuth2 access token is retrieved and believed to be fresh.
61  virtual bool HasAccessToken() const = 0;
62
63  // Gets the cached OAuth2 access token or if empty, then fetches a new one.
64  virtual void RequestAccessToken(
65      const google_apis::AuthStatusCallback& callback) = 0;
66
67  // True if OAuth2 refresh token is present.
68  virtual bool HasRefreshToken() const = 0;
69
70  // Clears OAuth2 access token.
71  virtual void ClearAccessToken() = 0;
72
73  // Clears OAuth2 refresh token.
74  virtual void ClearRefreshToken() = 0;
75
76  // Document access:
77
78  // Returns the resource id for the root directory.
79  virtual std::string GetRootResourceId() const = 0;
80
81  // Fetches a resource list of the account. |callback| will be called upon
82  // completion.
83  // If the list is too long, it may be paged. In such a case, a URL to fetch
84  // remaining results will be included in the returned result. See also
85  // ContinueGetResourceList.
86  //
87  // |callback| must not be null.
88  virtual google_apis::CancelCallback GetAllResourceList(
89      const google_apis::GetResourceListCallback& callback) = 0;
90
91  // Fetches a resource list in the directory with |directory_resource_id|.
92  // |callback| will be called upon completion.
93  // If the list is too long, it may be paged. In such a case, a URL to fetch
94  // remaining results will be included in the returned result. See also
95  // ContinueGetResourceList.
96  //
97  // |directory_resource_id| must not be empty.
98  // |callback| must not be null.
99  virtual google_apis::CancelCallback GetResourceListInDirectory(
100      const std::string& directory_resource_id,
101      const google_apis::GetResourceListCallback& callback) = 0;
102
103  // Searches the resources for the |search_query| from all the user's
104  // resources. |callback| will be called upon completion.
105  // If the list is too long, it may be paged. In such a case, a URL to fetch
106  // remaining results will be included in the returned result. See also
107  // ContinueGetResourceList.
108  //
109  // |search_query| must not be empty.
110  // |callback| must not be null.
111  virtual google_apis::CancelCallback Search(
112      const std::string& search_query,
113      const google_apis::GetResourceListCallback& callback) = 0;
114
115  // Searches the resources with the |title|.
116  // |directory_resource_id| is an optional parameter. If it is empty,
117  // the search target is all the existing resources. Otherwise, it is
118  // the resources directly under the directory with |directory_resource_id|.
119  // If the list is too long, it may be paged. In such a case, a URL to fetch
120  // remaining results will be included in the returned result. See also
121  // ContinueGetResourceList.
122  //
123  // |title| must not be empty, and |callback| must not be null.
124  virtual google_apis::CancelCallback SearchByTitle(
125      const std::string& title,
126      const std::string& directory_resource_id,
127      const google_apis::GetResourceListCallback& callback) = 0;
128
129  // Fetches change list since |start_changestamp|. |callback| will be
130  // called upon completion.
131  // If the list is too long, it may be paged. In such a case, a URL to fetch
132  // remaining results will be included in the returned result. See also
133  // ContinueGetResourceList.
134  //
135  // |callback| must not be null.
136  virtual google_apis::CancelCallback GetChangeList(
137      int64 start_changestamp,
138      const google_apis::GetResourceListCallback& callback) = 0;
139
140  // Requests returning GetResourceList may be paged. In such a case,
141  // a URL to fetch remaining result is returned. The URL can be used for this
142  // method. |callback| will be called upon completion.
143  //
144  // |override_url| must not be empty.
145  // |callback| must not be null.
146  virtual google_apis::CancelCallback ContinueGetResourceList(
147      const GURL& override_url,
148      const google_apis::GetResourceListCallback& callback) = 0;
149
150  // Fetches single entry metadata from server. The entry's resource id equals
151  // |resource_id|.
152  // Upon completion, invokes |callback| with results on the calling thread.
153  // |callback| must not be null.
154  virtual google_apis::CancelCallback GetResourceEntry(
155      const std::string& resource_id,
156      const google_apis::GetResourceEntryCallback& callback) = 0;
157
158  // Gets the about resource information from the server.
159  // Upon completion, invokes |callback| with results on the calling thread.
160  // |callback| must not be null.
161  virtual google_apis::CancelCallback GetAboutResource(
162      const google_apis::GetAboutResourceCallback& callback) = 0;
163
164  // Gets the application information from the server.
165  // Upon completion, invokes |callback| with results on the calling thread.
166  // |callback| must not be null.
167  virtual google_apis::CancelCallback GetAppList(
168      const google_apis::GetAppListCallback& callback) = 0;
169
170  // Deletes a resource identified by its |resource_id|.
171  // If |etag| is not empty and did not match, the deletion fails with
172  // HTTP_PRECONDITION error.
173  // Upon completion, invokes |callback| with results on the calling thread.
174  // |callback| must not be null.
175  virtual google_apis::CancelCallback DeleteResource(
176      const std::string& resource_id,
177      const std::string& etag,
178      const google_apis::EntryActionCallback& callback) = 0;
179
180  // Makes a copy of a resource with |resource_id|.
181  // The new resource will be put under a directory with |parent_resource_id|,
182  // and it'll be named |new_title|.
183  // This request is supported only on DriveAPIService, because GData WAPI
184  // doesn't support the function unfortunately.
185  // Upon completion, invokes |callback| with results on the calling thread.
186  // |callback| must not be null.
187  virtual google_apis::CancelCallback CopyResource(
188      const std::string& resource_id,
189      const std::string& parent_resource_id,
190      const std::string& new_title,
191      const google_apis::GetResourceEntryCallback& callback) = 0;
192
193  // Makes a copy of a hosted document identified by its |resource_id|.
194  // The copy is named as the UTF-8 encoded |new_title| and is not added to any
195  // collection. Use AddResourceToDirectory() to add the copy to a collection
196  // when needed. Upon completion, invokes |callback| with results on the
197  // calling thread.
198  // |callback| must not be null.
199  // TODO(hidehiko): After the migration to Drive API v2, remove this method,
200  // because we can use CopyResource instead.
201  virtual google_apis::CancelCallback CopyHostedDocument(
202      const std::string& resource_id,
203      const std::string& new_title,
204      const google_apis::GetResourceEntryCallback& callback) = 0;
205
206  // Renames a document or collection identified by its |resource_id|
207  // to the UTF-8 encoded |new_title|. Upon completion,
208  // invokes |callback| with results on the calling thread.
209  // |callback| must not be null.
210  virtual google_apis::CancelCallback RenameResource(
211      const std::string& resource_id,
212      const std::string& new_title,
213      const google_apis::EntryActionCallback& callback) = 0;
214
215  // Touches the resource with |resource_id|.
216  // Its modifiedDate and lastViewedByMeDate fields on the server will be
217  // updated to |modified_date| and |last_viewed_by_me_date| respectively.
218  // Upon completion, invokes |callback| with the updated resource data.
219  // |modified_date|, |last_viewed_by_me_date| and |callback| must not be null.
220  virtual google_apis::CancelCallback TouchResource(
221      const std::string& resource_id,
222      const base::Time& modified_date,
223      const base::Time& last_viewed_by_me_date,
224      const google_apis::GetResourceEntryCallback& callback) = 0;
225
226  // Adds a resource (document, file, or collection) identified by its
227  // |resource_id| to a collection represented by the |parent_resource_id|.
228  // Upon completion, invokes |callback| with results on the calling thread.
229  // |callback| must not be null.
230  virtual google_apis::CancelCallback AddResourceToDirectory(
231      const std::string& parent_resource_id,
232      const std::string& resource_id,
233      const google_apis::EntryActionCallback& callback) = 0;
234
235  // Removes a resource (document, file, collection) identified by its
236  // |resource_id| from a collection represented by the |parent_resource_id|.
237  // Upon completion, invokes |callback| with results on the calling thread.
238  // |callback| must not be null.
239  virtual google_apis::CancelCallback RemoveResourceFromDirectory(
240      const std::string& parent_resource_id,
241      const std::string& resource_id,
242      const google_apis::EntryActionCallback& callback) = 0;
243
244  // Adds new collection with |directory_title| under parent directory
245  // identified with |parent_resource_id|. |parent_resource_id| can be the
246  // value returned by GetRootResourceId to represent the root directory.
247  // Upon completion, invokes |callback| and passes newly created entry on
248  // the calling thread.
249  // This function cannot be named as "CreateDirectory" as it conflicts with
250  // a macro on Windows.
251  // |callback| must not be null.
252  virtual google_apis::CancelCallback AddNewDirectory(
253      const std::string& parent_resource_id,
254      const std::string& directory_title,
255      const google_apis::GetResourceEntryCallback& callback) = 0;
256
257  // Downloads a file with |resourced_id|. The downloaded file will
258  // be stored at |local_cache_path| location. Upon completion, invokes
259  // |download_action_callback| with results on the calling thread.
260  // If |get_content_callback| is not empty,
261  // URLFetcherDelegate::OnURLFetchDownloadData will be called, which will in
262  // turn invoke |get_content_callback| on the calling thread.
263  // If |progress_callback| is not empty, it is invoked periodically when
264  // the download made some progress.
265  //
266  // |download_action_callback| must not be null.
267  // |get_content_callback| and |progress_callback| may be null.
268  virtual google_apis::CancelCallback DownloadFile(
269      const base::FilePath& local_cache_path,
270      const std::string& resource_id,
271      const google_apis::DownloadActionCallback& download_action_callback,
272      const google_apis::GetContentCallback& get_content_callback,
273      const google_apis::ProgressCallback& progress_callback) = 0;
274
275  // Initiates uploading of a new document/file.
276  // |content_type| and |content_length| should be the ones of the file to be
277  // uploaded.
278  // |callback| must not be null.
279  virtual google_apis::CancelCallback InitiateUploadNewFile(
280      const std::string& content_type,
281      int64 content_length,
282      const std::string& parent_resource_id,
283      const std::string& title,
284      const google_apis::InitiateUploadCallback& callback) = 0;
285
286  // Initiates uploading of an existing document/file.
287  // |content_type| and |content_length| should be the ones of the file to be
288  // uploaded.
289  // |callback| must not be null.
290  virtual google_apis::CancelCallback InitiateUploadExistingFile(
291      const std::string& content_type,
292      int64 content_length,
293      const std::string& resource_id,
294      const std::string& etag,
295      const google_apis::InitiateUploadCallback& callback) = 0;
296
297  // Resumes uploading of a document/file on the calling thread.
298  // |callback| must not be null. |progress_callback| may be null.
299  virtual google_apis::CancelCallback ResumeUpload(
300      const GURL& upload_url,
301      int64 start_position,
302      int64 end_position,
303      int64 content_length,
304      const std::string& content_type,
305      const base::FilePath& local_file_path,
306      const google_apis::UploadRangeCallback& callback,
307      const google_apis::ProgressCallback& progress_callback) = 0;
308
309  // Gets the current status of the uploading to |upload_url| from the server.
310  // |drive_file_path| and |content_length| should be set to the same value
311  // which is used for ResumeUpload.
312  // |callback| must not be null.
313  virtual google_apis::CancelCallback GetUploadStatus(
314      const GURL& upload_url,
315      int64 content_length,
316      const google_apis::UploadRangeCallback& callback) = 0;
317
318  // Authorizes a Drive app with the id |app_id| to open the given file.
319  // Upon completion, invokes |callback| with the link to open the file with
320  // the provided app. |callback| must not be null.
321  virtual google_apis::CancelCallback AuthorizeApp(
322      const std::string& resource_id,
323      const std::string& app_id,
324      const google_apis::AuthorizeAppCallback& callback) = 0;
325};
326
327}  // namespace drive
328
329#endif  // CHROME_BROWSER_DRIVE_DRIVE_SERVICE_INTERFACE_H_
330