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
[email protected]
Review URL: https://codereview.chromium.org/508473002
Cr-Commit-Position: refs/heads/master@{#293861}
diff --git a/chrome/browser/component_updater/test/component_updater_service_unittest.cc b/chrome/browser/component_updater/test/component_updater_service_unittest.cc
index 7a18698..13e16d17 100644
--- a/chrome/browser/component_updater/test/component_updater_service_unittest.cc
+++ b/chrome/browser/component_updater/test/component_updater_service_unittest.cc
@@ -66,7 +66,10 @@
}
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();
diff --git a/chrome/browser/component_updater/test/component_updater_service_unittest.h b/chrome/browser/component_updater/test/component_updater_service_unittest.h
index d40bd05..eef15de 100644
--- a/chrome/browser/component_updater/test/component_updater_service_unittest.h
+++ b/chrome/browser/component_updater/test/component_updater_service_unittest.h
@@ -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 @@
class TestInstaller;
// Intercepts HTTP GET requests sent to "localhost".
-typedef content::URLLocalHostRequestPrepackagedInterceptor GetInterceptor;
+typedef net::LocalHostTestURLRequestInterceptor GetInterceptor;
class ComponentUpdaterTest : public testing::Test {
public:
diff --git a/chrome/browser/extensions/api/image_writer_private/write_from_url_operation_unittest.cc b/chrome/browser/extensions/api/image_writer_private/write_from_url_operation_unittest.cc
index fdfc3dc..7be001ce 100644
--- a/chrome/browser/extensions/api/image_writer_private/write_from_url_operation_unittest.cc
+++ b/chrome/browser/extensions/api/image_writer_private/write_from_url_operation_unittest.cc
@@ -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 {
+using content::BrowserThread;
using testing::_;
using testing::AnyNumber;
using testing::AtLeast;
@@ -22,7 +24,7 @@
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 @@
// 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());
}
diff --git a/chrome/browser/extensions/api/management/management_browsertest.cc b/chrome/browser/extensions/api/management/management_browsertest.cc
index e793265..4075f133 100644
--- a/chrome/browser/extensions/api/management/management_browsertest.cc
+++ b/chrome/browser/extensions/api/management/management_browsertest.cc
@@ -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 @@
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 @@
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 @@
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 @@
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 @@
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(
diff --git a/chrome/browser/extensions/extension_disabled_ui_browsertest.cc b/chrome/browser/extensions/extension_disabled_ui_browsertest.cc
index 4a523e7f..a125a8b 100644
--- a/chrome/browser/extensions/extension_disabled_ui_browsertest.cc
+++ b/chrome/browser/extensions/extension_disabled_ui_browsertest.cc
@@ -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 @@
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 @@
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"),
diff --git a/components/component_updater/test/crx_downloader_unittest.cc b/components/component_updater/test/crx_downloader_unittest.cc
index 7439db1..af71b5e 100644
--- a/components/component_updater/test/crx_downloader_unittest.cc
+++ b/components/component_updater/test/crx_downloader_unittest.cc
@@ -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 {
// Intercepts HTTP GET requests sent to "localhost".
-typedef content::URLLocalHostRequestPrepackagedInterceptor GetInterceptor;
+typedef net::LocalHostTestURLRequestInterceptor GetInterceptor;
const char kTestFileName[] = "jebgalgnebhfojomionfpkfelancnnkf.crx";
@@ -59,6 +56,8 @@
protected:
scoped_ptr<CrxDownloader> crx_downloader_;
+ scoped_ptr<GetInterceptor> get_interceptor_;
+
CrxDownloader::DownloadCallback callback_;
CrxDownloader::ProgressCallback progress_callback_;
@@ -75,9 +74,8 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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_);
diff --git a/content/content_tests.gypi b/content/content_tests.gypi
index 0151370..df0501a 100644
--- a/content/content_tests.gypi
+++ b/content/content_tests.gypi
@@ -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',
diff --git a/content/test/net/url_request_prepackaged_interceptor.cc b/content/test/net/url_request_prepackaged_interceptor.cc
deleted file mode 100644
index 95c4b03..0000000
--- a/content/test/net/url_request_prepackaged_interceptor.cc
+++ /dev/null
@@ -1,187 +0,0 @@
-// Copyright (c) 2012 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/test/net/url_request_prepackaged_interceptor.h"
-
-#include "base/files/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 {
-
-class URLRequestPrepackagedJob : 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)) {}
-
- virtual int GetResponseCode() const OVERRIDE { return 200; }
-
- private:
- virtual ~URLRequestPrepackagedJob() {}
-
- DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob);
-};
-
-} // namespace
-
-class URLRequestPrepackagedInterceptor::Delegate
- : public net::URLRequestInterceptor {
- public:
- Delegate(const std::string& scheme, const std::string& hostname)
- : scheme_(scheme), hostname_(hostname), hit_count_(0) {}
- virtual ~Delegate() {}
-
- void Register() {
- net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
- scheme_, hostname_,
- scoped_ptr<net::URLRequestInterceptor>(this));
- }
-
- static void Unregister(
- const std::string& scheme,
- const std::string& hostname) {
- net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme,
- hostname);
- }
-
- // 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,
- 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));
- if (ignore_query) {
- ignore_query_responses_[url] = path;
- } else {
- responses_[url] = path;
- }
- }
-
- // Returns how many requests have been issued that have a stored reply.
- int GetHitCount() const {
- base::AutoLock auto_lock(hit_count_lock_);
- return hit_count_;
- }
-
- private:
- typedef std::map<GURL, base::FilePath> ResponseMap;
-
- // When computing matches, this ignores the query parameters of the url.
- virtual net::URLRequestJob* MaybeInterceptRequest(
- net::URLRequest* request,
- net::NetworkDelegate* network_delegate) const OVERRIDE {
- CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
- if (request->url().scheme() != scheme_ ||
- request->url().host() != hostname_) {
- return NULL;
- }
-
- ResponseMap::const_iterator it = responses_.find(request->url());
- if (it == responses_.end()) {
- // Search for this request's url, ignoring any query parameters.
- GURL url = request->url();
- if (url.has_query()) {
- GURL::Replacements replacements;
- replacements.ClearQuery();
- url = url.ReplaceComponents(replacements);
- }
- it = ignore_query_responses_.find(url);
- if (it == ignore_query_responses_.end())
- return NULL;
- }
- {
- base::AutoLock auto_lock(hit_count_lock_);
- ++hit_count_;
- }
-
- return new URLRequestPrepackagedJob(request,
- network_delegate,
- it->second);
- }
-
- const std::string scheme_;
- const std::string hostname_;
-
- ResponseMap responses_;
- ResponseMap ignore_query_responses_;
-
- mutable base::Lock hit_count_lock_;
- mutable int hit_count_;
-
- DISALLOW_COPY_AND_ASSIGN(Delegate);
-};
-
-
-URLRequestPrepackagedInterceptor::URLRequestPrepackagedInterceptor(
- const std::string& scheme,
- const std::string& hostname)
- : scheme_(scheme),
- hostname_(hostname),
- delegate_(new Delegate(scheme, hostname)) {
- BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
- base::Bind(&Delegate::Register,
- base::Unretained(delegate_)));
-}
-
-URLRequestPrepackagedInterceptor::~URLRequestPrepackagedInterceptor() {
- BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
- base::Bind(&Delegate::Unregister,
- scheme_,
- hostname_));
-}
-
-void URLRequestPrepackagedInterceptor::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));
-}
-
-void URLRequestPrepackagedInterceptor::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));
-}
-
-int URLRequestPrepackagedInterceptor::GetHitCount() {
- return delegate_->GetHitCount();
-}
-
-
-URLLocalHostRequestPrepackagedInterceptor
- ::URLLocalHostRequestPrepackagedInterceptor()
- : URLRequestPrepackagedInterceptor("http", "localhost") {
-}
-
-} // namespace content
diff --git a/content/test/net/url_request_prepackaged_interceptor.h b/content/test/net/url_request_prepackaged_interceptor.h
deleted file mode 100644
index cc5736d..0000000
--- a/content/test/net/url_request_prepackaged_interceptor.h
+++ /dev/null
@@ -1,69 +0,0 @@
-// Copyright (c) 2012 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 CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_
-#define CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_
-
-#include <string>
-
-#include "base/basictypes.h"
-
-class GURL;
-
-namespace base {
-class FilePath;
-}
-
-namespace content {
-
-// 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 {
- public:
- // 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();
-
- // 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
- // incoming URLs when comparing against |url|.
- void SetResponseIgnoreQuery(const GURL& url, const base::FilePath& path);
-
- // Returns how many requests have been issued that have a stored reply.
- int GetHitCount();
-
- private:
- class Delegate;
-
- 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().
- Delegate* delegate_;
-
- DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedInterceptor);
-};
-
-// Specialization of URLRequestPrepackagedInterceptor where scheme is "http" and
-// hostname is "localhost".
-class URLLocalHostRequestPrepackagedInterceptor
- : public URLRequestPrepackagedInterceptor {
- public:
- URLLocalHostRequestPrepackagedInterceptor();
-
- private:
- DISALLOW_COPY_AND_ASSIGN(URLLocalHostRequestPrepackagedInterceptor);
-};
-
-} // namespace content
-
-#endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_
diff --git a/net/BUILD.gn b/net/BUILD.gn
index 94da4dd..c461a0f 100644
--- a/net/BUILD.gn
+++ b/net/BUILD.gn
@@ -689,6 +689,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",
]
diff --git a/net/net.gyp b/net/net.gyp
index 2a15488..4a1cfea 100644
--- a/net/net.gyp
+++ b/net/net.gyp
@@ -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',
],
diff --git a/net/url_request/test_url_request_interceptor.cc b/net/url_request/test_url_request_interceptor.cc
new file mode 100644
index 0000000..2c738f4
--- /dev/null
+++ b/net/url_request/test_url_request_interceptor.cc
@@ -0,0 +1,199 @@
+// Copyright (c) 2012 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/url_request/test_url_request_interceptor.h"
+
+#include "base/file_util.h"
+#include "base/threading/sequenced_worker_pool.h"
+#include "base/threading/thread_restrictions.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"
+
+namespace net {
+
+namespace {
+
+// This class is needed because net::URLRequestFileJob always returns a -1
+// HTTP response status code.
+class TestURLRequestJob : public net::URLRequestFileJob {
+ public:
+ 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 ~TestURLRequestJob() {}
+
+ DISALLOW_COPY_AND_ASSIGN(TestURLRequestJob);
+};
+
+} // namespace
+
+// 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,
+ 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));
+ }
+
+ static void Unregister(const std::string& scheme,
+ const std::string& hostname) {
+ net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme,
+ hostname);
+ }
+
+ // 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,
+ bool ignore_query) {
+ DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
+ if (ignore_query) {
+ ignore_query_responses_[url] = path;
+ } else {
+ responses_[url] = path;
+ }
+ }
+
+ // Returns how many requests have been issued that have a stored reply.
+ int GetHitCount() const {
+ base::AutoLock auto_lock(hit_count_lock_);
+ return hit_count_;
+ }
+
+ private:
+ typedef std::map<GURL, base::FilePath> ResponseMap;
+
+ // When computing matches, this ignores the query parameters of the url.
+ virtual net::URLRequestJob* MaybeInterceptRequest(
+ net::URLRequest* request,
+ net::NetworkDelegate* network_delegate) const OVERRIDE {
+ DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
+ if (request->url().scheme() != scheme_ ||
+ request->url().host() != hostname_) {
+ return NULL;
+ }
+
+ ResponseMap::const_iterator it = responses_.find(request->url());
+ if (it == responses_.end()) {
+ // Search for this request's url, ignoring any query parameters.
+ GURL url = request->url();
+ if (url.has_query()) {
+ GURL::Replacements replacements;
+ replacements.ClearQuery();
+ url = url.ReplaceComponents(replacements);
+ }
+ it = ignore_query_responses_.find(url);
+ if (it == ignore_query_responses_.end())
+ return NULL;
+ }
+ {
+ base::AutoLock auto_lock(hit_count_lock_);
+ ++hit_count_;
+ }
+
+ 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_;
+
+ mutable base::Lock hit_count_lock_;
+ mutable int hit_count_;
+
+ DISALLOW_COPY_AND_ASSIGN(Delegate);
+};
+
+TestURLRequestInterceptor::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)
+ : scheme_(scheme),
+ hostname_(hostname),
+ 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_)));
+}
+
+TestURLRequestInterceptor::~TestURLRequestInterceptor() {
+ network_task_runner_->PostTask(
+ FROM_HERE, base::Bind(&Delegate::Unregister, scheme_, hostname_));
+}
+
+void TestURLRequestInterceptor::SetResponse(const GURL& url,
+ const base::FilePath& path) {
+ CHECK_EQ(scheme_, url.scheme());
+ CHECK_EQ(hostname_, url.host());
+ network_task_runner_->PostTask(FROM_HERE,
+ base::Bind(&Delegate::SetResponse,
+ base::Unretained(delegate_),
+ url,
+ path,
+ false));
+}
+
+void TestURLRequestInterceptor::SetResponseIgnoreQuery(
+ const GURL& url,
+ const base::FilePath& path) {
+ CHECK_EQ(scheme_, url.scheme());
+ CHECK_EQ(hostname_, url.host());
+ network_task_runner_->PostTask(FROM_HERE,
+ base::Bind(&Delegate::SetResponse,
+ base::Unretained(delegate_),
+ url,
+ path,
+ true));
+}
+
+int TestURLRequestInterceptor::GetHitCount() {
+ return delegate_->GetHitCount();
+}
+
+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 net
diff --git a/net/url_request/test_url_request_interceptor.h b/net/url_request/test_url_request_interceptor.h
new file mode 100644
index 0000000..4b084c5
--- /dev/null
+++ b/net/url_request/test_url_request_interceptor.h
@@ -0,0 +1,84 @@
+// Copyright (c) 2012 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 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 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 TestURLRequestInterceptor is alive. This class may be
+// instantiated on any thread.
+class TestURLRequestInterceptor {
+ public:
+ // Registers an interceptor for URLs using |scheme| and |hostname|. URLs
+ // passed to "SetResponse" are required to use |scheme| and |hostname|.
+ // |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
+ // incoming URLs when comparing against |url|.
+ void SetResponseIgnoreQuery(const GURL& url, const base::FilePath& path);
+
+ // Returns how many requests have been issued that have a stored reply.
+ int GetHitCount();
+
+ private:
+ class Delegate;
+
+ const std::string scheme_;
+ const std::string hostname_;
+
+ 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(TestURLRequestInterceptor);
+};
+
+// Specialization of TestURLRequestInterceptor where scheme is "http" and
+// hostname is "localhost".
+class LocalHostTestURLRequestInterceptor : public TestURLRequestInterceptor {
+ public:
+ LocalHostTestURLRequestInterceptor(
+ const scoped_refptr<base::TaskRunner>& network_task_runner,
+ const scoped_refptr<base::TaskRunner>& worker_task_runner);
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(LocalHostTestURLRequestInterceptor);
+};
+
+} // namespace net
+
+#endif // NET_URL_REQUEST_TEST_URL_REQUEST_INTERCEPTOR_H_