OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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. |
| 4 |
| 5 #ifndef CONTENT_BROWSER_STREAMS_STREAM_H_ |
| 6 #define CONTENT_BROWSER_STREAMS_STREAM_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "content/browser/download/byte_stream.h" |
| 11 #include "content/common/content_export.h" |
| 12 #include "googleurl/src/gurl.h" |
| 13 |
| 14 namespace net { |
| 15 class IOBuffer; |
| 16 } |
| 17 |
| 18 namespace content { |
| 19 |
| 20 class StreamReadObserver; |
| 21 class StreamRegistry; |
| 22 class StreamWriteObserver; |
| 23 |
| 24 // A stream that sends data from an arbitrary source to an internal URL |
| 25 // that can be read by an internal consumer. It will continue to pull from the |
| 26 // original URL as long as there is data available. It can be read from |
| 27 // multiple clients, but only one can be reading at a time. This allows a |
| 28 // reader to consume part of the stream, then pass it along to another client |
| 29 // to continue processing the stream. |
| 30 class CONTENT_EXPORT Stream : public base::RefCountedThreadSafe<Stream> { |
| 31 public: |
| 32 enum StreamState { |
| 33 STREAM_HAS_DATA, |
| 34 STREAM_COMPLETE, |
| 35 STREAM_EMPTY, |
| 36 }; |
| 37 |
| 38 // Creates a stream useable from the |security_origin|. |
| 39 Stream(StreamRegistry* registry, |
| 40 StreamWriteObserver* write_observer, |
| 41 const GURL& security_origin, |
| 42 const GURL& url); |
| 43 |
| 44 // Sets the reader of this stream. Returns true on success, or false if there |
| 45 // is already a reader. |
| 46 bool SetReadObserver(StreamReadObserver* observer); |
| 47 |
| 48 // Removes the read observer. |observer| must be the current observer. |
| 49 void RemoveReadObserver(StreamReadObserver* observer); |
| 50 |
| 51 // Adds the data in |buffer| to the stream. Takes ownership of |buffer|. |
| 52 void AddData(scoped_refptr<net::IOBuffer> buffer, size_t size); |
| 53 |
| 54 // Notifies this stream that it will not be receiving any more data. |
| 55 void Finalize(); |
| 56 |
| 57 // Reads a maximum of |buf_size| from the stream into |buf|. Sets |
| 58 // |*bytes_read| to the number of bytes actually read. |
| 59 // Returns STREAM_HAS_DATA if data was read, STREAM_EMPTY if no data was read, |
| 60 // and STREAM_COMPLETE if the stream is finalized and all data has been read. |
| 61 StreamState ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read); |
| 62 |
| 63 // Indicates whether there is space in the buffer to add more data. |
| 64 bool can_add_data() const { return can_add_data_; } |
| 65 |
| 66 const GURL& url() const { return url_; } |
| 67 |
| 68 const GURL& security_origin() const { return security_origin_; } |
| 69 |
| 70 private: |
| 71 friend class base::RefCountedThreadSafe<Stream>; |
| 72 |
| 73 virtual ~Stream(); |
| 74 |
| 75 void OnSpaceAvailable(); |
| 76 void OnDataAvailable(); |
| 77 |
| 78 size_t bytes_read_; |
| 79 bool can_add_data_; |
| 80 |
| 81 GURL security_origin_; |
| 82 GURL url_; |
| 83 |
| 84 scoped_refptr<net::IOBuffer> data_; |
| 85 size_t data_length_; |
| 86 |
| 87 scoped_ptr<ByteStreamWriter> writer_; |
| 88 scoped_ptr<ByteStreamReader> reader_; |
| 89 |
| 90 StreamRegistry* registry_; |
| 91 StreamReadObserver* read_observer_; |
| 92 StreamWriteObserver* write_observer_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(Stream); |
| 95 }; |
| 96 |
| 97 } // namespace content |
| 98 |
| 99 #endif // CONTENT_BROWSER_STREAMS_STREAM_H_ |
OLD | NEW |