Commit eaae5d98 authored by tommycli's avatar tommycli Committed by Commit bot

Componentize component_updater: Move URLRequestPrepackagedInterceptor from content/ to net/.

This patch breaks the URLRequestPrepackagedInterceptor dependencies on content/.

We want to use URLRequestPrepackagedInterceptor in unit tests that will be in components/. These tests cannot depend on content/.

BUG=371463
TBR=wtc@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#293861}
parent c13b6049
......@@ -66,7 +66,10 @@ ComponentUpdaterTest::~ComponentUpdaterTest() {
}
void ComponentUpdaterTest::SetUp() {
get_interceptor_.reset(new GetInterceptor);
get_interceptor_.reset(new GetInterceptor(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
interceptor_factory_.reset(new InterceptorFactory(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
post_interceptor_ = interceptor_factory_->CreateInterceptor();
......
......@@ -15,7 +15,7 @@
#include "components/component_updater/test/test_configurator.h"
#include "components/component_updater/test/url_request_post_interceptor.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/test/net/url_request_prepackaged_interceptor.h"
#include "net/url_request/test_url_request_interceptor.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -25,7 +25,7 @@ namespace component_updater {
class TestInstaller;
// Intercepts HTTP GET requests sent to "localhost".
typedef content::URLLocalHostRequestPrepackagedInterceptor GetInterceptor;
typedef net::LocalHostTestURLRequestInterceptor GetInterceptor;
class ComponentUpdaterTest : public testing::Test {
public:
......
......@@ -6,7 +6,8 @@
#include "chrome/browser/extensions/api/image_writer_private/test_utils.h"
#include "chrome/browser/extensions/api/image_writer_private/write_from_url_operation.h"
#include "chrome/test/base/testing_profile.h"
#include "content/test/net/url_request_prepackaged_interceptor.h"
#include "content/public/browser/browser_thread.h"
#include "net/url_request/test_url_request_interceptor.h"
#include "net/url_request/url_fetcher.h"
namespace extensions {
......@@ -14,6 +15,7 @@ namespace image_writer {
namespace {
using content::BrowserThread;
using testing::_;
using testing::AnyNumber;
using testing::AtLeast;
......@@ -22,7 +24,7 @@ using testing::Lt;
const char kTestImageUrl[] = "http://localhost/test/image.zip";
typedef content::URLLocalHostRequestPrepackagedInterceptor GetInterceptor;
typedef net::LocalHostTestURLRequestInterceptor GetInterceptor;
// This class gives us a generic Operation with the ability to set or inspect
// the current path to the image file.
......@@ -76,7 +78,10 @@ class ImageWriterWriteFromUrlOperationTest : public ImageWriterUnitTestBase {
// Turn on interception and set up our dummy file.
net::URLFetcher::SetEnableInterceptionForTests(true);
get_interceptor_.reset(new GetInterceptor());
get_interceptor_.reset(new GetInterceptor(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
get_interceptor_->SetResponse(GURL(kTestImageUrl),
test_utils_.GetImagePath());
}
......
......@@ -23,19 +23,21 @@
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_map.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/test/browser_test_utils.h"
#include "content/test/net/url_request_prepackaged_interceptor.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/notification_types.h"
#include "net/url_request/test_url_request_interceptor.h"
#include "net/url_request/url_fetcher.h"
#include "policy/policy_constants.h"
#include "testing/gmock/include/gmock/gmock.h"
using content::BrowserThread;
using extensions::Extension;
using extensions::ExtensionRegistry;
using extensions::Manifest;
......@@ -283,8 +285,12 @@ class NotificationListener : public content::NotificationObserver {
IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, MAYBE_AutoUpdate) {
NotificationListener notification_listener;
base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
// Note: This interceptor gets requests on the IO thread.
content::URLLocalHostRequestPrepackagedInterceptor interceptor;
net::LocalHostTestURLRequestInterceptor interceptor(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
net::URLFetcher::SetEnableInterceptionForTests(true);
interceptor.SetResponseIgnoreQuery(
......@@ -368,8 +374,12 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
MAYBE_AutoUpdateDisabledExtensions) {
NotificationListener notification_listener;
base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
// Note: This interceptor gets requests on the IO thread.
content::URLLocalHostRequestPrepackagedInterceptor interceptor;
net::LocalHostTestURLRequestInterceptor interceptor(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
net::URLFetcher::SetEnableInterceptionForTests(true);
interceptor.SetResponseIgnoreQuery(
......@@ -435,7 +445,10 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, ExternalUrlUpdate) {
base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
// Note: This interceptor gets requests on the IO thread.
content::URLLocalHostRequestPrepackagedInterceptor interceptor;
net::LocalHostTestURLRequestInterceptor interceptor(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
net::URLFetcher::SetEnableInterceptionForTests(true);
interceptor.SetResponseIgnoreQuery(
......@@ -528,7 +541,10 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, ExternalPolicyRefresh) {
base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
// Note: This interceptor gets requests on the IO thread.
content::URLLocalHostRequestPrepackagedInterceptor interceptor;
net::LocalHostTestURLRequestInterceptor interceptor(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
net::URLFetcher::SetEnableInterceptionForTests(true);
interceptor.SetResponseIgnoreQuery(
......@@ -609,7 +625,10 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
ASSERT_TRUE(registry->disabled_extensions().is_empty());
// Note: This interceptor gets requests on the IO thread.
content::URLLocalHostRequestPrepackagedInterceptor interceptor;
net::LocalHostTestURLRequestInterceptor interceptor(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
net::URLFetcher::SetEnableInterceptionForTests(true);
interceptor.SetResponseIgnoreQuery(
......
......@@ -19,15 +19,16 @@
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_utils.h"
#include "content/test/net/url_request_prepackaged_interceptor.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension.h"
#include "net/url_request/test_url_request_interceptor.h"
#include "net/url_request/url_fetcher.h"
#include "sync/protocol/extension_specifics.pb.h"
#include "sync/protocol/sync.pb.h"
using content::BrowserThread;
using extensions::Extension;
using extensions::ExtensionRegistry;
using extensions::ExtensionPrefs;
......@@ -210,7 +211,10 @@ IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest,
InstallIncreasingPermissionExtensionV1();
// Note: This interceptor gets requests on the IO thread.
content::URLLocalHostRequestPrepackagedInterceptor interceptor;
net::LocalHostTestURLRequestInterceptor interceptor(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
net::URLFetcher::SetEnableInterceptionForTests(true);
interceptor.SetResponseIgnoreQuery(
GURL("http://localhost/autoupdate/updates.xml"),
......@@ -247,7 +251,10 @@ IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, RemoteInstall) {
ExtensionSyncService::Get(browser()->profile());
// Note: This interceptor gets requests on the IO thread.
content::URLLocalHostRequestPrepackagedInterceptor interceptor;
net::LocalHostTestURLRequestInterceptor interceptor(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
net::URLFetcher::SetEnableInterceptionForTests(true);
interceptor.SetResponseIgnoreQuery(
GURL("http://localhost/autoupdate/updates.xml"),
......
......@@ -7,18 +7,15 @@
#include "base/files/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/threading/sequenced_worker_pool.h"
#include "components/component_updater/crx_downloader.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/test/net/url_request_prepackaged_interceptor.h"
#include "net/base/net_errors.h"
#include "net/url_request/test_url_request_interceptor.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
using base::ContentsEqual;
namespace component_updater {
......@@ -26,7 +23,7 @@ namespace component_updater {
namespace {
// Intercepts HTTP GET requests sent to "localhost".
typedef content::URLLocalHostRequestPrepackagedInterceptor GetInterceptor;
typedef net::LocalHostTestURLRequestInterceptor GetInterceptor;
const char kTestFileName[] = "jebgalgnebhfojomionfpkfelancnnkf.crx";
......@@ -59,6 +56,8 @@ class CrxDownloaderTest : public testing::Test {
protected:
scoped_ptr<CrxDownloader> crx_downloader_;
scoped_ptr<GetInterceptor> get_interceptor_;
CrxDownloader::DownloadCallback callback_;
CrxDownloader::ProgressCallback progress_callback_;
......@@ -75,9 +74,8 @@ class CrxDownloaderTest : public testing::Test {
static const int kExpectedContext = 0xaabb;
private:
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
base::MessageLoopForIO loop_;
scoped_refptr<net::TestURLRequestContextGetter> context_;
content::TestBrowserThreadBundle thread_bundle_;
base::Closure quit_closure_;
};
......@@ -93,17 +91,16 @@ CrxDownloaderTest::CrxDownloaderTest()
crx_context_(0),
num_download_complete_calls_(0),
num_progress_calls_(0),
blocking_task_runner_(BrowserThread::GetBlockingPool()->
GetSequencedTaskRunnerWithShutdownBehavior(
BrowserThread::GetBlockingPool()->GetSequenceToken(),
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)),
context_(new net::TestURLRequestContextGetter(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))),
thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
base::MessageLoopProxy::current())) {
}
CrxDownloaderTest::~CrxDownloaderTest() {
context_ = NULL;
// The GetInterceptor requires the message loop to run to destruct correctly.
get_interceptor_.reset();
RunThreadsUntilIdle();
}
void CrxDownloaderTest::SetUp() {
......@@ -111,12 +108,16 @@ void CrxDownloaderTest::SetUp() {
download_complete_result_ = CrxDownloader::Result();
num_progress_calls_ = 0;
download_progress_result_ = CrxDownloader::Result();
crx_downloader_.reset(CrxDownloader::Create(
false, // Do not use the background downloader in these tests.
context_.get(),
blocking_task_runner_,
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)));
base::MessageLoopProxy::current(),
NULL)); // No |background_task_runner| because no background downloader.
crx_downloader_->set_progress_callback(progress_callback_);
get_interceptor_.reset(new GetInterceptor(base::MessageLoopProxy::current(),
base::MessageLoopProxy::current()));
}
void CrxDownloaderTest::TearDown() {
......@@ -179,13 +180,12 @@ TEST_F(CrxDownloaderTest, OneUrl) {
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
GetInterceptor interceptor;
interceptor.SetResponse(expected_crx_url, test_file);
get_interceptor_->SetResponse(expected_crx_url, test_file);
crx_downloader_->StartDownloadFromUrl(expected_crx_url, callback_);
RunThreads();
EXPECT_EQ(1, interceptor.GetHitCount());
EXPECT_EQ(1, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
......@@ -214,8 +214,7 @@ TEST_F(CrxDownloaderTest, MAYBE_TwoUrls) {
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
GetInterceptor interceptor;
interceptor.SetResponse(expected_crx_url, test_file);
get_interceptor_->SetResponse(expected_crx_url, test_file);
std::vector<GURL> urls;
urls.push_back(expected_crx_url);
......@@ -224,7 +223,7 @@ TEST_F(CrxDownloaderTest, MAYBE_TwoUrls) {
crx_downloader_->StartDownload(urls, callback_);
RunThreads();
EXPECT_EQ(1, interceptor.GetHitCount());
EXPECT_EQ(1, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
......@@ -246,8 +245,7 @@ TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) {
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
GetInterceptor interceptor;
interceptor.SetResponse(expected_crx_url, test_file);
get_interceptor_->SetResponse(expected_crx_url, test_file);
crx_downloader_->StartDownloadFromUrl(
GURL("http://no.such.host"
......@@ -255,7 +253,7 @@ TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) {
callback_);
RunThreads();
EXPECT_EQ(0, interceptor.GetHitCount());
EXPECT_EQ(0, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
......@@ -269,14 +267,13 @@ TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) {
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
GetInterceptor interceptor;
interceptor.SetResponse(expected_crx_url, test_file);
get_interceptor_->SetResponse(expected_crx_url, test_file);
crx_downloader_->StartDownloadFromUrl(GURL("http://localhost/no/such/file"),
callback_);
RunThreads();
EXPECT_EQ(0, interceptor.GetHitCount());
EXPECT_EQ(0, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
......@@ -296,8 +293,7 @@ TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) {
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
GetInterceptor interceptor;
interceptor.SetResponse(expected_crx_url, test_file);
get_interceptor_->SetResponse(expected_crx_url, test_file);
std::vector<GURL> urls;
urls.push_back(GURL("http://localhost/no/such/file"));
......@@ -306,7 +302,7 @@ TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) {
crx_downloader_->StartDownload(urls, callback_);
RunThreads();
EXPECT_EQ(1, interceptor.GetHitCount());
EXPECT_EQ(1, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
......@@ -329,8 +325,7 @@ TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) {
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
GetInterceptor interceptor;
interceptor.SetResponse(expected_crx_url, test_file);
get_interceptor_->SetResponse(expected_crx_url, test_file);
std::vector<GURL> urls;
urls.push_back(expected_crx_url);
......@@ -339,7 +334,7 @@ TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) {
crx_downloader_->StartDownload(urls, callback_);
RunThreads();
EXPECT_EQ(1, interceptor.GetHitCount());
EXPECT_EQ(1, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
......@@ -361,8 +356,7 @@ TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) {
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
GetInterceptor interceptor;
interceptor.SetResponse(expected_crx_url, test_file);
get_interceptor_->SetResponse(expected_crx_url, test_file);
std::vector<GURL> urls;
urls.push_back(GURL("http://localhost/no/such/file"));
......@@ -372,7 +366,7 @@ TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) {
crx_downloader_->StartDownload(urls, callback_);
RunThreads();
EXPECT_EQ(0, interceptor.GetHitCount());
EXPECT_EQ(0, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
......
......@@ -130,8 +130,6 @@
'test/net/url_request_failed_job.h',
'test/net/url_request_mock_http_job.cc',
'test/net/url_request_mock_http_job.h',
'test/net/url_request_prepackaged_interceptor.cc',
'test/net/url_request_prepackaged_interceptor.h',
'test/net/url_request_slow_download_job.cc',
'test/net/url_request_slow_download_job.h',
'test/ppapi_unittest.cc',
......
......@@ -689,6 +689,8 @@ source_set("test_support") {
"test/spawned_test_server/spawner_communicator.h",
"url_request/test_url_fetcher_factory.cc",
"url_request/test_url_fetcher_factory.h",
"url_request/test_url_request_interceptor.cc",
"url_request/test_url_request_interceptor.h",
"url_request/url_request_test_util.cc",
"url_request/url_request_test_util.h",
]
......
......@@ -966,6 +966,8 @@
'test/spawned_test_server/spawner_communicator.h',
'url_request/test_url_fetcher_factory.cc',
'url_request/test_url_fetcher_factory.h',
'url_request/test_url_request_interceptor.cc',
'url_request/test_url_request_interceptor.h',
'url_request/url_request_test_util.cc',
'url_request/url_request_test_util.h',
],
......
......@@ -2,61 +2,67 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/test/net/url_request_prepackaged_interceptor.h"
#include "net/url_request/test_url_request_interceptor.h"
#include "base/files/file_util.h"
#include "base/file_util.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/threading/thread_restrictions.h"
#include "content/public/browser/browser_thread.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_file_job.h"
#include "net/url_request/url_request_filter.h"
#include "net/url_request/url_request_interceptor.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
namespace content {
namespace net {
namespace {
class URLRequestPrepackagedJob : public net::URLRequestFileJob {
// This class is needed because net::URLRequestFileJob always returns a -1
// HTTP response status code.
class TestURLRequestJob : public net::URLRequestFileJob {
public:
URLRequestPrepackagedJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const base::FilePath& file_path)
: net::URLRequestFileJob(
request, network_delegate, file_path,
content::BrowserThread::GetBlockingPool()->
GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {}
TestURLRequestJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const base::FilePath& file_path,
const scoped_refptr<base::TaskRunner>& worker_task_runner)
: net::URLRequestFileJob(request,
network_delegate,
file_path,
worker_task_runner) {}
virtual int GetResponseCode() const OVERRIDE { return 200; }
private:
virtual ~URLRequestPrepackagedJob() {}
virtual ~TestURLRequestJob() {}
DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob);
DISALLOW_COPY_AND_ASSIGN(TestURLRequestJob);
};
} // namespace
class URLRequestPrepackagedInterceptor::Delegate
: public net::URLRequestInterceptor {
// This class handles the actual URL request interception. It may be constructed
// on any thread, but all other methods are called on the |network_task_runner|
// thread. It is destroyed by the net::URLRequestFilter singleton.
class TestURLRequestInterceptor::Delegate : public net::URLRequestInterceptor {
public:
Delegate(const std::string& scheme, const std::string& hostname)
: scheme_(scheme), hostname_(hostname), hit_count_(0) {}
Delegate(const std::string& scheme,
const std::string& hostname,
const scoped_refptr<base::TaskRunner>& network_task_runner,
const scoped_refptr<base::TaskRunner>& worker_task_runner)
: scheme_(scheme),
hostname_(hostname),
network_task_runner_(network_task_runner),
worker_task_runner_(worker_task_runner),
hit_count_(0) {}
virtual ~Delegate() {}
void Register() {
net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
scheme_, hostname_,
scoped_ptr<net::URLRequestInterceptor>(this));
scheme_, hostname_, scoped_ptr<net::URLRequestInterceptor>(this));
}
static void Unregister(
const std::string& scheme,
const std::string& hostname) {
static void Unregister(const std::string& scheme,
const std::string& hostname) {
net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme,
hostname);
}
......@@ -67,11 +73,7 @@ class URLRequestPrepackagedInterceptor::Delegate
void SetResponse(const GURL& url,
const base::FilePath& path,
bool ignore_query) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// It's ok to do a blocking disk access on this thread; this class
// is just used for tests.
base::ThreadRestrictions::ScopedAllowIO allow_io;
EXPECT_TRUE(base::PathExists(path));
DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
if (ignore_query) {
ignore_query_responses_[url] = path;
} else {
......@@ -92,7 +94,7 @@ class URLRequestPrepackagedInterceptor::Delegate
virtual net::URLRequestJob* MaybeInterceptRequest(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const OVERRIDE {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
if (request->url().scheme() != scheme_ ||
request->url().host() != hostname_) {
return NULL;
......@@ -116,14 +118,16 @@ class URLRequestPrepackagedInterceptor::Delegate
++hit_count_;
}
return new URLRequestPrepackagedJob(request,
network_delegate,
it->second);
return new TestURLRequestJob(
request, network_delegate, it->second, worker_task_runner_);
}
const std::string scheme_;
const std::string hostname_;
const scoped_refptr<base::TaskRunner> network_task_runner_;
const scoped_refptr<base::TaskRunner> worker_task_runner_;
ResponseMap responses_;
ResponseMap ignore_query_responses_;
......@@ -133,55 +137,63 @@ class URLRequestPrepackagedInterceptor::Delegate
DISALLOW_COPY_AND_ASSIGN(Delegate);
};
URLRequestPrepackagedInterceptor::URLRequestPrepackagedInterceptor(
TestURLRequestInterceptor::TestURLRequestInterceptor(
const std::string& scheme,
const std::string& hostname)
const std::string& hostname,
const scoped_refptr<base::TaskRunner>& network_task_runner,
const scoped_refptr<base::TaskRunner>& worker_task_runner)
: scheme_(scheme),
hostname_(hostname),
delegate_(new Delegate(scheme, hostname)) {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&Delegate::Register,
base::Unretained(delegate_)));
network_task_runner_(network_task_runner),
delegate_(new Delegate(scheme,
hostname,
network_task_runner_,
worker_task_runner)) {
network_task_runner_->PostTask(
FROM_HERE, base::Bind(&Delegate::Register, base::Unretained(delegate_)));
}
URLRequestPrepackagedInterceptor::~URLRequestPrepackagedInterceptor() {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&Delegate::Unregister,
scheme_,
hostname_));
TestURLRequestInterceptor::~TestURLRequestInterceptor() {
network_task_runner_->PostTask(
FROM_HERE, base::Bind(&Delegate::Unregister, scheme_, hostname_));
}
void URLRequestPrepackagedInterceptor::SetResponse(
const GURL& url,
const base::FilePath& path) {
void TestURLRequestInterceptor::SetResponse(const GURL& url,
const base::FilePath& path) {
CHECK_EQ(scheme_, url.scheme());
CHECK_EQ(hostname_, url.host());
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&Delegate::SetResponse,
base::Unretained(delegate_), url, path,
false));
network_task_runner_->PostTask(FROM_HERE,
base::Bind(&Delegate::SetResponse,
base::Unretained(delegate_),
url,
path,
false));
}
void URLRequestPrepackagedInterceptor::SetResponseIgnoreQuery(
void TestURLRequestInterceptor::SetResponseIgnoreQuery(
const GURL& url,
const base::FilePath& path) {
CHECK_EQ(scheme_, url.scheme());
CHECK_EQ(hostname_, url.host());
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&Delegate::SetResponse,
base::Unretained(delegate_), url, path,
true));
network_task_runner_->PostTask(FROM_HERE,
base::Bind(&Delegate::SetResponse,
base::Unretained(delegate_),
url,
path,
true));
}
int URLRequestPrepackagedInterceptor::GetHitCount() {
int TestURLRequestInterceptor::GetHitCount() {
return delegate_->GetHitCount();
}
URLLocalHostRequestPrepackagedInterceptor
::URLLocalHostRequestPrepackagedInterceptor()
: URLRequestPrepackagedInterceptor("http", "localhost") {
LocalHostTestURLRequestInterceptor::LocalHostTestURLRequestInterceptor(
const scoped_refptr<base::TaskRunner>& network_task_runner,
const scoped_refptr<base::TaskRunner>& worker_task_runner)
: TestURLRequestInterceptor("http",
"localhost",
network_task_runner,
worker_task_runner) {
}
} // namespace content
} // namespace net
......@@ -2,38 +2,49 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_
#define CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_
#ifndef NET_URL_REQUEST_TEST_URL_REQUEST_INTERCEPTOR_H_
#define NET_URL_REQUEST_TEST_URL_REQUEST_INTERCEPTOR_H_
#include <string>
#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
class GURL;
namespace base {
class FilePath;
class TaskRunner;
}
namespace content {
namespace net {
// Intercepts HTTP requests and gives pre-defined responses to specified URLs.
// The pre-defined responses are loaded from files on disk. The interception
// occurs while the URLRequestPrepackagedInterceptor is alive.
class URLRequestPrepackagedInterceptor {
// occurs while the TestURLRequestInterceptor is alive. This class may be
// instantiated on any thread.
class TestURLRequestInterceptor {
public:
// Registers an interceptor for urls using |scheme| and |hostname|. Urls
// Registers an interceptor for URLs using |scheme| and |hostname|. URLs
// passed to "SetResponse" are required to use |scheme| and |hostname|.
URLRequestPrepackagedInterceptor(const std::string& scheme,
const std::string& hostname);
virtual ~URLRequestPrepackagedInterceptor();
// |network_task_runner| is the task runner used for network activity
// (e.g. where URL requests are processed).
// |worker_task_runner| will be used to read the files specified by
// either SetResponse() or SetResponseIgnoreQuery() asynchronously. It
// must be a task runner allowed to perform disk IO.
TestURLRequestInterceptor(
const std::string& scheme,
const std::string& hostname,
const scoped_refptr<base::TaskRunner>& network_task_runner,
const scoped_refptr<base::TaskRunner>& worker_task_runner);
virtual ~TestURLRequestInterceptor();
// When requests for |url| arrive, respond with the contents of |path|. The
// hostname and scheme of |url| must match the corresponding parameters
// passed as constructor arguments.
void SetResponse(const GURL& url, const base::FilePath& path);
// Identical to SetResponse except that query parameters are ignored on
// Identical to SetResponse, except that query parameters are ignored on
// incoming URLs when comparing against |url|.
void SetResponseIgnoreQuery(const GURL& url, const base::FilePath& path);
......@@ -46,24 +57,28 @@ class URLRequestPrepackagedInterceptor {
const std::string scheme_;
const std::string hostname_;
// After creation, |delegate_| lives on the IO thread, and a task to delete
// it is posted from ~URLRequestPrepackagedInterceptor().
scoped_refptr<base::TaskRunner> network_task_runner_;
// After creation, |delegate_| lives on the thread of the
// |network_task_runner_|, and a task to delete it is posted from
// ~TestURLRequestInterceptor().
Delegate* delegate_;
DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedInterceptor);
DISALLOW_COPY_AND_ASSIGN(TestURLRequestInterceptor);
};
// Specialization of URLRequestPrepackagedInterceptor where scheme is "http" and
// Specialization of TestURLRequestInterceptor where scheme is "http" and
// hostname is "localhost".
class URLLocalHostRequestPrepackagedInterceptor
: public URLRequestPrepackagedInterceptor {
class LocalHostTestURLRequestInterceptor : public TestURLRequestInterceptor {
public:
URLLocalHostRequestPrepackagedInterceptor();
LocalHostTestURLRequestInterceptor(
const scoped_refptr<base::TaskRunner>& network_task_runner,
const scoped_refptr<base::TaskRunner>& worker_task_runner);
private:
DISALLOW_COPY_AND_ASSIGN(URLLocalHostRequestPrepackagedInterceptor);
DISALLOW_COPY_AND_ASSIGN(LocalHostTestURLRequestInterceptor);
};
} // namespace content
} // namespace net
#endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_
#endif // NET_URL_REQUEST_TEST_URL_REQUEST_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