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 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_
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"
17 class ScopedClosureRunner;
18 class SequencedTaskRunner;
24 class FileWriteWatcher;
25 class ResourceMetadata;
26 } // namespace internal
32 namespace file_system {
34 class CreateFileOperation;
35 class DownloadOperation;
36 class OperationObserver;
38 // Implements GetFileForSaving() operation that prepares a local cache for
39 // a Drive file whose next modification is monitored and notified to the
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 {
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();
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);
61 internal::FileWriteWatcher* file_write_watcher_for_testing() {
62 return file_write_watcher_.get();
66 void GetFileForSavingAfterCreate(const base::FilePath& file_path,
67 const GetFileCallback& callback,
69 void GetFileForSavingAfterDownload(const GetFileCallback& callback,
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,
79 void GetFileForSavingAfterWatch(const GetFileCallback& callback,
80 const base::FilePath& cache_path,
81 scoped_ptr<ResourceEntry> entry,
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);
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_;
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);
102 } // namespace file_system
105 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_GET_FILE_FOR_SAVING_OPERATION_H_