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 COMPONENTS_DRIVE_SERVICE_FAKE_DRIVE_SERVICE_H_
6 #define COMPONENTS_DRIVE_SERVICE_FAKE_DRIVE_SERVICE_H_
10 #include "base/files/file_path.h"
11 #include "base/threading/thread_checker.h"
12 #include "components/drive/service/drive_service_interface.h"
15 class DictionaryValue
;
18 namespace google_apis
{
26 // This class implements a fake DriveService which acts like a real Drive
27 // service. The fake service works as follows:
29 // 1) Load JSON files and construct the in-memory resource list.
30 // 2) Return valid responses based on the the in-memory resource list.
31 // 3) Update the in-memory resource list by requests like DeleteResource().
32 class FakeDriveService
: public DriveServiceInterface
{
34 class ChangeObserver
{
36 virtual ~ChangeObserver() {}
37 virtual void OnNewChangeAvailable() = 0;
41 ~FakeDriveService() override
;
43 // Loads the app list for Drive API. Returns true on success.
44 bool LoadAppListForDriveApi(const std::string
& relative_path
);
46 // Adds an app to app list.
47 void AddApp(const std::string
& app_id
,
48 const std::string
& app_name
,
49 const std::string
& product_id
,
50 const std::string
& create_url
,
53 // Removes an app by product id.
54 void RemoveAppByProductId(const std::string
& product_id
);
56 // Returns true if the service knows the given drive app id.
57 bool HasApp(const std::string
& app_id
) const;
59 // Changes the offline state. All functions fail with DRIVE_NO_CONNECTION
60 // when offline. By default the offline state is false.
61 void set_offline(bool offline
) { offline_
= offline
; }
63 // GetAllFileList never returns result when this is set to true.
64 // Used to emulate the real server's slowness.
65 void set_never_return_all_file_list(bool value
) {
66 never_return_all_file_list_
= value
;
69 // Changes the default max results returned from GetAllFileList().
70 // By default, it's set to 0, which is unlimited.
71 void set_default_max_results(int default_max_results
) {
72 default_max_results_
= default_max_results
;
75 // Sets the url to the test server to be used as a base for generated share
76 // urls to the share dialog.
77 void set_share_url_base(const GURL
& share_url_base
) {
78 share_url_base_
= share_url_base
;
81 // Changes the quota fields returned from GetAboutResource().
82 void SetQuotaValue(int64 used
, int64 total
);
84 // Returns the AboutResource.
85 const google_apis::AboutResource
& about_resource() const {
86 return *about_resource_
;
89 // Returns the number of times the file list is successfully loaded by
91 int file_list_load_count() const { return file_list_load_count_
; }
93 // Returns the number of times the resource list is successfully loaded by
95 int change_list_load_count() const { return change_list_load_count_
; }
97 // Returns the number of times the resource list is successfully loaded by
98 // GetFileListInDirectory().
99 int directory_load_count() const { return directory_load_count_
; }
101 // Returns the number of times the about resource is successfully loaded
102 // by GetAboutResource().
103 int about_resource_load_count() const {
104 return about_resource_load_count_
;
107 // Returns the number of times the app list is successfully loaded by
109 int app_list_load_count() const { return app_list_load_count_
; }
111 // Returns the number of times GetAllFileList are blocked due to
112 // set_never_return_all_file_list().
113 int blocked_file_list_load_count() const {
114 return blocked_file_list_load_count_
;
117 // Returns the file path whose request is cancelled just before this method
119 const base::FilePath
& last_cancelled_file() const {
120 return last_cancelled_file_
;
123 // Returns the (fake) URL for the link.
124 static GURL
GetFakeLinkUrl(const std::string
& resource_id
);
126 // Sets the printf format for constructing the response of AuthorizeApp().
127 // The format string must include two %s that are to be filled with
128 // resource_id and app_id.
129 void set_open_url_format(const std::string
& url_format
) {
130 open_url_format_
= url_format
;
133 // DriveServiceInterface Overrides
134 void Initialize(const std::string
& account_id
) override
;
135 void AddObserver(DriveServiceObserver
* observer
) override
;
136 void RemoveObserver(DriveServiceObserver
* observer
) override
;
137 bool CanSendRequest() const override
;
138 std::string
GetRootResourceId() const override
;
139 bool HasAccessToken() const override
;
140 void RequestAccessToken(
141 const google_apis::AuthStatusCallback
& callback
) override
;
142 bool HasRefreshToken() const override
;
143 void ClearAccessToken() override
;
144 void ClearRefreshToken() override
;
145 google_apis::CancelCallback
GetAllFileList(
146 const google_apis::FileListCallback
& callback
) override
;
147 google_apis::CancelCallback
GetFileListInDirectory(
148 const std::string
& directory_resource_id
,
149 const google_apis::FileListCallback
& callback
) override
;
150 // See the comment for EntryMatchWidthQuery() in .cc file for details about
151 // the supported search query types.
152 google_apis::CancelCallback
Search(
153 const std::string
& search_query
,
154 const google_apis::FileListCallback
& callback
) override
;
155 google_apis::CancelCallback
SearchByTitle(
156 const std::string
& title
,
157 const std::string
& directory_resource_id
,
158 const google_apis::FileListCallback
& callback
) override
;
159 google_apis::CancelCallback
GetChangeList(
160 int64 start_changestamp
,
161 const google_apis::ChangeListCallback
& callback
) override
;
162 google_apis::CancelCallback
GetRemainingChangeList(
163 const GURL
& next_link
,
164 const google_apis::ChangeListCallback
& callback
) override
;
165 google_apis::CancelCallback
GetRemainingFileList(
166 const GURL
& next_link
,
167 const google_apis::FileListCallback
& callback
) override
;
168 google_apis::CancelCallback
GetFileResource(
169 const std::string
& resource_id
,
170 const google_apis::FileResourceCallback
& callback
) override
;
171 google_apis::CancelCallback
GetShareUrl(
172 const std::string
& resource_id
,
173 const GURL
& embed_origin
,
174 const google_apis::GetShareUrlCallback
& callback
) override
;
175 google_apis::CancelCallback
GetAboutResource(
176 const google_apis::AboutResourceCallback
& callback
) override
;
177 google_apis::CancelCallback
GetAppList(
178 const google_apis::AppListCallback
& callback
) override
;
179 google_apis::CancelCallback
DeleteResource(
180 const std::string
& resource_id
,
181 const std::string
& etag
,
182 const google_apis::EntryActionCallback
& callback
) override
;
183 google_apis::CancelCallback
TrashResource(
184 const std::string
& resource_id
,
185 const google_apis::EntryActionCallback
& callback
) override
;
186 google_apis::CancelCallback
DownloadFile(
187 const base::FilePath
& local_cache_path
,
188 const std::string
& resource_id
,
189 const google_apis::DownloadActionCallback
& download_action_callback
,
190 const google_apis::GetContentCallback
& get_content_callback
,
191 const google_apis::ProgressCallback
& progress_callback
) override
;
192 google_apis::CancelCallback
CopyResource(
193 const std::string
& resource_id
,
194 const std::string
& parent_resource_id
,
195 const std::string
& new_title
,
196 const base::Time
& last_modified
,
197 const google_apis::FileResourceCallback
& callback
) override
;
198 google_apis::CancelCallback
UpdateResource(
199 const std::string
& resource_id
,
200 const std::string
& parent_resource_id
,
201 const std::string
& new_title
,
202 const base::Time
& last_modified
,
203 const base::Time
& last_viewed_by_me
,
204 const google_apis::drive::Properties
& properties
,
205 const google_apis::FileResourceCallback
& callback
) override
;
206 google_apis::CancelCallback
AddResourceToDirectory(
207 const std::string
& parent_resource_id
,
208 const std::string
& resource_id
,
209 const google_apis::EntryActionCallback
& callback
) override
;
210 google_apis::CancelCallback
RemoveResourceFromDirectory(
211 const std::string
& parent_resource_id
,
212 const std::string
& resource_id
,
213 const google_apis::EntryActionCallback
& callback
) override
;
214 google_apis::CancelCallback
AddNewDirectory(
215 const std::string
& parent_resource_id
,
216 const std::string
& directory_title
,
217 const AddNewDirectoryOptions
& options
,
218 const google_apis::FileResourceCallback
& callback
) override
;
219 google_apis::CancelCallback
InitiateUploadNewFile(
220 const std::string
& content_type
,
221 int64 content_length
,
222 const std::string
& parent_resource_id
,
223 const std::string
& title
,
224 const UploadNewFileOptions
& options
,
225 const google_apis::InitiateUploadCallback
& callback
) override
;
226 google_apis::CancelCallback
InitiateUploadExistingFile(
227 const std::string
& content_type
,
228 int64 content_length
,
229 const std::string
& resource_id
,
230 const UploadExistingFileOptions
& options
,
231 const google_apis::InitiateUploadCallback
& callback
) override
;
232 google_apis::CancelCallback
ResumeUpload(
233 const GURL
& upload_url
,
234 int64 start_position
,
236 int64 content_length
,
237 const std::string
& content_type
,
238 const base::FilePath
& local_file_path
,
239 const google_apis::drive::UploadRangeCallback
& callback
,
240 const google_apis::ProgressCallback
& progress_callback
) override
;
241 google_apis::CancelCallback
GetUploadStatus(
242 const GURL
& upload_url
,
243 int64 content_length
,
244 const google_apis::drive::UploadRangeCallback
& callback
) override
;
245 google_apis::CancelCallback
MultipartUploadNewFile(
246 const std::string
& content_type
,
247 int64 content_length
,
248 const std::string
& parent_resource_id
,
249 const std::string
& title
,
250 const base::FilePath
& local_file_path
,
251 const UploadNewFileOptions
& options
,
252 const google_apis::FileResourceCallback
& callback
,
253 const google_apis::ProgressCallback
& progress_callback
) override
;
254 google_apis::CancelCallback
MultipartUploadExistingFile(
255 const std::string
& content_type
,
256 int64 content_length
,
257 const std::string
& resource_id
,
258 const base::FilePath
& local_file_path
,
259 const UploadExistingFileOptions
& options
,
260 const google_apis::FileResourceCallback
& callback
,
261 const google_apis::ProgressCallback
& progress_callback
) override
;
262 google_apis::CancelCallback
AuthorizeApp(
263 const std::string
& resource_id
,
264 const std::string
& app_id
,
265 const google_apis::AuthorizeAppCallback
& callback
) override
;
266 google_apis::CancelCallback
UninstallApp(
267 const std::string
& app_id
,
268 const google_apis::EntryActionCallback
& callback
) override
;
269 google_apis::CancelCallback
AddPermission(
270 const std::string
& resource_id
,
271 const std::string
& email
,
272 google_apis::drive::PermissionRole role
,
273 const google_apis::EntryActionCallback
& callback
) override
;
274 scoped_ptr
<BatchRequestConfiguratorInterface
> StartBatchRequest() override
;
276 // Adds a new file with the given parameters. On success, returns
277 // HTTP_CREATED with the parsed entry.
278 // |callback| must not be null.
279 void AddNewFile(const std::string
& content_type
,
280 const std::string
& content_data
,
281 const std::string
& parent_resource_id
,
282 const std::string
& title
,
284 const google_apis::FileResourceCallback
& callback
);
286 // Adds a new file with the given |resource_id|. If the id already exists,
287 // it's an error. This is used for testing cross profile file sharing that
288 // needs to have matching resource IDs in different fake service instances.
289 // |callback| must not be null.
290 void AddNewFileWithResourceId(
291 const std::string
& resource_id
,
292 const std::string
& content_type
,
293 const std::string
& content_data
,
294 const std::string
& parent_resource_id
,
295 const std::string
& title
,
297 const google_apis::FileResourceCallback
& callback
);
299 // Adds a new directory with the given |resource_id|.
300 // |callback| must not be null.
301 google_apis::CancelCallback
AddNewDirectoryWithResourceId(
302 const std::string
& resource_id
,
303 const std::string
& parent_resource_id
,
304 const std::string
& directory_title
,
305 const AddNewDirectoryOptions
& options
,
306 const google_apis::FileResourceCallback
& callback
);
308 // Sets the last modified time for an entry specified by |resource_id|.
309 // On success, returns HTTP_SUCCESS with the parsed entry.
310 // |callback| must not be null.
311 void SetLastModifiedTime(
312 const std::string
& resource_id
,
313 const base::Time
& last_modified_time
,
314 const google_apis::FileResourceCallback
& callback
);
316 // Sets the user's permission for an entry specified by |resource_id|.
317 google_apis::DriveApiErrorCode
SetUserPermission(
318 const std::string
& resource_id
,
319 google_apis::drive::PermissionRole user_permission
);
321 google_apis::DriveApiErrorCode
SetFileVisibility(
322 const std::string
& resource_id
,
323 google_apis::drive::FileVisibility visibility
);
324 google_apis::DriveApiErrorCode
GetFileVisibility(
325 const std::string
& resource_id
,
326 google_apis::drive::FileVisibility
* visibility
);
328 void AddChangeObserver(ChangeObserver
* observer
);
329 void RemoveChangeObserver(ChangeObserver
* observer
);
333 struct UploadSession
;
335 // Returns a pointer to the entry that matches |resource_id|, or NULL if
337 EntryInfo
* FindEntryByResourceId(const std::string
& resource_id
);
339 // Returns a new resource ID, which looks like "resource_id_<num>" where
340 // <num> is a monotonically increasing number starting from 1.
341 std::string
GetNewResourceId();
343 // Increments |largest_changestamp_| and adds the new changestamp.
344 void AddNewChangestamp(google_apis::ChangeResource
* change
);
346 // Update ETag of |file| based on |largest_changestamp_|.
347 void UpdateETag(google_apis::FileResource
* file
);
349 // Adds a new entry based on the given parameters.
350 // |resource_id| can be empty, in the case, the id is automatically generated.
351 // Returns a pointer to the newly added entry, or NULL if failed.
352 const EntryInfo
* AddNewEntry(
353 const std::string
& resource_id
,
354 const std::string
& content_type
,
355 const std::string
& content_data
,
356 const std::string
& parent_resource_id
,
357 const std::string
& title
,
358 bool shared_with_me
);
360 // Core implementation of GetChangeList.
361 // This method returns the slice of the all matched entries, and its range
362 // is between |start_offset| (inclusive) and |start_offset| + |max_results|
364 // Increments *load_counter by 1 before it returns successfully.
365 void GetChangeListInternal(
366 int64 start_changestamp
,
367 const std::string
& search_query
,
368 const std::string
& directory_resource_id
,
372 const google_apis::ChangeListCallback
& callback
);
374 // Returns new upload session URL.
375 GURL
GetNewUploadSessionUrl();
377 void NotifyObservers();
379 // The class is expected to run on UI thread.
380 base::ThreadChecker thread_checker_
;
382 typedef std::map
<std::string
, EntryInfo
*> EntryInfoMap
;
383 EntryInfoMap entries_
;
384 scoped_ptr
<google_apis::AboutResource
> about_resource_
;
385 scoped_ptr
<base::DictionaryValue
> app_info_value_
;
386 std::map
<GURL
, UploadSession
> upload_sessions_
;
387 int64 published_date_seq_
;
388 int64 next_upload_sequence_number_
;
389 int default_max_results_
;
390 int resource_id_count_
;
391 int file_list_load_count_
;
392 int change_list_load_count_
;
393 int directory_load_count_
;
394 int about_resource_load_count_
;
395 int app_list_load_count_
;
396 int blocked_file_list_load_count_
;
398 bool never_return_all_file_list_
;
399 base::FilePath last_cancelled_file_
;
400 GURL share_url_base_
;
401 std::string app_json_template_
;
402 std::string open_url_format_
;
404 base::ObserverList
<ChangeObserver
> change_observers_
;
406 base::WeakPtrFactory
<FakeDriveService
> weak_ptr_factory_
;
408 DISALLOW_COPY_AND_ASSIGN(FakeDriveService
);
413 #endif // COMPONENTS_DRIVE_SERVICE_FAKE_DRIVE_SERVICE_H_