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