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 "chrome/browser/drive/drive_service_interface.h"
12 class DictionaryValue;
15 namespace google_apis {
23 // This class implements a fake DriveService which acts like a real Drive
24 // service. The fake service works as follows:
26 // 1) Load JSON files and construct the in-memory resource list.
27 // 2) Return valid responses based on the the in-memory resource list.
28 // 3) Update the in-memory resource list by requests like DeleteResource().
29 class FakeDriveService : public DriveServiceInterface {
32 virtual ~FakeDriveService();
34 // Loads the app list for Drive API. Returns true on success.
35 bool LoadAppListForDriveApi(const std::string& relative_path);
37 // Changes the offline state. All functions fail with GDATA_NO_CONNECTION
38 // when offline. By default the offline state is false.
39 void set_offline(bool offline) { offline_ = offline; }
41 // GetAllResourceList never returns result when this is set to true.
42 // Used to emulate the real server's slowness.
43 void set_never_return_all_resource_list(bool value) {
44 never_return_all_resource_list_ = value;
47 // Changes the default max results returned from GetResourceList().
48 // By default, it's set to 0, which is unlimited.
49 void set_default_max_results(int default_max_results) {
50 default_max_results_ = default_max_results;
53 // Sets the url to the test server to be used as a base for generated share
54 // urls to the share dialog.
55 void set_share_url_base(const GURL& share_url_base) {
56 share_url_base_ = share_url_base;
59 // Changes the quota fields returned from GetAboutResource().
60 void SetQuotaValue(int64 used, int64 total);
62 // Returns the AboutResource.
63 const google_apis::AboutResource& about_resource() const {
64 return *about_resource_;
67 // Returns the number of times the resource list is successfully loaded by
68 // GetAllResourceList().
69 int resource_list_load_count() const { return resource_list_load_count_; }
71 // Returns the number of times the resource list is successfully loaded by
73 int change_list_load_count() const { return change_list_load_count_; }
75 // Returns the number of times the resource list is successfully loaded by
76 // GetResourceListInDirectory().
77 int directory_load_count() const { return directory_load_count_; }
79 // Returns the number of times the about resource is successfully loaded
80 // by GetAboutResource().
81 int about_resource_load_count() const {
82 return about_resource_load_count_;
85 // Returns the number of times the app list is successfully loaded by
87 int app_list_load_count() const { return app_list_load_count_; }
89 // Returns the number of times GetAllResourceList are blocked due to
90 // set_never_return_all_resource_list().
91 int blocked_resource_list_load_count() const {
92 return blocked_resource_list_load_count_;
95 // Returns the file path whose request is cancelled just before this method
97 const base::FilePath& last_cancelled_file() const {
98 return last_cancelled_file_;
101 // Returns the (fake) URL for the link.
102 static GURL GetFakeLinkUrl(const std::string& resource_id);
104 // DriveServiceInterface Overrides
105 virtual void Initialize(const std::string& account_id) OVERRIDE;
106 virtual void AddObserver(DriveServiceObserver* observer) OVERRIDE;
107 virtual void RemoveObserver(DriveServiceObserver* observer) OVERRIDE;
108 virtual bool CanSendRequest() const OVERRIDE;
109 virtual ResourceIdCanonicalizer GetResourceIdCanonicalizer() const OVERRIDE;
110 virtual std::string GetRootResourceId() const OVERRIDE;
111 virtual bool HasAccessToken() const OVERRIDE;
112 virtual void RequestAccessToken(
113 const google_apis::AuthStatusCallback& callback) OVERRIDE;
114 virtual bool HasRefreshToken() const OVERRIDE;
115 virtual void ClearAccessToken() OVERRIDE;
116 virtual void ClearRefreshToken() OVERRIDE;
117 virtual google_apis::CancelCallback GetAllResourceList(
118 const google_apis::GetResourceListCallback& callback) OVERRIDE;
119 virtual google_apis::CancelCallback GetResourceListInDirectory(
120 const std::string& directory_resource_id,
121 const google_apis::GetResourceListCallback& callback) OVERRIDE;
122 // See the comment for EntryMatchWidthQuery() in .cc file for details about
123 // the supported search query types.
124 virtual google_apis::CancelCallback Search(
125 const std::string& search_query,
126 const google_apis::GetResourceListCallback& callback) OVERRIDE;
127 virtual google_apis::CancelCallback SearchByTitle(
128 const std::string& title,
129 const std::string& directory_resource_id,
130 const google_apis::GetResourceListCallback& callback) OVERRIDE;
131 virtual google_apis::CancelCallback GetChangeList(
132 int64 start_changestamp,
133 const google_apis::GetResourceListCallback& callback) OVERRIDE;
134 virtual google_apis::CancelCallback GetRemainingChangeList(
135 const GURL& next_link,
136 const google_apis::GetResourceListCallback& callback) OVERRIDE;
137 virtual google_apis::CancelCallback GetRemainingFileList(
138 const GURL& next_link,
139 const google_apis::GetResourceListCallback& callback) OVERRIDE;
140 virtual google_apis::CancelCallback GetResourceEntry(
141 const std::string& resource_id,
142 const google_apis::GetResourceEntryCallback& callback) OVERRIDE;
143 virtual google_apis::CancelCallback GetShareUrl(
144 const std::string& resource_id,
145 const GURL& embed_origin,
146 const google_apis::GetShareUrlCallback& callback) OVERRIDE;
147 virtual google_apis::CancelCallback GetAboutResource(
148 const google_apis::AboutResourceCallback& callback) OVERRIDE;
149 virtual google_apis::CancelCallback GetAppList(
150 const google_apis::AppListCallback& callback) OVERRIDE;
151 virtual google_apis::CancelCallback DeleteResource(
152 const std::string& resource_id,
153 const std::string& etag,
154 const google_apis::EntryActionCallback& callback) OVERRIDE;
155 virtual google_apis::CancelCallback TrashResource(
156 const std::string& resource_id,
157 const google_apis::EntryActionCallback& callback) OVERRIDE;
158 virtual google_apis::CancelCallback DownloadFile(
159 const base::FilePath& local_cache_path,
160 const std::string& resource_id,
161 const google_apis::DownloadActionCallback& download_action_callback,
162 const google_apis::GetContentCallback& get_content_callback,
163 const google_apis::ProgressCallback& progress_callback) OVERRIDE;
164 virtual google_apis::CancelCallback CopyResource(
165 const std::string& resource_id,
166 const std::string& parent_resource_id,
167 const std::string& new_title,
168 const base::Time& last_modified,
169 const google_apis::GetResourceEntryCallback& callback) OVERRIDE;
170 virtual google_apis::CancelCallback UpdateResource(
171 const std::string& resource_id,
172 const std::string& parent_resource_id,
173 const std::string& new_title,
174 const base::Time& last_modified,
175 const base::Time& last_viewed_by_me,
176 const google_apis::GetResourceEntryCallback& callback) OVERRIDE;
177 virtual google_apis::CancelCallback RenameResource(
178 const std::string& resource_id,
179 const std::string& new_title,
180 const google_apis::EntryActionCallback& callback) OVERRIDE;
181 virtual google_apis::CancelCallback AddResourceToDirectory(
182 const std::string& parent_resource_id,
183 const std::string& resource_id,
184 const google_apis::EntryActionCallback& callback) OVERRIDE;
185 virtual google_apis::CancelCallback RemoveResourceFromDirectory(
186 const std::string& parent_resource_id,
187 const std::string& resource_id,
188 const google_apis::EntryActionCallback& callback) OVERRIDE;
189 virtual google_apis::CancelCallback AddNewDirectory(
190 const std::string& parent_resource_id,
191 const std::string& directory_title,
192 const AddNewDirectoryOptions& options,
193 const google_apis::GetResourceEntryCallback& callback) OVERRIDE;
194 virtual google_apis::CancelCallback InitiateUploadNewFile(
195 const std::string& content_type,
196 int64 content_length,
197 const std::string& parent_resource_id,
198 const std::string& title,
199 const InitiateUploadNewFileOptions& options,
200 const google_apis::InitiateUploadCallback& callback) OVERRIDE;
201 virtual google_apis::CancelCallback InitiateUploadExistingFile(
202 const std::string& content_type,
203 int64 content_length,
204 const std::string& resource_id,
205 const InitiateUploadExistingFileOptions& options,
206 const google_apis::InitiateUploadCallback& callback) OVERRIDE;
207 virtual google_apis::CancelCallback ResumeUpload(
208 const GURL& upload_url,
209 int64 start_position,
211 int64 content_length,
212 const std::string& content_type,
213 const base::FilePath& local_file_path,
214 const google_apis::UploadRangeCallback& callback,
215 const google_apis::ProgressCallback& progress_callback) OVERRIDE;
216 virtual google_apis::CancelCallback GetUploadStatus(
217 const GURL& upload_url,
218 int64 content_length,
219 const google_apis::UploadRangeCallback& callback) OVERRIDE;
220 virtual google_apis::CancelCallback AuthorizeApp(
221 const std::string& resource_id,
222 const std::string& app_id,
223 const google_apis::AuthorizeAppCallback& callback) OVERRIDE;
224 virtual google_apis::CancelCallback UninstallApp(
225 const std::string& app_id,
226 const google_apis::EntryActionCallback& callback) OVERRIDE;
227 virtual google_apis::CancelCallback AddPermission(
228 const std::string& resource_id,
229 const std::string& email,
230 google_apis::drive::PermissionRole role,
231 const google_apis::EntryActionCallback& callback) OVERRIDE;
233 // Adds a new file with the given parameters. On success, returns
234 // HTTP_CREATED with the parsed entry.
235 // |callback| must not be null.
236 void AddNewFile(const std::string& content_type,
237 const std::string& content_data,
238 const std::string& parent_resource_id,
239 const std::string& title,
241 const google_apis::GetResourceEntryCallback& callback);
243 // Adds a new file with the given |resource_id|. If the id already exists,
244 // it's an error. This is used for testing cross profile file sharing that
245 // needs to have matching resource IDs in different fake service instances.
246 // |callback| must not be null.
247 void AddNewFileWithResourceId(
248 const std::string& resource_id,
249 const std::string& content_type,
250 const std::string& content_data,
251 const std::string& parent_resource_id,
252 const std::string& title,
254 const google_apis::GetResourceEntryCallback& callback);
256 // Adds a new directory with the given |resource_id|.
257 // |callback| must not be null.
258 google_apis::CancelCallback AddNewDirectoryWithResourceId(
259 const std::string& resource_id,
260 const std::string& parent_resource_id,
261 const std::string& directory_title,
262 const AddNewDirectoryOptions& options,
263 const google_apis::GetResourceEntryCallback& callback);
265 // Sets the last modified time for an entry specified by |resource_id|.
266 // On success, returns HTTP_SUCCESS with the parsed entry.
267 // |callback| must not be null.
268 void SetLastModifiedTime(
269 const std::string& resource_id,
270 const base::Time& last_modified_time,
271 const google_apis::GetResourceEntryCallback& callback);
275 struct UploadSession;
277 // Returns a pointer to the entry that matches |resource_id|, or NULL if
279 EntryInfo* FindEntryByResourceId(const std::string& resource_id);
281 // Returns a new resource ID, which looks like "resource_id_<num>" where
282 // <num> is a monotonically increasing number starting from 1.
283 std::string GetNewResourceId();
285 // Increments |largest_changestamp_| and adds the new changestamp.
286 void AddNewChangestamp(google_apis::ChangeResource* change);
288 // Update ETag of |file| based on |largest_changestamp_|.
289 void UpdateETag(google_apis::FileResource* file);
291 // Adds a new entry based on the given parameters.
292 // |resource_id| can be empty, in the case, the id is automatically generated.
293 // Returns a pointer to the newly added entry, or NULL if failed.
294 const EntryInfo* AddNewEntry(
295 const std::string& resource_id,
296 const std::string& content_type,
297 const std::string& content_data,
298 const std::string& parent_resource_id,
299 const std::string& title,
300 bool shared_with_me);
302 // Core implementation of GetResourceList.
303 // This method returns the slice of the all matched entries, and its range
304 // is between |start_offset| (inclusive) and |start_offset| + |max_results|
306 // Increments *load_counter by 1 before it returns successfully.
307 void GetResourceListInternal(
308 int64 start_changestamp,
309 const std::string& search_query,
310 const std::string& directory_resource_id,
314 const google_apis::GetResourceListCallback& callback);
316 // Returns new upload session URL.
317 GURL GetNewUploadSessionUrl();
319 typedef std::map<std::string, EntryInfo*> EntryInfoMap;
320 EntryInfoMap entries_;
321 scoped_ptr<google_apis::AboutResource> about_resource_;
322 scoped_ptr<base::DictionaryValue> app_info_value_;
323 std::map<GURL, UploadSession> upload_sessions_;
324 int64 published_date_seq_;
325 int64 next_upload_sequence_number_;
326 int default_max_results_;
327 int resource_id_count_;
328 int resource_list_load_count_;
329 int change_list_load_count_;
330 int directory_load_count_;
331 int about_resource_load_count_;
332 int app_list_load_count_;
333 int blocked_resource_list_load_count_;
335 bool never_return_all_resource_list_;
336 base::FilePath last_cancelled_file_;
337 GURL share_url_base_;
339 DISALLOW_COPY_AND_ASSIGN(FakeDriveService);
344 #endif // CHROME_BROWSER_DRIVE_FAKE_DRIVE_SERVICE_H_