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