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 COMPONENTS_DRIVE_FILE_SYSTEM_H_
6 #define COMPONENTS_DRIVE_FILE_SYSTEM_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/threading/thread_checker.h"
15 #include "components/drive/change_list_loader_observer.h"
16 #include "components/drive/file_system/operation_delegate.h"
17 #include "components/drive/file_system_interface.h"
18 #include "google_apis/drive/drive_api_error_codes.h"
23 class SequencedTaskRunner
;
26 namespace google_apis
{
29 } // namespace google_apis
35 class FileSystemObserver
;
39 class AboutResourceLoader
;
40 class ChangeListLoader
;
41 class DirectoryLoader
;
43 class LoaderController
;
44 class ResourceMetadata
;
46 } // namespace internal
48 namespace file_system
{
50 class CreateDirectoryOperation
;
51 class CreateFileOperation
;
52 class DownloadOperation
;
53 class GetFileForSavingOperation
;
55 class OpenFileOperation
;
56 class RemoveOperation
;
57 class SearchOperation
;
58 class SetPropertyOperation
;
60 class TruncateOperation
;
61 } // namespace file_system
63 // The production implementation of FileSystemInterface.
64 class FileSystem
: public FileSystemInterface
,
65 public internal::ChangeListLoaderObserver
,
66 public file_system::OperationDelegate
{
68 FileSystem(PrefService
* pref_service
,
70 internal::FileCache
* cache
,
71 JobScheduler
* scheduler
,
72 internal::ResourceMetadata
* resource_metadata
,
73 base::SequencedTaskRunner
* blocking_task_runner
,
74 base::SingleThreadTaskRunner
* file_task_runner
,
75 const base::FilePath
& temporary_file_directory
);
76 ~FileSystem() override
;
78 // FileSystemInterface overrides.
79 void AddObserver(FileSystemObserver
* observer
) override
;
80 void RemoveObserver(FileSystemObserver
* observer
) override
;
81 void CheckForUpdates() override
;
82 void Search(const std::string
& search_query
,
83 const GURL
& next_link
,
84 const SearchCallback
& callback
) override
;
85 void SearchMetadata(const std::string
& query
,
87 int at_most_num_matches
,
88 const SearchMetadataCallback
& callback
) override
;
89 void SearchByHashes(const std::set
<std::string
>& hashes
,
90 const SearchByHashesCallback
& callback
) override
;
91 void TransferFileFromLocalToRemote(
92 const base::FilePath
& local_src_file_path
,
93 const base::FilePath
& remote_dest_file_path
,
94 const FileOperationCallback
& callback
) override
;
95 void OpenFile(const base::FilePath
& file_path
,
97 const std::string
& mime_type
,
98 const OpenFileCallback
& callback
) override
;
99 void Copy(const base::FilePath
& src_file_path
,
100 const base::FilePath
& dest_file_path
,
101 bool preserve_last_modified
,
102 const FileOperationCallback
& callback
) override
;
103 void Move(const base::FilePath
& src_file_path
,
104 const base::FilePath
& dest_file_path
,
105 const FileOperationCallback
& callback
) override
;
106 void Remove(const base::FilePath
& file_path
,
108 const FileOperationCallback
& callback
) override
;
109 void CreateDirectory(const base::FilePath
& directory_path
,
112 const FileOperationCallback
& callback
) override
;
113 void CreateFile(const base::FilePath
& file_path
,
115 const std::string
& mime_type
,
116 const FileOperationCallback
& callback
) override
;
117 void TouchFile(const base::FilePath
& file_path
,
118 const base::Time
& last_access_time
,
119 const base::Time
& last_modified_time
,
120 const FileOperationCallback
& callback
) override
;
121 void TruncateFile(const base::FilePath
& file_path
,
123 const FileOperationCallback
& callback
) override
;
124 void Pin(const base::FilePath
& file_path
,
125 const FileOperationCallback
& callback
) override
;
126 void Unpin(const base::FilePath
& file_path
,
127 const FileOperationCallback
& callback
) override
;
128 void GetFile(const base::FilePath
& file_path
,
129 const GetFileCallback
& callback
) override
;
130 void GetFileForSaving(const base::FilePath
& file_path
,
131 const GetFileCallback
& callback
) override
;
132 base::Closure
GetFileContent(
133 const base::FilePath
& file_path
,
134 const GetFileContentInitializedCallback
& initialized_callback
,
135 const google_apis::GetContentCallback
& get_content_callback
,
136 const FileOperationCallback
& completion_callback
) override
;
137 void GetResourceEntry(const base::FilePath
& file_path
,
138 const GetResourceEntryCallback
& callback
) override
;
139 void ReadDirectory(const base::FilePath
& directory_path
,
140 const ReadDirectoryEntriesCallback
& entries_callback
,
141 const FileOperationCallback
& completion_callback
) override
;
142 void GetAvailableSpace(const GetAvailableSpaceCallback
& callback
) override
;
143 void GetShareUrl(const base::FilePath
& file_path
,
144 const GURL
& embed_origin
,
145 const GetShareUrlCallback
& callback
) override
;
146 void GetMetadata(const GetFilesystemMetadataCallback
& callback
) override
;
147 void MarkCacheFileAsMounted(const base::FilePath
& drive_file_path
,
148 const MarkMountedCallback
& callback
) override
;
149 void MarkCacheFileAsUnmounted(const base::FilePath
& cache_file_path
,
150 const FileOperationCallback
& callback
) override
;
151 void AddPermission(const base::FilePath
& drive_file_path
,
152 const std::string
& email
,
153 google_apis::drive::PermissionRole role
,
154 const FileOperationCallback
& callback
) override
;
155 void SetProperty(const base::FilePath
& drive_file_path
,
156 google_apis::drive::Property::Visibility visibility
,
157 const std::string
& key
,
158 const std::string
& value
,
159 const FileOperationCallback
& callback
) override
;
160 void Reset(const FileOperationCallback
& callback
) override
;
161 void GetPathFromResourceId(const std::string
& resource_id
,
162 const GetFilePathCallback
& callback
) override
;
163 void FreeDiskSpaceIfNeededFor(int64 num_bytes
,
164 const FreeDiskSpaceCallback
& callback
) override
;
165 void CalculateEvictableCacheSize(
166 const EvictableCacheSizeCallback
& callback
) override
;
168 // file_system::OperationDelegate overrides.
169 void OnFileChangedByOperation(const FileChange
& changed_files
) override
;
170 void OnEntryUpdatedByOperation(const ClientContext
& context
,
171 const std::string
& local_id
) override
;
172 void OnDriveSyncError(file_system::DriveSyncErrorType type
,
173 const std::string
& local_id
) override
;
174 bool WaitForSyncComplete(const std::string
& local_id
,
175 const FileOperationCallback
& callback
) override
;
177 // ChangeListLoader::Observer overrides.
178 // Used to propagate events from ChangeListLoader.
179 void OnDirectoryReloaded(const base::FilePath
& directory_path
) override
;
180 void OnFileChanged(const FileChange
& changed_files
) override
;
181 void OnLoadFromServerComplete() override
;
182 void OnInitialLoadComplete() override
;
185 internal::ChangeListLoader
* change_list_loader_for_testing() {
186 return change_list_loader_
.get();
188 internal::SyncClient
* sync_client_for_testing() { return sync_client_
.get(); }
191 struct CreateDirectoryParams
;
193 // Used for initialization and Reset(). (Re-)initializes sub components that
194 // need to be recreated during the reset of resource metadata and the cache.
195 void ResetComponents();
197 // Part of CreateDirectory(). Called after ReadDirectory()
198 // is called and made sure that the resource metadata is loaded.
199 void CreateDirectoryAfterRead(const CreateDirectoryParams
& params
,
202 void FinishPin(const FileOperationCallback
& callback
,
203 const std::string
* local_id
,
206 void FinishUnpin(const FileOperationCallback
& callback
,
207 const std::string
* local_id
,
210 // Callback for handling about resource fetch.
211 void OnGetAboutResource(
212 const GetAvailableSpaceCallback
& callback
,
213 google_apis::DriveApiErrorCode status
,
214 scoped_ptr
<google_apis::AboutResource
> about_resource
);
216 // Part of CheckForUpdates(). Called when
217 // ChangeListLoader::CheckForUpdates() is complete.
218 void OnUpdateChecked(FileError error
);
220 // Part of GetResourceEntry().
221 // Called when ReadDirectory() is complete.
222 void GetResourceEntryAfterRead(const base::FilePath
& file_path
,
223 const GetResourceEntryCallback
& callback
,
226 // Part of GetShareUrl. Resolves the resource entry to get the resource it,
227 // and then uses it to ask for the share url. |callback| must not be null.
228 void GetShareUrlAfterGetResourceEntry(const base::FilePath
& file_path
,
229 const GURL
& embed_origin
,
230 const GetShareUrlCallback
& callback
,
231 ResourceEntry
* entry
,
233 void OnGetResourceEntryForGetShareUrl(const GetShareUrlCallback
& callback
,
234 google_apis::DriveApiErrorCode status
,
235 const GURL
& share_url
);
236 // Part of AddPermission.
237 void AddPermissionAfterGetResourceEntry(
238 const std::string
& email
,
239 google_apis::drive::PermissionRole role
,
240 const FileOperationCallback
& callback
,
241 ResourceEntry
* entry
,
244 // Part of OnDriveSyncError().
245 virtual void OnDriveSyncErrorAfterGetFilePath(
246 file_system::DriveSyncErrorType type
,
247 const base::FilePath
* file_path
,
250 // Used to get Drive related preferences.
251 PrefService
* pref_service_
;
253 // Sub components owned by DriveIntegrationService.
254 EventLogger
* logger_
;
255 internal::FileCache
* cache_
;
256 JobScheduler
* scheduler_
;
257 internal::ResourceMetadata
* resource_metadata_
;
259 // Time of the last update check.
260 base::Time last_update_check_time_
;
262 // Error of the last update check.
263 FileError last_update_check_error_
;
265 // Used to load about resource.
266 scoped_ptr
<internal::AboutResourceLoader
> about_resource_loader_
;
268 // Used to control ChangeListLoader.
269 scoped_ptr
<internal::LoaderController
> loader_controller_
;
271 // The loader is used to load the change lists.
272 scoped_ptr
<internal::ChangeListLoader
> change_list_loader_
;
274 scoped_ptr
<internal::DirectoryLoader
> directory_loader_
;
276 scoped_ptr
<internal::SyncClient
> sync_client_
;
278 base::ObserverList
<FileSystemObserver
> observers_
;
280 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
281 scoped_refptr
<base::SingleThreadTaskRunner
> file_task_runner_
;
283 base::FilePath temporary_file_directory_
;
285 // Implementation of each file system operation.
286 scoped_ptr
<file_system::CopyOperation
> copy_operation_
;
287 scoped_ptr
<file_system::CreateDirectoryOperation
> create_directory_operation_
;
288 scoped_ptr
<file_system::CreateFileOperation
> create_file_operation_
;
289 scoped_ptr
<file_system::MoveOperation
> move_operation_
;
290 scoped_ptr
<file_system::OpenFileOperation
> open_file_operation_
;
291 scoped_ptr
<file_system::RemoveOperation
> remove_operation_
;
292 scoped_ptr
<file_system::TouchOperation
> touch_operation_
;
293 scoped_ptr
<file_system::TruncateOperation
> truncate_operation_
;
294 scoped_ptr
<file_system::DownloadOperation
> download_operation_
;
295 scoped_ptr
<file_system::SearchOperation
> search_operation_
;
296 scoped_ptr
<file_system::GetFileForSavingOperation
>
297 get_file_for_saving_operation_
;
298 scoped_ptr
<file_system::SetPropertyOperation
> set_property_operation_
;
300 base::ThreadChecker thread_checker_
;
302 // Note: This should remain the last member so it'll be destroyed and
303 // invalidate the weak pointers before any other members are destroyed.
304 base::WeakPtrFactory
<FileSystem
> weak_ptr_factory_
;
306 DISALLOW_COPY_AND_ASSIGN(FileSystem
);
311 #endif // COMPONENTS_DRIVE_FILE_SYSTEM_H_