diff options
author | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-25 17:45:51 +0000 |
---|---|---|
committer | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-25 17:45:51 +0000 |
commit | 8380c0910bfff5ee0af6b37536cd815023d055ef (patch) | |
tree | e8a4850c7aa9738f1fc3706cdbcd32efd6f77b6e /webkit/glue/media | |
parent | 5de4b03e3bcd97feae59de8828704088f97c5705 (diff) | |
download | chromium_src-8380c0910bfff5ee0af6b37536cd815023d055ef.zip chromium_src-8380c0910bfff5ee0af6b37536cd815023d055ef.tar.gz chromium_src-8380c0910bfff5ee0af6b37536cd815023d055ef.tar.bz2 |
Refactorying to create the ResourceLoaderBridge through a factory class for <video>
Extract the code to create a ResourceLoaderBridge to a factory class
to enable testing on the data source filters.
Review URL: http://codereview.chromium.org/146007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@19262 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/glue/media')
-rw-r--r-- | webkit/glue/media/media_resource_loader_bridge_factory.cc | 72 | ||||
-rw-r--r-- | webkit/glue/media/media_resource_loader_bridge_factory.h | 71 | ||||
-rw-r--r-- | webkit/glue/media/media_resource_loader_bridge_factory_unittest.cc | 44 | ||||
-rw-r--r-- | webkit/glue/media/simple_data_source.cc | 26 | ||||
-rw-r--r-- | webkit/glue/media/simple_data_source.h | 30 |
5 files changed, 213 insertions, 30 deletions
diff --git a/webkit/glue/media/media_resource_loader_bridge_factory.cc b/webkit/glue/media/media_resource_loader_bridge_factory.cc new file mode 100644 index 0000000..9f1485d --- /dev/null +++ b/webkit/glue/media/media_resource_loader_bridge_factory.cc @@ -0,0 +1,72 @@ +// Copyright (c) 2009 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/string_util.h" +#include "webkit/glue/media/media_resource_loader_bridge_factory.h" + +namespace { + +// A constant for an unknown position. +const int64 kPositionNotSpecified = -1; + +} // namespace + +namespace webkit_glue { + +MediaResourceLoaderBridgeFactory::MediaResourceLoaderBridgeFactory( + const GURL& referrer, + const std::string& frame_origin, + const std::string& main_frame_origin, + int origin_pid, + int app_cache_context_id, + int32 routing_id) + : referrer_(referrer), + frame_origin_(frame_origin), + main_frame_origin_(main_frame_origin), + origin_pid_(origin_pid), + app_cache_context_id_(app_cache_context_id), + routing_id_(routing_id) { +} + +ResourceLoaderBridge* MediaResourceLoaderBridgeFactory::CreateBridge( + const GURL& url, + int load_flags, + int64 first_byte_position, + int64 last_byte_position) { + return webkit_glue::ResourceLoaderBridge::Create( + "GET", + url, + url, + referrer_, + frame_origin_, + main_frame_origin_, + GenerateHeaders(first_byte_position, last_byte_position), + load_flags, + origin_pid_, + ResourceType::MEDIA, + app_cache_context_id_, + routing_id_); +} + +// static +const std::string MediaResourceLoaderBridgeFactory::GenerateHeaders ( + int64 first_byte_position, int64 last_byte_position) { + // Construct the range header. + std::string header; + if (first_byte_position > kPositionNotSpecified && + last_byte_position > kPositionNotSpecified) { + if (first_byte_position <= last_byte_position) { + header = StringPrintf("Range: bytes=%lld-%lld", + first_byte_position, + last_byte_position); + } + } else if (first_byte_position > kPositionNotSpecified) { + header = StringPrintf("Range: bytes=%lld-", first_byte_position); + } else { + NOTIMPLEMENTED() << "Operation not supported."; + } + return header; +} + +} // namespace webkit_glue diff --git a/webkit/glue/media/media_resource_loader_bridge_factory.h b/webkit/glue/media/media_resource_loader_bridge_factory.h new file mode 100644 index 0000000..80c0ed2 --- /dev/null +++ b/webkit/glue/media/media_resource_loader_bridge_factory.h @@ -0,0 +1,71 @@ +// Copyright (c) 2009 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 WEBKIT_GLUE_MEDIA_MEDIA_RESOURCE_LOADER_BRIDGE_FACTORY_H_ +#define WEBKIT_GLUE_MEDIA_MEDIA_RESOURCE_LOADER_BRIDGE_FACTORY_H_ + +#include "testing/gtest/include/gtest/gtest_prod.h" +#include "webkit/glue/resource_loader_bridge.h" + +namespace webkit_glue { + +// A factory used to create a ResourceLoaderBridge for the media player. +// This factory is used also for testing. Testing code can use this class and +// override CreateBridge() to inject a mock ResourceLoaderBridge for code that +// interacts with it, e.g. BufferedDataSource. +class MediaResourceLoaderBridgeFactory { + public: + MediaResourceLoaderBridgeFactory( + const GURL& referrer, + const std::string& frame_origin, + const std::string& main_frame_origin, + int origin_pid, + int app_cache_context_id, + int32 routing_id); + + virtual ~MediaResourceLoaderBridgeFactory() {} + + // Factory method to create a ResourceLoaderBridge with the following + // parameters: + // |url| - URL of the resource to be loaded. + // |load_flags| - Load flags for this loading. + // |first_byte_position| - First byte position for a range request, -1 if not. + // |last_byte_position| - Last byte position for a range request, -1 if not. + virtual ResourceLoaderBridge* CreateBridge( + const GURL& url, + int load_flags, + int64 first_byte_position, + int64 last_byte_position); + + private: + FRIEND_TEST(MediaResourceLoaderBridgeFactoryTest, GenerateHeaders); + + // Returns a range request header using parameters |first_byte_position| and + // |last_byte_position|. + // Negative numbers other than -1 are not allowed for |first_byte_position| + // and |last_byte_position|. |first_byte_position| should always be less than + // or equal to |last_byte_position| if they are both not -1. + // Here's a list of valid parameters: + // |first_byte_position| |last_byte_position| + // 0 1000 + // 4096 4096 + // 0 -1 + // -1 -1 + // Empty string is returned on invalid parameters. + static const std::string GenerateHeaders(int64 first_byte_position, + int64 last_byte_position); + + GURL first_party_for_cookies_; + GURL referrer_; + std::string frame_origin_; + std::string main_frame_origin_; + std::string headers_; + int origin_pid_; + int app_cache_context_id_; + int32 routing_id_; +}; + +} // namespace webkit_glue + +#endif // WEBKIT_GLUE_MEDIA_MEDIA_RESOURCE_LOADER_BRIDGE_FACTORY_H_ diff --git a/webkit/glue/media/media_resource_loader_bridge_factory_unittest.cc b/webkit/glue/media/media_resource_loader_bridge_factory_unittest.cc new file mode 100644 index 0000000..4c0126b --- /dev/null +++ b/webkit/glue/media/media_resource_loader_bridge_factory_unittest.cc @@ -0,0 +1,44 @@ +// Copyright (c) 2009 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 "net/http/http_util.h" +#include "webkit/glue/media/media_resource_loader_bridge_factory.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace webkit_glue { + +TEST(MediaResourceLoaderBridgeFactoryTest, GenerateHeaders) { + static const struct { + const bool success; + const int64 first_byte_position; + const int64 last_byte_position; + } data[] = { + { false, -1, -1 }, + { false, -5, 0 }, + { false, 100, 0 }, + { true, 0, -1 }, + { true, 0, 0 }, + { true, 100, 100 }, + { true, 50, -1 }, + { true, 10000, -1 }, + { true, 50, 100 }, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { + std::string headers = MediaResourceLoaderBridgeFactory::GenerateHeaders( + data[i].first_byte_position, data[i].last_byte_position); + std::vector<net::HttpByteRange> ranges; + bool ret = net::HttpUtil::ParseRanges(headers, &ranges); + EXPECT_EQ(data[i].success, ret); + if (ret) { + EXPECT_EQ(1u, ranges.size()); + EXPECT_EQ(data[i].first_byte_position, + ranges[0].first_byte_position()); + EXPECT_EQ(data[i].last_byte_position, + ranges[0].last_byte_position()); + } + } +} + +} // namespace webkit_glue diff --git a/webkit/glue/media/simple_data_source.cc b/webkit/glue/media/simple_data_source.cc index 6207ddd..d7ba9c1 100644 --- a/webkit/glue/media/simple_data_source.cc +++ b/webkit/glue/media/simple_data_source.cc @@ -30,9 +30,11 @@ bool IsSchemeSupported(const GURL& url) { namespace webkit_glue { -SimpleDataSource::SimpleDataSource(MessageLoop* render_loop, int32 routing_id) - : routing_id_(routing_id), - render_loop_(render_loop), +SimpleDataSource::SimpleDataSource( + MessageLoop* render_loop, + webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory) + : render_loop_(render_loop), + bridge_factory_(bridge_factory), size_(-1), position_(0), state_(UNINITIALIZED) { @@ -177,22 +179,8 @@ void SimpleDataSource::StartTask() { DCHECK_EQ(state_, INITIALIZING); // Create our bridge and start loading the resource. - bridge_.reset(webkit_glue::ResourceLoaderBridge::Create( - "GET", - url_, - url_, - GURL::EmptyGURL(), // TODO(scherkus): provide referer here. - "null", // TODO(abarth): provide frame_origin - "null", // TODO(abarth): provide main_frame_origin - "", - net::LOAD_BYPASS_CACHE, - base::GetCurrentProcId(), - ResourceType::MEDIA, - // TODO(michaeln): delegate->mediaplayer->frame-> - // app_cache_context()->context_id() - // For now don't service media resource requests from the appcache. - WebAppCacheContext::kNoAppCacheContextId, - routing_id_)); + bridge_.reset(bridge_factory_->CreateBridge( + url_, net::LOAD_BYPASS_CACHE, -1, -1)); bridge_->Start(this); } diff --git a/webkit/glue/media/simple_data_source.h b/webkit/glue/media/simple_data_source.h index 55693ff..7918034 100644 --- a/webkit/glue/media/simple_data_source.h +++ b/webkit/glue/media/simple_data_source.h @@ -14,7 +14,7 @@ #include "base/scoped_ptr.h" #include "media/base/factory.h" #include "media/base/filters.h" -#include "webkit/glue/resource_loader_bridge.h" +#include "webkit/glue/media/media_resource_loader_bridge_factory.h" class MessageLoop; class WebMediaPlayerDelegateImpl; @@ -24,11 +24,14 @@ namespace webkit_glue { class SimpleDataSource : public media::DataSource, public webkit_glue::ResourceLoaderBridge::Peer { public: - static media::FilterFactory* CreateFactory(MessageLoop* message_loop, - int32 routing_id) { - return new media::FilterFactoryImpl2<SimpleDataSource, - MessageLoop*, - int32>(message_loop, routing_id); + static media::FilterFactory* CreateFactory( + MessageLoop* message_loop, + webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory) { + return new media::FilterFactoryImpl2< + SimpleDataSource, + MessageLoop*, + webkit_glue::MediaResourceLoaderBridgeFactory*>(message_loop, + bridge_factory); } // MediaFilter implementation. @@ -56,8 +59,13 @@ class SimpleDataSource : public media::DataSource, virtual std::string GetURLForDebugging(); private: - friend class media::FilterFactoryImpl2<SimpleDataSource, MessageLoop*, int32>; - SimpleDataSource(MessageLoop* render_loop, int32 routing_id); + friend class media::FilterFactoryImpl2< + SimpleDataSource, + MessageLoop*, + webkit_glue::MediaResourceLoaderBridgeFactory*>; + SimpleDataSource( + MessageLoop* render_loop, + webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory); virtual ~SimpleDataSource(); // Updates |url_| and |media_format_| with the given URL. @@ -69,12 +77,12 @@ class SimpleDataSource : public media::DataSource, // Cancels and deletes the resource loading on the render thread. void CancelTask(); - // Passed in during construction, used when creating the bridge. - int32 routing_id_; - // Primarily used for asserting the bridge is loading on the render thread. MessageLoop* render_loop_; + // Factory to create a bridge. + scoped_ptr<webkit_glue::MediaResourceLoaderBridgeFactory> bridge_factory_; + // Bridge used to load the media resource. scoped_ptr<webkit_glue::ResourceLoaderBridge> bridge_; |