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
{
31 class ChangeObserver
{
33 virtual ~ChangeObserver() {}
34 virtual void OnNewChangeAvailable() = 0;
38 virtual ~FakeDriveService();
40 // Loads the app list for Drive API. Returns true on success.
41 bool LoadAppListForDriveApi(const std::string
& relative_path
);
43 // Adds an app to app list.
44 void AddApp(const std::string
& app_id
,
45 const std::string
& app_name
,
46 const std::string
& product_id
,
47 const std::string
& create_url
,
50 // Removes an app by product id.
51 void RemoveAppByProductId(const std::string
& product_id
);
53 // Returns true if the service knows the given drive app id.
54 bool HasApp(const std::string
& app_id
) const;
56 // Changes the offline state. All functions fail with GDATA_NO_CONNECTION
57 // when offline. By default the offline state is false.
58 void set_offline(bool offline
) { offline_
= offline
; }
60 // GetAllFileList never returns result when this is set to true.
61 // Used to emulate the real server's slowness.
62 void set_never_return_all_file_list(bool value
) {
63 never_return_all_file_list_
= value
;
66 // Changes the default max results returned from GetAllFileList().
67 // By default, it's set to 0, which is unlimited.
68 void set_default_max_results(int default_max_results
) {
69 default_max_results_
= default_max_results
;
72 // Sets the url to the test server to be used as a base for generated share
73 // urls to the share dialog.
74 void set_share_url_base(const GURL
& share_url_base
) {
75 share_url_base_
= share_url_base
;
78 // Changes the quota fields returned from GetAboutResource().
79 void SetQuotaValue(int64 used
, int64 total
);
81 // Returns the AboutResource.
82 const google_apis::AboutResource
& about_resource() const {
83 return *about_resource_
;
86 // Returns the number of times the file list is successfully loaded by
88 int file_list_load_count() const { return file_list_load_count_
; }
90 // Returns the number of times the resource list is successfully loaded by
92 int change_list_load_count() const { return change_list_load_count_
; }
94 // Returns the number of times the resource list is successfully loaded by
95 // GetFileListInDirectory().
96 int directory_load_count() const { return directory_load_count_
; }
98 // Returns the number of times the about resource is successfully loaded
99 // by GetAboutResource().
100 int about_resource_load_count() const {
101 return about_resource_load_count_
;
104 // Returns the number of times the app list is successfully loaded by
106 int app_list_load_count() const { return app_list_load_count_
; }
108 // Returns the number of times GetAllFileList are blocked due to
109 // set_never_return_all_file_list().
110 int blocked_file_list_load_count() const {
111 return blocked_file_list_load_count_
;
114 // Returns the file path whose request is cancelled just before this method
116 const base::FilePath
& last_cancelled_file() const {
117 return last_cancelled_file_
;
120 // Returns the (fake) URL for the link.
121 static GURL
GetFakeLinkUrl(const std::string
& resource_id
);
123 // Sets the printf format for constructing the response of AuthorizeApp().
124 // The format string must include two %s that are to be filled with
125 // resource_id and app_id.
126 void set_open_url_format(const std::string
& url_format
) {
127 open_url_format_
= url_format
;
130 // DriveServiceInterface Overrides
131 virtual void Initialize(const std::string
& account_id
) override
;
132 virtual void AddObserver(DriveServiceObserver
* observer
) override
;
133 virtual void RemoveObserver(DriveServiceObserver
* observer
) override
;
134 virtual bool CanSendRequest() const override
;
135 virtual std::string
GetRootResourceId() const override
;
136 virtual bool HasAccessToken() const override
;
137 virtual void RequestAccessToken(
138 const google_apis::AuthStatusCallback
& callback
) override
;
139 virtual bool HasRefreshToken() const override
;
140 virtual void ClearAccessToken() override
;
141 virtual void ClearRefreshToken() override
;
142 virtual google_apis::CancelCallback
GetAllFileList(
143 const google_apis::FileListCallback
& callback
) override
;
144 virtual google_apis::CancelCallback
GetFileListInDirectory(
145 const std::string
& directory_resource_id
,
146 const google_apis::FileListCallback
& callback
) override
;
147 // See the comment for EntryMatchWidthQuery() in .cc file for details about
148 // the supported search query types.
149 virtual google_apis::CancelCallback
Search(
150 const std::string
& search_query
,
151 const google_apis::FileListCallback
& callback
) override
;
152 virtual google_apis::CancelCallback
SearchByTitle(
153 const std::string
& title
,
154 const std::string
& directory_resource_id
,
155 const google_apis::FileListCallback
& callback
) override
;
156 virtual google_apis::CancelCallback
GetChangeList(
157 int64 start_changestamp
,
158 const google_apis::ChangeListCallback
& callback
) override
;
159 virtual google_apis::CancelCallback
GetRemainingChangeList(
160 const GURL
& next_link
,
161 const google_apis::ChangeListCallback
& callback
) override
;
162 virtual google_apis::CancelCallback
GetRemainingFileList(
163 const GURL
& next_link
,
164 const google_apis::FileListCallback
& callback
) override
;
165 virtual google_apis::CancelCallback
GetFileResource(
166 const std::string
& resource_id
,
167 const google_apis::FileResourceCallback
& callback
) override
;
168 virtual google_apis::CancelCallback
GetShareUrl(
169 const std::string
& resource_id
,
170 const GURL
& embed_origin
,
171 const google_apis::GetShareUrlCallback
& callback
) override
;
172 virtual google_apis::CancelCallback
GetAboutResource(
173 const google_apis::AboutResourceCallback
& callback
) override
;
174 virtual google_apis::CancelCallback
GetAppList(
175 const google_apis::AppListCallback
& callback
) override
;
176 virtual google_apis::CancelCallback
DeleteResource(
177 const std::string
& resource_id
,
178 const std::string
& etag
,
179 const google_apis::EntryActionCallback
& callback
) override
;
180 virtual google_apis::CancelCallback
TrashResource(
181 const std::string
& resource_id
,
182 const google_apis::EntryActionCallback
& callback
) override
;
183 virtual google_apis::CancelCallback
DownloadFile(
184 const base::FilePath
& local_cache_path
,
185 const std::string
& resource_id
,
186 const google_apis::DownloadActionCallback
& download_action_callback
,
187 const google_apis::GetContentCallback
& get_content_callback
,
188 const google_apis::ProgressCallback
& progress_callback
) override
;
189 virtual google_apis::CancelCallback
CopyResource(
190 const std::string
& resource_id
,
191 const std::string
& parent_resource_id
,
192 const std::string
& new_title
,
193 const base::Time
& last_modified
,
194 const google_apis::FileResourceCallback
& callback
) override
;
195 virtual google_apis::CancelCallback
UpdateResource(
196 const std::string
& resource_id
,
197 const std::string
& parent_resource_id
,
198 const std::string
& new_title
,
199 const base::Time
& last_modified
,
200 const base::Time
& last_viewed_by_me
,
201 const google_apis::FileResourceCallback
& callback
) override
;
202 virtual google_apis::CancelCallback
AddResourceToDirectory(
203 const std::string
& parent_resource_id
,
204 const std::string
& resource_id
,
205 const google_apis::EntryActionCallback
& callback
) override
;
206 virtual google_apis::CancelCallback
RemoveResourceFromDirectory(
207 const std::string
& parent_resource_id
,
208 const std::string
& resource_id
,
209 const google_apis::EntryActionCallback
& callback
) override
;
210 virtual google_apis::CancelCallback
AddNewDirectory(
211 const std::string
& parent_resource_id
,
212 const std::string
& directory_title
,
213 const AddNewDirectoryOptions
& options
,
214 const google_apis::FileResourceCallback
& callback
) override
;
215 virtual google_apis::CancelCallback
InitiateUploadNewFile(
216 const std::string
& content_type
,
217 int64 content_length
,
218 const std::string
& parent_resource_id
,
219 const std::string
& title
,
220 const InitiateUploadNewFileOptions
& options
,
221 const google_apis::InitiateUploadCallback
& callback
) override
;
222 virtual google_apis::CancelCallback
InitiateUploadExistingFile(
223 const std::string
& content_type
,
224 int64 content_length
,
225 const std::string
& resource_id
,
226 const InitiateUploadExistingFileOptions
& options
,
227 const google_apis::InitiateUploadCallback
& callback
) override
;
228 virtual google_apis::CancelCallback
ResumeUpload(
229 const GURL
& upload_url
,
230 int64 start_position
,
232 int64 content_length
,
233 const std::string
& content_type
,
234 const base::FilePath
& local_file_path
,
235 const google_apis::drive::UploadRangeCallback
& callback
,
236 const google_apis::ProgressCallback
& progress_callback
) override
;
237 virtual google_apis::CancelCallback
GetUploadStatus(
238 const GURL
& upload_url
,
239 int64 content_length
,
240 const google_apis::drive::UploadRangeCallback
& callback
) override
;
241 virtual google_apis::CancelCallback
AuthorizeApp(
242 const std::string
& resource_id
,
243 const std::string
& app_id
,
244 const google_apis::AuthorizeAppCallback
& callback
) override
;
245 virtual google_apis::CancelCallback
UninstallApp(
246 const std::string
& app_id
,
247 const google_apis::EntryActionCallback
& callback
) override
;
248 virtual google_apis::CancelCallback
AddPermission(
249 const std::string
& resource_id
,
250 const std::string
& email
,
251 google_apis::drive::PermissionRole role
,
252 const google_apis::EntryActionCallback
& callback
) override
;
254 // Adds a new file with the given parameters. On success, returns
255 // HTTP_CREATED with the parsed entry.
256 // |callback| must not be null.
257 void AddNewFile(const std::string
& content_type
,
258 const std::string
& content_data
,
259 const std::string
& parent_resource_id
,
260 const std::string
& title
,
262 const google_apis::FileResourceCallback
& callback
);
264 // Adds a new file with the given |resource_id|. If the id already exists,
265 // it's an error. This is used for testing cross profile file sharing that
266 // needs to have matching resource IDs in different fake service instances.
267 // |callback| must not be null.
268 void AddNewFileWithResourceId(
269 const std::string
& resource_id
,
270 const std::string
& content_type
,
271 const std::string
& content_data
,
272 const std::string
& parent_resource_id
,
273 const std::string
& title
,
275 const google_apis::FileResourceCallback
& callback
);
277 // Adds a new directory with the given |resource_id|.
278 // |callback| must not be null.
279 google_apis::CancelCallback
AddNewDirectoryWithResourceId(
280 const std::string
& resource_id
,
281 const std::string
& parent_resource_id
,
282 const std::string
& directory_title
,
283 const AddNewDirectoryOptions
& options
,
284 const google_apis::FileResourceCallback
& callback
);
286 // Sets the last modified time for an entry specified by |resource_id|.
287 // On success, returns HTTP_SUCCESS with the parsed entry.
288 // |callback| must not be null.
289 void SetLastModifiedTime(
290 const std::string
& resource_id
,
291 const base::Time
& last_modified_time
,
292 const google_apis::FileResourceCallback
& callback
);
294 // Sets the user's permission for an entry specified by |resource_id|.
295 google_apis::GDataErrorCode
SetUserPermission(
296 const std::string
& resource_id
,
297 google_apis::drive::PermissionRole user_permission
);
299 void AddChangeObserver(ChangeObserver
* observer
);
300 void RemoveChangeObserver(ChangeObserver
* observer
);
304 struct UploadSession
;
306 // Returns a pointer to the entry that matches |resource_id|, or NULL if
308 EntryInfo
* FindEntryByResourceId(const std::string
& resource_id
);
310 // Returns a new resource ID, which looks like "resource_id_<num>" where
311 // <num> is a monotonically increasing number starting from 1.
312 std::string
GetNewResourceId();
314 // Increments |largest_changestamp_| and adds the new changestamp.
315 void AddNewChangestamp(google_apis::ChangeResource
* change
);
317 // Update ETag of |file| based on |largest_changestamp_|.
318 void UpdateETag(google_apis::FileResource
* file
);
320 // Adds a new entry based on the given parameters.
321 // |resource_id| can be empty, in the case, the id is automatically generated.
322 // Returns a pointer to the newly added entry, or NULL if failed.
323 const EntryInfo
* AddNewEntry(
324 const std::string
& resource_id
,
325 const std::string
& content_type
,
326 const std::string
& content_data
,
327 const std::string
& parent_resource_id
,
328 const std::string
& title
,
329 bool shared_with_me
);
331 // Core implementation of GetChangeList.
332 // This method returns the slice of the all matched entries, and its range
333 // is between |start_offset| (inclusive) and |start_offset| + |max_results|
335 // Increments *load_counter by 1 before it returns successfully.
336 void GetChangeListInternal(
337 int64 start_changestamp
,
338 const std::string
& search_query
,
339 const std::string
& directory_resource_id
,
343 const google_apis::ChangeListCallback
& callback
);
345 // Returns new upload session URL.
346 GURL
GetNewUploadSessionUrl();
348 void NotifyObservers();
350 typedef std::map
<std::string
, EntryInfo
*> EntryInfoMap
;
351 EntryInfoMap entries_
;
352 scoped_ptr
<google_apis::AboutResource
> about_resource_
;
353 scoped_ptr
<base::DictionaryValue
> app_info_value_
;
354 std::map
<GURL
, UploadSession
> upload_sessions_
;
355 int64 published_date_seq_
;
356 int64 next_upload_sequence_number_
;
357 int default_max_results_
;
358 int resource_id_count_
;
359 int file_list_load_count_
;
360 int change_list_load_count_
;
361 int directory_load_count_
;
362 int about_resource_load_count_
;
363 int app_list_load_count_
;
364 int blocked_file_list_load_count_
;
366 bool never_return_all_file_list_
;
367 base::FilePath last_cancelled_file_
;
368 GURL share_url_base_
;
369 std::string app_json_template_
;
370 std::string open_url_format_
;
372 ObserverList
<ChangeObserver
> change_observers_
;
374 base::WeakPtrFactory
<FakeDriveService
> weak_ptr_factory_
;
376 DISALLOW_COPY_AND_ASSIGN(FakeDriveService
);
381 #endif // CHROME_BROWSER_DRIVE_FAKE_DRIVE_SERVICE_H_