Commit 097d3a4a authored by tommycli's avatar tommycli Committed by Commit bot

Componentize component_updater: Break content/ dependency for rest of component_updater tests.

BUG=371463

Review URL: https://codereview.chromium.org/514473002

Cr-Commit-Position: refs/heads/master@{#292202}
parent fb47659a
...@@ -44,10 +44,16 @@ MockServiceObserver::~MockServiceObserver() { ...@@ -44,10 +44,16 @@ MockServiceObserver::~MockServiceObserver() {
} }
ComponentUpdaterTest::ComponentUpdaterTest() ComponentUpdaterTest::ComponentUpdaterTest()
: test_config_(NULL), : post_interceptor_(NULL),
test_config_(NULL),
thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
// The component updater instance under test. // The component updater instance under test.
test_config_ = new TestConfigurator; test_config_ = new TestConfigurator(
BrowserThread::GetBlockingPool()
->GetSequencedTaskRunnerWithShutdownBehavior(
BrowserThread::GetBlockingPool()->GetSequenceToken(),
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN),
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
component_updater_.reset(ComponentUpdateServiceFactory(test_config_)); component_updater_.reset(ComponentUpdateServiceFactory(test_config_));
net::URLFetcher::SetEnableInterceptionForTests(true); net::URLFetcher::SetEnableInterceptionForTests(true);
...@@ -59,7 +65,8 @@ ComponentUpdaterTest::~ComponentUpdaterTest() { ...@@ -59,7 +65,8 @@ ComponentUpdaterTest::~ComponentUpdaterTest() {
void ComponentUpdaterTest::SetUp() { void ComponentUpdaterTest::SetUp() {
get_interceptor_.reset(new GetInterceptor); get_interceptor_.reset(new GetInterceptor);
interceptor_factory_.reset(new InterceptorFactory); interceptor_factory_.reset(new InterceptorFactory(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
post_interceptor_ = interceptor_factory_->CreateInterceptor(); post_interceptor_ = interceptor_factory_->CreateInterceptor();
EXPECT_TRUE(post_interceptor_); EXPECT_TRUE(post_interceptor_);
} }
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/values.h" #include "base/values.h"
...@@ -17,7 +16,6 @@ ...@@ -17,7 +16,6 @@
#include "components/component_updater/component_updater_service.h" #include "components/component_updater/component_updater_service.h"
#include "components/component_updater/test/component_patcher_unittest.h" #include "components/component_updater/test/component_patcher_unittest.h"
#include "components/component_updater/test/test_installer.h" #include "components/component_updater/test/test_installer.h"
#include "content/public/browser/browser_thread.h"
#include "courgette/courgette.h" #include "courgette/courgette.h"
#include "courgette/third_party/bsdiff.h" #include "courgette/third_party/bsdiff.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -68,8 +66,7 @@ ComponentPatcherOperationTest::ComponentPatcherOperationTest() { ...@@ -68,8 +66,7 @@ ComponentPatcherOperationTest::ComponentPatcherOperationTest() {
EXPECT_TRUE(input_dir_.CreateUniqueTempDir()); EXPECT_TRUE(input_dir_.CreateUniqueTempDir());
EXPECT_TRUE(installed_dir_.CreateUniqueTempDir()); EXPECT_TRUE(installed_dir_.CreateUniqueTempDir());
installer_.reset(new ReadOnlyTestInstaller(installed_dir_.path())); installer_.reset(new ReadOnlyTestInstaller(installed_dir_.path()));
task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread( task_runner_ = base::MessageLoop::current()->task_runner();
content::BrowserThread::FILE);
} }
ComponentPatcherOperationTest::~ComponentPatcherOperationTest() { ComponentPatcherOperationTest::~ComponentPatcherOperationTest() {
......
...@@ -2,13 +2,13 @@ ...@@ -2,13 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef CHROME_BROWSER_COMPONENT_UPDATER_TEST_COMPONENT_PATCHER_UNITTEST_H_ #ifndef COMPONENTS_COMPONENT_UPDATER_TEST_COMPONENT_PATCHER_UNITTEST_H_
#define CHROME_BROWSER_COMPONENT_UPDATER_TEST_COMPONENT_PATCHER_UNITTEST_H_ #define COMPONENTS_COMPONENT_UPDATER_TEST_COMPONENT_PATCHER_UNITTEST_H_
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "content/public/test/test_browser_thread_bundle.h" #include "base/message_loop/message_loop.h"
#include "courgette/courgette.h" #include "courgette/courgette.h"
#include "courgette/third_party/bsdiff.h" #include "courgette/third_party/bsdiff.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -34,9 +34,9 @@ class ComponentPatcherOperationTest : public testing::Test { ...@@ -34,9 +34,9 @@ class ComponentPatcherOperationTest : public testing::Test {
scoped_refptr<base::SequencedTaskRunner> task_runner_; scoped_refptr<base::SequencedTaskRunner> task_runner_;
private: private:
content::TestBrowserThreadBundle thread_bundle_; base::MessageLoopForIO loop_;
}; };
} // namespace component_updater } // namespace component_updater
#endif // CHROME_BROWSER_COMPONENT_UPDATER_TEST_COMPONENT_PATCHER_UNITTEST_H_ #endif // COMPONENTS_COMPONENT_UPDATER_TEST_COMPONENT_PATCHER_UNITTEST_H_
...@@ -3,19 +3,16 @@ ...@@ -3,19 +3,16 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/version.h" #include "base/version.h"
#include "components/component_updater/component_updater_ping_manager.h" #include "components/component_updater/component_updater_ping_manager.h"
#include "components/component_updater/crx_update_item.h" #include "components/component_updater/crx_update_item.h"
#include "components/component_updater/test/test_configurator.h" #include "components/component_updater/test/test_configurator.h"
#include "components/component_updater/test/url_request_post_interceptor.h" #include "components/component_updater/test/url_request_post_interceptor.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/url_request/url_request_test_util.h" #include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
namespace component_updater { namespace component_updater {
class ComponentUpdaterPingManagerTest : public testing::Test { class ComponentUpdaterPingManagerTest : public testing::Test {
...@@ -34,12 +31,12 @@ class ComponentUpdaterPingManagerTest : public testing::Test { ...@@ -34,12 +31,12 @@ class ComponentUpdaterPingManagerTest : public testing::Test {
scoped_ptr<PingManager> ping_manager_; scoped_ptr<PingManager> ping_manager_;
private: private:
content::TestBrowserThreadBundle thread_bundle_; base::MessageLoopForIO loop_;
}; };
ComponentUpdaterPingManagerTest::ComponentUpdaterPingManagerTest() ComponentUpdaterPingManagerTest::ComponentUpdaterPingManagerTest()
: config_(new TestConfigurator), : config_(new TestConfigurator(base::MessageLoopProxy::current(),
thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { base::MessageLoopProxy::current())) {
} }
void ComponentUpdaterPingManagerTest::SetUp() { void ComponentUpdaterPingManagerTest::SetUp() {
...@@ -57,7 +54,8 @@ void ComponentUpdaterPingManagerTest::RunThreadsUntilIdle() { ...@@ -57,7 +54,8 @@ void ComponentUpdaterPingManagerTest::RunThreadsUntilIdle() {
// Test is flaky: http://crbug.com/349547 // Test is flaky: http://crbug.com/349547
TEST_F(ComponentUpdaterPingManagerTest, DISABLED_PingManagerTest) { TEST_F(ComponentUpdaterPingManagerTest, DISABLED_PingManagerTest) {
scoped_ptr<InterceptorFactory> interceptor_factory(new InterceptorFactory); scoped_ptr<InterceptorFactory> interceptor_factory(
new InterceptorFactory(base::MessageLoopProxy::current()));
URLRequestPostInterceptor* interceptor = URLRequestPostInterceptor* interceptor =
interceptor_factory->CreateInterceptor(); interceptor_factory->CreateInterceptor();
EXPECT_TRUE(interceptor); EXPECT_TRUE(interceptor);
......
...@@ -8,20 +8,19 @@ ...@@ -8,20 +8,19 @@
#include "base/version.h" #include "base/version.h"
#include "components/component_updater/component_patcher_operation.h" #include "components/component_updater/component_patcher_operation.h"
#include "components/component_updater/test/test_configurator.h" #include "components/component_updater/test/test_configurator.h"
#include "content/public/browser/browser_thread.h"
#include "url/gurl.h" #include "url/gurl.h"
using content::BrowserThread;
namespace component_updater { namespace component_updater {
TestConfigurator::TestConfigurator() TestConfigurator::TestConfigurator(
: initial_time_(0), const scoped_refptr<base::SequencedTaskRunner>& worker_task_runner,
const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner)
: worker_task_runner_(worker_task_runner),
initial_time_(0),
times_(1), times_(1),
recheck_time_(0), recheck_time_(0),
ondemand_time_(0), ondemand_time_(0),
context_(new net::TestURLRequestContextGetter( context_(new net::TestURLRequestContextGetter(network_task_runner)) {
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))) {
} }
TestConfigurator::~TestConfigurator() { TestConfigurator::~TestConfigurator() {
...@@ -133,16 +132,14 @@ void TestConfigurator::SetInitialDelay(int seconds) { ...@@ -133,16 +132,14 @@ void TestConfigurator::SetInitialDelay(int seconds) {
scoped_refptr<base::SequencedTaskRunner> scoped_refptr<base::SequencedTaskRunner>
TestConfigurator::GetSequencedTaskRunner() const { TestConfigurator::GetSequencedTaskRunner() const {
return content::BrowserThread::GetBlockingPool() DCHECK(worker_task_runner_.get());
->GetSequencedTaskRunnerWithShutdownBehavior( return worker_task_runner_;
content::BrowserThread::GetBlockingPool()->GetSequenceToken(),
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
} }
scoped_refptr<base::SingleThreadTaskRunner> scoped_refptr<base::SingleThreadTaskRunner>
TestConfigurator::GetSingleThreadTaskRunner() const { TestConfigurator::GetSingleThreadTaskRunner() const {
return content::BrowserThread::GetMessageLoopProxyForThread( // This is NULL because tests do not use the background downloader.
content::BrowserThread::FILE); return NULL;
} }
} // namespace component_updater } // namespace component_updater
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef CHROME_BROWSER_COMPONENT_UPDATER_TEST_TEST_CONFIGURATOR_H_ #ifndef COMPONENTS_COMPONENT_UPDATER_TEST_TEST_CONFIGURATOR_H_
#define CHROME_BROWSER_COMPONENT_UPDATER_TEST_TEST_CONFIGURATOR_H_ #define COMPONENTS_COMPONENT_UPDATER_TEST_TEST_CONFIGURATOR_H_
#include <string> #include <string>
#include <utility> #include <utility>
...@@ -25,7 +25,9 @@ struct CrxComponent; ...@@ -25,7 +25,9 @@ struct CrxComponent;
class TestConfigurator : public Configurator { class TestConfigurator : public Configurator {
public: public:
TestConfigurator(); TestConfigurator(
const scoped_refptr<base::SequencedTaskRunner>& worker_task_runner,
const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner);
virtual ~TestConfigurator(); virtual ~TestConfigurator();
// Overrrides for Configurator. // Overrrides for Configurator.
...@@ -61,6 +63,9 @@ class TestConfigurator : public Configurator { ...@@ -61,6 +63,9 @@ class TestConfigurator : public Configurator {
void SetInitialDelay(int seconds); void SetInitialDelay(int seconds);
private: private:
scoped_refptr<base::SequencedTaskRunner> worker_task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
int initial_time_; int initial_time_;
int times_; int times_;
int recheck_time_; int recheck_time_;
...@@ -74,4 +79,4 @@ class TestConfigurator : public Configurator { ...@@ -74,4 +79,4 @@ class TestConfigurator : public Configurator {
} // namespace component_updater } // namespace component_updater
#endif // CHROME_BROWSER_COMPONENT_UPDATER_TEST_TEST_CONFIGURATOR_H_ #endif // COMPONENTS_COMPONENT_UPDATER_TEST_TEST_CONFIGURATOR_H_
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/version.h" #include "base/version.h"
...@@ -14,14 +15,10 @@ ...@@ -14,14 +15,10 @@
#include "components/component_updater/test/test_configurator.h" #include "components/component_updater/test/test_configurator.h"
#include "components/component_updater/test/url_request_post_interceptor.h" #include "components/component_updater/test/url_request_post_interceptor.h"
#include "components/component_updater/update_checker.h" #include "components/component_updater/update_checker.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/url_request/url_fetcher.h" #include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_test_util.h" #include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
namespace component_updater { namespace component_updater {
namespace { namespace {
...@@ -67,16 +64,13 @@ class UpdateCheckerTest : public testing::Test { ...@@ -67,16 +64,13 @@ class UpdateCheckerTest : public testing::Test {
UpdateResponse::Results results_; UpdateResponse::Results results_;
private: private:
content::TestBrowserThreadBundle thread_bundle_; base::MessageLoopForIO loop_;
base::Closure quit_closure_; base::Closure quit_closure_;
DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest); DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest);
}; };
UpdateCheckerTest::UpdateCheckerTest() UpdateCheckerTest::UpdateCheckerTest() : post_interceptor_(NULL), error_(0) {
: config_(new TestConfigurator),
error_(0),
thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
net::URLFetcher::SetEnableInterceptionForTests(true); net::URLFetcher::SetEnableInterceptionForTests(true);
} }
...@@ -85,7 +79,10 @@ UpdateCheckerTest::~UpdateCheckerTest() { ...@@ -85,7 +79,10 @@ UpdateCheckerTest::~UpdateCheckerTest() {
} }
void UpdateCheckerTest::SetUp() { void UpdateCheckerTest::SetUp() {
interceptor_factory_.reset(new InterceptorFactory); config_.reset(new TestConfigurator(base::MessageLoopProxy::current(),
base::MessageLoopProxy::current()));
interceptor_factory_.reset(
new InterceptorFactory(base::MessageLoopProxy::current()));
post_interceptor_ = interceptor_factory_->CreateInterceptor(); post_interceptor_ = interceptor_factory_->CreateInterceptor();
EXPECT_TRUE(post_interceptor_); EXPECT_TRUE(post_interceptor_);
...@@ -103,6 +100,10 @@ void UpdateCheckerTest::TearDown() { ...@@ -103,6 +100,10 @@ void UpdateCheckerTest::TearDown() {
interceptor_factory_.reset(); interceptor_factory_.reset();
config_.reset(); config_.reset();
// The PostInterceptor requires the message loop to run to destruct correctly.
// TODO: This is fragile and should be fixed.
RunThreadsUntilIdle();
} }
void UpdateCheckerTest::RunThreads() { void UpdateCheckerTest::RunThreads() {
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "components/component_updater/test/test_configurator.h" #include "components/component_updater/test/test_configurator.h"
#include "content/public/test/test_browser_thread.h"
#include "net/base/upload_bytes_element_reader.h" #include "net/base/upload_bytes_element_reader.h"
#include "net/url_request/url_request.h" #include "net/url_request/url_request.h"
#include "net/url_request/url_request_filter.h" #include "net/url_request/url_request_filter.h"
...@@ -16,8 +15,6 @@ ...@@ -16,8 +15,6 @@
#include "net/url_request/url_request_simple_job.h" #include "net/url_request/url_request_simple_job.h"
#include "net/url_request/url_request_test_util.h" #include "net/url_request/url_request_test_util.h"
using content::BrowserThread;
namespace component_updater { namespace component_updater {
// Returns a canned response. // Returns a canned response.
...@@ -49,12 +46,14 @@ class URLRequestMockJob : public net::URLRequestSimpleJob { ...@@ -49,12 +46,14 @@ class URLRequestMockJob : public net::URLRequestSimpleJob {
DISALLOW_COPY_AND_ASSIGN(URLRequestMockJob); DISALLOW_COPY_AND_ASSIGN(URLRequestMockJob);
}; };
URLRequestPostInterceptor::URLRequestPostInterceptor(const GURL& url) URLRequestPostInterceptor::URLRequestPostInterceptor(
: url_(url), hit_count_(0) { const GURL& url,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
: url_(url), io_task_runner_(io_task_runner), hit_count_(0) {
} }
URLRequestPostInterceptor::~URLRequestPostInterceptor() { URLRequestPostInterceptor::~URLRequestPostInterceptor() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
ClearExpectations(); ClearExpectations();
} }
...@@ -125,17 +124,19 @@ void URLRequestPostInterceptor::Reset() { ...@@ -125,17 +124,19 @@ void URLRequestPostInterceptor::Reset() {
class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor { class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor {
public: public:
Delegate(const std::string& scheme, const std::string& hostname) Delegate(const std::string& scheme,
: scheme_(scheme), hostname_(hostname) {} const std::string& hostname,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
: scheme_(scheme), hostname_(hostname), io_task_runner_(io_task_runner) {}
void Register() { void Register() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
scheme_, hostname_, scoped_ptr<net::URLRequestInterceptor>(this)); scheme_, hostname_, scoped_ptr<net::URLRequestInterceptor>(this));
} }
void Unregister() { void Unregister() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
for (InterceptorMap::iterator it = interceptors_.begin(); for (InterceptorMap::iterator it = interceptors_.begin();
it != interceptors_.end(); it != interceptors_.end();
++it) ++it)
...@@ -145,7 +146,7 @@ class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor { ...@@ -145,7 +146,7 @@ class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor {
} }
void OnCreateInterceptor(URLRequestPostInterceptor* interceptor) { void OnCreateInterceptor(URLRequestPostInterceptor* interceptor) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
CHECK(interceptors_.find(interceptor->GetUrl()) == interceptors_.end()); CHECK(interceptors_.find(interceptor->GetUrl()) == interceptors_.end());
interceptors_.insert(std::make_pair(interceptor->GetUrl(), interceptor)); interceptors_.insert(std::make_pair(interceptor->GetUrl(), interceptor));
...@@ -157,7 +158,7 @@ class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor { ...@@ -157,7 +158,7 @@ class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor {
virtual net::URLRequestJob* MaybeInterceptRequest( virtual net::URLRequestJob* MaybeInterceptRequest(
net::URLRequest* request, net::URLRequest* request,
net::NetworkDelegate* network_delegate) const OVERRIDE { net::NetworkDelegate* network_delegate) const OVERRIDE {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
// Only intercepts POST. // Only intercepts POST.
if (!request->has_upload()) if (!request->has_upload())
...@@ -212,26 +213,29 @@ class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor { ...@@ -212,26 +213,29 @@ class URLRequestPostInterceptor::Delegate : public net::URLRequestInterceptor {
const std::string scheme_; const std::string scheme_;
const std::string hostname_; const std::string hostname_;
scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
DISALLOW_COPY_AND_ASSIGN(Delegate); DISALLOW_COPY_AND_ASSIGN(Delegate);
}; };
URLRequestPostInterceptorFactory::URLRequestPostInterceptorFactory( URLRequestPostInterceptorFactory::URLRequestPostInterceptorFactory(
const std::string& scheme, const std::string& scheme,
const std::string& hostname) const std::string& hostname,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
: scheme_(scheme), : scheme_(scheme),
hostname_(hostname), hostname_(hostname),
delegate_(new URLRequestPostInterceptor::Delegate(scheme, hostname)) { io_task_runner_(io_task_runner),
BrowserThread::PostTask( delegate_(new URLRequestPostInterceptor::Delegate(scheme,
BrowserThread::IO, hostname,
io_task_runner)) {
io_task_runner_->PostTask(
FROM_HERE, FROM_HERE,
base::Bind(&URLRequestPostInterceptor::Delegate::Register, base::Bind(&URLRequestPostInterceptor::Delegate::Register,
base::Unretained(delegate_))); base::Unretained(delegate_)));
} }
URLRequestPostInterceptorFactory::~URLRequestPostInterceptorFactory() { URLRequestPostInterceptorFactory::~URLRequestPostInterceptorFactory() {
BrowserThread::PostTask( io_task_runner_->PostTask(
BrowserThread::IO,
FROM_HERE, FROM_HERE,
base::Bind(&URLRequestPostInterceptor::Delegate::Unregister, base::Bind(&URLRequestPostInterceptor::Delegate::Unregister,
base::Unretained(delegate_))); base::Unretained(delegate_)));
...@@ -243,9 +247,8 @@ URLRequestPostInterceptor* URLRequestPostInterceptorFactory::CreateInterceptor( ...@@ -243,9 +247,8 @@ URLRequestPostInterceptor* URLRequestPostInterceptorFactory::CreateInterceptor(
base::StringPrintf("%s://%s", scheme_.c_str(), hostname_.c_str())); base::StringPrintf("%s://%s", scheme_.c_str(), hostname_.c_str()));
GURL absolute_url(base_url.Resolve(filepath.MaybeAsASCII())); GURL absolute_url(base_url.Resolve(filepath.MaybeAsASCII()));
URLRequestPostInterceptor* interceptor( URLRequestPostInterceptor* interceptor(
new URLRequestPostInterceptor(absolute_url)); new URLRequestPostInterceptor(absolute_url, io_task_runner_));
bool res = BrowserThread::PostTask( bool res = io_task_runner_->PostTask(
BrowserThread::IO,
FROM_HERE, FROM_HERE,
base::Bind(&URLRequestPostInterceptor::Delegate::OnCreateInterceptor, base::Bind(&URLRequestPostInterceptor::Delegate::OnCreateInterceptor,
base::Unretained(delegate_), base::Unretained(delegate_),
...@@ -262,9 +265,11 @@ bool PartialMatch::Match(const std::string& actual) const { ...@@ -262,9 +265,11 @@ bool PartialMatch::Match(const std::string& actual) const {
return actual.find(expected_) != std::string::npos; return actual.find(expected_) != std::string::npos;
} }
InterceptorFactory::InterceptorFactory() InterceptorFactory::InterceptorFactory(
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
: URLRequestPostInterceptorFactory(POST_INTERCEPT_SCHEME, : URLRequestPostInterceptorFactory(POST_INTERCEPT_SCHEME,
POST_INTERCEPT_HOSTNAME) { POST_INTERCEPT_HOSTNAME,
io_task_runner) {
} }
InterceptorFactory::~InterceptorFactory() { InterceptorFactory::~InterceptorFactory() {
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ #ifndef COMPONENTS_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
#define CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ #define COMPONENTS_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
#include <map> #include <map>
#include <queue> #include <queue>
...@@ -12,11 +12,13 @@ ...@@ -12,11 +12,13 @@
#include <vector> #include <vector>
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h" #include "base/synchronization/lock.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace base { namespace base {
class FilePath; class FilePath;
class SequencedTaskRunner;
} }
namespace net { namespace net {
...@@ -92,11 +94,14 @@ class URLRequestPostInterceptor { ...@@ -92,11 +94,14 @@ class URLRequestPostInterceptor {
friend class URLRequestPostInterceptorFactory; friend class URLRequestPostInterceptorFactory;
typedef std::pair<const RequestMatcher*, std::string> Expectation; typedef std::pair<const RequestMatcher*, std::string> Expectation;
explicit URLRequestPostInterceptor(const GURL& url); URLRequestPostInterceptor(
const GURL& url,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
~URLRequestPostInterceptor(); ~URLRequestPostInterceptor();
void ClearExpectations(); void ClearExpectations();
const GURL url_; const GURL url_;
scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
mutable base::Lock interceptor_lock_; mutable base::Lock interceptor_lock_;
mutable int hit_count_; mutable int hit_count_;
...@@ -108,8 +113,10 @@ class URLRequestPostInterceptor { ...@@ -108,8 +113,10 @@ class URLRequestPostInterceptor {
class URLRequestPostInterceptorFactory { class URLRequestPostInterceptorFactory {
public: public:
URLRequestPostInterceptorFactory(const std::string& scheme, URLRequestPostInterceptorFactory(
const std::string& hostname); const std::string& scheme,
const std::string& hostname,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
~URLRequestPostInterceptorFactory(); ~URLRequestPostInterceptorFactory();
// Creates an interceptor object for the specified url path. Returns NULL // Creates an interceptor object for the specified url path. Returns NULL
...@@ -120,6 +127,7 @@ class URLRequestPostInterceptorFactory { ...@@ -120,6 +127,7 @@ class URLRequestPostInterceptorFactory {
private: private:
const std::string scheme_; const std::string scheme_;
const std::string hostname_; const std::string hostname_;
scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
// After creation, |delegate_| lives on the IO thread and it is owned by // After creation, |delegate_| lives on the IO thread and it is owned by
// a URLRequestFilter after registration. A task to unregister it and // a URLRequestFilter after registration. A task to unregister it and
...@@ -132,7 +140,8 @@ class URLRequestPostInterceptorFactory { ...@@ -132,7 +140,8 @@ class URLRequestPostInterceptorFactory {
// Intercepts HTTP POST requests sent to "localhost2". // Intercepts HTTP POST requests sent to "localhost2".
class InterceptorFactory : public URLRequestPostInterceptorFactory { class InterceptorFactory : public URLRequestPostInterceptorFactory {
public: public:
InterceptorFactory(); explicit InterceptorFactory(
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
~InterceptorFactory(); ~InterceptorFactory();
URLRequestPostInterceptor* CreateInterceptor(); URLRequestPostInterceptor* CreateInterceptor();
...@@ -154,4 +163,4 @@ class PartialMatch : public URLRequestPostInterceptor::RequestMatcher { ...@@ -154,4 +163,4 @@ class PartialMatch : public URLRequestPostInterceptor::RequestMatcher {
} // namespace component_updater } // namespace component_updater
#endif // CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ #endif // COMPONENTS_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment