Upstream version 5.34.92.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 FileCacheEntry;
34 class FileSystemObserver;
35 class JobScheduler;
36
37 namespace internal {
38 class ChangeListLoader;
39 class FileCache;
40 class ResourceMetadata;
41 class SyncClient;
42 }  // namespace internal
43
44 namespace file_system {
45 class CopyOperation;
46 class CreateDirectoryOperation;
47 class CreateFileOperation;
48 class DownloadOperation;
49 class GetFileForSavingOperation;
50 class MoveOperation;
51 class OpenFileOperation;
52 class OperationObserver;
53 class RemoveOperation;
54 class SearchOperation;
55 class TouchOperation;
56 class TruncateOperation;
57 }  // namespace file_system
58
59 // The production implementation of FileSystemInterface.
60 class 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                     bool preserve_last_modified,
95                     const FileOperationCallback& callback) OVERRIDE;
96   virtual void Move(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 Remove(const base::FilePath& file_path,
101                       bool is_recursive,
102                       const FileOperationCallback& callback) OVERRIDE;
103   virtual void CreateDirectory(const base::FilePath& directory_path,
104                                bool is_exclusive,
105                                bool is_recursive,
106                                const FileOperationCallback& callback) OVERRIDE;
107   virtual void CreateFile(const base::FilePath& file_path,
108                           bool is_exclusive,
109                           const std::string& mime_type,
110                           const FileOperationCallback& callback) OVERRIDE;
111   virtual void TouchFile(const base::FilePath& file_path,
112                          const base::Time& last_access_time,
113                          const base::Time& last_modified_time,
114                          const FileOperationCallback& callback) OVERRIDE;
115   virtual void TruncateFile(const base::FilePath& file_path,
116                             int64 length,
117                             const FileOperationCallback& callback) OVERRIDE;
118   virtual void Pin(const base::FilePath& file_path,
119                    const FileOperationCallback& callback) OVERRIDE;
120   virtual void Unpin(const base::FilePath& file_path,
121                      const FileOperationCallback& callback) OVERRIDE;
122   virtual void GetFile(const base::FilePath& file_path,
123                              const GetFileCallback& callback) OVERRIDE;
124   virtual void GetFileForSaving(const base::FilePath& file_path,
125                                       const GetFileCallback& callback) OVERRIDE;
126   virtual void GetFileContent(
127       const base::FilePath& file_path,
128       const GetFileContentInitializedCallback& initialized_callback,
129       const google_apis::GetContentCallback& get_content_callback,
130       const FileOperationCallback& completion_callback) OVERRIDE;
131   virtual void GetResourceEntry(
132       const base::FilePath& file_path,
133       const GetResourceEntryCallback& callback) OVERRIDE;
134   virtual void ReadDirectory(
135       const base::FilePath& directory_path,
136       const ReadDirectoryCallback& callback) OVERRIDE;
137   virtual void GetAvailableSpace(
138       const GetAvailableSpaceCallback& callback) OVERRIDE;
139   virtual void GetShareUrl(
140       const base::FilePath& file_path,
141       const GURL& embed_origin,
142       const GetShareUrlCallback& callback) OVERRIDE;
143   virtual void GetMetadata(
144       const GetFilesystemMetadataCallback& callback) OVERRIDE;
145   virtual void MarkCacheFileAsMounted(
146       const base::FilePath& drive_file_path,
147       const MarkMountedCallback& callback) OVERRIDE;
148   virtual void MarkCacheFileAsUnmounted(
149       const base::FilePath& cache_file_path,
150       const FileOperationCallback& callback) OVERRIDE;
151   virtual void GetCacheEntry(
152       const base::FilePath& drive_file_path,
153       const GetCacheEntryCallback& callback) OVERRIDE;
154   virtual void Reset(const FileOperationCallback& callback) OVERRIDE;
155
156   // file_system::OperationObserver overrides.
157   virtual void OnDirectoryChangedByOperation(
158       const base::FilePath& directory_path) OVERRIDE;
159   virtual void OnEntryUpdatedByOperation(const std::string& local_id) OVERRIDE;
160   virtual void OnDriveSyncError(file_system::DriveSyncErrorType type,
161                                 const std::string& local_id) OVERRIDE;
162
163   // ChangeListLoader::Observer overrides.
164   // Used to propagate events from ChangeListLoader.
165   virtual void OnDirectoryChanged(
166       const base::FilePath& directory_path) OVERRIDE;
167   virtual void OnLoadFromServerComplete() OVERRIDE;
168   virtual void OnInitialLoadComplete() OVERRIDE;
169
170   // Used by tests.
171   internal::ChangeListLoader* change_list_loader_for_testing() {
172     return change_list_loader_.get();
173   }
174   internal::SyncClient* sync_client_for_testing() { return sync_client_.get(); }
175
176  private:
177   // Used for initialization and Reset(). (Re-)initializes sub components that
178   // need to be recreated during the reset of resource metadata and the cache.
179   void ResetComponents();
180
181   // Part of CreateDirectory(). Called after ChangeListLoader::LoadIfNeeded()
182   // is called and made sure that the resource metadata is loaded.
183   void CreateDirectoryAfterLoad(const base::FilePath& directory_path,
184                                 bool is_exclusive,
185                                 bool is_recursive,
186                                 const FileOperationCallback& callback,
187                                 FileError load_error);
188
189   void FinishPin(const FileOperationCallback& callback,
190                  const std::string* local_id,
191                  FileError error);
192
193   void FinishUnpin(const FileOperationCallback& callback,
194                    const std::string* local_id,
195                    FileError error);
196
197   // Callback for handling about resource fetch.
198   void OnGetAboutResource(
199       const GetAvailableSpaceCallback& callback,
200       google_apis::GDataErrorCode status,
201       scoped_ptr<google_apis::AboutResource> about_resource);
202
203   // Part of CheckForUpdates(). Called when
204   // ChangeListLoader::CheckForUpdates() is complete.
205   void OnUpdateChecked(FileError error);
206
207   // Part of GetResourceEntry().
208   // Called when LoadDirectoryIfNeeded() is complete.
209   void GetResourceEntryAfterLoad(const base::FilePath& file_path,
210                                  const GetResourceEntryCallback& callback,
211                                  FileError error);
212
213   // Part of ReadDirectory()
214   // 1) Called when LoadDirectoryIfNeeded() is complete.
215   // 2) Called when ResourceMetadata::ReadDirectory() is complete.
216   // |callback| must not be null.
217   void ReadDirectoryAfterLoad(const base::FilePath& directory_path,
218                               const ReadDirectoryCallback& callback,
219                               FileError error);
220   void ReadDirectoryAfterRead(const base::FilePath& directory_path,
221                               const ReadDirectoryCallback& callback,
222                               FileError error,
223                               scoped_ptr<ResourceEntryVector> entries);
224
225   // Part of GetShareUrl. Resolves the resource entry to get the resource it,
226   // and then uses it to ask for the share url. |callback| must not be null.
227   void GetShareUrlAfterGetResourceEntry(
228       const base::FilePath& file_path,
229       const GURL& embed_origin,
230       const GetShareUrlCallback& callback,
231       FileError error,
232       scoped_ptr<ResourceEntry> entry);
233   void OnGetResourceEntryForGetShareUrl(
234       const GetShareUrlCallback& callback,
235       google_apis::GDataErrorCode status,
236       const GURL& share_url);
237
238   // Part of OnDriveSyncError().
239   virtual void OnDriveSyncErrorAfterGetFilePath(
240       file_system::DriveSyncErrorType type,
241       const base::FilePath& fiepath);
242
243   // Used to get Drive related preferences.
244   PrefService* pref_service_;
245
246   // Sub components owned by DriveIntegrationService.
247   internal::FileCache* cache_;
248   DriveServiceInterface* drive_service_;
249   JobScheduler* scheduler_;
250   internal::ResourceMetadata* resource_metadata_;
251
252   // Time of the last update check.
253   base::Time last_update_check_time_;
254
255   // Error of the last update check.
256   FileError last_update_check_error_;
257
258   scoped_ptr<internal::SyncClient> sync_client_;
259
260   // The loader is used to load the change lists.
261   scoped_ptr<internal::ChangeListLoader> change_list_loader_;
262
263   ObserverList<FileSystemObserver> observers_;
264
265   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
266
267   base::FilePath temporary_file_directory_;
268
269   // Implementation of each file system operation.
270   scoped_ptr<file_system::CopyOperation> copy_operation_;
271   scoped_ptr<file_system::CreateDirectoryOperation> create_directory_operation_;
272   scoped_ptr<file_system::CreateFileOperation> create_file_operation_;
273   scoped_ptr<file_system::MoveOperation> move_operation_;
274   scoped_ptr<file_system::OpenFileOperation> open_file_operation_;
275   scoped_ptr<file_system::RemoveOperation> remove_operation_;
276   scoped_ptr<file_system::TouchOperation> touch_operation_;
277   scoped_ptr<file_system::TruncateOperation> truncate_operation_;
278   scoped_ptr<file_system::DownloadOperation> download_operation_;
279   scoped_ptr<file_system::SearchOperation> search_operation_;
280   scoped_ptr<file_system::GetFileForSavingOperation>
281       get_file_for_saving_operation_;
282
283   // Note: This should remain the last member so it'll be destroyed and
284   // invalidate the weak pointers before any other members are destroyed.
285   base::WeakPtrFactory<FileSystem> weak_ptr_factory_;
286
287   DISALLOW_COPY_AND_ASSIGN(FileSystem);
288 };
289
290 }  // namespace drive
291
292 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_H_