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_CHROMEOS_DRIVE_FILE_SYSTEM_H_
6 #define CHROME_BROWSER_CHROMEOS_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 "chrome/browser/chromeos/drive/change_list_loader_observer.h"
15 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
16 #include "chrome/browser/chromeos/drive/file_system_interface.h"
17 #include "google_apis/drive/gdata_errorcode.h"
22 class SequencedTaskRunner
;
25 namespace google_apis
{
28 } // namespace google_apis
32 class DriveServiceInterface
;
34 class FileSystemObserver
;
38 class ChangeListLoader
;
40 class ResourceMetadata
;
42 } // namespace internal
44 namespace file_system
{
46 class CreateDirectoryOperation
;
47 class CreateFileOperation
;
48 class DownloadOperation
;
49 class GetFileForSavingOperation
;
51 class OpenFileOperation
;
52 class OperationObserver
;
53 class RemoveOperation
;
54 class SearchOperation
;
56 class TruncateOperation
;
57 class UpdateOperation
;
58 } // namespace file_system
60 // The production implementation of FileSystemInterface.
61 class FileSystem
: public FileSystemInterface
,
62 public internal::ChangeListLoaderObserver
,
63 public file_system::OperationObserver
{
65 FileSystem(PrefService
* pref_service
,
66 internal::FileCache
* cache
,
67 DriveServiceInterface
* drive_service
,
68 JobScheduler
* scheduler
,
69 internal::ResourceMetadata
* resource_metadata
,
70 base::SequencedTaskRunner
* blocking_task_runner
,
71 const base::FilePath
& temporary_file_directory
);
72 virtual ~FileSystem();
74 // FileSystemInterface overrides.
75 virtual void AddObserver(FileSystemObserver
* observer
) OVERRIDE
;
76 virtual void RemoveObserver(FileSystemObserver
* observer
) OVERRIDE
;
77 virtual void CheckForUpdates() OVERRIDE
;
78 virtual void Search(const std::string
& search_query
,
79 const GURL
& next_link
,
80 const SearchCallback
& callback
) OVERRIDE
;
81 virtual void SearchMetadata(const std::string
& query
,
83 int at_most_num_matches
,
84 const SearchMetadataCallback
& callback
) OVERRIDE
;
85 virtual void TransferFileFromLocalToRemote(
86 const base::FilePath
& local_src_file_path
,
87 const base::FilePath
& remote_dest_file_path
,
88 const FileOperationCallback
& callback
) OVERRIDE
;
89 virtual void OpenFile(const base::FilePath
& file_path
,
91 const std::string
& mime_type
,
92 const OpenFileCallback
& callback
) OVERRIDE
;
93 virtual void Copy(const base::FilePath
& src_file_path
,
94 const base::FilePath
& dest_file_path
,
95 bool preserve_last_modified
,
96 const FileOperationCallback
& callback
) OVERRIDE
;
97 virtual void Move(const base::FilePath
& src_file_path
,
98 const base::FilePath
& dest_file_path
,
99 bool preserve_last_modified
,
100 const FileOperationCallback
& callback
) OVERRIDE
;
101 virtual void Remove(const base::FilePath
& file_path
,
103 const FileOperationCallback
& callback
) OVERRIDE
;
104 virtual void CreateDirectory(const base::FilePath
& directory_path
,
107 const FileOperationCallback
& callback
) OVERRIDE
;
108 virtual void CreateFile(const base::FilePath
& file_path
,
110 const std::string
& mime_type
,
111 const FileOperationCallback
& callback
) OVERRIDE
;
112 virtual void TouchFile(const base::FilePath
& file_path
,
113 const base::Time
& last_access_time
,
114 const base::Time
& last_modified_time
,
115 const FileOperationCallback
& callback
) OVERRIDE
;
116 virtual void TruncateFile(const base::FilePath
& file_path
,
118 const FileOperationCallback
& callback
) OVERRIDE
;
119 virtual void Pin(const base::FilePath
& file_path
,
120 const FileOperationCallback
& callback
) OVERRIDE
;
121 virtual void Unpin(const base::FilePath
& file_path
,
122 const FileOperationCallback
& callback
) OVERRIDE
;
123 virtual void GetFile(const base::FilePath
& file_path
,
124 const GetFileCallback
& callback
) OVERRIDE
;
125 virtual void GetFileForSaving(const base::FilePath
& file_path
,
126 const GetFileCallback
& callback
) OVERRIDE
;
127 virtual void GetFileContent(
128 const base::FilePath
& file_path
,
129 const GetFileContentInitializedCallback
& initialized_callback
,
130 const google_apis::GetContentCallback
& get_content_callback
,
131 const FileOperationCallback
& completion_callback
) OVERRIDE
;
132 virtual void GetResourceEntry(
133 const base::FilePath
& file_path
,
134 const GetResourceEntryCallback
& callback
) OVERRIDE
;
135 virtual void ReadDirectory(
136 const base::FilePath
& directory_path
,
137 const ReadDirectoryCallback
& callback
) OVERRIDE
;
138 virtual void GetAvailableSpace(
139 const GetAvailableSpaceCallback
& callback
) OVERRIDE
;
140 virtual void GetShareUrl(
141 const base::FilePath
& file_path
,
142 const GURL
& embed_origin
,
143 const GetShareUrlCallback
& callback
) OVERRIDE
;
144 virtual void GetMetadata(
145 const GetFilesystemMetadataCallback
& callback
) OVERRIDE
;
146 virtual void MarkCacheFileAsMounted(
147 const base::FilePath
& drive_file_path
,
148 const MarkMountedCallback
& callback
) OVERRIDE
;
149 virtual void MarkCacheFileAsUnmounted(
150 const base::FilePath
& cache_file_path
,
151 const FileOperationCallback
& callback
) OVERRIDE
;
152 virtual void GetCacheEntry(
153 const base::FilePath
& drive_file_path
,
154 const GetCacheEntryCallback
& callback
) OVERRIDE
;
155 virtual void Reset(const FileOperationCallback
& callback
) OVERRIDE
;
157 // file_system::OperationObserver overrides.
158 virtual void OnDirectoryChangedByOperation(
159 const base::FilePath
& directory_path
) OVERRIDE
;
160 virtual void OnCacheFileUploadNeededByOperation(
161 const std::string
& local_id
) OVERRIDE
;
162 virtual void OnEntryUpdatedByOperation(const std::string
& local_id
) OVERRIDE
;
163 virtual void OnDriveSyncError(file_system::DriveSyncErrorType type
,
164 const std::string
& local_id
) OVERRIDE
;
166 // ChangeListLoader::Observer overrides.
167 // Used to propagate events from ChangeListLoader.
168 virtual void OnDirectoryChanged(
169 const base::FilePath
& directory_path
) OVERRIDE
;
170 virtual void OnLoadFromServerComplete() OVERRIDE
;
171 virtual void OnInitialLoadComplete() OVERRIDE
;
174 internal::ChangeListLoader
* change_list_loader_for_testing() {
175 return change_list_loader_
.get();
177 internal::SyncClient
* sync_client_for_testing() { return sync_client_
.get(); }
180 // Used for initialization and Reset(). (Re-)initializes sub components that
181 // need to be recreated during the reset of resource metadata and the cache.
182 void ResetComponents();
184 // Part of CreateDirectory(). Called after ChangeListLoader::LoadIfNeeded()
185 // is called and made sure that the resource metadata is loaded.
186 void CreateDirectoryAfterLoad(const base::FilePath
& directory_path
,
189 const FileOperationCallback
& callback
,
190 FileError load_error
);
192 void FinishPin(const FileOperationCallback
& callback
,
193 const std::string
* local_id
,
196 void FinishUnpin(const FileOperationCallback
& callback
,
197 const std::string
* local_id
,
200 // Callback for handling about resource fetch.
201 void OnGetAboutResource(
202 const GetAvailableSpaceCallback
& callback
,
203 google_apis::GDataErrorCode status
,
204 scoped_ptr
<google_apis::AboutResource
> about_resource
);
206 // Part of CheckForUpdates(). Called when
207 // ChangeListLoader::CheckForUpdates() is complete.
208 void OnUpdateChecked(FileError error
);
210 // Part of GetResourceEntry().
211 // Called when LoadDirectoryIfNeeded() is complete.
212 void GetResourceEntryAfterLoad(const base::FilePath
& file_path
,
213 const GetResourceEntryCallback
& callback
,
216 // Part of ReadDirectory()
217 // 1) Called when LoadDirectoryIfNeeded() is complete.
218 // 2) Called when ResourceMetadata::ReadDirectory() is complete.
219 // |callback| must not be null.
220 void ReadDirectoryAfterLoad(const base::FilePath
& directory_path
,
221 const ReadDirectoryCallback
& callback
,
223 void ReadDirectoryAfterRead(const base::FilePath
& directory_path
,
224 const ReadDirectoryCallback
& callback
,
226 scoped_ptr
<ResourceEntryVector
> entries
);
228 // Part of GetShareUrl. Resolves the resource entry to get the resource it,
229 // and then uses it to ask for the share url. |callback| must not be null.
230 void GetShareUrlAfterGetResourceEntry(
231 const base::FilePath
& file_path
,
232 const GURL
& embed_origin
,
233 const GetShareUrlCallback
& callback
,
235 scoped_ptr
<ResourceEntry
> entry
);
236 void OnGetResourceEntryForGetShareUrl(
237 const GetShareUrlCallback
& callback
,
238 google_apis::GDataErrorCode status
,
239 const GURL
& share_url
);
241 // Part of OnDriveSyncError().
242 virtual void OnDriveSyncErrorAfterGetFilePath(
243 file_system::DriveSyncErrorType type
,
244 const base::FilePath
& fiepath
);
246 // Used to get Drive related preferences.
247 PrefService
* pref_service_
;
249 // Sub components owned by DriveIntegrationService.
250 internal::FileCache
* cache_
;
251 DriveServiceInterface
* drive_service_
;
252 JobScheduler
* scheduler_
;
253 internal::ResourceMetadata
* resource_metadata_
;
255 // Time of the last update check.
256 base::Time last_update_check_time_
;
258 // Error of the last update check.
259 FileError last_update_check_error_
;
261 scoped_ptr
<internal::SyncClient
> sync_client_
;
263 // The loader is used to load the change lists.
264 scoped_ptr
<internal::ChangeListLoader
> change_list_loader_
;
266 ObserverList
<FileSystemObserver
> observers_
;
268 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
270 base::FilePath temporary_file_directory_
;
272 // Implementation of each file system operation.
273 scoped_ptr
<file_system::CopyOperation
> copy_operation_
;
274 scoped_ptr
<file_system::CreateDirectoryOperation
> create_directory_operation_
;
275 scoped_ptr
<file_system::CreateFileOperation
> create_file_operation_
;
276 scoped_ptr
<file_system::MoveOperation
> move_operation_
;
277 scoped_ptr
<file_system::OpenFileOperation
> open_file_operation_
;
278 scoped_ptr
<file_system::RemoveOperation
> remove_operation_
;
279 scoped_ptr
<file_system::TouchOperation
> touch_operation_
;
280 scoped_ptr
<file_system::TruncateOperation
> truncate_operation_
;
281 scoped_ptr
<file_system::DownloadOperation
> download_operation_
;
282 scoped_ptr
<file_system::UpdateOperation
> update_operation_
;
283 scoped_ptr
<file_system::SearchOperation
> search_operation_
;
284 scoped_ptr
<file_system::GetFileForSavingOperation
>
285 get_file_for_saving_operation_
;
287 // Note: This should remain the last member so it'll be destroyed and
288 // invalidate the weak pointers before any other members are destroyed.
289 base::WeakPtrFactory
<FileSystem
> weak_ptr_factory_
;
291 DISALLOW_COPY_AND_ASSIGN(FileSystem
);
296 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_H_