Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / fileapi / buffering_file_stream_writer.h
blob269149c5523f0a4578b6345e704f83feb077df2d
1 // Copyright 2014 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_FILE_SYSTEM_PROVIDER_FILEAPI_BUFFERING_FILE_STREAM_WRITER_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_BUFFERING_FILE_STREAM_WRITER_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 "storage/browser/fileapi/file_stream_writer.h"
14 namespace net {
15 class IOBuffer;
16 } // namespace net
18 namespace chromeos {
19 namespace file_system_provider {
21 // Wraps the file stream writer implementation with an intermediate buffer.
22 // Writes data from the inner file stream writer in chunks of size at least
23 // |intermediate_buffer_length| bytes (or less for the last chunk, or when
24 // Flush() is explicitely called).
26 // The underlying inner file stream writer *must not* return any values
27 // synchronously. Instead, results must be returned by a callback, including
28 // errors. Moreover, partial writes are *not* supported.
29 class BufferingFileStreamWriter : public storage::FileStreamWriter {
30 public:
31 BufferingFileStreamWriter(
32 scoped_ptr<storage::FileStreamWriter> file_stream_writer,
33 int intermediate_buffer_length);
35 ~BufferingFileStreamWriter() override;
37 // storage::FileStreamWriter overrides.
38 int Write(net::IOBuffer* buf,
39 int buf_len,
40 const net::CompletionCallback& callback) override;
41 int Cancel(const net::CompletionCallback& callback) override;
42 int Flush(const net::CompletionCallback& callback) override;
44 private:
45 // Copies |buffer_length| bytes of data from the |buffer| starting at
46 // |buffer_offset| position to the current position of the intermediate
47 // buffer.
48 void CopyToIntermediateBuffer(scoped_refptr<net::IOBuffer> buffer,
49 int buffer_offset,
50 int buffer_length);
52 // Flushes all of the bytes in the intermediate buffer to the inner file
53 // stream writer.
54 void FlushIntermediateBuffer(const net::CompletionCallback& callback);
56 // Called when flushing the intermediate buffer is completed with either
57 // a success or an error.
58 void OnFlushIntermediateBufferCompleted(
59 int length,
60 const net::CompletionCallback& callback,
61 int result);
63 // Called when flushing the intermediate buffer for direct write is completed
64 // with either a success or an error.
65 void OnFlushIntermediateBufferForDirectWriteCompleted(
66 scoped_refptr<net::IOBuffer> buffer,
67 int length,
68 const net::CompletionCallback& callback,
69 int result);
71 // Called when flushing the intermediate buffer for a buffered write is
72 // completed with either a success or an error.
73 void OnFlushIntermediateBufferForBufferedWriteCompleted(
74 scoped_refptr<net::IOBuffer> buffer,
75 int buffered_bytes,
76 int bytes_left,
77 const net::CompletionCallback& callback,
78 int result);
80 // Called when flushing the intermediate buffer for a flush call is completed
81 // with either a success or an error.
82 void OnFlushIntermediateBufferForFlushCompleted(
83 const net::CompletionCallback& callback,
84 int result);
86 scoped_ptr<storage::FileStreamWriter> file_stream_writer_;
87 int intermediate_buffer_length_;
88 scoped_refptr<net::IOBuffer> intermediate_buffer_;
89 int buffered_bytes_;
91 base::WeakPtrFactory<BufferingFileStreamWriter> weak_ptr_factory_;
92 DISALLOW_COPY_AND_ASSIGN(BufferingFileStreamWriter);
95 } // namespace file_system_provider
96 } // namespace chromeos
98 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_BUFFERING_FILE_STREAM_WRITER_H_