file_system.h revision ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16
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 "chrome/browser/google_apis/gdata_errorcode.h"
18
19class PrefChangeRegistrar;
20class PrefService;
21
22namespace base {
23class SequencedTaskRunner;
24}  // namespace base
25
26namespace google_apis {
27class AboutResource;
28class ResourceEntry;
29}  // namespace google_apis
30
31namespace drive {
32
33class DriveServiceInterface;
34class FileCacheEntry;
35class FileSystemObserver;
36class JobScheduler;
37
38namespace internal {
39class ChangeListLoader;
40class ResourceMetadata;
41class SyncClient;
42}  // namespace internal
43
44namespace file_system {
45class CopyOperation;
46class CreateDirectoryOperation;
47class CreateFileOperation;
48class DownloadOperation;
49class GetFileForSavingOperation;
50class MoveOperation;
51class OpenFileOperation;
52class OperationObserver;
53class RemoveOperation;
54class SearchOperation;
55class TouchOperation;
56class TruncateOperation;
57class UpdateOperation;
58}  // namespace file_system
59
60// The production implementation of FileSystemInterface.
61class FileSystem : public FileSystemInterface,
62                   public internal::ChangeListLoaderObserver,
63                   public file_system::OperationObserver {
64 public:
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();
73
74  // FileSystemInterface overrides.
75  virtual void Initialize() OVERRIDE;
76  virtual void AddObserver(FileSystemObserver* observer) OVERRIDE;
77  virtual void RemoveObserver(FileSystemObserver* observer) OVERRIDE;
78  virtual void CheckForUpdates() OVERRIDE;
79  virtual void Search(const std::string& search_query,
80                      const GURL& next_url,
81                      const SearchCallback& callback) OVERRIDE;
82  virtual void SearchMetadata(const std::string& query,
83                              int options,
84                              int at_most_num_matches,
85                              const SearchMetadataCallback& callback) OVERRIDE;
86  virtual void TransferFileFromRemoteToLocal(
87      const base::FilePath& remote_src_file_path,
88      const base::FilePath& local_dest_file_path,
89      const FileOperationCallback& callback) OVERRIDE;
90  virtual void TransferFileFromLocalToRemote(
91      const base::FilePath& local_src_file_path,
92      const base::FilePath& remote_dest_file_path,
93      const FileOperationCallback& callback) OVERRIDE;
94  virtual void OpenFile(const base::FilePath& file_path,
95                        OpenMode open_mode,
96                        const OpenFileCallback& callback) OVERRIDE;
97  virtual void Copy(const base::FilePath& src_file_path,
98                    const base::FilePath& dest_file_path,
99                    const FileOperationCallback& callback) OVERRIDE;
100  virtual void Move(const base::FilePath& src_file_path,
101                    const base::FilePath& dest_file_path,
102                    const FileOperationCallback& callback) OVERRIDE;
103  virtual void Remove(const base::FilePath& file_path,
104                      bool is_recursive,
105                      const FileOperationCallback& callback) OVERRIDE;
106  virtual void CreateDirectory(const base::FilePath& directory_path,
107                               bool is_exclusive,
108                               bool is_recursive,
109                               const FileOperationCallback& callback) OVERRIDE;
110  virtual void CreateFile(const base::FilePath& file_path,
111                          bool is_exclusive,
112                          const FileOperationCallback& callback) OVERRIDE;
113  virtual void TouchFile(const base::FilePath& file_path,
114                         const base::Time& last_access_time,
115                         const base::Time& last_modified_time,
116                         const FileOperationCallback& callback) OVERRIDE;
117  virtual void TruncateFile(const base::FilePath& file_path,
118                            int64 length,
119                            const FileOperationCallback& callback) OVERRIDE;
120  virtual void Pin(const base::FilePath& file_path,
121                   const FileOperationCallback& callback) OVERRIDE;
122  virtual void Unpin(const base::FilePath& file_path,
123                     const FileOperationCallback& callback) OVERRIDE;
124  virtual void GetFileByPath(const base::FilePath& file_path,
125                             const GetFileCallback& callback) OVERRIDE;
126  virtual void GetFileByPathForSaving(const base::FilePath& file_path,
127                                      const GetFileCallback& callback) OVERRIDE;
128  virtual void GetFileContentByPath(
129      const base::FilePath& file_path,
130      const GetFileContentInitializedCallback& initialized_callback,
131      const google_apis::GetContentCallback& get_content_callback,
132      const FileOperationCallback& completion_callback) OVERRIDE;
133  virtual void GetResourceEntryByPath(
134      const base::FilePath& file_path,
135      const GetResourceEntryCallback& callback) OVERRIDE;
136  virtual void ReadDirectoryByPath(
137      const base::FilePath& directory_path,
138      const ReadDirectoryCallback& callback) OVERRIDE;
139  virtual void GetAvailableSpace(
140      const GetAvailableSpaceCallback& callback) OVERRIDE;
141  virtual void GetShareUrl(
142      const base::FilePath& file_path,
143      const GURL& embed_origin,
144      const GetShareUrlCallback& callback) OVERRIDE;
145  virtual void GetMetadata(
146      const GetFilesystemMetadataCallback& callback) OVERRIDE;
147  virtual void MarkCacheFileAsMounted(
148      const base::FilePath& drive_file_path,
149      const MarkMountedCallback& callback) OVERRIDE;
150  virtual void MarkCacheFileAsUnmounted(
151      const base::FilePath& cache_file_path,
152      const FileOperationCallback& callback) OVERRIDE;
153  virtual void GetCacheEntryByResourceId(
154      const std::string& resource_id,
155      const GetCacheEntryCallback& callback) OVERRIDE;
156  virtual void Reload() OVERRIDE;
157
158  // file_system::OperationObserver overrides.
159  virtual void OnDirectoryChangedByOperation(
160      const base::FilePath& directory_path) OVERRIDE;
161  virtual void OnCacheFileUploadNeededByOperation(
162      const std::string& resource_id) OVERRIDE;
163
164  // ChangeListLoader::Observer overrides.
165  // Used to propagate events from ChangeListLoader.
166  virtual void OnDirectoryChanged(
167      const base::FilePath& directory_path) OVERRIDE;
168  virtual void OnLoadFromServerComplete() OVERRIDE;
169  virtual void OnInitialLoadComplete() OVERRIDE;
170
171  // Used by tests.
172  internal::ChangeListLoader* change_list_loader_for_testing() {
173    return change_list_loader_.get();
174  }
175  internal::SyncClient* sync_client_for_testing() { return sync_client_.get(); }
176
177 private:
178  // Used to implement Reload().
179  void ReloadAfterReset(FileError error);
180
181  // Sets up ChangeListLoader.
182  void SetupChangeListLoader();
183
184  // Called on preference change.
185  void OnDisableDriveHostedFilesChanged();
186
187  // Part of CreateDirectory(). Called after ChangeListLoader::LoadIfNeeded()
188  // is called and made sure that the resource metadata is loaded.
189  void CreateDirectoryAfterLoad(const base::FilePath& directory_path,
190                                bool is_exclusive,
191                                bool is_recursive,
192                                const FileOperationCallback& callback,
193                                FileError load_error);
194
195  // Used to implement Pin().
196  void PinAfterGetResourceEntryByPath(const FileOperationCallback& callback,
197                                      FileError error,
198                                      scoped_ptr<ResourceEntry> entry);
199  void FinishPin(const FileOperationCallback& callback,
200                 const std::string& resource_id,
201                 FileError error);
202
203  // Used to implement Unpin().
204  void UnpinAfterGetResourceEntryByPath(const FileOperationCallback& callback,
205                                        FileError error,
206                                        scoped_ptr<ResourceEntry> entry);
207  void FinishUnpin(const FileOperationCallback& callback,
208                   const std::string& resource_id,
209                   FileError error);
210
211  // Callback for handling about resource fetch.
212  void OnGetAboutResource(
213      const GetAvailableSpaceCallback& callback,
214      google_apis::GDataErrorCode status,
215      scoped_ptr<google_apis::AboutResource> about_resource);
216
217  // Part of CheckForUpdates(). Called when
218  // ChangeListLoader::CheckForUpdates() is complete.
219  void OnUpdateChecked(FileError error);
220
221  // Changes state of hosted documents visibility, triggers directory refresh.
222  void SetHideHostedDocuments(bool hide);
223
224  // Initializes preference change observer.
225  void InitializePreferenceObserver();
226
227  // Part of GetResourceEntryByPath()
228  // 1) Called when GetLocallyStoredResourceEntry() is complete.
229  // 2) Called when LoadDirectoryIfNeeded() is complete.
230  void GetResourceEntryByPathAfterGetEntry(
231      const base::FilePath& file_path,
232      const GetResourceEntryCallback& callback,
233      scoped_ptr<ResourceEntry> entry,
234      FileError error);
235  void GetResourceEntryByPathAfterLoad(const base::FilePath& file_path,
236                                       const GetResourceEntryCallback& callback,
237                                       FileError error);
238
239  // Loads the entry info of the children of |directory_path| to resource
240  // metadata. |callback| must not be null.
241  void LoadDirectoryIfNeeded(const base::FilePath& directory_path,
242                             const FileOperationCallback& callback);
243  void LoadDirectoryIfNeededAfterGetEntry(
244      const base::FilePath& directory_path,
245      const FileOperationCallback& callback,
246      FileError error,
247      scoped_ptr<ResourceEntry> entry);
248
249  // Part of ReadDirectoryByPath()
250  // 1) Called when LoadDirectoryIfNeeded() is complete.
251  // 2) Called when ResourceMetadata::ReadDirectoryByPath() is complete.
252  // |callback| must not be null.
253  void ReadDirectoryByPathAfterLoad(
254      const base::FilePath& directory_path,
255      const ReadDirectoryCallback& callback,
256      FileError error);
257  void ReadDirectoryByPathAfterRead(
258      const ReadDirectoryCallback& callback,
259      FileError error,
260      scoped_ptr<ResourceEntryVector> entries);
261
262  // Part of MarkCacheFileAsMounted. Called after GetResourceEntryByPath is
263  // completed. |callback| must not be null.
264  void MarkCacheFileAsMountedAfterGetResourceEntry(
265      const MarkMountedCallback& callback,
266      FileError error,
267      scoped_ptr<ResourceEntry> entry);
268
269  // Part of GetShareUrl. Resolves the resource entry to get the resource it,
270  // and then uses it to ask for the share url. |callback| must not be null.
271  void GetShareUrlAfterGetResourceEntry(
272      const base::FilePath& file_path,
273      const GURL& embed_origin,
274      const GetShareUrlCallback& callback,
275      FileError error,
276      scoped_ptr<ResourceEntry> entry);
277  void OnGetResourceEntryForGetShareUrl(
278      const GetShareUrlCallback& callback,
279      google_apis::GDataErrorCode status,
280      const GURL& share_url);
281
282  // Used to get Drive related preferences.
283  PrefService* pref_service_;
284
285  // Sub components owned by DriveIntegrationService.
286  internal::FileCache* cache_;
287  DriveServiceInterface* drive_service_;
288  JobScheduler* scheduler_;
289  internal::ResourceMetadata* resource_metadata_;
290
291  // Time of the last update check.
292  base::Time last_update_check_time_;
293
294  // Error of the last update check.
295  FileError last_update_check_error_;
296
297  // True if hosted documents should be hidden.
298  bool hide_hosted_docs_;
299
300  scoped_ptr<PrefChangeRegistrar> pref_registrar_;
301
302  scoped_ptr<internal::SyncClient> sync_client_;
303
304  // The loader is used to load the change lists.
305  scoped_ptr<internal::ChangeListLoader> change_list_loader_;
306
307  ObserverList<FileSystemObserver> observers_;
308
309  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
310
311  base::FilePath temporary_file_directory_;
312
313  // Implementation of each file system operation.
314  scoped_ptr<file_system::CopyOperation> copy_operation_;
315  scoped_ptr<file_system::CreateDirectoryOperation> create_directory_operation_;
316  scoped_ptr<file_system::CreateFileOperation> create_file_operation_;
317  scoped_ptr<file_system::MoveOperation> move_operation_;
318  scoped_ptr<file_system::OpenFileOperation> open_file_operation_;
319  scoped_ptr<file_system::RemoveOperation> remove_operation_;
320  scoped_ptr<file_system::TouchOperation> touch_operation_;
321  scoped_ptr<file_system::TruncateOperation> truncate_operation_;
322  scoped_ptr<file_system::DownloadOperation> download_operation_;
323  scoped_ptr<file_system::UpdateOperation> update_operation_;
324  scoped_ptr<file_system::SearchOperation> search_operation_;
325  scoped_ptr<file_system::GetFileForSavingOperation>
326      get_file_for_saving_operation_;
327
328  // Note: This should remain the last member so it'll be destroyed and
329  // invalidate the weak pointers before any other members are destroyed.
330  base::WeakPtrFactory<FileSystem> weak_ptr_factory_;
331
332  DISALLOW_COPY_AND_ASSIGN(FileSystem);
333};
334
335}  // namespace drive
336
337#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_H_
338