OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef WEBKIT_FILEAPI_LOCAL_FILE_STREAM_WRITER_H_ | |
6 #define WEBKIT_FILEAPI_LOCAL_FILE_STREAM_WRITER_H_ | |
7 | |
8 #include <utility> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/compiler_specific.h" | |
12 #include "base/files/file_path.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/platform_file.h" | |
16 #include "webkit/fileapi/file_stream_writer.h" | |
17 #include "webkit/storage/webkit_storage_export.h" | |
18 | |
19 namespace net { | |
20 class FileStream; | |
21 } | |
22 | |
23 namespace fileapi { | |
24 | |
25 // This class is a thin wrapper around net::FileStream for writing local files. | |
26 class WEBKIT_STORAGE_EXPORT_PRIVATE LocalFileStreamWriter | |
27 : public FileStreamWriter { | |
28 public: | |
29 // Create a writer for the existing file in the path |file_path| starting from | |
30 // |initial_offset|. | |
31 LocalFileStreamWriter(const base::FilePath& file_path, int64 initial_offset); | |
32 virtual ~LocalFileStreamWriter(); | |
33 | |
34 // FileStreamWriter overrides. | |
35 virtual int Write(net::IOBuffer* buf, int buf_len, | |
36 const net::CompletionCallback& callback) OVERRIDE; | |
37 virtual int Cancel(const net::CompletionCallback& callback) OVERRIDE; | |
38 virtual int Flush(const net::CompletionCallback& callback) OVERRIDE; | |
39 | |
40 private: | |
41 // Opens |file_path_| and if it succeeds, proceeds to InitiateSeek(). | |
42 // If failed, the error code is returned by calling |error_callback|. | |
43 int InitiateOpen(const net::CompletionCallback& error_callback, | |
44 const base::Closure& main_operation); | |
45 void DidOpen(const net::CompletionCallback& error_callback, | |
46 const base::Closure& main_operation, | |
47 int result); | |
48 | |
49 // Seeks to |initial_offset_| and proceeds to |main_operation| if it succeeds. | |
50 // If failed, the error code is returned by calling |error_callback|. | |
51 void InitiateSeek(const net::CompletionCallback& error_callback, | |
52 const base::Closure& main_operation); | |
53 void DidSeek(const net::CompletionCallback& error_callback, | |
54 const base::Closure& main_operation, | |
55 int64 result); | |
56 | |
57 // Passed as the |main_operation| of InitiateOpen() function. | |
58 void ReadyToWrite(net::IOBuffer* buf, int buf_len, | |
59 const net::CompletionCallback& callback); | |
60 | |
61 // Writes asynchronously to the file. | |
62 int InitiateWrite(net::IOBuffer* buf, int buf_len, | |
63 const net::CompletionCallback& callback); | |
64 void DidWrite(const net::CompletionCallback& callback, int result); | |
65 | |
66 // Flushes asynchronously to the file. | |
67 int InitiateFlush(const net::CompletionCallback& callback); | |
68 void DidFlush(const net::CompletionCallback& callback, int result); | |
69 | |
70 // Stops the in-flight operation and calls |cancel_callback_| if it has been | |
71 // set by Cancel() for the current operation. | |
72 bool CancelIfRequested(); | |
73 | |
74 // Initialization parameters. | |
75 const base::FilePath file_path_; | |
76 const int64 initial_offset_; | |
77 | |
78 // Current states of the operation. | |
79 bool has_pending_operation_; | |
80 scoped_ptr<net::FileStream> stream_impl_; | |
81 net::CompletionCallback cancel_callback_; | |
82 | |
83 base::WeakPtrFactory<LocalFileStreamWriter> weak_factory_; | |
84 DISALLOW_COPY_AND_ASSIGN(LocalFileStreamWriter); | |
85 }; | |
86 | |
87 } // namespace fileapi | |
88 | |
89 #endif // WEBKIT_FILEAPI_LOCAL_FILE_STREAM_WRITER_H_ | |
OLD | NEW |