Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / drive / file_system / get_file_for_saving_operation.h
blob9b2fc7c09063cefa0185f4384d900876edd392b8
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.
5 #ifndef COMPONENTS_DRIVE_FILE_SYSTEM_GET_FILE_FOR_SAVING_OPERATION_H_
6 #define COMPONENTS_DRIVE_FILE_SYSTEM_GET_FILE_FOR_SAVING_OPERATION_H_
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 "base/threading/thread_checker.h"
13 #include "components/drive/file_errors.h"
14 #include "components/drive/file_system_interface.h"
16 namespace base {
17 class FilePath;
18 class ScopedClosureRunner;
19 class SequencedTaskRunner;
20 } // namespace base
22 namespace drive {
23 namespace internal {
24 class FileCache;
25 class FileWriteWatcher;
26 class ResourceMetadata;
27 } // namespace internal
29 class EventLogger;
30 class JobScheduler;
31 class ResourceEntry;
33 namespace file_system {
35 class CreateFileOperation;
36 class DownloadOperation;
37 class OperationDelegate;
39 // Implements GetFileForSaving() operation that prepares a local cache for
40 // a Drive file whose next modification is monitored and notified to the
41 // OperationDelegate.
42 // TODO(kinaba): crbug.com/269424: we might want to monitor all the changes
43 // to the cache directory, not just the one immediately after the save dialog.
44 class GetFileForSavingOperation {
45 public:
46 GetFileForSavingOperation(EventLogger* logger,
47 base::SequencedTaskRunner* blocking_task_runner,
48 base::SingleThreadTaskRunner* file_task_runner,
49 OperationDelegate* delegate,
50 JobScheduler* scheduler,
51 internal::ResourceMetadata* metadata,
52 internal::FileCache* cache,
53 const base::FilePath& temporary_file_directory);
54 ~GetFileForSavingOperation();
56 // Makes sure that |file_path| in the file system is available in the local
57 // cache, and marks it as dirty. The next modification to the cache file is
58 // watched and is automatically notified to the delegate. If the entry is not
59 // present in the file system, it is created.
60 void GetFileForSaving(const base::FilePath& file_path,
61 const GetFileCallback& callback);
63 internal::FileWriteWatcher* file_write_watcher_for_testing() {
64 return file_write_watcher_.get();
67 private:
68 void GetFileForSavingAfterCreate(const base::FilePath& file_path,
69 const GetFileCallback& callback,
70 FileError error);
71 void GetFileForSavingAfterDownload(const GetFileCallback& callback,
72 FileError error,
73 const base::FilePath& cache_path,
74 scoped_ptr<ResourceEntry> entry);
75 void GetFileForSavingAfterOpenForWrite(
76 const GetFileCallback& callback,
77 const base::FilePath& cache_path,
78 scoped_ptr<ResourceEntry> entry,
79 scoped_ptr<base::ScopedClosureRunner>* file_closer,
80 FileError error);
81 void GetFileForSavingAfterWatch(const GetFileCallback& callback,
82 const base::FilePath& cache_path,
83 scoped_ptr<ResourceEntry> entry,
84 bool success);
85 // Called when the cache file for |local_id| is written.
86 void OnWriteEvent(const std::string& local_id,
87 scoped_ptr<base::ScopedClosureRunner> file_closer);
89 EventLogger* logger_;
90 scoped_ptr<CreateFileOperation> create_file_operation_;
91 scoped_ptr<DownloadOperation> download_operation_;
92 scoped_ptr<internal::FileWriteWatcher> file_write_watcher_;
93 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
94 OperationDelegate* delegate_;
95 internal::ResourceMetadata* metadata_;
96 internal::FileCache* cache_;
98 base::ThreadChecker thread_checker_;
100 // Note: This should remain the last member so it'll be destroyed and
101 // invalidate the weak pointers before any other members are destroyed.
102 base::WeakPtrFactory<GetFileForSavingOperation> weak_ptr_factory_;
103 DISALLOW_COPY_AND_ASSIGN(GetFileForSavingOperation);
106 } // namespace file_system
107 } // namespace drive
109 #endif // COMPONENTS_DRIVE_FILE_SYSTEM_GET_FILE_FOR_SAVING_OPERATION_H_