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_GOOGLE_APIS_FAKE_DRIVE_SERVICE_H_
6 #define CHROME_BROWSER_GOOGLE_APIS_FAKE_DRIVE_SERVICE_H_
8 #include "base/files/file_path.h"
9 #include "base/values.h"
10 #include "chrome/browser/google_apis/drive_service_interface.h"
12 namespace google_apis
{
14 // This class implements a fake DriveService which acts like a real Drive
15 // service. The fake service works as follows:
17 // 1) Load JSON files and construct the in-memory resource list.
18 // 2) Return valid responses based on the the in-memory resource list.
19 // 3) Update the in-memory resource list by operations like DeleteResource().
20 class FakeDriveService
: public DriveServiceInterface
{
23 virtual ~FakeDriveService();
25 // Loads the resource list for WAPI. Returns true on success.
26 bool LoadResourceListForWapi(const std::string
& relative_path
);
28 // Loads the account metadata for WAPI. Returns true on success. Also adds
29 // the largest changestamp in the account metadata to the existing
30 // entries. The changestamp information will be used to generate change
31 // lists in GetResourceList() when non-zero |start_changestamp| is
33 bool LoadAccountMetadataForWapi(const std::string
& relative_path
);
35 // Loads the app list for Drive API. Returns true on success.
36 bool LoadAppListForDriveApi(const std::string
& relative_path
);
38 // Changes the offline state. All functions fail with GDATA_NO_CONNECTION
39 // when offline. By default the offline state is false.
40 void set_offline(bool offline
) { offline_
= offline
; }
42 // Changes the default max results returned from GetResourceList().
43 // By default, it's set to 0, which is unlimited.
44 void set_default_max_results(int default_max_results
) {
45 default_max_results_
= default_max_results
;
48 // Returns the largest changestamp, which starts from 0 by default. See
49 // also comments at LoadAccountMetadataForWapi().
50 int64
largest_changestamp() const { return largest_changestamp_
; }
52 // Returns the number of times the resource list is successfully loaded by
54 int resource_list_load_count() const { return resource_list_load_count_
; }
56 // Returns the number of times the account metadata is successfully loaded
57 // by GetAccountMetadata().
58 int account_metadata_load_count() const {
59 return account_metadata_load_count_
;
62 // Returns the number of times the about resource is successfully loaded
63 // by GetAboutResource().
64 int about_resource_load_count() const {
65 return about_resource_load_count_
;
68 // Returns the file path whose operation is cancelled just before this method
70 const base::FilePath
& last_cancelled_file() const {
71 return last_cancelled_file_
;
74 // Returns the (fake) URL for the link.
75 static GURL
GetFakeLinkUrl(const std::string
& resource_id
);
77 // DriveServiceInterface Overrides
78 virtual void Initialize(Profile
* profile
) OVERRIDE
;
79 virtual void AddObserver(DriveServiceObserver
* observer
) OVERRIDE
;
80 virtual void RemoveObserver(DriveServiceObserver
* observer
) OVERRIDE
;
81 virtual bool CanStartOperation() const OVERRIDE
;
82 virtual void CancelAll() OVERRIDE
;
83 virtual bool CancelForFilePath(const base::FilePath
& file_path
) OVERRIDE
;
84 virtual OperationProgressStatusList
GetProgressStatusList() const OVERRIDE
;
85 virtual std::string
GetRootResourceId() const OVERRIDE
;
86 virtual bool HasAccessToken() const OVERRIDE
;
87 virtual bool HasRefreshToken() const OVERRIDE
;
88 virtual void ClearAccessToken() OVERRIDE
;
89 virtual void ClearRefreshToken() OVERRIDE
;
90 virtual void GetAllResourceList(
91 const GetResourceListCallback
& callback
) OVERRIDE
;
92 virtual void GetResourceListInDirectory(
93 const std::string
& directory_resource_id
,
94 const GetResourceListCallback
& callback
) OVERRIDE
;
95 // See the comment for EntryMatchWidthQuery() in .cc file for details about
96 // the supported search query types.
98 const std::string
& search_query
,
99 const GetResourceListCallback
& callback
) OVERRIDE
;
100 virtual void SearchInDirectory(
101 const std::string
& search_query
,
102 const std::string
& directory_resource_id
,
103 const GetResourceListCallback
& callback
) OVERRIDE
;
104 virtual void GetChangeList(
105 int64 start_changestamp
,
106 const GetResourceListCallback
& callback
) OVERRIDE
;
107 virtual void ContinueGetResourceList(
108 const GURL
& override_url
,
109 const GetResourceListCallback
& callback
) OVERRIDE
;
110 virtual void GetResourceEntry(
111 const std::string
& resource_id
,
112 const GetResourceEntryCallback
& callback
) OVERRIDE
;
113 virtual void GetAccountMetadata(
114 const GetAccountMetadataCallback
& callback
) OVERRIDE
;
115 virtual void GetAboutResource(
116 const GetAboutResourceCallback
& callback
) OVERRIDE
;
117 virtual void GetAppList(const GetAppListCallback
& callback
) OVERRIDE
;
118 virtual void DeleteResource(const std::string
& resource_id
,
119 const std::string
& etag
,
120 const EntryActionCallback
& callback
) OVERRIDE
;
121 virtual void DownloadFile(
122 const base::FilePath
& virtual_path
,
123 const base::FilePath
& local_cache_path
,
124 const GURL
& download_url
,
125 const DownloadActionCallback
& download_action_callback
,
126 const GetContentCallback
& get_content_callback
,
127 const ProgressCallback
& progress_callback
) OVERRIDE
;
128 // The new resource ID for the copied document will look like
129 // |resource_id| + "_copied".
130 virtual void CopyHostedDocument(
131 const std::string
& resource_id
,
132 const std::string
& new_name
,
133 const GetResourceEntryCallback
& callback
) OVERRIDE
;
134 virtual void RenameResource(const std::string
& resource_id
,
135 const std::string
& new_name
,
136 const EntryActionCallback
& callback
) OVERRIDE
;
137 virtual void AddResourceToDirectory(
138 const std::string
& parent_resource_id
,
139 const std::string
& resource_id
,
140 const EntryActionCallback
& callback
) OVERRIDE
;
141 virtual void RemoveResourceFromDirectory(
142 const std::string
& parent_resource_id
,
143 const std::string
& resource_id
,
144 const EntryActionCallback
& callback
) OVERRIDE
;
145 virtual void AddNewDirectory(
146 const std::string
& parent_resource_id
,
147 const std::string
& directory_name
,
148 const GetResourceEntryCallback
& callback
) OVERRIDE
;
149 virtual void InitiateUploadNewFile(
150 const base::FilePath
& drive_file_path
,
151 const std::string
& content_type
,
152 int64 content_length
,
153 const std::string
& parent_resource_id
,
154 const std::string
& title
,
155 const InitiateUploadCallback
& callback
) OVERRIDE
;
156 virtual void InitiateUploadExistingFile(
157 const base::FilePath
& drive_file_path
,
158 const std::string
& content_type
,
159 int64 content_length
,
160 const std::string
& resource_id
,
161 const std::string
& etag
,
162 const InitiateUploadCallback
& callback
) OVERRIDE
;
163 virtual void ResumeUpload(
164 UploadMode upload_mode
,
165 const base::FilePath
& drive_file_path
,
166 const GURL
& upload_url
,
167 int64 start_position
,
169 int64 content_length
,
170 const std::string
& content_type
,
171 const scoped_refptr
<net::IOBuffer
>& buf
,
172 const UploadRangeCallback
& callback
,
173 const ProgressCallback
& progress_callback
) OVERRIDE
;
174 virtual void GetUploadStatus(
175 UploadMode upload_mode
,
176 const base::FilePath
& drive_file_path
,
177 const GURL
& upload_url
,
178 int64 content_length
,
179 const UploadRangeCallback
& callback
) OVERRIDE
;
180 virtual void AuthorizeApp(const std::string
& resource_id
,
181 const std::string
& app_id
,
182 const AuthorizeAppCallback
& callback
) OVERRIDE
;
184 // Adds a new file with the given parameters. On success, returns
185 // HTTP_CREATED with the parsed entry.
186 // |callback| must not be null.
187 void AddNewFile(const std::string
& content_type
,
188 int64 content_length
,
189 const std::string
& parent_resource_id
,
190 const std::string
& title
,
191 const GetResourceEntryCallback
& callback
);
193 // Sets the last modified time for an entry specified by |resource_id|.
194 // On success, returns HTTP_SUCCESS with the parsed entry.
195 // |callback| must not be null.
196 void SetLastModifiedTime(const std::string
& resource_id
,
197 const base::Time
& last_modified_time
,
198 const GetResourceEntryCallback
& callback
);
200 // Returns a pointer to the entry that matches |resource_id|, or NULL if
202 base::DictionaryValue
* FindEntryByResourceId(const std::string
& resource_id
);
204 // Returns a pointer to the entry that matches |content_url|, or NULL if
206 base::DictionaryValue
* FindEntryByContentUrl(const GURL
& content_url
);
208 // Returns a pointer to the entry that matches |upload_url|, or NULL if
210 base::DictionaryValue
* FindEntryByUploadUrl(const GURL
& upload_url
);
212 // Returns a new resource ID, which looks like "resource_id_<num>" where
213 // <num> is a monotonically increasing number starting from 1.
214 std::string
GetNewResourceId();
216 // Increments |largest_changestamp_| and adds the new changestamp to
218 void AddNewChangestamp(base::DictionaryValue
* entry
);
220 // Adds a new entry based on the given parameters. |entry_kind| should be
221 // "file" or "folder". Returns a pointer to the newly added entry, or NULL
223 const base::DictionaryValue
* AddNewEntry(
224 const std::string
& content_type
,
225 int64 content_length
,
226 const std::string
& parent_resource_id
,
227 const std::string
& title
,
228 const std::string
& entry_kind
);
230 // Core implementation of GetResourceList.
231 // This method returns the slice of the all matched entries, and its range
232 // is between |start_offset| (inclusive) and |start_offset| + |max_results|
234 void GetResourceListInternal(int64 start_changestamp
,
235 const std::string
& search_query
,
236 const std::string
& directory_resource_id
,
239 const GetResourceListCallback
& callback
);
241 scoped_ptr
<base::Value
> resource_list_value_
;
242 scoped_ptr
<base::Value
> account_metadata_value_
;
243 scoped_ptr
<base::Value
> app_info_value_
;
244 int64 largest_changestamp_
;
245 int default_max_results_
;
246 int resource_id_count_
;
247 int resource_list_load_count_
;
248 int account_metadata_load_count_
;
249 int about_resource_load_count_
;
251 base::FilePath last_cancelled_file_
;
253 DISALLOW_COPY_AND_ASSIGN(FakeDriveService
);
256 } // namespace google_apis
258 #endif // CHROME_BROWSER_GOOGLE_APIS_FAKE_DRIVE_SERVICE_H_