- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / file_system / get_file_for_saving_operation.h
1 // Copyright 2013 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_GET_FILE_FOR_SAVING_OPERATION_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_GET_FILE_FOR_SAVING_OPERATION_H_
7
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "chrome/browser/chromeos/drive/file_errors.h"
13 #include "chrome/browser/chromeos/drive/file_system_interface.h"
14
15 namespace base {
16 class FilePath;
17 class SequencedTaskRunner;
18 }  // namespace base
19
20 namespace drive {
21 namespace internal {
22 class FileCache;
23 class FileWriteWatcher;
24 class ResourceMetadata;
25 }  // namespace internal
26
27 class JobScheduler;
28 class ResourceEntry;
29
30 namespace file_system {
31
32 class CreateFileOperation;
33 class DownloadOperation;
34 class OperationObserver;
35
36 // Implements GetFileForSaving() operation that prepares a local cache for
37 // a Drive file whose next modification is monitored and notified to the
38 // OperationObserver.
39 // TODO(kinaba): crbug.com/269424: we might want to monitor all the changes
40 // to the cache directory, not just the one immediately after the save dialog.
41 class GetFileForSavingOperation {
42  public:
43   GetFileForSavingOperation(base::SequencedTaskRunner* blocking_task_runner,
44                             OperationObserver* observer,
45                             JobScheduler* scheduler,
46                             internal::ResourceMetadata* metadata,
47                             internal::FileCache* cache,
48                             const base::FilePath& temporary_file_directory);
49   ~GetFileForSavingOperation();
50
51   // Makes sure that |file_path| in the file system is available in the local
52   // cache, and marks it as dirty. The next modification to the cache file is
53   // watched and is automatically notified to the observer. If the entry is not
54   // present in the file system, it is created.
55   void GetFileForSaving(const base::FilePath& file_path,
56                         const GetFileCallback& callback);
57
58   internal::FileWriteWatcher* file_write_watcher_for_testing() {
59     return file_write_watcher_.get();
60   }
61
62  private:
63   void GetFileForSavingAfterCreate(const base::FilePath& file_path,
64                                    const GetFileCallback& callback,
65                                    FileError error);
66   void GetFileForSavingAfterDownload(const GetFileCallback& callback,
67                                      FileError error,
68                                      const base::FilePath& cache_path,
69                                      scoped_ptr<ResourceEntry> entry);
70   void GetFileForSavingAfterMarkDirty(const GetFileCallback& callback,
71                                       const base::FilePath& cache_path,
72                                       const std::string* local_id,
73                                       scoped_ptr<ResourceEntry> entry,
74                                       FileError error);
75   void GetFileForSavingAfterWatch(const GetFileCallback& callback,
76                                   const base::FilePath& cache_path,
77                                   scoped_ptr<ResourceEntry> entry,
78                                   bool success);
79   // Called when the cache file for |local_id| is written.
80   void OnWriteEvent(const std::string& local_id);
81
82   scoped_ptr<CreateFileOperation> create_file_operation_;
83   scoped_ptr<DownloadOperation> download_operation_;
84   scoped_ptr<internal::FileWriteWatcher> file_write_watcher_;
85   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
86   OperationObserver* observer_;
87   internal::ResourceMetadata* metadata_;
88   internal::FileCache* cache_;
89
90   // Note: This should remain the last member so it'll be destroyed and
91   // invalidate the weak pointers before any other members are destroyed.
92   base::WeakPtrFactory<GetFileForSavingOperation> weak_ptr_factory_;
93   DISALLOW_COPY_AND_ASSIGN(GetFileForSavingOperation);
94 };
95
96 }  // namespace file_system
97 }  // namespace drive
98
99 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_GET_FILE_FOR_SAVING_OPERATION_H_