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 CONTENT_BROWSER_LOADER_STREAM_WRITER_H_
6 #define CONTENT_BROWSER_LOADER_STREAM_WRITER_H_
8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h"
10 #include "content/browser/streams/stream_write_observer.h"
20 class ResourceController
;
24 // StreamWriter is a helper class for ResourceHandlers which route their output
25 // into a Stream. It manages an internal buffer and handles back-pressure from
26 // the Stream's reader.
27 class StreamWriter
: public StreamWriteObserver
{
29 // Creates a new StreamWriter without an initialized Stream or controller. The
30 // controller must be set before the writer is used.
32 ~StreamWriter() override
;
34 Stream
* stream() { return stream_
.get(); }
36 void set_controller(ResourceController
* controller
) {
37 controller_
= controller
;
40 // When immediate mode is enabled, the |stream_| is flushed every time new
41 // data is made available by calls to OnReadCompleted.
42 void set_immediate_mode(bool enabled
) { immediate_mode_
= enabled
; }
44 // Initializes the writer with a new Stream in |registry|. |origin| will be
45 // used to construct the URL for the Stream. See WebCore::BlobURL and and
46 // WebCore::SecurityOrigin in Blink to understand how origin check is done on
48 void InitializeStream(StreamRegistry
* registry
,
51 // Prepares a buffer to read data from the request. This call will be followed
52 // by either OnReadCompleted (on successful read or EOF) or destruction. The
53 // buffer may not be recycled until OnReadCompleted is called. If |min_size|
54 // is not -1, it is the minimum size of the returned buffer.
56 // OnWillRead may be called before the stream is initialized. This is to
57 // support MimeTypeResourceHandler which reads the initial chunk of data
59 void OnWillRead(scoped_refptr
<net::IOBuffer
>* buf
,
63 // A read was completed, forward the data to the Stream. If |*defer| is set to
64 // true, the implementation must not continue to process the request until
65 // Resume is called on |controller_|.
67 // InitializeStream must have been called before calling OnReadCompleted.
68 void OnReadCompleted(int bytes_read
, bool* defer
);
70 // Called when there is no more data to read to the stream.
74 // StreamWriteObserver implementation.
75 void OnSpaceAvailable(Stream
* stream
) override
;
76 void OnClose(Stream
* stream
) override
;
78 ResourceController
* controller_
;
79 scoped_refptr
<Stream
> stream_
;
80 scoped_refptr
<net::IOBuffer
> read_buffer_
;
83 DISALLOW_COPY_AND_ASSIGN(StreamWriter
);
86 } // namespace content
88 #endif // CONTENT_BROWSER_LOADER_STREAM_WRITER_H_