Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / file_system.h
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 FileCache;
42 class LoaderController;
43 class ResourceMetadata;
44 class SyncClient;
45 }  // namespace internal
46
47 namespace file_system {
48 class CopyOperation;
49 class CreateDirectoryOperation;
50 class CreateFileOperation;
51 class DownloadOperation;
52 class GetFileForSavingOperation;
53 class MoveOperation;
54 class OpenFileOperation;
55 class OperationObserver;
56 class RemoveOperation;
57 class SearchOperation;
58 class TouchOperation;
59 class TruncateOperation;
60 }  // namespace file_system
61
62 // The production implementation of FileSystemInterface.
63 class FileSystem : public FileSystemInterface,
64                    public internal::ChangeListLoaderObserver,
65                    public file_system::OperationObserver {
66  public:
67   FileSystem(PrefService* pref_service,
68              EventLogger* logger,
69              internal::FileCache* cache,
70              DriveServiceInterface* drive_service,
71              JobScheduler* scheduler,
72              internal::ResourceMetadata* resource_metadata,
73              base::SequencedTaskRunner* blocking_task_runner,
74              const base::FilePath& temporary_file_directory);
75   virtual ~FileSystem();
76
77   // FileSystemInterface overrides.
78   virtual void AddObserver(FileSystemObserver* observer) OVERRIDE;
79   virtual void RemoveObserver(FileSystemObserver* observer) OVERRIDE;
80   virtual void CheckForUpdates() OVERRIDE;
81   virtual void Search(const std::string& search_query,
82                       const GURL& next_link,
83                       const SearchCallback& callback) OVERRIDE;
84   virtual void SearchMetadata(const std::string& query,
85                               int options,
86                               int at_most_num_matches,
87                               const SearchMetadataCallback& callback) OVERRIDE;
88   virtual void TransferFileFromLocalToRemote(
89       const base::FilePath& local_src_file_path,
90       const base::FilePath& remote_dest_file_path,
91       const FileOperationCallback& callback) OVERRIDE;
92   virtual void OpenFile(const base::FilePath& file_path,
93                         OpenMode open_mode,
94                         const std::string& mime_type,
95                         const OpenFileCallback& callback) OVERRIDE;
96   virtual void Copy(const base::FilePath& src_file_path,
97                     const base::FilePath& dest_file_path,
98                     bool preserve_last_modified,
99                     const FileOperationCallback& callback) OVERRIDE;
100   virtual void Move(const base::FilePath& src_file_path,
101                     const base::FilePath& dest_file_path,
102                     bool preserve_last_modified,
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 void 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 ReadDirectoryCallback& callback) OVERRIDE;
141   virtual void GetAvailableSpace(
142       const GetAvailableSpaceCallback& callback) OVERRIDE;
143   virtual void GetShareUrl(
144       const base::FilePath& file_path,
145       const GURL& embed_origin,
146       const GetShareUrlCallback& callback) OVERRIDE;
147   virtual void GetMetadata(
148       const GetFilesystemMetadataCallback& callback) OVERRIDE;
149   virtual void MarkCacheFileAsMounted(
150       const base::FilePath& drive_file_path,
151       const MarkMountedCallback& callback) OVERRIDE;
152   virtual void MarkCacheFileAsUnmounted(
153       const base::FilePath& cache_file_path,
154       const FileOperationCallback& callback) OVERRIDE;
155   virtual void GetCacheEntry(
156       const base::FilePath& drive_file_path,
157       const GetCacheEntryCallback& callback) OVERRIDE;
158   virtual void Reset(const FileOperationCallback& callback) OVERRIDE;
159
160   // file_system::OperationObserver overrides.
161   virtual void OnDirectoryChangedByOperation(
162       const base::FilePath& directory_path) OVERRIDE;
163   virtual void OnEntryUpdatedByOperation(const std::string& local_id) OVERRIDE;
164   virtual void OnDriveSyncError(file_system::DriveSyncErrorType type,
165                                 const std::string& local_id) OVERRIDE;
166
167   // ChangeListLoader::Observer overrides.
168   // Used to propagate events from ChangeListLoader.
169   virtual void OnDirectoryChanged(
170       const base::FilePath& directory_path) OVERRIDE;
171   virtual void OnLoadFromServerComplete() OVERRIDE;
172   virtual void OnInitialLoadComplete() OVERRIDE;
173
174   // Used by tests.
175   internal::ChangeListLoader* change_list_loader_for_testing() {
176     return change_list_loader_.get();
177   }
178   internal::SyncClient* sync_client_for_testing() { return sync_client_.get(); }
179
180  private:
181   // Used for initialization and Reset(). (Re-)initializes sub components that
182   // need to be recreated during the reset of resource metadata and the cache.
183   void ResetComponents();
184
185   // Part of CreateDirectory(). Called after ChangeListLoader::LoadIfNeeded()
186   // is called and made sure that the resource metadata is loaded.
187   void CreateDirectoryAfterLoad(const base::FilePath& directory_path,
188                                 bool is_exclusive,
189                                 bool is_recursive,
190                                 const FileOperationCallback& callback,
191                                 FileError load_error);
192
193   void FinishPin(const FileOperationCallback& callback,
194                  const std::string* local_id,
195                  FileError error);
196
197   void FinishUnpin(const FileOperationCallback& callback,
198                    const std::string* local_id,
199                    FileError error);
200
201   // Callback for handling about resource fetch.
202   void OnGetAboutResource(
203       const GetAvailableSpaceCallback& callback,
204       google_apis::GDataErrorCode status,
205       scoped_ptr<google_apis::AboutResource> about_resource);
206
207   // Part of CheckForUpdates(). Called when
208   // ChangeListLoader::CheckForUpdates() is complete.
209   void OnUpdateChecked(FileError error);
210
211   // Part of GetResourceEntry().
212   // Called when LoadDirectoryIfNeeded() is complete.
213   void GetResourceEntryAfterLoad(const base::FilePath& file_path,
214                                  const GetResourceEntryCallback& callback,
215                                  FileError error);
216
217   // Part of ReadDirectory()
218   // 1) Called when LoadDirectoryIfNeeded() is complete.
219   // 2) Called when ResourceMetadata::ReadDirectory() is complete.
220   // |callback| must not be null.
221   void ReadDirectoryAfterLoad(const base::FilePath& directory_path,
222                               const ReadDirectoryCallback& callback,
223                               FileError error);
224   void ReadDirectoryAfterRead(const base::FilePath& directory_path,
225                               const ReadDirectoryCallback& callback,
226                               const ResourceEntryVector* entries,
227                               FileError error);
228
229   // Part of GetShareUrl. Resolves the resource entry to get the resource it,
230   // and then uses it to ask for the share url. |callback| must not be null.
231   void GetShareUrlAfterGetResourceEntry(
232       const base::FilePath& file_path,
233       const GURL& embed_origin,
234       const GetShareUrlCallback& callback,
235       FileError error,
236       scoped_ptr<ResourceEntry> entry);
237   void OnGetResourceEntryForGetShareUrl(
238       const GetShareUrlCallback& callback,
239       google_apis::GDataErrorCode status,
240       const GURL& share_url);
241
242   // Part of OnDriveSyncError().
243   virtual void OnDriveSyncErrorAfterGetFilePath(
244       file_system::DriveSyncErrorType type,
245       const base::FilePath& fiepath);
246
247   // Used to get Drive related preferences.
248   PrefService* pref_service_;
249
250   // Sub components owned by DriveIntegrationService.
251   EventLogger* logger_;
252   internal::FileCache* cache_;
253   DriveServiceInterface* drive_service_;
254   JobScheduler* scheduler_;
255   internal::ResourceMetadata* resource_metadata_;
256
257   // Time of the last update check.
258   base::Time last_update_check_time_;
259
260   // Error of the last update check.
261   FileError last_update_check_error_;
262
263   // Used to load about resource.
264   scoped_ptr<internal::AboutResourceLoader> about_resource_loader_;
265
266   // Used to control ChangeListLoader.
267   scoped_ptr<internal::LoaderController> loader_controller_;
268
269   // The loader is used to load the change lists.
270   scoped_ptr<internal::ChangeListLoader> change_list_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_