• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
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"
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"
18 
19 class PrefService;
20 
21 namespace base {
22 class SequencedTaskRunner;
23 }  // namespace base
24 
25 namespace google_apis {
26 class AboutResource;
27 class ResourceEntry;
28 }  // namespace google_apis
29 
30 namespace drive {
31 
32 class DriveServiceInterface;
33 class EventLogger;
34 class FileCacheEntry;
35 class FileSystemObserver;
36 class JobScheduler;
37 
38 namespace internal {
39 class AboutResourceLoader;
40 class ChangeListLoader;
41 class DirectoryLoader;
42 class FileCache;
43 class LoaderController;
44 class ResourceMetadata;
45 class SyncClient;
46 }  // namespace internal
47 
48 namespace file_system {
49 class CopyOperation;
50 class CreateDirectoryOperation;
51 class CreateFileOperation;
52 class DownloadOperation;
53 class GetFileForSavingOperation;
54 class MoveOperation;
55 class OpenFileOperation;
56 class OperationObserver;
57 class RemoveOperation;
58 class SearchOperation;
59 class TouchOperation;
60 class TruncateOperation;
61 }  // namespace file_system
62 
63 // The production implementation of FileSystemInterface.
64 class FileSystem : public FileSystemInterface,
65                    public internal::ChangeListLoaderObserver,
66                    public file_system::OperationObserver {
67  public:
68   FileSystem(PrefService* pref_service,
69              EventLogger* logger,
70              internal::FileCache* cache,
71              DriveServiceInterface* drive_service,
72              JobScheduler* scheduler,
73              internal::ResourceMetadata* resource_metadata,
74              base::SequencedTaskRunner* blocking_task_runner,
75              const base::FilePath& temporary_file_directory);
76   virtual ~FileSystem();
77 
78   // FileSystemInterface overrides.
79   virtual void AddObserver(FileSystemObserver* observer) OVERRIDE;
80   virtual void RemoveObserver(FileSystemObserver* observer) OVERRIDE;
81   virtual void CheckForUpdates() OVERRIDE;
82   virtual void Search(const std::string& search_query,
83                       const GURL& next_link,
84                       const SearchCallback& callback) OVERRIDE;
85   virtual void SearchMetadata(const std::string& query,
86                               int options,
87                               int at_most_num_matches,
88                               const SearchMetadataCallback& 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,
94                         OpenMode open_mode,
95                         const std::string& mime_type,
96                         const OpenFileCallback& callback) OVERRIDE;
97   virtual void Copy(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 Move(const base::FilePath& src_file_path,
102                     const base::FilePath& dest_file_path,
103                     const FileOperationCallback& callback) OVERRIDE;
104   virtual void Remove(const base::FilePath& file_path,
105                       bool is_recursive,
106                       const FileOperationCallback& callback) OVERRIDE;
107   virtual void CreateDirectory(const base::FilePath& directory_path,
108                                bool is_exclusive,
109                                bool is_recursive,
110                                const FileOperationCallback& callback) OVERRIDE;
111   virtual void CreateFile(const base::FilePath& file_path,
112                           bool is_exclusive,
113                           const std::string& mime_type,
114                           const FileOperationCallback& callback) OVERRIDE;
115   virtual void TouchFile(const base::FilePath& file_path,
116                          const base::Time& last_access_time,
117                          const base::Time& last_modified_time,
118                          const FileOperationCallback& callback) OVERRIDE;
119   virtual void TruncateFile(const base::FilePath& file_path,
120                             int64 length,
121                             const FileOperationCallback& callback) OVERRIDE;
122   virtual void Pin(const base::FilePath& file_path,
123                    const FileOperationCallback& callback) OVERRIDE;
124   virtual void Unpin(const base::FilePath& file_path,
125                      const FileOperationCallback& callback) OVERRIDE;
126   virtual void GetFile(const base::FilePath& file_path,
127                              const GetFileCallback& callback) OVERRIDE;
128   virtual void GetFileForSaving(const base::FilePath& file_path,
129                                       const GetFileCallback& callback) OVERRIDE;
130   virtual base::Closure GetFileContent(
131       const base::FilePath& file_path,
132       const GetFileContentInitializedCallback& initialized_callback,
133       const google_apis::GetContentCallback& get_content_callback,
134       const FileOperationCallback& completion_callback) OVERRIDE;
135   virtual void GetResourceEntry(
136       const base::FilePath& file_path,
137       const GetResourceEntryCallback& callback) OVERRIDE;
138   virtual void ReadDirectory(
139       const base::FilePath& directory_path,
140       const ReadDirectoryEntriesCallback& entries_callback,
141       const FileOperationCallback& completion_callback) OVERRIDE;
142   virtual void GetAvailableSpace(
143       const GetAvailableSpaceCallback& callback) OVERRIDE;
144   virtual void GetShareUrl(
145       const base::FilePath& file_path,
146       const GURL& embed_origin,
147       const GetShareUrlCallback& callback) OVERRIDE;
148   virtual void GetMetadata(
149       const GetFilesystemMetadataCallback& callback) OVERRIDE;
150   virtual void MarkCacheFileAsMounted(
151       const base::FilePath& drive_file_path,
152       const MarkMountedCallback& callback) OVERRIDE;
153   virtual void MarkCacheFileAsUnmounted(
154       const base::FilePath& cache_file_path,
155       const FileOperationCallback& callback) OVERRIDE;
156   virtual void AddPermission(const base::FilePath& drive_file_path,
157                              const std::string& email,
158                              google_apis::drive::PermissionRole role,
159                              const FileOperationCallback& callback) OVERRIDE;
160   virtual void Reset(const FileOperationCallback& callback) OVERRIDE;
161   virtual void GetPathFromResourceId(const std::string& resource_id,
162                                      const GetFilePathCallback& callback)
163       OVERRIDE;
164 
165   // file_system::OperationObserver overrides.
166   virtual void OnDirectoryChangedByOperation(
167       const base::FilePath& directory_path) OVERRIDE;
168   virtual void OnEntryUpdatedByOperation(const std::string& local_id) OVERRIDE;
169   virtual void OnDriveSyncError(file_system::DriveSyncErrorType type,
170                                 const std::string& local_id) OVERRIDE;
171 
172   // ChangeListLoader::Observer overrides.
173   // Used to propagate events from ChangeListLoader.
174   virtual void OnDirectoryChanged(
175       const base::FilePath& directory_path) OVERRIDE;
176   virtual void OnLoadFromServerComplete() OVERRIDE;
177   virtual void OnInitialLoadComplete() OVERRIDE;
178 
179   // Used by tests.
change_list_loader_for_testing()180   internal::ChangeListLoader* change_list_loader_for_testing() {
181     return change_list_loader_.get();
182   }
sync_client_for_testing()183   internal::SyncClient* sync_client_for_testing() { return sync_client_.get(); }
184 
185  private:
186   struct CreateDirectoryParams;
187 
188   // Used for initialization and Reset(). (Re-)initializes sub components that
189   // need to be recreated during the reset of resource metadata and the cache.
190   void ResetComponents();
191 
192   // Part of CreateDirectory(). Called after ReadDirectory()
193   // is called and made sure that the resource metadata is loaded.
194   void CreateDirectoryAfterRead(const CreateDirectoryParams& params,
195                                 FileError error);
196 
197   void FinishPin(const FileOperationCallback& callback,
198                  const std::string* local_id,
199                  FileError error);
200 
201   void FinishUnpin(const FileOperationCallback& callback,
202                    const std::string* local_id,
203                    FileError error);
204 
205   // Callback for handling about resource fetch.
206   void OnGetAboutResource(
207       const GetAvailableSpaceCallback& callback,
208       google_apis::GDataErrorCode status,
209       scoped_ptr<google_apis::AboutResource> about_resource);
210 
211   // Part of CheckForUpdates(). Called when
212   // ChangeListLoader::CheckForUpdates() is complete.
213   void OnUpdateChecked(FileError error);
214 
215   // Part of GetResourceEntry().
216   // Called when ReadDirectory() is complete.
217   void GetResourceEntryAfterRead(const base::FilePath& file_path,
218                                  const GetResourceEntryCallback& callback,
219                                  FileError error);
220 
221   // Part of GetShareUrl. Resolves the resource entry to get the resource it,
222   // and then uses it to ask for the share url. |callback| must not be null.
223   void GetShareUrlAfterGetResourceEntry(const base::FilePath& file_path,
224                                         const GURL& embed_origin,
225                                         const GetShareUrlCallback& callback,
226                                         ResourceEntry* entry,
227                                         FileError error);
228   void OnGetResourceEntryForGetShareUrl(const GetShareUrlCallback& callback,
229                                         google_apis::GDataErrorCode status,
230                                         const GURL& share_url);
231   // Part of AddPermission.
232   void AddPermissionAfterGetResourceEntry(
233       const std::string& email,
234       google_apis::drive::PermissionRole role,
235       const FileOperationCallback& callback,
236       ResourceEntry* entry,
237       FileError error);
238 
239   // Part of OnDriveSyncError().
240   virtual void OnDriveSyncErrorAfterGetFilePath(
241       file_system::DriveSyncErrorType type,
242       const base::FilePath* file_path,
243       FileError error);
244 
245   // Used to get Drive related preferences.
246   PrefService* pref_service_;
247 
248   // Sub components owned by DriveIntegrationService.
249   EventLogger* logger_;
250   internal::FileCache* cache_;
251   DriveServiceInterface* drive_service_;
252   JobScheduler* scheduler_;
253   internal::ResourceMetadata* resource_metadata_;
254 
255   // Time of the last update check.
256   base::Time last_update_check_time_;
257 
258   // Error of the last update check.
259   FileError last_update_check_error_;
260 
261   // Used to load about resource.
262   scoped_ptr<internal::AboutResourceLoader> about_resource_loader_;
263 
264   // Used to control ChangeListLoader.
265   scoped_ptr<internal::LoaderController> loader_controller_;
266 
267   // The loader is used to load the change lists.
268   scoped_ptr<internal::ChangeListLoader> change_list_loader_;
269 
270   scoped_ptr<internal::DirectoryLoader> directory_loader_;
271 
272   scoped_ptr<internal::SyncClient> sync_client_;
273 
274   ObserverList<FileSystemObserver> observers_;
275 
276   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
277 
278   base::FilePath temporary_file_directory_;
279 
280   // Implementation of each file system operation.
281   scoped_ptr<file_system::CopyOperation> copy_operation_;
282   scoped_ptr<file_system::CreateDirectoryOperation> create_directory_operation_;
283   scoped_ptr<file_system::CreateFileOperation> create_file_operation_;
284   scoped_ptr<file_system::MoveOperation> move_operation_;
285   scoped_ptr<file_system::OpenFileOperation> open_file_operation_;
286   scoped_ptr<file_system::RemoveOperation> remove_operation_;
287   scoped_ptr<file_system::TouchOperation> touch_operation_;
288   scoped_ptr<file_system::TruncateOperation> truncate_operation_;
289   scoped_ptr<file_system::DownloadOperation> download_operation_;
290   scoped_ptr<file_system::SearchOperation> search_operation_;
291   scoped_ptr<file_system::GetFileForSavingOperation>
292       get_file_for_saving_operation_;
293 
294   // Note: This should remain the last member so it'll be destroyed and
295   // invalidate the weak pointers before any other members are destroyed.
296   base::WeakPtrFactory<FileSystem> weak_ptr_factory_;
297 
298   DISALLOW_COPY_AND_ASSIGN(FileSystem);
299 };
300 
301 }  // namespace drive
302 
303 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_H_
304