Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / streams / stream.h
blob27788b305a2f2d37d85d27de4f09d2228e1f5cdf
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.
5 #ifndef CONTENT_BROWSER_STREAMS_STREAM_H_
6 #define CONTENT_BROWSER_STREAMS_STREAM_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/browser/byte_stream.h"
12 #include "content/common/content_export.h"
13 #include "url/gurl.h"
15 namespace net {
16 class IOBuffer;
19 namespace content {
21 class StreamHandle;
22 class StreamHandleImpl;
23 class StreamReadObserver;
24 class StreamRegistry;
25 class StreamWriteObserver;
27 // A stream that sends data from an arbitrary source to an internal URL
28 // that can be read by an internal consumer. It will continue to pull from the
29 // original URL as long as there is data available. It can be read from
30 // multiple clients, but only one can be reading at a time. This allows a
31 // reader to consume part of the stream, then pass it along to another client
32 // to continue processing the stream.
33 class CONTENT_EXPORT Stream : public base::RefCountedThreadSafe<Stream> {
34 public:
35 enum StreamState {
36 STREAM_HAS_DATA,
37 STREAM_COMPLETE,
38 STREAM_EMPTY,
39 STREAM_ABORTED,
42 // Creates a stream.
44 // Security origin of Streams is checked in Blink (See BlobRegistry,
45 // BlobURL and SecurityOrigin to understand how it works). There's no security
46 // origin check in Chromium side for now.
47 Stream(StreamRegistry* registry,
48 StreamWriteObserver* write_observer,
49 const GURL& url);
51 // Sets the reader of this stream. Returns true on success, or false if there
52 // is already a reader.
53 bool SetReadObserver(StreamReadObserver* observer);
55 // Removes the read observer. |observer| must be the current observer.
56 void RemoveReadObserver(StreamReadObserver* observer);
58 // Removes the write observer. |observer| must be the current observer.
59 void RemoveWriteObserver(StreamWriteObserver* observer);
61 // Stops accepting new data, clears all buffer, unregisters this stream from
62 // |registry_| and make coming ReadRawData() calls return STREAM_ABORTED.
63 void Abort();
65 // Adds the data in |buffer| to the stream. Takes ownership of |buffer|.
66 void AddData(scoped_refptr<net::IOBuffer> buffer, size_t size);
67 // Adds data of |size| at |data| to the stream. This method creates a copy
68 // of the data, and then passes it to |writer_|.
69 void AddData(const char* data, size_t size);
71 // Flushes contents buffered in the stream to the corresponding reader.
72 void Flush();
74 // Notifies this stream that it will not be receiving any more data.
75 void Finalize();
77 // Reads a maximum of |buf_size| from the stream into |buf|. Sets
78 // |*bytes_read| to the number of bytes actually read.
79 // Returns STREAM_HAS_DATA if data was read, STREAM_EMPTY if no data was read,
80 // and STREAM_COMPLETE if the stream is finalized and all data has been read.
81 StreamState ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read);
83 scoped_ptr<StreamHandle> CreateHandle();
84 void CloseHandle();
86 // Indicates whether there is space in the buffer to add more data.
87 bool can_add_data() const { return can_add_data_; }
89 const GURL& url() const { return url_; }
91 // For StreamRegistry to remember the last memory usage reported to it.
92 size_t last_total_buffered_bytes() const {
93 return last_total_buffered_bytes_;
96 private:
97 friend class base::RefCountedThreadSafe<Stream>;
99 virtual ~Stream();
101 void OnSpaceAvailable();
102 void OnDataAvailable();
104 // Clears |data_| and related variables.
105 void ClearBuffer();
107 bool can_add_data_;
109 GURL url_;
111 // Buffer for storing data read from |reader_| but not yet read out from this
112 // Stream by ReadRawData() method.
113 scoped_refptr<net::IOBuffer> data_;
114 // Number of bytes read from |reader_| into |data_| including bytes already
115 // read out.
116 size_t data_length_;
117 // Number of bytes in |data_| that are already read out.
118 size_t data_bytes_read_;
120 // Last value returned by writer_->TotalBufferedBytes() in AddData(). Stored
121 // in order to check memory usage.
122 size_t last_total_buffered_bytes_;
124 scoped_ptr<ByteStreamWriter> writer_;
125 scoped_ptr<ByteStreamReader> reader_;
127 StreamRegistry* registry_;
128 StreamReadObserver* read_observer_;
129 StreamWriteObserver* write_observer_;
131 StreamHandleImpl* stream_handle_;
133 base::WeakPtrFactory<Stream> weak_ptr_factory_;
134 DISALLOW_COPY_AND_ASSIGN(Stream);
137 } // namespace content
139 #endif // CONTENT_BROWSER_STREAMS_STREAM_H_