Commit ea0733b1 authored by koz@chromium.org's avatar koz@chromium.org

Give URLRequestJobFactory::Interceptors the ability to specify protocols they handle.

TEST=Unit tests provided.

Review URL: http://codereview.chromium.org/7056003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86714 0039d316-1c4b-4281-b951-d872f2087c98
parent 9783c168
......@@ -155,6 +155,10 @@ class ProtocolHandlerRegistryInterceptor
return protocol_handler_registry_->MaybeCreateJob(request);
}
virtual bool WillHandleProtocol(const std::string& protocol) const {
return protocol_handler_registry_->IsHandledProtocol(protocol);
}
virtual net::URLRequestJob* MaybeInterceptRedirect(
const GURL& url, net::URLRequest* request) const OVERRIDE {
return NULL;
......
......@@ -78,6 +78,11 @@ URLRequestJob* URLRequestJobFactory::MaybeCreateJobWithProtocolHandler(
bool URLRequestJobFactory::IsHandledProtocol(const std::string& scheme) const {
DCHECK(CalledOnValidThread());
InterceptorList::const_iterator i;
for (i = interceptors_.begin(); i != interceptors_.end(); ++i) {
if ((*i)->WillHandleProtocol(scheme))
return true;
}
return ContainsKey(protocol_handler_map_, scheme) ||
URLRequestJobManager::GetInstance()->SupportsScheme(scheme);
}
......
......@@ -59,6 +59,13 @@ class NET_TEST URLRequestJobFactory
// returned.
virtual URLRequestJob* MaybeInterceptResponse(
URLRequest* request) const = 0;
// Returns true if this interceptor handles requests for URLs with the
// given protocol. Returning false does not imply that this interceptor
// can't or won't handle requests with the given protocol.
virtual bool WillHandleProtocol(const std::string& protocol) const {
return false;
}
};
URLRequestJobFactory();
......
......@@ -48,7 +48,12 @@ class DummyProtocolHandler : public URLRequestJobFactory::ProtocolHandler {
class DummyInterceptor : public URLRequestJobFactory::Interceptor {
public:
DummyInterceptor()
: did_intercept_(false),
handle_all_protocols_(false) { }
virtual URLRequestJob* MaybeIntercept(URLRequest* request) const {
did_intercept_ = true;
return new MockURLRequestJob(
request,
URLRequestStatus(URLRequestStatus::FAILED, ERR_FAILED));
......@@ -64,6 +69,14 @@ class DummyInterceptor : public URLRequestJobFactory::Interceptor {
URLRequest* /* request */) const {
return NULL;
}
virtual bool WillHandleProtocol(
const std::string& /* protocol */) const {
return handle_all_protocols_;
}
mutable bool did_intercept_;
mutable bool handle_all_protocols_;
};
TEST(URLRequestJobFactoryTest, NoProtocolHandler) {
......@@ -147,6 +160,50 @@ TEST(URLRequestJobFactoryTest, InterceptorOverridesProtocolHandler) {
EXPECT_EQ(ERR_FAILED, request.status().os_error());
}
TEST(URLRequestJobFactoryTest, InterceptorDoesntInterceptUnknownProtocols) {
TestDelegate delegate;
scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext);
URLRequestJobFactory job_factory;
request_context->set_job_factory(&job_factory);
DummyInterceptor* interceptor = new DummyInterceptor;
job_factory.AddInterceptor(new DummyInterceptor);
TestURLRequest request(GURL("foo://bar"), &delegate);
request.set_context(request_context);
request.Start();
MessageLoop::current()->Run();
EXPECT_FALSE(interceptor->did_intercept_);
}
TEST(URLRequestJobFactoryTest, InterceptorInterceptsHandledUnknownProtocols) {
TestDelegate delegate;
scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext);
URLRequestJobFactory job_factory;
request_context->set_job_factory(&job_factory);
DummyInterceptor* interceptor = new DummyInterceptor;
interceptor->handle_all_protocols_ = true;
job_factory.AddInterceptor(interceptor);
TestURLRequest request(GURL("foo://bar"), &delegate);
request.set_context(request_context);
request.Start();
MessageLoop::current()->Run();
EXPECT_TRUE(interceptor->did_intercept_);
EXPECT_EQ(URLRequestStatus::FAILED, request.status().status());
EXPECT_EQ(ERR_FAILED, request.status().os_error());
}
TEST(URLRequestJobFactoryTest, InterceptorAffectsIsHandledProtocol) {
DummyInterceptor* interceptor = new DummyInterceptor;
URLRequestJobFactory job_factory;
job_factory.AddInterceptor(interceptor);
EXPECT_FALSE(interceptor->WillHandleProtocol("anything"));
EXPECT_FALSE(job_factory.IsHandledProtocol("anything"));
interceptor->handle_all_protocols_ = true;
EXPECT_TRUE(interceptor->WillHandleProtocol("anything"));
EXPECT_TRUE(job_factory.IsHandledProtocol("anything"));
}
} // namespace
} // namespace net
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