blob: db96933a5b0936db558f95fb1de86f6fdf390661 [file] [log] [blame]
[email protected]88e15d2e2013-02-15 10:53:471// Copyright (c) 2013 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.
4
[email protected]ec837782013-05-01 07:20:245#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_
6#define CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_
[email protected]88e15d2e2013-02-15 10:53:477
8#include <string>
9
10#include "base/basictypes.h"
[email protected]d7752262013-03-26 12:56:5911#include "base/callback_forward.h"
[email protected]7e8cd6e32013-04-22 08:01:0112#include "base/files/scoped_temp_dir.h"
[email protected]88e15d2e2013-02-15 10:53:4713#include "base/memory/scoped_ptr.h"
[email protected]78a158b2013-04-23 06:57:4914#include "chrome/browser/chromeos/drive/file_errors.h"
[email protected]976309f12013-05-02 01:19:1515#include "chrome/browser/chromeos/drive/file_system_interface.h"
[email protected]d7752262013-03-26 12:56:5916#include "chrome/browser/google_apis/gdata_errorcode.h"
[email protected]88e15d2e2013-02-15 10:53:4717
18namespace google_apis {
19
[email protected]d7752262013-03-26 12:56:5920class AboutResource;
21class DriveServiceInterface;
[email protected]88e15d2e2013-02-15 10:53:4722class ResourceEntry;
23
24} // namespace google_apis
25
26namespace drive {
27
[email protected]43309dd92013-04-25 07:28:0328class FileSystemObserver;
[email protected]67241b832013-05-02 04:55:5229class ResourceEntry;
[email protected]88e15d2e2013-02-15 10:53:4730
31namespace test_util {
32
[email protected]0d52ed52013-05-01 08:21:2133// This class implements a fake FileSystem which acts like a real Drive
[email protected]88e15d2e2013-02-15 10:53:4734// file system with FakeDriveService, for testing purpose.
35// Note that this class doesn't support "caching" at the moment, so the number
36// of interactions to the FakeDriveService may be bigger than the real
37// implementation.
38// Currently most methods are empty (not implemented).
[email protected]976309f12013-05-02 01:19:1539class FakeFileSystem : public FileSystemInterface {
[email protected]88e15d2e2013-02-15 10:53:4740 public:
[email protected]ec837782013-05-01 07:20:2441 explicit FakeFileSystem(
[email protected]d7752262013-03-26 12:56:5942 google_apis::DriveServiceInterface* drive_service);
[email protected]ec837782013-05-01 07:20:2443 virtual ~FakeFileSystem();
[email protected]88e15d2e2013-02-15 10:53:4744
[email protected]7e8cd6e32013-04-22 08:01:0145 // Initialization for testing. This can be called instead of Initialize
46 // for testing purpose. Returns true for success.
47 bool InitializeForTesting();
48
[email protected]976309f12013-05-02 01:19:1549 // FileSystemInterface Overrides.
[email protected]88e15d2e2013-02-15 10:53:4750 virtual void Initialize() OVERRIDE;
[email protected]43309dd92013-04-25 07:28:0351 virtual void AddObserver(FileSystemObserver* observer) OVERRIDE;
52 virtual void RemoveObserver(FileSystemObserver* observer) OVERRIDE;
[email protected]88e15d2e2013-02-15 10:53:4753 virtual void CheckForUpdates() OVERRIDE;
54 virtual void GetEntryInfoByResourceId(
55 const std::string& resource_id,
56 const GetEntryInfoWithFilePathCallback& callback) OVERRIDE;
57 virtual void TransferFileFromRemoteToLocal(
58 const base::FilePath& remote_src_file_path,
59 const base::FilePath& local_dest_file_path,
60 const FileOperationCallback& callback) OVERRIDE;
61 virtual void TransferFileFromLocalToRemote(
62 const base::FilePath& local_src_file_path,
63 const base::FilePath& remote_dest_file_path,
64 const FileOperationCallback& callback) OVERRIDE;
65 virtual void OpenFile(const base::FilePath& file_path,
66 const OpenFileCallback& callback) OVERRIDE;
67 virtual void CloseFile(const base::FilePath& file_path,
68 const FileOperationCallback& callback) OVERRIDE;
69 virtual void Copy(const base::FilePath& src_file_path,
70 const base::FilePath& dest_file_path,
71 const FileOperationCallback& callback) OVERRIDE;
72 virtual void Move(const base::FilePath& src_file_path,
73 const base::FilePath& dest_file_path,
74 const FileOperationCallback& callback) OVERRIDE;
75 virtual void Remove(const base::FilePath& file_path,
76 bool is_recursive,
77 const FileOperationCallback& callback) OVERRIDE;
78 virtual void CreateDirectory(const base::FilePath& directory_path,
79 bool is_exclusive,
80 bool is_recursive,
81 const FileOperationCallback& callback) OVERRIDE;
82 virtual void CreateFile(const base::FilePath& file_path,
83 bool is_exclusive,
84 const FileOperationCallback& callback) OVERRIDE;
[email protected]378b6132013-04-19 18:05:1885 virtual void Pin(const base::FilePath& file_path,
86 const FileOperationCallback& callback) OVERRIDE;
87 virtual void Unpin(const base::FilePath& file_path,
88 const FileOperationCallback& callback) OVERRIDE;
[email protected]88e15d2e2013-02-15 10:53:4789 virtual void GetFileByPath(const base::FilePath& file_path,
90 const GetFileCallback& callback) OVERRIDE;
91 virtual void GetFileByResourceId(
92 const std::string& resource_id,
93 const DriveClientContext& context,
94 const GetFileCallback& get_file_callback,
95 const google_apis::GetContentCallback& get_content_callback) OVERRIDE;
[email protected]8e6e51f82013-04-16 05:04:0096 virtual void GetFileContentByPath(
97 const base::FilePath& file_path,
98 const GetFileContentInitializedCallback& initialized_callback,
99 const google_apis::GetContentCallback& get_content_callback,
100 const FileOperationCallback& completion_callback) OVERRIDE;
[email protected]88e15d2e2013-02-15 10:53:47101 virtual void UpdateFileByResourceId(
102 const std::string& resource_id,
103 const DriveClientContext& context,
104 const FileOperationCallback& callback) OVERRIDE;
105 virtual void GetEntryInfoByPath(
106 const base::FilePath& file_path,
107 const GetEntryInfoCallback& callback) OVERRIDE;
108 virtual void ReadDirectoryByPath(
109 const base::FilePath& file_path,
110 const ReadDirectoryWithSettingCallback& callback) OVERRIDE;
[email protected]6d77798882013-03-06 01:25:24111 virtual void RefreshDirectory(
112 const base::FilePath& file_path,
113 const FileOperationCallback& callback) OVERRIDE;
[email protected]88e15d2e2013-02-15 10:53:47114 virtual void Search(const std::string& search_query,
[email protected]88e15d2e2013-02-15 10:53:47115 const GURL& next_feed,
116 const SearchCallback& callback) OVERRIDE;
[email protected]14b69c52013-03-27 09:48:47117 virtual void SearchMetadata(const std::string& query,
[email protected]48c884ca2013-04-09 12:40:01118 int options,
[email protected]14b69c52013-03-27 09:48:47119 int at_most_num_matches,
120 const SearchMetadataCallback& callback) OVERRIDE;
[email protected]88e15d2e2013-02-15 10:53:47121 virtual void GetAvailableSpace(
122 const GetAvailableSpaceCallback& callback) OVERRIDE;
[email protected]ec529fa2013-03-07 10:22:17123 virtual void AddUploadedFile(scoped_ptr<google_apis::ResourceEntry> doc_entry,
[email protected]88e15d2e2013-02-15 10:53:47124 const base::FilePath& file_content_path,
125 const FileOperationCallback& callback) OVERRIDE;
126 virtual void GetMetadata(
127 const GetFilesystemMetadataCallback& callback) OVERRIDE;
[email protected]48348d5c2013-04-22 17:44:22128 virtual void MarkCacheFileAsMounted(
129 const base::FilePath& drive_file_path,
130 const OpenFileCallback& callback) OVERRIDE;
131 virtual void MarkCacheFileAsUnmounted(
132 const base::FilePath& cache_file_path,
133 const FileOperationCallback& callback) OVERRIDE;
[email protected]45dff6d2013-04-23 17:47:31134 virtual void GetCacheEntryByResourceId(
135 const std::string& resource_id,
136 const std::string& md5,
137 const GetCacheEntryCallback& callback) OVERRIDE;
[email protected]88e15d2e2013-02-15 10:53:47138 virtual void Reload() OVERRIDE;
139
140 private:
[email protected]d7752262013-03-26 12:56:59141 // Callback to return the result of GetFilePath.
142 typedef base::Callback<void(const base::FilePath& file_path)>
143 GetFilePathCallback;
144
145 // Returns the path for the |resource_id| via |callback|.
146 // How the method works:
147 // 1) Gets AboutResource from the drive service to obtain root resource id.
148 // 2) Gets ResourceEntry from the drive service to get the base name,
149 // prepends it to the |file_path|. Unless it is root, also tries for
150 // the parent recursively.
151 void GetFilePath(const std::string& resource_id,
152 const GetFilePathCallback& callback);
153 void GetFilePathAfterGetAboutResource(
154 const std::string& resource_id,
155 const GetFilePathCallback& callback,
156 google_apis::GDataErrorCode error,
157 scoped_ptr<google_apis::AboutResource> about_resource);
158 void GetFilePathInternal(
159 const std::string& root_resource_id,
160 const std::string& resource_id,
161 const base::FilePath& file_path,
162 const GetFilePathCallback& callback);
163 void GetFilePathAfterGetResourceEntry(
164 const std::string& root_resource_id,
165 const base::FilePath& remaining_file_path,
166 const GetFilePathCallback& callback,
167 google_apis::GDataErrorCode error_in,
168 scoped_ptr<google_apis::ResourceEntry> resource_entry);
169
170 // Helpers of GetEntryInfoByResourceId.
171 // How the method works:
172 // 1) Gets ResourceEntry from the drive service.
173 // 2) Gets the file path of the resource.
174 // 3) Runs the |callback|.
175 void GetEntryInfoByResourceIdAfterGetResourceEntry(
176 const GetEntryInfoWithFilePathCallback& callback,
177 google_apis::GDataErrorCode error_in,
178 scoped_ptr<google_apis::ResourceEntry> resource_entry);
179 void GetEntryInfoByResourceIdAfterGetFilePath(
180 const GetEntryInfoWithFilePathCallback& callback,
[email protected]78a158b2013-04-23 06:57:49181 FileError error,
[email protected]67241b832013-05-02 04:55:52182 scoped_ptr<ResourceEntry> entry,
[email protected]d7752262013-03-26 12:56:59183 const base::FilePath& parent_file_path);
184
[email protected]7e8cd6e32013-04-22 08:01:01185 // Helpers of GetFileContentByPath.
186 // How the method works:
[email protected]67241b832013-05-02 04:55:52187 // 1) Gets ResourceEntry of the path.
[email protected]7e8cd6e32013-04-22 08:01:01188 // 2) Look at if there is a cache file or not. If found return it.
189 // 3) Otherwise start DownloadFile.
190 // 4) Runs the |completion_callback| upon the download completion.
191 void GetFileContentByPathAfterGetEntryInfo(
192 const base::FilePath& file_path,
193 const GetFileContentInitializedCallback& initialized_callback,
194 const google_apis::GetContentCallback& get_content_callback,
195 const FileOperationCallback& completion_callback,
[email protected]78a158b2013-04-23 06:57:49196 FileError error,
[email protected]67241b832013-05-02 04:55:52197 scoped_ptr<ResourceEntry> entry);
[email protected]5b82c9d2013-05-10 03:06:27198 void GetFileContentByPathAfterGetResourceEntry(
199 const base::FilePath& file_path,
200 const GetFileContentInitializedCallback& initialized_callback,
201 const google_apis::GetContentCallback& get_content_callback,
202 const FileOperationCallback& completion_callback,
203 google_apis::GDataErrorCode gdata_error,
204 scoped_ptr<google_apis::ResourceEntry> gdata_entry);
[email protected]7e8cd6e32013-04-22 08:01:01205 void GetFileContentByPathAfterDownloadFile(
206 const FileOperationCallback& completion_callback,
207 google_apis::GDataErrorCode gdata_error,
208 const base::FilePath& temp_file);
209
[email protected]d14c29b2013-04-01 09:44:18210 // Helpers of GetEntryInfoByPath.
211 // How the method works:
212 // 1) If the path is root, gets AboutResrouce from the drive service
[email protected]67241b832013-05-02 04:55:52213 // and create ResourceEntry.
214 // 2-1) Otherwise, gets the parent's ResourceEntry by recursive call.
[email protected]d14c29b2013-04-01 09:44:18215 // 2-2) Then, gets the resource list by restricting the parent with its id.
[email protected]67241b832013-05-02 04:55:52216 // 2-3) Search the results based on title, and return the ResourceEntry.
[email protected]d14c29b2013-04-01 09:44:18217 // Note that adding suffix (e.g. " (2)") for files sharing a same name is
[email protected]ec837782013-05-01 07:20:24218 // not supported in FakeFileSystem. Thus, even if the server has
[email protected]d14c29b2013-04-01 09:44:18219 // files sharing the same name under a directory, the second (or later)
220 // file cannot be taken with the suffixed name.
221 void GetEntryInfoByPathAfterGetAboutResource(
222 const GetEntryInfoCallback& callback,
223 google_apis::GDataErrorCode gdata_error,
224 scoped_ptr<google_apis::AboutResource> about_resource);
225 void GetEntryInfoByPathAfterGetParentEntryInfo(
226 const base::FilePath& base_name,
227 const GetEntryInfoCallback& callback,
[email protected]78a158b2013-04-23 06:57:49228 FileError error,
[email protected]67241b832013-05-02 04:55:52229 scoped_ptr<ResourceEntry> parent_entry);
[email protected]d14c29b2013-04-01 09:44:18230 void GetEntryInfoByPathAfterGetResourceList(
231 const base::FilePath& base_name,
232 const GetEntryInfoCallback& callback,
233 google_apis::GDataErrorCode gdata_error,
234 scoped_ptr<google_apis::ResourceList> resource_list);
235
[email protected]d7752262013-03-26 12:56:59236 google_apis::DriveServiceInterface* drive_service_; // Not owned.
[email protected]7e8cd6e32013-04-22 08:01:01237 base::ScopedTempDir cache_dir_;
[email protected]d7752262013-03-26 12:56:59238
239 // Note: This should remain the last member so it'll be destroyed and
240 // invalidate the weak pointers before any other members are destroyed.
[email protected]ec837782013-05-01 07:20:24241 base::WeakPtrFactory<FakeFileSystem> weak_ptr_factory_;
[email protected]d7752262013-03-26 12:56:59242
[email protected]ec837782013-05-01 07:20:24243 DISALLOW_COPY_AND_ASSIGN(FakeFileSystem);
[email protected]88e15d2e2013-02-15 10:53:47244};
245
246} // namespace test_util
247} // namespace drive
248
[email protected]ec837782013-05-01 07:20:24249#endif // CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_