| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_LOADER_STREAM_WRITER_H_ | 5 #ifndef CONTENT_BROWSER_LOADER_MOJO_STREAM_WRITER_H_ |
| 6 #define CONTENT_BROWSER_LOADER_STREAM_WRITER_H_ | 6 #define CONTENT_BROWSER_LOADER_MOJO_STREAM_WRITER_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "content/browser/streams/stream_write_observer.h" | 10 #include "mojo/message_pump/handle_watcher.h" |
| 11 #include "mojo/public/cpp/system/data_pipe.h" |
| 11 | 12 |
| 12 class GURL; | 13 class GURL; |
| 13 | 14 |
| 14 namespace net { | 15 namespace net { |
| 15 class IOBuffer; | 16 class IOBuffer; |
| 16 } | 17 } |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 | 20 |
| 20 class ResourceController; | 21 class ResourceController; |
| 21 class Stream; | |
| 22 class StreamRegistry; | |
| 23 | 22 |
| 24 // StreamWriter is a helper class for ResourceHandlers which route their output | 23 // MojoStreamWriter is a helper class for ResourceHandlers which route their out
put |
| 25 // into a Stream. It manages an internal buffer and handles back-pressure from | 24 // into a Stream. It manages an internal buffer and handles back-pressure from |
| 26 // the Stream's reader. | 25 // the Stream's reader. |
| 27 class StreamWriter : public StreamWriteObserver { | 26 class MojoStreamWriter { |
| 28 public: | 27 public: |
| 29 // Creates a new StreamWriter without an initialized Stream or controller. The | 28 // Creates a new MojoStreamWriter without an initialized Stream or controller.
The |
| 30 // controller must be set before the writer is used. | 29 // controller must be set before the writer is used. |
| 31 StreamWriter(); | 30 MojoStreamWriter(); |
| 32 ~StreamWriter() override; | 31 ~MojoStreamWriter(); |
| 33 | 32 |
| 34 Stream* stream() { return stream_.get(); } | 33 bool has_stream() { return data_producer_handle_.is_valid(); } |
| 35 | 34 |
| 36 void set_controller(ResourceController* controller) { | 35 void set_controller(ResourceController* controller) { |
| 37 controller_ = controller; | 36 controller_ = controller; |
| 38 } | 37 } |
| 39 | 38 |
| 40 // When immediate mode is enabled, the |stream_| is flushed every time new | 39 // When immediate mode is enabled, the |stream_| is flushed every time new |
| 41 // data is made available by calls to OnReadCompleted. | 40 // data is made available by calls to OnReadCompleted. |
| 42 void set_immediate_mode(bool enabled) { immediate_mode_ = enabled; } | 41 void set_immediate_mode(bool enabled) { immediate_mode_ = enabled; } |
| 43 | 42 |
| 44 // Initializes the writer with a new Stream in |registry|. |origin| will be | 43 // 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 | 44 // 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 | 45 // WebCore::SecurityOrigin in Blink to understand how origin check is done on |
| 47 // resource loading. | 46 // resource loading. |
| 48 void InitializeStream(StreamRegistry* registry, | 47 void InitializeStream(mojo::ScopedDataPipeConsumerHandle* data_consumer_handle
); |
| 49 const GURL& origin); | |
| 50 | 48 |
| 51 // Prepares a buffer to read data from the request. This call will be followed | 49 // 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 | 50 // by either OnReadCompleted (on successful read or EOF) or destruction. The |
| 53 // buffer may not be recycled until OnReadCompleted is called. If |min_size| | 51 // 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. | 52 // is not -1, it is the minimum size of the returned buffer. |
| 55 // | 53 // |
| 56 // OnWillRead may be called before the stream is initialized. This is to | 54 // OnWillRead may be called before the stream is initialized. This is to |
| 57 // support MimeTypeResourceHandler which reads the initial chunk of data | 55 // support MimeTypeResourceHandler which reads the initial chunk of data |
| 58 // early. | 56 // early. |
| 59 void OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 57 void OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 60 int* buf_size, | 58 int* buf_size, |
| 61 int min_size); | 59 int min_size); |
| 62 | 60 |
| 63 // A read was completed, forward the data to the Stream. If |*defer| is set to | 61 // 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 | 62 // true, the implementation must not continue to process the request until |
| 65 // Resume is called on |controller_|. | 63 // Resume is called on |controller_|. |
| 66 // | 64 // |
| 67 // InitializeStream must have been called before calling OnReadCompleted. | 65 // InitializeStream must have been called before calling OnReadCompleted. |
| 68 void OnReadCompleted(int bytes_read, bool* defer); | 66 void OnReadCompleted(int bytes_read, bool* defer); |
| 69 | 67 |
| 70 // Called when there is no more data to read to the stream. | 68 // Called when there is no more data to read to the stream. |
| 71 void Finalize(); | 69 void Finalize(); |
| 72 | 70 |
| 71 static const int kReadBufSize = 32768; |
| 72 |
| 73 private: | 73 private: |
| 74 // StreamWriteObserver implementation. | 74 // TODO(carlosk): maybe there is (or we need to create) an "official" pipe |
| 75 void OnSpaceAvailable(Stream* stream) override; | 75 // observer API. |
| 76 void OnClose(Stream* stream) override; | 76 void OnSpaceAvailable(MojoResult result); |
| 77 // void OnClose(Stream* stream) override; |
| 77 | 78 |
| 78 ResourceController* controller_; | 79 ResourceController* controller_; |
| 79 scoped_refptr<Stream> stream_; | 80 // TODO(carlosk): do we need this with Mojo? |
| 80 scoped_refptr<net::IOBuffer> read_buffer_; | |
| 81 bool immediate_mode_; | 81 bool immediate_mode_; |
| 82 | 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(StreamWriter); | 83 // MIGHT NEED TO MOVE THIS ELSEWHERE AND STILL OFFER THE ResourceController IN
TERFACE. |
| 84 // https://code.google.com/p/chromium/codesearch#chromium/src/content/public/b
rowser/resource_controller.h |
| 85 // Use as model: https://code.google.com/p/chromium/codesearch#chromium/src/mo
jo/services/tracing/trace_data_sink.cc |
| 86 mojo::ScopedDataPipeProducerHandle data_producer_handle_; |
| 87 mojo::common::HandleWatcher handle_watcher_; |
| 88 void* buffer_; |
| 89 uint32_t buffer_size_; |
| 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(MojoStreamWriter); |
| 84 }; | 92 }; |
| 85 | 93 |
| 86 } // namespace content | 94 } // namespace content |
| 87 | 95 |
| 88 #endif // CONTENT_BROWSER_LOADER_STREAM_WRITER_H_ | 96 #endif // CONTENT_BROWSER_LOADER_MOJO_STREAM_WRITER_H_ |
| OLD | NEW |