blob: 2c6ce36ab2d3970e42282f11c2c0cea359af0703 [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]88e15d2e2013-02-15 10:53:4714#include "chrome/browser/chromeos/drive/drive_file_system_interface.h"
[email protected]78a158b2013-04-23 06:57:4915#include "chrome/browser/chromeos/drive/file_errors.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]d7752262013-03-26 12:56:5928class DriveEntryProto;
[email protected]43309dd92013-04-25 07:28:0329class FileSystemObserver;
[email protected]88e15d2e2013-02-15 10:53:4730
31namespace test_util {
32
33// This class implements a fake DriveFileSystem which acts like a real Drive
34// 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]ec837782013-05-01 07:20:2439class FakeFileSystem : public DriveFileSystemInterface {
[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]88e15d2e2013-02-15 10:53:4749 // DriveFileSystemInterface Overrides.
50 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]cc45a5f2013-04-24 04:49:00138 virtual void IterateCache(const CacheIterateCallback& iteration_callback,
139 const base::Closure& completion_callback) OVERRIDE;
[email protected]88e15d2e2013-02-15 10:53:47140 virtual void Reload() OVERRIDE;
141
142 private:
[email protected]d7752262013-03-26 12:56:59143 // Callback to return the result of GetFilePath.
144 typedef base::Callback<void(const base::FilePath& file_path)>
145 GetFilePathCallback;
146
147 // Returns the path for the |resource_id| via |callback|.
148 // How the method works:
149 // 1) Gets AboutResource from the drive service to obtain root resource id.
150 // 2) Gets ResourceEntry from the drive service to get the base name,
151 // prepends it to the |file_path|. Unless it is root, also tries for
152 // the parent recursively.
153 void GetFilePath(const std::string& resource_id,
154 const GetFilePathCallback& callback);
155 void GetFilePathAfterGetAboutResource(
156 const std::string& resource_id,
157 const GetFilePathCallback& callback,
158 google_apis::GDataErrorCode error,
159 scoped_ptr<google_apis::AboutResource> about_resource);
160 void GetFilePathInternal(
161 const std::string& root_resource_id,
162 const std::string& resource_id,
163 const base::FilePath& file_path,
164 const GetFilePathCallback& callback);
165 void GetFilePathAfterGetResourceEntry(
166 const std::string& root_resource_id,
167 const base::FilePath& remaining_file_path,
168 const GetFilePathCallback& callback,
169 google_apis::GDataErrorCode error_in,
170 scoped_ptr<google_apis::ResourceEntry> resource_entry);
171
172 // Helpers of GetEntryInfoByResourceId.
173 // How the method works:
174 // 1) Gets ResourceEntry from the drive service.
175 // 2) Gets the file path of the resource.
176 // 3) Runs the |callback|.
177 void GetEntryInfoByResourceIdAfterGetResourceEntry(
178 const GetEntryInfoWithFilePathCallback& callback,
179 google_apis::GDataErrorCode error_in,
180 scoped_ptr<google_apis::ResourceEntry> resource_entry);
181 void GetEntryInfoByResourceIdAfterGetFilePath(
182 const GetEntryInfoWithFilePathCallback& callback,
[email protected]78a158b2013-04-23 06:57:49183 FileError error,
[email protected]d7752262013-03-26 12:56:59184 scoped_ptr<DriveEntryProto> entry_proto,
185 const base::FilePath& parent_file_path);
186
[email protected]7e8cd6e32013-04-22 08:01:01187 // Helpers of GetFileContentByPath.
188 // How the method works:
189 // 1) Gets DriveEntryProto of the path.
190 // 2) Look at if there is a cache file or not. If found return it.
191 // 3) Otherwise start DownloadFile.
192 // 4) Runs the |completion_callback| upon the download completion.
193 void GetFileContentByPathAfterGetEntryInfo(
194 const base::FilePath& file_path,
195 const GetFileContentInitializedCallback& initialized_callback,
196 const google_apis::GetContentCallback& get_content_callback,
197 const FileOperationCallback& completion_callback,
[email protected]78a158b2013-04-23 06:57:49198 FileError error,
[email protected]7e8cd6e32013-04-22 08:01:01199 scoped_ptr<DriveEntryProto> entry_proto);
200 void GetFileContentByPathAfterDownloadFile(
201 const FileOperationCallback& completion_callback,
202 google_apis::GDataErrorCode gdata_error,
203 const base::FilePath& temp_file);
204
[email protected]d14c29b2013-04-01 09:44:18205 // Helpers of GetEntryInfoByPath.
206 // How the method works:
207 // 1) If the path is root, gets AboutResrouce from the drive service
208 // and create DriveEntryProto.
209 // 2-1) Otherwise, gets the parent's DriveEntryProto by recursive call.
210 // 2-2) Then, gets the resource list by restricting the parent with its id.
211 // 2-3) Search the results based on title, and return the DriveEntryProto.
212 // Note that adding suffix (e.g. " (2)") for files sharing a same name is
[email protected]ec837782013-05-01 07:20:24213 // not supported in FakeFileSystem. Thus, even if the server has
[email protected]d14c29b2013-04-01 09:44:18214 // files sharing the same name under a directory, the second (or later)
215 // file cannot be taken with the suffixed name.
216 void GetEntryInfoByPathAfterGetAboutResource(
217 const GetEntryInfoCallback& callback,
218 google_apis::GDataErrorCode gdata_error,
219 scoped_ptr<google_apis::AboutResource> about_resource);
220 void GetEntryInfoByPathAfterGetParentEntryInfo(
221 const base::FilePath& base_name,
222 const GetEntryInfoCallback& callback,
[email protected]78a158b2013-04-23 06:57:49223 FileError error,
[email protected]d14c29b2013-04-01 09:44:18224 scoped_ptr<DriveEntryProto> parent_entry_proto);
225 void GetEntryInfoByPathAfterGetResourceList(
226 const base::FilePath& base_name,
227 const GetEntryInfoCallback& callback,
228 google_apis::GDataErrorCode gdata_error,
229 scoped_ptr<google_apis::ResourceList> resource_list);
230
[email protected]d7752262013-03-26 12:56:59231 google_apis::DriveServiceInterface* drive_service_; // Not owned.
[email protected]7e8cd6e32013-04-22 08:01:01232 base::ScopedTempDir cache_dir_;
[email protected]d7752262013-03-26 12:56:59233
234 // Note: This should remain the last member so it'll be destroyed and
235 // invalidate the weak pointers before any other members are destroyed.
[email protected]ec837782013-05-01 07:20:24236 base::WeakPtrFactory<FakeFileSystem> weak_ptr_factory_;
[email protected]d7752262013-03-26 12:56:59237
[email protected]ec837782013-05-01 07:20:24238 DISALLOW_COPY_AND_ASSIGN(FakeFileSystem);
[email protected]88e15d2e2013-02-15 10:53:47239};
240
241} // namespace test_util
242} // namespace drive
243
[email protected]ec837782013-05-01 07:20:24244#endif // CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_