NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / drive / fake_drive_service.h
blob1ddf0945b85f6f73d0aecefa7c68cc625ad3f94c
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_FAKE_DRIVE_SERVICE_H_
6 #define CHROME_BROWSER_DRIVE_FAKE_DRIVE_SERVICE_H_
8 #include "base/files/file_path.h"
9 #include "base/values.h"
10 #include "chrome/browser/drive/drive_service_interface.h"
11 #include "google_apis/drive/auth_service_interface.h"
13 namespace google_apis {
14 class AboutResource;
15 class ChangeResource;
16 class FileResource;
19 namespace drive {
21 // This class implements a fake DriveService which acts like a real Drive
22 // service. The fake service works as follows:
24 // 1) Load JSON files and construct the in-memory resource list.
25 // 2) Return valid responses based on the the in-memory resource list.
26 // 3) Update the in-memory resource list by requests like DeleteResource().
27 class FakeDriveService : public DriveServiceInterface {
28 public:
29 FakeDriveService();
30 virtual ~FakeDriveService();
32 // Loads the resource list for WAPI. Returns true on success.
33 bool LoadResourceListForWapi(const std::string& relative_path);
35 // Loads the account metadata for WAPI. Returns true on success. Also adds
36 // the largest changestamp in the account metadata to the existing
37 // entries. The changestamp information will be used to generate change
38 // lists in GetResourceList() when non-zero |start_changestamp| is
39 // specified.
40 bool LoadAccountMetadataForWapi(const std::string& relative_path);
42 // Loads the app list for Drive API. Returns true on success.
43 bool LoadAppListForDriveApi(const std::string& relative_path);
45 // Changes the offline state. All functions fail with GDATA_NO_CONNECTION
46 // when offline. By default the offline state is false.
47 void set_offline(bool offline) { offline_ = offline; }
49 // GetAllResourceList never returns result when this is set to true.
50 // Used to emulate the real server's slowness.
51 void set_never_return_all_resource_list(bool value) {
52 never_return_all_resource_list_ = value;
55 // Changes the default max results returned from GetResourceList().
56 // By default, it's set to 0, which is unlimited.
57 void set_default_max_results(int default_max_results) {
58 default_max_results_ = default_max_results;
61 // Sets the url to the test server to be used as a base for generated share
62 // urls to the share dialog.
63 void set_share_url_base(const GURL& share_url_base) {
64 share_url_base_ = share_url_base;
67 // Changes the quota fields returned from GetAboutResource().
68 void SetQuotaValue(int64 used, int64 total);
70 // Returns the AboutResource.
71 const google_apis::AboutResource& about_resource() const {
72 return *about_resource_;
75 // Returns the number of times the resource list is successfully loaded by
76 // GetAllResourceList().
77 int resource_list_load_count() const { return resource_list_load_count_; }
79 // Returns the number of times the resource list is successfully loaded by
80 // GetChangeList().
81 int change_list_load_count() const { return change_list_load_count_; }
83 // Returns the number of times the resource list is successfully loaded by
84 // GetResourceListInDirectory().
85 int directory_load_count() const { return directory_load_count_; }
87 // Returns the number of times the about resource is successfully loaded
88 // by GetAboutResource().
89 int about_resource_load_count() const {
90 return about_resource_load_count_;
93 // Returns the number of times the app list is successfully loaded by
94 // GetAppList().
95 int app_list_load_count() const { return app_list_load_count_; }
97 // Returns the number of times GetAllResourceList are blocked due to
98 // set_never_return_all_resource_list().
99 int blocked_resource_list_load_count() const {
100 return blocked_resource_list_load_count_;
103 // Returns the file path whose request is cancelled just before this method
104 // invocation.
105 const base::FilePath& last_cancelled_file() const {
106 return last_cancelled_file_;
109 // Returns the (fake) URL for the link.
110 static GURL GetFakeLinkUrl(const std::string& resource_id);
112 // DriveServiceInterface Overrides
113 virtual void Initialize(const std::string& account_id) OVERRIDE;
114 virtual void AddObserver(DriveServiceObserver* observer) OVERRIDE;
115 virtual void RemoveObserver(DriveServiceObserver* observer) OVERRIDE;
116 virtual bool CanSendRequest() const OVERRIDE;
117 virtual ResourceIdCanonicalizer GetResourceIdCanonicalizer() const OVERRIDE;
118 virtual std::string GetRootResourceId() const OVERRIDE;
119 virtual bool HasAccessToken() const OVERRIDE;
120 virtual void RequestAccessToken(
121 const google_apis::AuthStatusCallback& callback) OVERRIDE;
122 virtual bool HasRefreshToken() const OVERRIDE;
123 virtual void ClearAccessToken() OVERRIDE;
124 virtual void ClearRefreshToken() OVERRIDE;
125 virtual google_apis::CancelCallback GetAllResourceList(
126 const google_apis::GetResourceListCallback& callback) OVERRIDE;
127 virtual google_apis::CancelCallback GetResourceListInDirectory(
128 const std::string& directory_resource_id,
129 const google_apis::GetResourceListCallback& callback) OVERRIDE;
130 // See the comment for EntryMatchWidthQuery() in .cc file for details about
131 // the supported search query types.
132 virtual google_apis::CancelCallback Search(
133 const std::string& search_query,
134 const google_apis::GetResourceListCallback& callback) OVERRIDE;
135 virtual google_apis::CancelCallback SearchByTitle(
136 const std::string& title,
137 const std::string& directory_resource_id,
138 const google_apis::GetResourceListCallback& callback) OVERRIDE;
139 virtual google_apis::CancelCallback GetChangeList(
140 int64 start_changestamp,
141 const google_apis::GetResourceListCallback& callback) OVERRIDE;
142 virtual google_apis::CancelCallback GetRemainingChangeList(
143 const GURL& next_link,
144 const google_apis::GetResourceListCallback& callback) OVERRIDE;
145 virtual google_apis::CancelCallback GetRemainingFileList(
146 const GURL& next_link,
147 const google_apis::GetResourceListCallback& callback) OVERRIDE;
148 virtual google_apis::CancelCallback GetResourceEntry(
149 const std::string& resource_id,
150 const google_apis::GetResourceEntryCallback& callback) OVERRIDE;
151 virtual google_apis::CancelCallback GetShareUrl(
152 const std::string& resource_id,
153 const GURL& embed_origin,
154 const google_apis::GetShareUrlCallback& callback) OVERRIDE;
155 virtual google_apis::CancelCallback GetAboutResource(
156 const google_apis::AboutResourceCallback& callback) OVERRIDE;
157 virtual google_apis::CancelCallback GetAppList(
158 const google_apis::AppListCallback& callback) OVERRIDE;
159 virtual google_apis::CancelCallback DeleteResource(
160 const std::string& resource_id,
161 const std::string& etag,
162 const google_apis::EntryActionCallback& callback) OVERRIDE;
163 virtual google_apis::CancelCallback TrashResource(
164 const std::string& resource_id,
165 const google_apis::EntryActionCallback& callback) OVERRIDE;
166 virtual google_apis::CancelCallback DownloadFile(
167 const base::FilePath& local_cache_path,
168 const std::string& resource_id,
169 const google_apis::DownloadActionCallback& download_action_callback,
170 const google_apis::GetContentCallback& get_content_callback,
171 const google_apis::ProgressCallback& progress_callback) OVERRIDE;
172 virtual google_apis::CancelCallback CopyResource(
173 const std::string& resource_id,
174 const std::string& parent_resource_id,
175 const std::string& new_title,
176 const base::Time& last_modified,
177 const google_apis::GetResourceEntryCallback& callback) OVERRIDE;
178 virtual google_apis::CancelCallback UpdateResource(
179 const std::string& resource_id,
180 const std::string& parent_resource_id,
181 const std::string& new_title,
182 const base::Time& last_modified,
183 const base::Time& last_viewed_by_me,
184 const google_apis::GetResourceEntryCallback& callback) OVERRIDE;
185 virtual google_apis::CancelCallback RenameResource(
186 const std::string& resource_id,
187 const std::string& new_title,
188 const google_apis::EntryActionCallback& callback) OVERRIDE;
189 virtual google_apis::CancelCallback AddResourceToDirectory(
190 const std::string& parent_resource_id,
191 const std::string& resource_id,
192 const google_apis::EntryActionCallback& callback) OVERRIDE;
193 virtual google_apis::CancelCallback RemoveResourceFromDirectory(
194 const std::string& parent_resource_id,
195 const std::string& resource_id,
196 const google_apis::EntryActionCallback& callback) OVERRIDE;
197 virtual google_apis::CancelCallback AddNewDirectory(
198 const std::string& parent_resource_id,
199 const std::string& directory_title,
200 const AddNewDirectoryOptions& options,
201 const google_apis::GetResourceEntryCallback& callback) OVERRIDE;
202 virtual google_apis::CancelCallback InitiateUploadNewFile(
203 const std::string& content_type,
204 int64 content_length,
205 const std::string& parent_resource_id,
206 const std::string& title,
207 const InitiateUploadNewFileOptions& options,
208 const google_apis::InitiateUploadCallback& callback) OVERRIDE;
209 virtual google_apis::CancelCallback InitiateUploadExistingFile(
210 const std::string& content_type,
211 int64 content_length,
212 const std::string& resource_id,
213 const InitiateUploadExistingFileOptions& options,
214 const google_apis::InitiateUploadCallback& callback) OVERRIDE;
215 virtual google_apis::CancelCallback ResumeUpload(
216 const GURL& upload_url,
217 int64 start_position,
218 int64 end_position,
219 int64 content_length,
220 const std::string& content_type,
221 const base::FilePath& local_file_path,
222 const google_apis::UploadRangeCallback& callback,
223 const google_apis::ProgressCallback& progress_callback) OVERRIDE;
224 virtual google_apis::CancelCallback GetUploadStatus(
225 const GURL& upload_url,
226 int64 content_length,
227 const google_apis::UploadRangeCallback& callback) OVERRIDE;
228 virtual google_apis::CancelCallback AuthorizeApp(
229 const std::string& resource_id,
230 const std::string& app_id,
231 const google_apis::AuthorizeAppCallback& callback) OVERRIDE;
232 virtual google_apis::CancelCallback UninstallApp(
233 const std::string& app_id,
234 const google_apis::EntryActionCallback& callback) OVERRIDE;
235 virtual google_apis::CancelCallback GetResourceListInDirectoryByWapi(
236 const std::string& directory_resource_id,
237 const google_apis::GetResourceListCallback& callback) OVERRIDE;
238 virtual google_apis::CancelCallback GetRemainingResourceList(
239 const GURL& next_link,
240 const google_apis::GetResourceListCallback& callback) OVERRIDE;
242 // Adds a new file with the given parameters. On success, returns
243 // HTTP_CREATED with the parsed entry.
244 // |callback| must not be null.
245 void AddNewFile(const std::string& content_type,
246 const std::string& content_data,
247 const std::string& parent_resource_id,
248 const std::string& title,
249 bool shared_with_me,
250 const google_apis::GetResourceEntryCallback& callback);
252 // Sets the last modified time for an entry specified by |resource_id|.
253 // On success, returns HTTP_SUCCESS with the parsed entry.
254 // |callback| must not be null.
255 void SetLastModifiedTime(
256 const std::string& resource_id,
257 const base::Time& last_modified_time,
258 const google_apis::GetResourceEntryCallback& callback);
260 private:
261 struct EntryInfo;
262 struct UploadSession;
264 // Returns a pointer to the entry that matches |resource_id|, or NULL if
265 // not found.
266 EntryInfo* FindEntryByResourceId(const std::string& resource_id);
268 // Returns a new resource ID, which looks like "resource_id_<num>" where
269 // <num> is a monotonically increasing number starting from 1.
270 std::string GetNewResourceId();
272 // Increments |largest_changestamp_| and adds the new changestamp.
273 void AddNewChangestamp(google_apis::ChangeResource* change);
275 // Update ETag of |file| based on |largest_changestamp_|.
276 void UpdateETag(google_apis::FileResource* file);
278 // Adds a new entry based on the given parameters.
279 // Returns a pointer to the newly added entry, or NULL if failed.
280 const EntryInfo* AddNewEntry(
281 const std::string& content_type,
282 const std::string& content_data,
283 const std::string& parent_resource_id,
284 const std::string& title,
285 bool shared_with_me);
287 // Core implementation of GetResourceList.
288 // This method returns the slice of the all matched entries, and its range
289 // is between |start_offset| (inclusive) and |start_offset| + |max_results|
290 // (exclusive).
291 // Increments *load_counter by 1 before it returns successfully.
292 void GetResourceListInternal(
293 int64 start_changestamp,
294 const std::string& search_query,
295 const std::string& directory_resource_id,
296 int start_offset,
297 int max_results,
298 int* load_counter,
299 const google_apis::GetResourceListCallback& callback);
301 // Returns new upload session URL.
302 GURL GetNewUploadSessionUrl();
304 typedef std::map<std::string, EntryInfo*> EntryInfoMap;
305 EntryInfoMap entries_;
306 scoped_ptr<google_apis::AboutResource> about_resource_;
307 scoped_ptr<base::DictionaryValue> app_info_value_;
308 std::map<GURL, UploadSession> upload_sessions_;
309 int64 published_date_seq_;
310 int64 next_upload_sequence_number_;
311 int default_max_results_;
312 int resource_id_count_;
313 int resource_list_load_count_;
314 int change_list_load_count_;
315 int directory_load_count_;
316 int about_resource_load_count_;
317 int app_list_load_count_;
318 int blocked_resource_list_load_count_;
319 bool offline_;
320 bool never_return_all_resource_list_;
321 base::FilePath last_cancelled_file_;
322 GURL share_url_base_;
324 DISALLOW_COPY_AND_ASSIGN(FakeDriveService);
327 } // namespace drive
329 #endif // CHROME_BROWSER_DRIVE_FAKE_DRIVE_SERVICE_H_