This source file includes following definitions.
- MaybeCreateJob
- Factory
- GetInstance
- AddHostnameHandler
- AddHostnameProtocolHandler
- RemoveHostnameHandler
- AddUrlHandler
- AddUrlProtocolHandler
- RemoveUrlHandler
- ClearHandlers
- FindRequestHandler
#include "net/url_request/url_request_filter.h"
#include <set>
#include "base/logging.h"
#include "base/stl_util.h"
namespace net {
namespace {
class URLRequestFilterProtocolHandler
: public URLRequestJobFactory::ProtocolHandler {
public:
explicit URLRequestFilterProtocolHandler(URLRequest::ProtocolFactory* factory)
: factory_(factory) {}
virtual ~URLRequestFilterProtocolHandler() {}
virtual URLRequestJob* MaybeCreateJob(
URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE {
return factory_(request, network_delegate, request->url().scheme());
}
private:
URLRequest::ProtocolFactory* factory_;
DISALLOW_COPY_AND_ASSIGN(URLRequestFilterProtocolHandler);
};
}
URLRequestFilter* URLRequestFilter::shared_instance_ = NULL;
URLRequestFilter::~URLRequestFilter() {}
URLRequestJob* URLRequestFilter::Factory(URLRequest* request,
NetworkDelegate* network_delegate,
const std::string& scheme) {
return GetInstance()->FindRequestHandler(request, network_delegate, scheme);
}
URLRequestFilter* URLRequestFilter::GetInstance() {
if (!shared_instance_)
shared_instance_ = new URLRequestFilter;
return shared_instance_;
}
void URLRequestFilter::AddHostnameHandler(const std::string& scheme,
const std::string& hostname, URLRequest::ProtocolFactory* factory) {
AddHostnameProtocolHandler(
scheme, hostname,
scoped_ptr<URLRequestJobFactory::ProtocolHandler>(
new URLRequestFilterProtocolHandler(factory)));
}
void URLRequestFilter::AddHostnameProtocolHandler(
const std::string& scheme,
const std::string& hostname,
scoped_ptr<URLRequestJobFactory::ProtocolHandler> protocol_handler) {
DCHECK_EQ(0u, hostname_handler_map_.count(make_pair(scheme, hostname)));
hostname_handler_map_[make_pair(scheme, hostname)] =
protocol_handler.release();
URLRequest::Deprecated::RegisterProtocolFactory(
scheme, &URLRequestFilter::Factory);
#ifndef NDEBUG
for (UrlHandlerMap::const_iterator i = url_handler_map_.begin();
i != url_handler_map_.end(); ++i) {
const GURL& url = GURL(i->first);
HostnameHandlerMap::iterator host_it =
hostname_handler_map_.find(make_pair(url.scheme(), url.host()));
if (host_it != hostname_handler_map_.end())
NOTREACHED();
}
#endif
}
void URLRequestFilter::RemoveHostnameHandler(const std::string& scheme,
const std::string& hostname) {
HostnameHandlerMap::iterator iter =
hostname_handler_map_.find(make_pair(scheme, hostname));
DCHECK(iter != hostname_handler_map_.end());
delete iter->second;
hostname_handler_map_.erase(iter);
}
bool URLRequestFilter::AddUrlHandler(
const GURL& url,
URLRequest::ProtocolFactory* factory) {
return AddUrlProtocolHandler(
url,
scoped_ptr<URLRequestJobFactory::ProtocolHandler>(
new URLRequestFilterProtocolHandler(factory)));
}
bool URLRequestFilter::AddUrlProtocolHandler(
const GURL& url,
scoped_ptr<URLRequestJobFactory::ProtocolHandler> protocol_handler) {
if (!url.is_valid())
return false;
DCHECK_EQ(0u, url_handler_map_.count(url.spec()));
url_handler_map_[url.spec()] = protocol_handler.release();
URLRequest::Deprecated::RegisterProtocolFactory(url.scheme(),
&URLRequestFilter::Factory);
DCHECK_EQ(0u, hostname_handler_map_.count(make_pair(url.scheme(),
url.host())));
return true;
}
void URLRequestFilter::RemoveUrlHandler(const GURL& url) {
UrlHandlerMap::iterator iter = url_handler_map_.find(url.spec());
DCHECK(iter != url_handler_map_.end());
delete iter->second;
url_handler_map_.erase(iter);
}
void URLRequestFilter::ClearHandlers() {
std::set<std::string> schemes;
for (UrlHandlerMap::const_iterator i = url_handler_map_.begin();
i != url_handler_map_.end(); ++i) {
schemes.insert(GURL(i->first).scheme());
}
for (HostnameHandlerMap::const_iterator i = hostname_handler_map_.begin();
i != hostname_handler_map_.end(); ++i) {
schemes.insert(i->first.first);
}
for (std::set<std::string>::const_iterator scheme = schemes.begin();
scheme != schemes.end(); ++scheme) {
URLRequest::Deprecated::RegisterProtocolFactory(*scheme, NULL);
}
STLDeleteValues(&url_handler_map_);
STLDeleteValues(&hostname_handler_map_);
hit_count_ = 0;
}
URLRequestFilter::URLRequestFilter() : hit_count_(0) { }
URLRequestJob* URLRequestFilter::FindRequestHandler(
URLRequest* request,
NetworkDelegate* network_delegate,
const std::string& scheme) {
URLRequestJob* job = NULL;
if (request->url().is_valid()) {
const std::string& hostname = request->url().host();
HostnameHandlerMap::iterator i =
hostname_handler_map_.find(make_pair(scheme, hostname));
if (i != hostname_handler_map_.end())
job = i->second->MaybeCreateJob(request, network_delegate);
if (!job) {
const std::string& url = request->url().spec();
UrlHandlerMap::iterator i = url_handler_map_.find(url);
if (i != url_handler_map_.end())
job = i->second->MaybeCreateJob(request, network_delegate);
}
}
if (job) {
DVLOG(1) << "URLRequestFilter hit for " << request->url().spec();
hit_count_++;
}
return job;
}
}