Only sync parent directory once after a leveldb file rename.
[chromium-blink-merge.git] / webkit / fileapi / local_file_stream_writer.h
blobb12c8ed259731b61bcc3c1162e25b7965f9cedcb
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.
5 #ifndef WEBKIT_FILEAPI_LOCAL_FILE_STREAM_WRITER_H_
6 #define WEBKIT_FILEAPI_LOCAL_FILE_STREAM_WRITER_H_
8 #include <utility>
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"
19 namespace net {
20 class FileStream;
23 namespace fileapi {
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();
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;
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);
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);
57 // Passed as the |main_operation| of InitiateOpen() function.
58 void ReadyToWrite(net::IOBuffer* buf, int buf_len,
59 const net::CompletionCallback& callback);
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);
66 // Flushes asynchronously to the file.
67 int InitiateFlush(const net::CompletionCallback& callback);
68 void DidFlush(const net::CompletionCallback& callback, int result);
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();
74 // Initialization parameters.
75 const base::FilePath file_path_;
76 const int64 initial_offset_;
78 // Current states of the operation.
79 bool has_pending_operation_;
80 scoped_ptr<net::FileStream> stream_impl_;
81 net::CompletionCallback cancel_callback_;
83 base::WeakPtrFactory<LocalFileStreamWriter> weak_factory_;
84 DISALLOW_COPY_AND_ASSIGN(LocalFileStreamWriter);
87 } // namespace fileapi
89 #endif // WEBKIT_FILEAPI_LOCAL_FILE_STREAM_WRITER_H_