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 | |
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')
-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 | ||||
-rw-r--r-- | webkit/glue/webmediaplayer_impl.cc | 16 | ||||
-rw-r--r-- | webkit/glue/webmediaplayer_impl.h | 3 | ||||
-rw-r--r-- | webkit/tools/test_shell/test_shell.gyp | 1 | ||||
-rw-r--r-- | webkit/tools/test_shell/test_webview_delegate.cc | 22 | ||||
-rw-r--r-- | webkit/webkit.gyp | 2 |
10 files changed, 243 insertions, 44 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_; diff --git a/webkit/glue/webmediaplayer_impl.cc b/webkit/glue/webmediaplayer_impl.cc index 836ef12..d538a72 100644 --- a/webkit/glue/webmediaplayer_impl.cc +++ b/webkit/glue/webmediaplayer_impl.cc @@ -11,7 +11,6 @@ #include "webkit/api/public/WebRect.h" #include "webkit/api/public/WebSize.h" #include "webkit/api/public/WebURL.h" -#include "webkit/glue/media/simple_data_source.h" #include "webkit/glue/media/video_renderer_impl.h" #include "webkit/glue/webmediaplayer_impl.h" @@ -22,12 +21,12 @@ using WebKit::WebSize; namespace webkit_glue { ///////////////////////////////////////////////////////////////////////////// -// Task to be posted on main thread that fire WebMediaPlayer methods. +// Task to be posted on main thread that fire WebMediaPlayerClient methods. -class NotifyWebMediaPlayerTask : public CancelableTask { +class NotifyWebMediaPlayerClientTask : public CancelableTask { public: - NotifyWebMediaPlayerTask(WebMediaPlayerImpl* media_player, - WebMediaPlayerClientMethod method) + NotifyWebMediaPlayerClientTask(WebMediaPlayerImpl* media_player, + WebMediaPlayerClientMethod method) : media_player_(media_player), method_(method) {} @@ -46,7 +45,7 @@ class NotifyWebMediaPlayerTask : public CancelableTask { WebMediaPlayerImpl* media_player_; WebMediaPlayerClientMethod method_; - DISALLOW_COPY_AND_ASSIGN(NotifyWebMediaPlayerTask); + DISALLOW_COPY_AND_ASSIGN(NotifyWebMediaPlayerClientTask); }; ///////////////////////////////////////////////////////////////////////////// @@ -67,9 +66,6 @@ WebMediaPlayerImpl::WebMediaPlayerImpl(WebKit::WebMediaPlayerClient* client, filter_factory_->AddFactory(media::FFmpegVideoDecoder::CreateFactory()); filter_factory_->AddFactory(media::NullAudioRenderer::CreateFilterFactory()); filter_factory_->AddFactory(VideoRendererImpl::CreateFactory(this)); - // TODO(hclam): Provide a valid routing id to simple data source. - filter_factory_->AddFactory( - SimpleDataSource::CreateFactory(MessageLoop::current(), 0)); DCHECK(client_); @@ -349,7 +345,7 @@ void WebMediaPlayerImpl::PostTask(int index, AutoLock auto_lock(task_lock_); if (!tasks_[index]) { - CancelableTask* task = new NotifyWebMediaPlayerTask(this, method); + CancelableTask* task = new NotifyWebMediaPlayerClientTask(this, method); tasks_[index] = task; main_loop_->PostTask(FROM_HERE, task); } diff --git a/webkit/glue/webmediaplayer_impl.h b/webkit/glue/webmediaplayer_impl.h index 0971125..d8a365c 100644 --- a/webkit/glue/webmediaplayer_impl.h +++ b/webkit/glue/webmediaplayer_impl.h @@ -85,8 +85,7 @@ class WebMediaPlayerImpl : public WebKit::WebMediaPlayer, // 2. FFmpeg audio decoder // 3. FFmpeg video decoder // 4. Video renderer - // 5. Simple data source - // 6. Null audio renderer + // 5. Null audio renderer // The video renderer provided by this class is using the graphics context // provided by WebKit to perform renderering. The simple data source does // resource loading by loading the whole resource object into memory. Null diff --git a/webkit/tools/test_shell/test_shell.gyp b/webkit/tools/test_shell/test_shell.gyp index ec42d92..c57a376 100644 --- a/webkit/tools/test_shell/test_shell.gyp +++ b/webkit/tools/test_shell/test_shell.gyp @@ -460,6 +460,7 @@ '../../glue/dom_serializer_unittest.cc', '../../glue/glue_serialize_unittest.cc', '../../glue/iframe_redirect_unittest.cc', + '../../glue/media/media_resource_loader_bridge_factory_unittest.cc', '../../glue/mimetype_unittest.cc', '../../glue/multipart_response_delegate_unittest.cc', '../../glue/password_autocomplete_listener_unittest.cc', diff --git a/webkit/tools/test_shell/test_webview_delegate.cc b/webkit/tools/test_shell/test_webview_delegate.cc index be1f75a..91aa19b 100644 --- a/webkit/tools/test_shell/test_webview_delegate.cc +++ b/webkit/tools/test_shell/test_webview_delegate.cc @@ -14,6 +14,7 @@ #include "base/gfx/point.h" #include "base/gfx/native_widget_types.h" #include "base/message_loop.h" +#include "base/process_util.h" #include "base/string_util.h" #include "base/trace_event.h" #include "net/base/net_errors.h" @@ -25,6 +26,9 @@ #include "webkit/api/public/WebURL.h" #include "webkit/api/public/WebURLError.h" #include "webkit/api/public/WebURLRequest.h" +#include "webkit/glue/media/media_resource_loader_bridge_factory.h" +#include "webkit/glue/media/simple_data_source.h" +#include "webkit/glue/webappcachecontext.h" #include "webkit/glue/webdropdata.h" #include "webkit/glue/webframe.h" #include "webkit/glue/webpreferences.h" @@ -124,8 +128,22 @@ WebWidget* TestWebViewDelegate::CreatePopupWidget(WebView* webview, WebKit::WebMediaPlayer* TestWebViewDelegate::CreateWebMediaPlayer( WebKit::WebMediaPlayerClient* client) { - return new webkit_glue::WebMediaPlayerImpl( - client, new media::FilterFactoryCollection()); + scoped_refptr<media::FilterFactoryCollection> factory = + new media::FilterFactoryCollection(); + + // TODO(hclam): this is the same piece of code as in RenderView, maybe they + // should be grouped together. + webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory = + new webkit_glue::MediaResourceLoaderBridgeFactory( + GURL::EmptyGURL(), // referrer + "null", // frame origin + "null", // main_frame_origin + base::GetCurrentProcId(), + WebAppCacheContext::kNoAppCacheContextId, + 0); + factory->AddFactory(webkit_glue::SimpleDataSource::CreateFactory( + MessageLoop::current(), bridge_factory)); + return new webkit_glue::WebMediaPlayerImpl(client, factory); } WebWorker* TestWebViewDelegate::CreateWebWorker(WebWorkerClient* client) { diff --git a/webkit/webkit.gyp b/webkit/webkit.gyp index e7b3dfe..9e97cad 100644 --- a/webkit/webkit.gyp +++ b/webkit/webkit.gyp @@ -4447,6 +4447,8 @@ 'glue/devtools/dom_agent_impl.cc', 'glue/devtools/dom_agent_impl.h', 'glue/devtools/tools_agent.h', + 'glue/media/media_resource_loader_bridge_factory.cc', + 'glue/media/media_resource_loader_bridge_factory.h', 'glue/media/simple_data_source.cc', 'glue/media/simple_data_source.h', 'glue/media/video_renderer_impl.cc', |