stream.h revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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/byte_stream.h" 12#include "content/common/content_export.h" 13#include "url/gurl.h" 14 15namespace net { 16class IOBuffer; 17} 18 19namespace content { 20 21class StreamHandle; 22class StreamHandleImpl; 23class StreamReadObserver; 24class StreamRegistry; 25class StreamWriteObserver; 26 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. 33class CONTENT_EXPORT Stream : public base::RefCountedThreadSafe<Stream> { 34 public: 35 enum StreamState { 36 STREAM_HAS_DATA, 37 STREAM_COMPLETE, 38 STREAM_EMPTY, 39 }; 40 41 // Creates a stream useable from the |security_origin|. 42 Stream(StreamRegistry* registry, 43 StreamWriteObserver* write_observer, 44 const GURL& security_origin, 45 const GURL& url); 46 47 // Sets the reader of this stream. Returns true on success, or false if there 48 // is already a reader. 49 bool SetReadObserver(StreamReadObserver* observer); 50 51 // Removes the read observer. |observer| must be the current observer. 52 void RemoveReadObserver(StreamReadObserver* observer); 53 54 // Removes the write observer. |observer| must be the current observer. 55 void RemoveWriteObserver(StreamWriteObserver* observer); 56 57 // Adds the data in |buffer| to the stream. Takes ownership of |buffer|. 58 void AddData(scoped_refptr<net::IOBuffer> buffer, size_t size); 59 60 // Notifies this stream that it will not be receiving any more data. 61 void Finalize(); 62 63 // Reads a maximum of |buf_size| from the stream into |buf|. Sets 64 // |*bytes_read| to the number of bytes actually read. 65 // Returns STREAM_HAS_DATA if data was read, STREAM_EMPTY if no data was read, 66 // and STREAM_COMPLETE if the stream is finalized and all data has been read. 67 StreamState ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read); 68 69 scoped_ptr<StreamHandle> CreateHandle(const GURL& original_url, 70 const std::string& mime_type); 71 void CloseHandle(); 72 73 // Indicates whether there is space in the buffer to add more data. 74 bool can_add_data() const { return can_add_data_; } 75 76 const GURL& url() const { return url_; } 77 78 const GURL& security_origin() const { return security_origin_; } 79 80 private: 81 friend class base::RefCountedThreadSafe<Stream>; 82 83 virtual ~Stream(); 84 85 void OnSpaceAvailable(); 86 void OnDataAvailable(); 87 88 size_t data_bytes_read_; 89 bool can_add_data_; 90 91 GURL security_origin_; 92 GURL url_; 93 94 scoped_refptr<net::IOBuffer> data_; 95 size_t data_length_; 96 97 scoped_ptr<ByteStreamWriter> writer_; 98 scoped_ptr<ByteStreamReader> reader_; 99 100 StreamRegistry* registry_; 101 StreamReadObserver* read_observer_; 102 StreamWriteObserver* write_observer_; 103 104 StreamHandleImpl* stream_handle_; 105 106 base::WeakPtrFactory<Stream> weak_ptr_factory_; 107 DISALLOW_COPY_AND_ASSIGN(Stream); 108}; 109 110} // namespace content 111 112#endif // CONTENT_BROWSER_STREAMS_STREAM_H_ 113