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