blob: 5bb1ca8e23b08b994103c887b8a01ba4f11e3275 [file] [log] [blame]
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001// 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.
4
5#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_H_
6#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_H_
7
8#include <string>
9#include <vector>
10
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"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010015#include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
16#include "chrome/browser/chromeos/drive/file_system_interface.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010017#include "chrome/browser/google_apis/gdata_errorcode.h"
18
19class PrefChangeRegistrar;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010020class PrefService;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010021
22namespace base {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010023class SequencedTaskRunner;
Ben Murdochbb1529c2013-08-08 10:24:53 +010024} // namespace base
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010025
26namespace google_apis {
27class AboutResource;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010028class ResourceEntry;
Ben Murdochbb1529c2013-08-08 10:24:53 +010029} // namespace google_apis
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010030
31namespace drive {
32
Ben Murdocheb525c52013-07-10 11:40:50 +010033class DriveServiceInterface;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010034class FileCacheEntry;
35class FileSystemObserver;
36class JobScheduler;
37
38namespace internal {
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010039class ChangeListLoader;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010040class ResourceMetadata;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010041class SyncClient;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010042} // namespace internal
43
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010044namespace file_system {
45class CopyOperation;
46class CreateDirectoryOperation;
47class CreateFileOperation;
48class DownloadOperation;
49class MoveOperation;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010050class OpenFileOperation;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010051class OperationObserver;
52class RemoveOperation;
53class SearchOperation;
54class TouchOperation;
Ben Murdocheb525c52013-07-10 11:40:50 +010055class TruncateOperation;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010056class UpdateOperation;
57} // namespace file_system
58
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010059// The production implementation of FileSystemInterface.
60class FileSystem : public FileSystemInterface,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010061 public internal::ChangeListLoaderObserver,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010062 public file_system::OperationObserver {
63 public:
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010064 FileSystem(PrefService* pref_service,
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +010065 internal::FileCache* cache,
Ben Murdocheb525c52013-07-10 11:40:50 +010066 DriveServiceInterface* drive_service,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010067 JobScheduler* scheduler,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010068 internal::ResourceMetadata* resource_metadata,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010069 base::SequencedTaskRunner* blocking_task_runner,
70 const base::FilePath& temporary_file_directory);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010071 virtual ~FileSystem();
72
73 // FileSystemInterface overrides.
74 virtual void Initialize() OVERRIDE;
75 virtual void AddObserver(FileSystemObserver* observer) OVERRIDE;
76 virtual void RemoveObserver(FileSystemObserver* observer) OVERRIDE;
77 virtual void CheckForUpdates() OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010078 virtual void Search(const std::string& search_query,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010079 const GURL& next_url,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010080 const SearchCallback& callback) OVERRIDE;
81 virtual void SearchMetadata(const std::string& query,
82 int options,
83 int at_most_num_matches,
84 const SearchMetadataCallback& callback) OVERRIDE;
85 virtual void TransferFileFromRemoteToLocal(
86 const base::FilePath& remote_src_file_path,
87 const base::FilePath& local_dest_file_path,
88 const FileOperationCallback& callback) OVERRIDE;
89 virtual void TransferFileFromLocalToRemote(
90 const base::FilePath& local_src_file_path,
91 const base::FilePath& remote_dest_file_path,
92 const FileOperationCallback& callback) OVERRIDE;
93 virtual void OpenFile(const base::FilePath& file_path,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010094 OpenMode open_mode,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010095 const OpenFileCallback& callback) OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010096 virtual void Copy(const base::FilePath& src_file_path,
97 const base::FilePath& dest_file_path,
98 const FileOperationCallback& callback) OVERRIDE;
99 virtual void Move(const base::FilePath& src_file_path,
100 const base::FilePath& dest_file_path,
101 const FileOperationCallback& callback) OVERRIDE;
102 virtual void Remove(const base::FilePath& file_path,
103 bool is_recursive,
104 const FileOperationCallback& callback) OVERRIDE;
105 virtual void CreateDirectory(const base::FilePath& directory_path,
106 bool is_exclusive,
107 bool is_recursive,
108 const FileOperationCallback& callback) OVERRIDE;
109 virtual void CreateFile(const base::FilePath& file_path,
110 bool is_exclusive,
111 const FileOperationCallback& callback) OVERRIDE;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100112 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;
Ben Murdocheb525c52013-07-10 11:40:50 +0100116 virtual void TruncateFile(const base::FilePath& file_path,
117 int64 length,
118 const FileOperationCallback& callback) OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100119 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 GetFileByPath(const base::FilePath& file_path,
124 const GetFileCallback& callback) OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100125 virtual void GetFileContentByPath(
126 const base::FilePath& file_path,
127 const GetFileContentInitializedCallback& initialized_callback,
128 const google_apis::GetContentCallback& get_content_callback,
129 const FileOperationCallback& completion_callback) OVERRIDE;
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100130 virtual void GetResourceEntryByPath(
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100131 const base::FilePath& file_path,
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100132 const GetResourceEntryCallback& callback) OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100133 virtual void ReadDirectoryByPath(
134 const base::FilePath& directory_path,
Ben Murdocheb525c52013-07-10 11:40:50 +0100135 const ReadDirectoryCallback& callback) OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100136 virtual void GetAvailableSpace(
137 const GetAvailableSpaceCallback& callback) OVERRIDE;
Ben Murdochbbcdd452013-07-25 10:06:34 +0100138 virtual void GetShareUrl(
139 const base::FilePath& file_path,
140 const GURL& embed_origin,
141 const GetShareUrlCallback& callback) OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100142 virtual void GetMetadata(
143 const GetFilesystemMetadataCallback& callback) OVERRIDE;
144 virtual void MarkCacheFileAsMounted(
145 const base::FilePath& drive_file_path,
Ben Murdocha3f7b4e2013-07-24 10:36:34 +0100146 const MarkMountedCallback& callback) OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100147 virtual void MarkCacheFileAsUnmounted(
148 const base::FilePath& cache_file_path,
149 const FileOperationCallback& callback) OVERRIDE;
150 virtual void GetCacheEntryByResourceId(
151 const std::string& resource_id,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100152 const GetCacheEntryCallback& callback) OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100153 virtual void Reload() OVERRIDE;
154
155 // file_system::OperationObserver overrides.
156 virtual void OnDirectoryChangedByOperation(
157 const base::FilePath& directory_path) OVERRIDE;
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100158 virtual void OnCacheFileUploadNeededByOperation(
159 const std::string& resource_id) OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100160
161 // ChangeListLoader::Observer overrides.
162 // Used to propagate events from ChangeListLoader.
163 virtual void OnDirectoryChanged(
164 const base::FilePath& directory_path) OVERRIDE;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100165 virtual void OnLoadFromServerComplete() OVERRIDE;
166 virtual void OnInitialLoadComplete() OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100167
Ben Murdochbb1529c2013-08-08 10:24:53 +0100168 // Used by tests.
169 internal::ChangeListLoader* change_list_loader_for_testing() {
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100170 return change_list_loader_.get();
171 }
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100172 internal::SyncClient* sync_client_for_testing() { return sync_client_.get(); }
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100173
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100174 private:
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100175 // Used to implement Reload().
176 void ReloadAfterReset(FileError error);
177
178 // Sets up ChangeListLoader.
179 void SetupChangeListLoader();
180
181 // Called on preference change.
182 void OnDisableDriveHostedFilesChanged();
183
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100184 // 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,
187 bool is_exclusive,
188 bool is_recursive,
189 const FileOperationCallback& callback,
190 FileError load_error);
191
192 // Used to implement Pin().
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100193 void PinAfterGetResourceEntryByPath(const FileOperationCallback& callback,
194 FileError error,
195 scoped_ptr<ResourceEntry> entry);
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100196 void FinishPin(const FileOperationCallback& callback,
197 const std::string& resource_id,
198 FileError error);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100199
200 // Used to implement Unpin().
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100201 void UnpinAfterGetResourceEntryByPath(const FileOperationCallback& callback,
202 FileError error,
203 scoped_ptr<ResourceEntry> entry);
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100204 void FinishUnpin(const FileOperationCallback& callback,
205 const std::string& resource_id,
206 FileError error);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100207
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100208 // Callback for handling about resource fetch.
209 void OnGetAboutResource(
210 const GetAvailableSpaceCallback& callback,
211 google_apis::GDataErrorCode status,
212 scoped_ptr<google_apis::AboutResource> about_resource);
213
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100214 // Part of CheckForUpdates(). Called when
215 // ChangeListLoader::CheckForUpdates() is complete.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100216 void OnUpdateChecked(FileError error);
217
218 // Changes state of hosted documents visibility, triggers directory refresh.
219 void SetHideHostedDocuments(bool hide);
220
221 // Initializes preference change observer.
222 void InitializePreferenceObserver();
223
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100224 // Part of GetResourceEntryByPath()
Ben Murdoch558790d2013-07-30 15:19:42 +0100225 // 1) Called when GetLocallyStoredResourceEntry() is complete.
226 // 2) Called when LoadDirectoryIfNeeded() is complete.
227 void GetResourceEntryByPathAfterGetEntry(
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100228 const base::FilePath& file_path,
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100229 const GetResourceEntryCallback& callback,
Ben Murdoch558790d2013-07-30 15:19:42 +0100230 scoped_ptr<ResourceEntry> entry,
231 FileError error);
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100232 void GetResourceEntryByPathAfterLoad(const base::FilePath& file_path,
233 const GetResourceEntryCallback& callback,
234 FileError error);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100235
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100236 // Loads the entry info of the children of |directory_path| to resource
237 // metadata. |callback| must not be null.
238 void LoadDirectoryIfNeeded(const base::FilePath& directory_path,
239 const FileOperationCallback& callback);
240 void LoadDirectoryIfNeededAfterGetEntry(
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100241 const base::FilePath& directory_path,
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100242 const FileOperationCallback& callback,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100243 FileError error,
244 scoped_ptr<ResourceEntry> entry);
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100245
246 // Part of ReadDirectoryByPath()
247 // 1) Called when LoadDirectoryIfNeeded() is complete.
248 // 2) Called when ResourceMetadata::ReadDirectoryByPath() is complete.
249 // |callback| must not be null.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100250 void ReadDirectoryByPathAfterLoad(
251 const base::FilePath& directory_path,
Ben Murdocheb525c52013-07-10 11:40:50 +0100252 const ReadDirectoryCallback& callback,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100253 FileError error);
254 void ReadDirectoryByPathAfterRead(
Ben Murdocheb525c52013-07-10 11:40:50 +0100255 const ReadDirectoryCallback& callback,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100256 FileError error,
257 scoped_ptr<ResourceEntryVector> entries);
258
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100259 // Part of MarkCacheFileAsMounted. Called after GetResourceEntryByPath is
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100260 // completed. |callback| must not be null.
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +0100261 void MarkCacheFileAsMountedAfterGetResourceEntry(
Ben Murdocha3f7b4e2013-07-24 10:36:34 +0100262 const MarkMountedCallback& callback,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100263 FileError error,
264 scoped_ptr<ResourceEntry> entry);
265
Ben Murdochbbcdd452013-07-25 10:06:34 +0100266 // Part of GetShareUrl. Resolves the resource entry to get the resource it,
267 // and then uses it to ask for the share url. |callback| must not be null.
268 void GetShareUrlAfterGetResourceEntry(
269 const base::FilePath& file_path,
270 const GURL& embed_origin,
271 const GetShareUrlCallback& callback,
272 FileError error,
273 scoped_ptr<ResourceEntry> entry);
274 void OnGetResourceEntryForGetShareUrl(
275 const GetShareUrlCallback& callback,
276 google_apis::GDataErrorCode status,
277 const GURL& share_url);
278
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100279 // Used to get Drive related preferences.
280 PrefService* pref_service_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100281
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100282 // Sub components owned by DriveIntegrationService.
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100283 internal::FileCache* cache_;
Ben Murdocheb525c52013-07-10 11:40:50 +0100284 DriveServiceInterface* drive_service_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100285 JobScheduler* scheduler_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100286 internal::ResourceMetadata* resource_metadata_;
287
288 // Time of the last update check.
289 base::Time last_update_check_time_;
290
291 // Error of the last update check.
292 FileError last_update_check_error_;
293
294 // True if hosted documents should be hidden.
295 bool hide_hosted_docs_;
296
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100297 scoped_ptr<PrefChangeRegistrar> pref_registrar_;
298
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100299 scoped_ptr<internal::SyncClient> sync_client_;
300
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100301 // The loader is used to load the change lists.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100302 scoped_ptr<internal::ChangeListLoader> change_list_loader_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100303
304 ObserverList<FileSystemObserver> observers_;
305
306 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
307
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100308 base::FilePath temporary_file_directory_;
309
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100310 // Implementation of each file system operation.
311 scoped_ptr<file_system::CopyOperation> copy_operation_;
312 scoped_ptr<file_system::CreateDirectoryOperation> create_directory_operation_;
313 scoped_ptr<file_system::CreateFileOperation> create_file_operation_;
314 scoped_ptr<file_system::MoveOperation> move_operation_;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100315 scoped_ptr<file_system::OpenFileOperation> open_file_operation_;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100316 scoped_ptr<file_system::RemoveOperation> remove_operation_;
317 scoped_ptr<file_system::TouchOperation> touch_operation_;
Ben Murdocheb525c52013-07-10 11:40:50 +0100318 scoped_ptr<file_system::TruncateOperation> truncate_operation_;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100319 scoped_ptr<file_system::DownloadOperation> download_operation_;
320 scoped_ptr<file_system::UpdateOperation> update_operation_;
321 scoped_ptr<file_system::SearchOperation> search_operation_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100322
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100323 // Note: This should remain the last member so it'll be destroyed and
324 // invalidate the weak pointers before any other members are destroyed.
325 base::WeakPtrFactory<FileSystem> weak_ptr_factory_;
326
327 DISALLOW_COPY_AND_ASSIGN(FileSystem);
328};
329
330} // namespace drive
331
332#endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_H_