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"
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
{
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
,
40 const net::CompletionCallback
& callback
) override
;
41 int Cancel(const net::CompletionCallback
& callback
) override
;
42 int Flush(const net::CompletionCallback
& callback
) override
;
45 // Copies |buffer_length| bytes of data from the |buffer| starting at
46 // |buffer_offset| position to the current position of the intermediate
48 void CopyToIntermediateBuffer(scoped_refptr
<net::IOBuffer
> buffer
,
52 // Flushes all of the bytes in the intermediate buffer to the inner file
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(
60 const net::CompletionCallback
& callback
,
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
,
68 const net::CompletionCallback
& callback
,
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
,
77 const net::CompletionCallback
& callback
,
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
,
86 scoped_ptr
<storage::FileStreamWriter
> file_stream_writer_
;
87 int intermediate_buffer_length_
;
88 scoped_refptr
<net::IOBuffer
> intermediate_buffer_
;
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_