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