OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_WRITE_HELPER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_WRITE_HELPER_H_ |
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_WRITE_HELPER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_WRITE_HELPER_H_ |
7 | 7 |
| 8 #include "base/callback_forward.h" |
8 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
9 #include "chrome/browser/chromeos/drive/file_errors.h" | 10 #include "chrome/browser/chromeos/drive/file_errors.h" |
10 #include "chrome/browser/chromeos/drive/file_system_interface.h" | |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 class FilePath; | 13 class FilePath; |
14 } | 14 } |
15 | 15 |
16 namespace drive { | 16 namespace drive { |
17 | 17 |
| 18 class FileSystemInterface; |
| 19 |
18 // This class provides higher level operations for writing to Drive files over | 20 // This class provides higher level operations for writing to Drive files over |
19 // FileSystemInterface. | 21 // FileSystemInterface. |
20 class FileWriteHelper { | 22 class FileWriteHelper { |
21 public: | 23 public: |
| 24 // Callback for PrepareWritableFileAndRun. |
| 25 typedef base::Callback<void(FileError error, |
| 26 const base::FilePath& file_path)> |
| 27 OpenFileCallback; |
| 28 |
22 explicit FileWriteHelper(FileSystemInterface* file_system); | 29 explicit FileWriteHelper(FileSystemInterface* file_system); |
23 ~FileWriteHelper(); | 30 ~FileWriteHelper(); |
24 | 31 |
25 // Prepares a local temporary file path and passes it to |callback| on the | 32 // Prepares a local temporary file path and passes it to |callback| on the |
26 // blocking thread pool that allows file operations. The modification to | 33 // blocking thread pool that allows file operations. The modification to |
27 // the file is reflected to GData |path|. If |path| does not exist, a new | 34 // the file is reflected to GData |path|. If |path| does not exist, a new |
28 // file is created. | 35 // file is created. |
29 // | 36 // |
30 // Must be called from UI thread. | 37 // Must be called from UI thread. |
31 void PrepareWritableFileAndRun(const base::FilePath& path, | 38 void PrepareWritableFileAndRun(const base::FilePath& path, |
32 const OpenFileCallback& callback); | 39 const OpenFileCallback& callback); |
33 | 40 |
34 private: | 41 private: |
35 // Part of PrepareWritableFilePathAndRun(). It tries CreateFile for the case | 42 // Part of PrepareWritableFilePathAndRun(). It tries CreateFile for the case |
36 // file does not exist yet, does OpenFile to download and mark the file as | 43 // file does not exist yet, does OpenFile to download and mark the file as |
37 // dirty, runs |callback|, and finally calls CloseFile. | 44 // dirty, runs |callback|, and finally closes the file. |
38 void PrepareWritableFileAndRunAfterOpenFile( | 45 void PrepareWritableFileAndRunAfterOpenFile( |
39 const base::FilePath& file_path, | 46 const base::FilePath& file_path, |
40 const OpenFileCallback& callback, | 47 const OpenFileCallback& callback, |
41 FileError result, | 48 FileError result, |
42 const base::FilePath& local_cache_path); | 49 const base::FilePath& local_cache_path, |
43 void PrepareWritableFileAndRunAfterCallback(const base::FilePath& file_path); | 50 const base::Closure& close_callback); |
44 | 51 |
45 FileSystemInterface* file_system_; // Owned by DriveIntegrationService. | 52 FileSystemInterface* file_system_; // Owned by DriveIntegrationService. |
46 | 53 |
47 // WeakPtrFactory bound to the UI thread. | 54 // WeakPtrFactory bound to the UI thread. |
48 // Note: This should remain the last member so it'll be destroyed and | 55 // Note: This should remain the last member so it'll be destroyed and |
49 // invalidate its weak pointers before any other members are destroyed. | 56 // invalidate its weak pointers before any other members are destroyed. |
50 base::WeakPtrFactory<FileWriteHelper> weak_ptr_factory_; | 57 base::WeakPtrFactory<FileWriteHelper> weak_ptr_factory_; |
51 | 58 |
52 DISALLOW_COPY_AND_ASSIGN(FileWriteHelper); | 59 DISALLOW_COPY_AND_ASSIGN(FileWriteHelper); |
53 }; | 60 }; |
54 | 61 |
55 } // namespace drive | 62 } // namespace drive |
56 | 63 |
57 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_WRITE_HELPER_H_ | 64 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_WRITE_HELPER_H_ |
OLD | NEW |