Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / drive / fake_drive_service.h
blobebb1658176dc0738f1d77900a4e71161db5f6028
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 <string>
10 #include "base/files/file_path.h"
11 #include "base/threading/thread_checker.h"
12 #include "chrome/browser/drive/drive_service_interface.h"
14 namespace base {
15 class DictionaryValue;
18 namespace google_apis {
19 class AboutResource;
20 class ChangeResource;
21 class FileResource;
24 namespace drive {
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 {
33 public:
34 class ChangeObserver {
35 public:
36 virtual ~ChangeObserver() {}
37 virtual void OnNewChangeAvailable() = 0;
40 FakeDriveService();
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,
51 bool is_removable);
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
90 // GetAllFileList().
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
94 // GetChangeList().
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
108 // GetAppList().
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
118 // invocation.
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,
235 int64 end_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;
275 // Adds a new file with the given parameters. On success, returns
276 // HTTP_CREATED with the parsed entry.
277 // |callback| must not be null.
278 void AddNewFile(const std::string& content_type,
279 const std::string& content_data,
280 const std::string& parent_resource_id,
281 const std::string& title,
282 bool shared_with_me,
283 const google_apis::FileResourceCallback& callback);
285 // Adds a new file with the given |resource_id|. If the id already exists,
286 // it's an error. This is used for testing cross profile file sharing that
287 // needs to have matching resource IDs in different fake service instances.
288 // |callback| must not be null.
289 void AddNewFileWithResourceId(
290 const std::string& resource_id,
291 const std::string& content_type,
292 const std::string& content_data,
293 const std::string& parent_resource_id,
294 const std::string& title,
295 bool shared_with_me,
296 const google_apis::FileResourceCallback& callback);
298 // Adds a new directory with the given |resource_id|.
299 // |callback| must not be null.
300 google_apis::CancelCallback AddNewDirectoryWithResourceId(
301 const std::string& resource_id,
302 const std::string& parent_resource_id,
303 const std::string& directory_title,
304 const AddNewDirectoryOptions& options,
305 const google_apis::FileResourceCallback& callback);
307 // Sets the last modified time for an entry specified by |resource_id|.
308 // On success, returns HTTP_SUCCESS with the parsed entry.
309 // |callback| must not be null.
310 void SetLastModifiedTime(
311 const std::string& resource_id,
312 const base::Time& last_modified_time,
313 const google_apis::FileResourceCallback& callback);
315 // Sets the user's permission for an entry specified by |resource_id|.
316 google_apis::DriveApiErrorCode SetUserPermission(
317 const std::string& resource_id,
318 google_apis::drive::PermissionRole user_permission);
320 void AddChangeObserver(ChangeObserver* observer);
321 void RemoveChangeObserver(ChangeObserver* observer);
323 private:
324 struct EntryInfo;
325 struct UploadSession;
327 // Returns a pointer to the entry that matches |resource_id|, or NULL if
328 // not found.
329 EntryInfo* FindEntryByResourceId(const std::string& resource_id);
331 // Returns a new resource ID, which looks like "resource_id_<num>" where
332 // <num> is a monotonically increasing number starting from 1.
333 std::string GetNewResourceId();
335 // Increments |largest_changestamp_| and adds the new changestamp.
336 void AddNewChangestamp(google_apis::ChangeResource* change);
338 // Update ETag of |file| based on |largest_changestamp_|.
339 void UpdateETag(google_apis::FileResource* file);
341 // Adds a new entry based on the given parameters.
342 // |resource_id| can be empty, in the case, the id is automatically generated.
343 // Returns a pointer to the newly added entry, or NULL if failed.
344 const EntryInfo* AddNewEntry(
345 const std::string& resource_id,
346 const std::string& content_type,
347 const std::string& content_data,
348 const std::string& parent_resource_id,
349 const std::string& title,
350 bool shared_with_me);
352 // Core implementation of GetChangeList.
353 // This method returns the slice of the all matched entries, and its range
354 // is between |start_offset| (inclusive) and |start_offset| + |max_results|
355 // (exclusive).
356 // Increments *load_counter by 1 before it returns successfully.
357 void GetChangeListInternal(
358 int64 start_changestamp,
359 const std::string& search_query,
360 const std::string& directory_resource_id,
361 int start_offset,
362 int max_results,
363 int* load_counter,
364 const google_apis::ChangeListCallback& callback);
366 // Returns new upload session URL.
367 GURL GetNewUploadSessionUrl();
369 void NotifyObservers();
371 // The class is expected to run on UI thread.
372 base::ThreadChecker thread_checker_;
374 typedef std::map<std::string, EntryInfo*> EntryInfoMap;
375 EntryInfoMap entries_;
376 scoped_ptr<google_apis::AboutResource> about_resource_;
377 scoped_ptr<base::DictionaryValue> app_info_value_;
378 std::map<GURL, UploadSession> upload_sessions_;
379 int64 published_date_seq_;
380 int64 next_upload_sequence_number_;
381 int default_max_results_;
382 int resource_id_count_;
383 int file_list_load_count_;
384 int change_list_load_count_;
385 int directory_load_count_;
386 int about_resource_load_count_;
387 int app_list_load_count_;
388 int blocked_file_list_load_count_;
389 bool offline_;
390 bool never_return_all_file_list_;
391 base::FilePath last_cancelled_file_;
392 GURL share_url_base_;
393 std::string app_json_template_;
394 std::string open_url_format_;
396 ObserverList<ChangeObserver> change_observers_;
398 base::WeakPtrFactory<FakeDriveService> weak_ptr_factory_;
400 DISALLOW_COPY_AND_ASSIGN(FakeDriveService);
403 } // namespace drive
405 #endif // CHROME_BROWSER_DRIVE_FAKE_DRIVE_SERVICE_H_