1 // 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.
5 #ifndef COMPONENTS_DRIVE_FAKE_FILE_SYSTEM_H_
6 #define COMPONENTS_DRIVE_FAKE_FILE_SYSTEM_H_
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "components/drive/file_errors.h"
15 #include "components/drive/file_system_interface.h"
16 #include "google_apis/drive/drive_api_error_codes.h"
18 namespace google_apis
{
23 } // namespace google_apis
27 class DriveServiceInterface
;
28 class FileSystemObserver
;
33 // This class implements a fake FileSystem 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
38 // Currently most methods are empty (not implemented).
39 class FakeFileSystem
: public FileSystemInterface
{
41 explicit FakeFileSystem(DriveServiceInterface
* drive_service
);
42 ~FakeFileSystem() override
;
44 // FileSystemInterface Overrides.
45 void AddObserver(FileSystemObserver
* observer
) override
;
46 void RemoveObserver(FileSystemObserver
* observer
) override
;
47 void CheckForUpdates() override
;
48 void TransferFileFromLocalToRemote(
49 const base::FilePath
& local_src_file_path
,
50 const base::FilePath
& remote_dest_file_path
,
51 const FileOperationCallback
& callback
) override
;
52 void OpenFile(const base::FilePath
& file_path
,
54 const std::string
& mime_type
,
55 const OpenFileCallback
& callback
) override
;
56 void Copy(const base::FilePath
& src_file_path
,
57 const base::FilePath
& dest_file_path
,
58 bool preserve_last_modified
,
59 const FileOperationCallback
& callback
) override
;
60 void Move(const base::FilePath
& src_file_path
,
61 const base::FilePath
& dest_file_path
,
62 const FileOperationCallback
& callback
) override
;
63 void Remove(const base::FilePath
& file_path
,
65 const FileOperationCallback
& callback
) override
;
66 void CreateDirectory(const base::FilePath
& directory_path
,
69 const FileOperationCallback
& callback
) override
;
70 void CreateFile(const base::FilePath
& file_path
,
72 const std::string
& mime_type
,
73 const FileOperationCallback
& callback
) override
;
74 void TouchFile(const base::FilePath
& file_path
,
75 const base::Time
& last_access_time
,
76 const base::Time
& last_modified_time
,
77 const FileOperationCallback
& callback
) override
;
78 void TruncateFile(const base::FilePath
& file_path
,
80 const FileOperationCallback
& callback
) override
;
81 void Pin(const base::FilePath
& file_path
,
82 const FileOperationCallback
& callback
) override
;
83 void Unpin(const base::FilePath
& file_path
,
84 const FileOperationCallback
& callback
) override
;
85 void GetFile(const base::FilePath
& file_path
,
86 const GetFileCallback
& callback
) override
;
87 void GetFileForSaving(const base::FilePath
& file_path
,
88 const GetFileCallback
& callback
) override
;
89 base::Closure
GetFileContent(
90 const base::FilePath
& file_path
,
91 const GetFileContentInitializedCallback
& initialized_callback
,
92 const google_apis::GetContentCallback
& get_content_callback
,
93 const FileOperationCallback
& completion_callback
) override
;
94 void GetResourceEntry(const base::FilePath
& file_path
,
95 const GetResourceEntryCallback
& callback
) override
;
96 void ReadDirectory(const base::FilePath
& file_path
,
97 const ReadDirectoryEntriesCallback
& entries_callback
,
98 const FileOperationCallback
& completion_callback
) override
;
99 void Search(const std::string
& search_query
,
100 const GURL
& next_link
,
101 const SearchCallback
& callback
) override
;
102 void SearchMetadata(const std::string
& query
,
104 int at_most_num_matches
,
105 const SearchMetadataCallback
& callback
) override
;
106 void SearchByHashes(const std::set
<std::string
>& hashes
,
107 const SearchByHashesCallback
& callback
) override
;
108 void GetAvailableSpace(const GetAvailableSpaceCallback
& callback
) override
;
109 void GetShareUrl(const base::FilePath
& file_path
,
110 const GURL
& embed_origin
,
111 const GetShareUrlCallback
& callback
) override
;
112 void GetMetadata(const GetFilesystemMetadataCallback
& callback
) override
;
113 void MarkCacheFileAsMounted(const base::FilePath
& drive_file_path
,
114 const MarkMountedCallback
& callback
) override
;
115 void MarkCacheFileAsUnmounted(const base::FilePath
& cache_file_path
,
116 const FileOperationCallback
& callback
) override
;
117 void AddPermission(const base::FilePath
& drive_file_path
,
118 const std::string
& email
,
119 google_apis::drive::PermissionRole role
,
120 const FileOperationCallback
& callback
) override
;
121 void SetProperty(const base::FilePath
& drive_file_path
,
122 google_apis::drive::Property::Visibility visibility
,
123 const std::string
& key
,
124 const std::string
& value
,
125 const FileOperationCallback
& callback
) override
;
126 void Reset(const FileOperationCallback
& callback
) override
;
127 void GetPathFromResourceId(const std::string
& resource_id
,
128 const GetFilePathCallback
& callback
) override
;
129 void FreeDiskSpaceIfNeededFor(int64 num_bytes
,
130 const FreeDiskSpaceCallback
& callback
) override
;
131 void CalculateEvictableCacheSize(
132 const EvictableCacheSizeCallback
& callback
) override
;
135 // Helpers of GetFileContent.
136 // How the method works:
137 // 1) Gets ResourceEntry of the path.
138 // 2) Look at if there is a cache file or not. If found return it.
139 // 3) Otherwise start DownloadFile.
140 // 4) Runs the |completion_callback| upon the download completion.
141 void GetFileContentAfterGetResourceEntry(
142 const GetFileContentInitializedCallback
& initialized_callback
,
143 const google_apis::GetContentCallback
& get_content_callback
,
144 const FileOperationCallback
& completion_callback
,
146 scoped_ptr
<ResourceEntry
> entry
);
147 void GetFileContentAfterGetFileResource(
148 const GetFileContentInitializedCallback
& initialized_callback
,
149 const google_apis::GetContentCallback
& get_content_callback
,
150 const FileOperationCallback
& completion_callback
,
151 google_apis::DriveApiErrorCode gdata_error
,
152 scoped_ptr
<google_apis::FileResource
> gdata_entry
);
153 void GetFileContentAfterDownloadFile(
154 const FileOperationCallback
& completion_callback
,
155 google_apis::DriveApiErrorCode gdata_error
,
156 const base::FilePath
& temp_file
);
158 // Helpers of GetResourceEntry.
159 // How the method works:
160 // 1) If the path is root, gets AboutResrouce from the drive service
161 // and create ResourceEntry.
162 // 2-1) Otherwise, gets the parent's ResourceEntry by recursive call.
163 // 2-2) Then, gets the resource list by restricting the parent with its id.
164 // 2-3) Search the results based on title, and return the ResourceEntry.
165 // Note that adding suffix (e.g. " (2)") for files sharing a same name is
166 // not supported in FakeFileSystem. Thus, even if the server has
167 // files sharing the same name under a directory, the second (or later)
168 // file cannot be taken with the suffixed name.
169 void GetResourceEntryAfterGetAboutResource(
170 const GetResourceEntryCallback
& callback
,
171 google_apis::DriveApiErrorCode gdata_error
,
172 scoped_ptr
<google_apis::AboutResource
> about_resource
);
173 void GetResourceEntryAfterGetParentEntryInfo(
174 const base::FilePath
& base_name
,
175 const GetResourceEntryCallback
& callback
,
177 scoped_ptr
<ResourceEntry
> parent_entry
);
178 void GetResourceEntryAfterGetFileList(
179 const base::FilePath
& base_name
,
180 const GetResourceEntryCallback
& callback
,
181 google_apis::DriveApiErrorCode gdata_error
,
182 scoped_ptr
<google_apis::FileList
> file_list
);
184 DriveServiceInterface
* drive_service_
; // Not owned.
185 base::ScopedTempDir cache_dir_
;
187 // Note: This should remain the last member so it'll be destroyed and
188 // invalidate the weak pointers before any other members are destroyed.
189 base::WeakPtrFactory
<FakeFileSystem
> weak_ptr_factory_
;
191 DISALLOW_COPY_AND_ASSIGN(FakeFileSystem
);
194 } // namespace test_util
197 #endif // COMPONENTS_DRIVE_FAKE_FILE_SYSTEM_H_