diff options
author | zork@chromium.org <zork@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-11 11:23:25 +0000 |
---|---|---|
committer | zork@chromium.org <zork@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-11 11:23:25 +0000 |
commit | e19a0936b577c524ee26d506c97bd5d48521cce5 (patch) | |
tree | 7c2528effbe49016f6291dfc092646fe05f41b11 /content/browser/streams | |
parent | eb0e52e386277785069e84e5ab89077e43a011a6 (diff) | |
download | chromium_src-e19a0936b577c524ee26d506c97bd5d48521cce5.zip chromium_src-e19a0936b577c524ee26d506c97bd5d48521cce5.tar.gz chromium_src-e19a0936b577c524ee26d506c97bd5d48521cce5.tar.bz2 |
Revert 187230
> Implement the Stream registry in content
>
> This is the first part of the content side of the Streams api.
> See:
> https://bugs.webkit.org/show_bug.cgi?id=110194
> https://dvcs.w3.org/hg/streams-api/raw-file/tip/Overview.htm
>
> BUG=171585
>
>
> Review URL: https://chromiumcodereview.appspot.com/12335087
TBR=zork@chromium.org
Review URL: https://codereview.chromium.org/12611018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@187274 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser/streams')
-rw-r--r-- | content/browser/streams/stream.cc | 113 | ||||
-rw-r--r-- | content/browser/streams/stream.h | 99 | ||||
-rw-r--r-- | content/browser/streams/stream_context.cc | 44 | ||||
-rw-r--r-- | content/browser/streams/stream_context.h | 49 | ||||
-rw-r--r-- | content/browser/streams/stream_read_observer.h | 24 | ||||
-rw-r--r-- | content/browser/streams/stream_registry.cc | 48 | ||||
-rw-r--r-- | content/browser/streams/stream_registry.h | 51 | ||||
-rw-r--r-- | content/browser/streams/stream_unittest.cc | 207 | ||||
-rw-r--r-- | content/browser/streams/stream_write_observer.h | 24 |
9 files changed, 0 insertions, 659 deletions
diff --git a/content/browser/streams/stream.cc b/content/browser/streams/stream.cc deleted file mode 100644 index 0efec13..0000000 --- a/content/browser/streams/stream.cc +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "content/browser/streams/stream.h" - -#include "base/bind.h" -#include "base/message_loop_proxy.h" -#include "content/browser/streams/stream_read_observer.h" -#include "content/browser/streams/stream_registry.h" -#include "content/browser/streams/stream_write_observer.h" -#include "net/base/io_buffer.h" - -namespace { -// Start throttling the connection at about 1MB. -const size_t kDeferSizeThreshold = 40 * 32768; -} - -namespace content { - -Stream::Stream(StreamRegistry* registry, - StreamWriteObserver* write_observer, - const GURL& security_origin, - const GURL& url) - : bytes_read_(0), - can_add_data_(true), - security_origin_(security_origin), - url_(url), - data_length_(0), - registry_(registry), - read_observer_(NULL), - write_observer_(write_observer) { - CreateByteStream(base::MessageLoopProxy::current(), - base::MessageLoopProxy::current(), - kDeferSizeThreshold, - &writer_, - &reader_); - - // Setup callback for writing. - writer_->RegisterCallback(base::Bind(&Stream::OnSpaceAvailable, this)); - reader_->RegisterCallback(base::Bind(&Stream::OnDataAvailable, this)); - - registry_->RegisterStream(this); -} - -Stream::~Stream() { -} - -bool Stream::SetReadObserver(StreamReadObserver* observer) { - if (read_observer_) - return false; - read_observer_ = observer; - return true; -} - -void Stream::RemoveReadObserver(StreamReadObserver* observer) { - DCHECK(observer == read_observer_); - read_observer_ = NULL; -} - -void Stream::AddData(scoped_refptr<net::IOBuffer> buffer, size_t size) { - can_add_data_ = writer_->Write(buffer, size); -} - -void Stream::Finalize() { - writer_->Close(DOWNLOAD_INTERRUPT_REASON_NONE); - writer_.reset(NULL); - - OnDataAvailable(); -} - -Stream::StreamState Stream::ReadRawData(net::IOBuffer* buf, - int buf_size, - int* bytes_read) { - if (!data_) { - data_length_ = 0; - bytes_read_ = 0; - ByteStreamReader::StreamState state = reader_->Read(&data_, &data_length_); - switch (state) { - case ByteStreamReader::STREAM_HAS_DATA: - break; - case ByteStreamReader::STREAM_COMPLETE: - registry_->UnregisterStream(url()); - return STREAM_COMPLETE; - case ByteStreamReader::STREAM_EMPTY: - return STREAM_EMPTY; - } - } - - size_t remaining_bytes = data_length_ - bytes_read_; - size_t to_read = - static_cast<size_t>(buf_size) < remaining_bytes ? - buf_size : remaining_bytes; - memcpy(buf->data(), data_->data() + bytes_read_, to_read); - bytes_read_ += to_read; - if (bytes_read_ >= data_length_) - data_ = NULL; - - *bytes_read = to_read; - return STREAM_HAS_DATA; -} - -void Stream::OnSpaceAvailable() { - can_add_data_ = true; - write_observer_->OnSpaceAvailable(this); -} - -void Stream::OnDataAvailable() { - read_observer_->OnDataAvailable(this); -} - -} // namespace content - diff --git a/content/browser/streams/stream.h b/content/browser/streams/stream.h deleted file mode 100644 index 567b10b..0000000 --- a/content/browser/streams/stream.h +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CONTENT_BROWSER_STREAMS_STREAM_H_ -#define CONTENT_BROWSER_STREAMS_STREAM_H_ - -#include "base/basictypes.h" -#include "base/memory/ref_counted.h" -#include "content/browser/download/byte_stream.h" -#include "content/common/content_export.h" -#include "googleurl/src/gurl.h" - -namespace net { -class IOBuffer; -} - -namespace content { - -class StreamReadObserver; -class StreamRegistry; -class StreamWriteObserver; - -// A stream that sends data from an arbitrary source to an internal URL -// that can be read by an internal consumer. It will continue to pull from the -// original URL as long as there is data available. It can be read from -// multiple clients, but only one can be reading at a time. This allows a -// reader to consume part of the stream, then pass it along to another client -// to continue processing the stream. -class CONTENT_EXPORT Stream : public base::RefCountedThreadSafe<Stream> { - public: - enum StreamState { - STREAM_HAS_DATA, - STREAM_COMPLETE, - STREAM_EMPTY, - }; - - // Creates a stream useable from the |security_origin|. - Stream(StreamRegistry* registry, - StreamWriteObserver* write_observer, - const GURL& security_origin, - const GURL& url); - - // Sets the reader of this stream. Returns true on success, or false if there - // is already a reader. - bool SetReadObserver(StreamReadObserver* observer); - - // Removes the read observer. |observer| must be the current observer. - void RemoveReadObserver(StreamReadObserver* observer); - - // Adds the data in |buffer| to the stream. Takes ownership of |buffer|. - void AddData(scoped_refptr<net::IOBuffer> buffer, size_t size); - - // Notifies this stream that it will not be receiving any more data. - void Finalize(); - - // Reads a maximum of |buf_size| from the stream into |buf|. Sets - // |*bytes_read| to the number of bytes actually read. - // Returns STREAM_HAS_DATA if data was read, STREAM_EMPTY if no data was read, - // and STREAM_COMPLETE if the stream is finalized and all data has been read. - StreamState ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read); - - // Indicates whether there is space in the buffer to add more data. - bool can_add_data() const { return can_add_data_; } - - const GURL& url() const { return url_; } - - const GURL& security_origin() const { return security_origin_; } - - private: - friend class base::RefCountedThreadSafe<Stream>; - - virtual ~Stream(); - - void OnSpaceAvailable(); - void OnDataAvailable(); - - size_t bytes_read_; - bool can_add_data_; - - GURL security_origin_; - GURL url_; - - scoped_refptr<net::IOBuffer> data_; - size_t data_length_; - - scoped_ptr<ByteStreamWriter> writer_; - scoped_ptr<ByteStreamReader> reader_; - - StreamRegistry* registry_; - StreamReadObserver* read_observer_; - StreamWriteObserver* write_observer_; - - DISALLOW_COPY_AND_ASSIGN(Stream); -}; - -} // namespace content - -#endif // CONTENT_BROWSER_STREAMS_STREAM_H_ diff --git a/content/browser/streams/stream_context.cc b/content/browser/streams/stream_context.cc deleted file mode 100644 index ca77df1..0000000 --- a/content/browser/streams/stream_context.cc +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "content/browser/streams/stream_context.h" - -#include "base/bind.h" -#include "content/browser/streams/stream_registry.h" -#include "content/public/browser/browser_context.h" - -using base::UserDataAdapter; - -namespace { -const char* kStreamContextKeyName = "content_stream_context"; -} - -namespace content { - -StreamContext::StreamContext() {} - -StreamContext* StreamContext::GetFor(BrowserContext* context) { - if (!context->GetUserData(kStreamContextKeyName)) { - scoped_refptr<StreamContext> stream = new StreamContext(); - context->SetUserData(kStreamContextKeyName, - new UserDataAdapter<StreamContext>(stream)); - // Check first to avoid memory leak in unittests. - if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - base::Bind(&StreamContext::InitializeOnIOThread, stream)); - } - } - - return UserDataAdapter<StreamContext>::Get(context, kStreamContextKeyName); -} - -void StreamContext::InitializeOnIOThread() { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); - registry_.reset(new StreamRegistry()); -} - -StreamContext::~StreamContext() {} - -} // namespace content diff --git a/content/browser/streams/stream_context.h b/content/browser/streams/stream_context.h deleted file mode 100644 index b4c6d30..0000000 --- a/content/browser/streams/stream_context.h +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CONTENT_BROWSER_STREAMS_STREAM_CONTEXT_H_ -#define CONTENT_BROWSER_STREAMS_STREAM_CONTEXT_H_ - -#include "base/memory/ref_counted.h" -#include "base/memory/scoped_ptr.h" -#include "base/sequenced_task_runner_helpers.h" -#include "content/public/browser/browser_thread.h" - -namespace content { -class BrowserContext; -class StreamRegistry; - -// A context class that keeps track of StreamRegistry used by the chrome. -// There is an instance associated with each BrowserContext. There could be -// multiple URLRequestContexts in the same browser context that refers to the -// same instance. -// -// All methods, except the ctor, are expected to be called on -// the IO thread (unless specifically called out in doc comments). -class StreamContext - : public base::RefCountedThreadSafe<StreamContext, - BrowserThread::DeleteOnIOThread> { - public: - StreamContext(); - - static StreamContext* GetFor(BrowserContext* browser_context); - - void InitializeOnIOThread(); - - StreamRegistry* registry() const { return registry_.get(); } - - protected: - virtual ~StreamContext(); - - private: - friend class base::DeleteHelper<StreamContext>; - friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; - - scoped_ptr<StreamRegistry> registry_; -}; - -} // namespace content - -#endif // CONTENT_BROWSER_STREAMS_STREAM_CONTEXT_H_ - diff --git a/content/browser/streams/stream_read_observer.h b/content/browser/streams/stream_read_observer.h deleted file mode 100644 index a2b1bdc..0000000 --- a/content/browser/streams/stream_read_observer.h +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CONTENT_BROWSER_STREAMS_STREAM_READ_OBSERVER_H_ -#define CONTENT_BROWSER_STREAMS_STREAM_READ_OBSERVER_H_ - -namespace content { - -class Stream; - -class StreamReadObserver { - public: - // Sent when there is data available to be read from the stream. - virtual void OnDataAvailable(Stream* stream) = 0; - - protected: - virtual ~StreamReadObserver() {} -}; - -} // namespace content - -#endif // CONTENT_BROWSER_STREAMS_STREAM_READ_OBSERVER_H_ - diff --git a/content/browser/streams/stream_registry.cc b/content/browser/streams/stream_registry.cc deleted file mode 100644 index 7159831..0000000 --- a/content/browser/streams/stream_registry.cc +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "content/browser/streams/stream_registry.h" - -#include "content/browser/streams/stream.h" - -namespace content { - -StreamRegistry::StreamRegistry() { -} - -StreamRegistry::~StreamRegistry() { -} - -void StreamRegistry::RegisterStream(scoped_refptr<Stream> stream) { - DCHECK(CalledOnValidThread()); - DCHECK(stream); - DCHECK(!stream->url().is_empty()); - streams_[stream->url()] = stream; -} - -scoped_refptr<Stream> StreamRegistry::GetStream(const GURL& url) { - DCHECK(CalledOnValidThread()); - StreamMap::const_iterator stream = streams_.find(url); - if (stream != streams_.end()) - return stream->second; - - return NULL; -} - -bool StreamRegistry::CloneStream(const GURL& url, const GURL& src_url) { - DCHECK(CalledOnValidThread()); - scoped_refptr<Stream> stream(GetStream(src_url)); - if (stream) { - streams_[url] = stream; - return true; - } - return false; -} - -void StreamRegistry::UnregisterStream(const GURL& url) { - DCHECK(CalledOnValidThread()); - streams_.erase(url); -} - -} // namespace content diff --git a/content/browser/streams/stream_registry.h b/content/browser/streams/stream_registry.h deleted file mode 100644 index eaab7ef..0000000 --- a/content/browser/streams/stream_registry.h +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CONTENT_BROWSER_STREAMS_STREAM_REGISTRY_H_ -#define CONTENT_BROWSER_STREAMS_STREAM_REGISTRY_H_ - -#include <map> - -#include "base/basictypes.h" -#include "base/memory/ref_counted.h" -#include "base/threading/non_thread_safe.h" -#include "content/common/content_export.h" -#include "googleurl/src/gurl.h" - -namespace content { - -class Stream; - -// Maintains a mapping of blob: URLs to active streams. -class CONTENT_EXPORT StreamRegistry : public base::NonThreadSafe { - public: - StreamRegistry(); - virtual ~StreamRegistry(); - - // Registers a stream, and sets its URL. - void RegisterStream(scoped_refptr<Stream> stream); - - // Clones a stream. Returns true on success, or false if |src_url| doesn't - // exist. - bool CloneStream(const GURL& url, const GURL& src_url); - - void UnregisterStream(const GURL& url); - - // Gets the stream associated with |url|. Returns NULL if there is no such - // stream. - scoped_refptr<Stream> GetStream(const GURL& url); - - private: - typedef std::map<GURL, scoped_refptr<Stream> > StreamMap; - - StreamMap streams_; - - DISALLOW_COPY_AND_ASSIGN(StreamRegistry); -}; - -} // namespace content - -#endif // CONTENT_BROWSER_STREAMS_STREAM_REGISTRY_H_ - - diff --git a/content/browser/streams/stream_unittest.cc b/content/browser/streams/stream_unittest.cc deleted file mode 100644 index 46a9eee..0000000 --- a/content/browser/streams/stream_unittest.cc +++ /dev/null @@ -1,207 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/message_loop.h" -#include "base/test/test_simple_task_runner.h" -#include "content/browser/streams/stream.h" -#include "content/browser/streams/stream_read_observer.h" -#include "content/browser/streams/stream_registry.h" -#include "content/browser/streams/stream_write_observer.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace content { - -class StreamTest : public testing::Test { - public: - StreamTest() {} - - virtual void SetUp() OVERRIDE { - registry_.reset(new StreamRegistry()); - } - - // Create a new IO buffer of the given |buffer_size| and fill it with random - // data. - scoped_refptr<net::IOBuffer> NewIOBuffer(size_t buffer_size) { - scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(buffer_size)); - char *bufferp = buffer->data(); - for (size_t i = 0; i < buffer_size; i++) - bufferp[i] = (i + producing_seed_key_) % (1 << sizeof(char)); - ++producing_seed_key_; - return buffer; - } - - protected: - MessageLoop message_loop_; - scoped_ptr<StreamRegistry> registry_; - - private: - int producing_seed_key_; -}; - -class TestStreamReader : public StreamReadObserver { - public: - TestStreamReader() : buffer_(new net::GrowableIOBuffer()) { - } - virtual ~TestStreamReader() {} - - void Read(Stream* stream) { - const size_t kBufferSize = 32768; - scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); - - int bytes_read = 0; - while (stream->ReadRawData(buffer, kBufferSize, &bytes_read) == - Stream::STREAM_HAS_DATA) { - size_t old_capacity = buffer_->capacity(); - buffer_->SetCapacity(old_capacity + bytes_read); - memcpy(buffer_->data() + old_capacity, buffer->data(), bytes_read); - } - } - - virtual void OnDataAvailable(Stream* stream) OVERRIDE { - Read(stream); - } - - scoped_refptr<net::GrowableIOBuffer> buffer() { return buffer_; } - - private: - scoped_refptr<net::GrowableIOBuffer> buffer_; -}; - -class TestStreamWriter : public StreamWriteObserver { - public: - TestStreamWriter() {} - virtual ~TestStreamWriter() {} - - void Write(Stream* stream, - scoped_refptr<net::IOBuffer> buffer, - size_t buffer_size) { - stream->AddData(buffer, buffer_size); - } - - virtual void OnSpaceAvailable(Stream* stream) OVERRIDE { - } -}; - -TEST_F(StreamTest, SetReadObserver) { - TestStreamReader reader; - TestStreamWriter writer; - - GURL url("blob://stream"); - scoped_refptr<Stream> stream( - new Stream(registry_.get(), &writer, GURL(), url)); - EXPECT_TRUE(stream->SetReadObserver(&reader)); -} - -TEST_F(StreamTest, SetReadObserver_SecondFails) { - TestStreamReader reader1; - TestStreamReader reader2; - TestStreamWriter writer; - - GURL url("blob://stream"); - scoped_refptr<Stream> stream( - new Stream(registry_.get(), &writer, GURL(), url)); - EXPECT_TRUE(stream->SetReadObserver(&reader1)); - EXPECT_FALSE(stream->SetReadObserver(&reader2)); -} - -TEST_F(StreamTest, SetReadObserver_TwoReaders) { - TestStreamReader reader1; - TestStreamReader reader2; - TestStreamWriter writer; - - GURL url("blob://stream"); - scoped_refptr<Stream> stream( - new Stream(registry_.get(), &writer, GURL(), url)); - EXPECT_TRUE(stream->SetReadObserver(&reader1)); - - // Once the first read observer is removed, a new one can be added. - stream->RemoveReadObserver(&reader1); - EXPECT_TRUE(stream->SetReadObserver(&reader2)); -} - -TEST_F(StreamTest, Stream) { - TestStreamReader reader; - TestStreamWriter writer; - - GURL url("blob://stream"); - scoped_refptr<Stream> stream( - new Stream(registry_.get(), &writer, GURL(), url)); - EXPECT_TRUE(stream->SetReadObserver(&reader)); - - int buffer_size = 1000000; - scoped_refptr<net::IOBuffer> buffer(NewIOBuffer(buffer_size)); - writer.Write(stream, buffer, buffer_size); - stream->Finalize(); - reader.Read(stream); - MessageLoop::current()->RunUntilIdle(); - - ASSERT_EQ(reader.buffer()->capacity(), buffer_size); - ASSERT_EQ(0, memcmp(buffer->data(), - reader.buffer()->data(), - buffer_size)); -} - -TEST_F(StreamTest, GetStream) { - TestStreamWriter writer; - - GURL url("blob://stream"); - scoped_refptr<Stream> stream1( - new Stream(registry_.get(), &writer, GURL(), url)); - - scoped_refptr<Stream> stream2 = registry_->GetStream(url); - ASSERT_EQ(stream1, stream2); -} - -TEST_F(StreamTest, GetStream_Missing) { - TestStreamWriter writer; - - GURL url1("blob://stream"); - scoped_refptr<Stream> stream1( - new Stream(registry_.get(), &writer, GURL(), url1)); - - GURL url2("blob://stream2"); - scoped_refptr<Stream> stream2 = registry_->GetStream(url2); - ASSERT_FALSE(stream2); -} - -TEST_F(StreamTest, CloneStream) { - TestStreamWriter writer; - - GURL url1("blob://stream"); - scoped_refptr<Stream> stream1( - new Stream(registry_.get(), &writer, GURL(), url1)); - - GURL url2("blob://stream2"); - ASSERT_TRUE(registry_->CloneStream(url2, url1)); - scoped_refptr<Stream> stream2 = registry_->GetStream(url2); - ASSERT_EQ(stream1, stream2); -} - -TEST_F(StreamTest, CloneStream_Missing) { - TestStreamWriter writer; - - GURL url1("blob://stream"); - scoped_refptr<Stream> stream1( - new Stream(registry_.get(), &writer, GURL(), url1)); - - GURL url2("blob://stream2"); - GURL url3("blob://stream3"); - ASSERT_FALSE(registry_->CloneStream(url2, url3)); - scoped_refptr<Stream> stream2 = registry_->GetStream(url2); - ASSERT_FALSE(stream2); -} - -TEST_F(StreamTest, UnregisterStream) { - TestStreamWriter writer; - - GURL url("blob://stream"); - scoped_refptr<Stream> stream1( - new Stream(registry_.get(), &writer, GURL(), url)); - - registry_->UnregisterStream(url); - scoped_refptr<Stream> stream2 = registry_->GetStream(url); - ASSERT_FALSE(stream2); -} - -} // namespace content diff --git a/content/browser/streams/stream_write_observer.h b/content/browser/streams/stream_write_observer.h deleted file mode 100644 index 31d84cd..0000000 --- a/content/browser/streams/stream_write_observer.h +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CONTENT_BROWSER_STREAMS_STREAM_WRITE_OBSERVER_H_ -#define CONTENT_BROWSER_STREAMS_STREAM_WRITE_OBSERVER_H_ - -namespace content { - -class Stream; - -class StreamWriteObserver { - public: - // Sent when space becomes available in the stream, and the source should - // resume writing. - virtual void OnSpaceAvailable(Stream* stream) = 0; - - protected: - virtual ~StreamWriteObserver() {} -}; - -} // namespace content - -#endif // CONTENT_BROWSER_STREAMS_STREAM_WRITE_OBSERVER_H_ |