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