Upstream version 7.35.139.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 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 GetCacheEntry(
157       const base::FilePath& drive_file_path,
158       const GetCacheEntryCallback& callback) OVERRIDE;
159   virtual void AddPermission(const base::FilePath& drive_file_path,
160                              const std::string& email,
161                              google_apis::drive::PermissionRole role,
162                              const FileOperationCallback& callback) OVERRIDE;
163   virtual void Reset(const FileOperationCallback& callback) OVERRIDE;
164   virtual void GetPathFromResourceId(const std::string& resource_id,
165                                      const GetFilePathCallback& callback)
166       OVERRIDE;
167
168   // file_system::OperationObserver overrides.
169   virtual void OnDirectoryChangedByOperation(
170       const base::FilePath& directory_path) OVERRIDE;
171   virtual void OnEntryUpdatedByOperation(const std::string& local_id) OVERRIDE;
172   virtual void OnDriveSyncError(file_system::DriveSyncErrorType type,
173                                 const std::string& local_id) OVERRIDE;
174
175   // ChangeListLoader::Observer overrides.
176   // Used to propagate events from ChangeListLoader.
177   virtual void OnDirectoryChanged(
178       const base::FilePath& directory_path) OVERRIDE;
179   virtual void OnLoadFromServerComplete() OVERRIDE;
180   virtual void OnInitialLoadComplete() OVERRIDE;
181
182   // Used by tests.
183   internal::ChangeListLoader* change_list_loader_for_testing() {
184     return change_list_loader_.get();
185   }
186   internal::SyncClient* sync_client_for_testing() { return sync_client_.get(); }
187
188  private:
189   struct CreateDirectoryParams;
190
191   // Used for initialization and Reset(). (Re-)initializes sub components that
192   // need to be recreated during the reset of resource metadata and the cache.
193   void ResetComponents();
194
195   // Part of CreateDirectory(). Called after ReadDirectory()
196   // is called and made sure that the resource metadata is loaded.
197   void CreateDirectoryAfterRead(const CreateDirectoryParams& params,
198                                 FileError error);
199
200   void FinishPin(const FileOperationCallback& callback,
201                  const std::string* local_id,
202                  FileError error);
203
204   void FinishUnpin(const FileOperationCallback& callback,
205                    const std::string* local_id,
206                    FileError error);
207
208   // Callback for handling about resource fetch.
209   void OnGetAboutResource(
210       const GetAvailableSpaceCallback& callback,
211       google_apis::GDataErrorCode status,
212       scoped_ptr<google_apis::AboutResource> about_resource);
213
214   // Part of CheckForUpdates(). Called when
215   // ChangeListLoader::CheckForUpdates() is complete.
216   void OnUpdateChecked(FileError error);
217
218   // Part of GetResourceEntry().
219   // Called when ReadDirectory() is complete.
220   void GetResourceEntryAfterRead(const base::FilePath& file_path,
221                                  const GetResourceEntryCallback& callback,
222                                  FileError error);
223
224   // Part of GetShareUrl. Resolves the resource entry to get the resource it,
225   // and then uses it to ask for the share url. |callback| must not be null.
226   void GetShareUrlAfterGetResourceEntry(const base::FilePath& file_path,
227                                         const GURL& embed_origin,
228                                         const GetShareUrlCallback& callback,
229                                         ResourceEntry* entry,
230                                         FileError error);
231   void OnGetResourceEntryForGetShareUrl(const GetShareUrlCallback& callback,
232                                         google_apis::GDataErrorCode status,
233                                         const GURL& share_url);
234   // Part of AddPermission.
235   void AddPermissionAfterGetResourceEntry(
236       const std::string& email,
237       google_apis::drive::PermissionRole role,
238       const FileOperationCallback& callback,
239       ResourceEntry* entry,
240       FileError error);
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::DirectoryLoader> directory_loader_;
273
274   scoped_ptr<internal::SyncClient> sync_client_;
275
276   ObserverList<FileSystemObserver> observers_;
277
278   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
279
280   base::FilePath temporary_file_directory_;
281
282   // Implementation of each file system operation.
283   scoped_ptr<file_system::CopyOperation> copy_operation_;
284   scoped_ptr<file_system::CreateDirectoryOperation> create_directory_operation_;
285   scoped_ptr<file_system::CreateFileOperation> create_file_operation_;
286   scoped_ptr<file_system::MoveOperation> move_operation_;
287   scoped_ptr<file_system::OpenFileOperation> open_file_operation_;
288   scoped_ptr<file_system::RemoveOperation> remove_operation_;
289   scoped_ptr<file_system::TouchOperation> touch_operation_;
290   scoped_ptr<file_system::TruncateOperation> truncate_operation_;
291   scoped_ptr<file_system::DownloadOperation> download_operation_;
292   scoped_ptr<file_system::SearchOperation> search_operation_;
293   scoped_ptr<file_system::GetFileForSavingOperation>
294       get_file_for_saving_operation_;
295
296   // Note: This should remain the last member so it'll be destroyed and
297   // invalidate the weak pointers before any other members are destroyed.
298   base::WeakPtrFactory<FileSystem> weak_ptr_factory_;
299
300   DISALLOW_COPY_AND_ASSIGN(FileSystem);
301 };
302
303 }  // namespace drive
304
305 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_H_