1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/url_request/url_request_job_factory_impl.h"
6
7#include "base/stl_util.h"
8#include "net/base/load_flags.h"
9#include "net/url_request/url_request_job_manager.h"
10#include "url/gurl.h"
11
12namespace net {
13
14URLRequestJobFactoryImpl::URLRequestJobFactoryImpl() {}
15
16URLRequestJobFactoryImpl::~URLRequestJobFactoryImpl() {
17  STLDeleteValues(&protocol_handler_map_);
18}
19
20bool URLRequestJobFactoryImpl::SetProtocolHandler(
21    const std::string& scheme,
22    ProtocolHandler* protocol_handler) {
23  DCHECK(CalledOnValidThread());
24
25  if (!protocol_handler) {
26    ProtocolHandlerMap::iterator it = protocol_handler_map_.find(scheme);
27    if (it == protocol_handler_map_.end())
28      return false;
29
30    delete it->second;
31    protocol_handler_map_.erase(it);
32    return true;
33  }
34
35  if (ContainsKey(protocol_handler_map_, scheme))
36    return false;
37  protocol_handler_map_[scheme] = protocol_handler;
38  return true;
39}
40
41URLRequestJob* URLRequestJobFactoryImpl::MaybeCreateJobWithProtocolHandler(
42    const std::string& scheme,
43    URLRequest* request,
44    NetworkDelegate* network_delegate) const {
45  DCHECK(CalledOnValidThread());
46  ProtocolHandlerMap::const_iterator it = protocol_handler_map_.find(scheme);
47  if (it == protocol_handler_map_.end())
48    return NULL;
49  return it->second->MaybeCreateJob(request, network_delegate);
50}
51
52bool URLRequestJobFactoryImpl::IsHandledProtocol(
53    const std::string& scheme) const {
54  DCHECK(CalledOnValidThread());
55  return ContainsKey(protocol_handler_map_, scheme) ||
56      URLRequestJobManager::GetInstance()->SupportsScheme(scheme);
57}
58
59bool URLRequestJobFactoryImpl::IsHandledURL(const GURL& url) const {
60  if (!url.is_valid()) {
61    // We handle error cases.
62    return true;
63  }
64  return IsHandledProtocol(url.scheme());
65}
66
67bool URLRequestJobFactoryImpl::IsSafeRedirectTarget(
68    const GURL& location) const {
69  DCHECK(CalledOnValidThread());
70  if (!location.is_valid()) {
71    // Error cases are safely handled.
72    return true;
73  }
74  ProtocolHandlerMap::const_iterator it = protocol_handler_map_.find(
75      location.scheme());
76  if (it == protocol_handler_map_.end()) {
77    // Unhandled cases are safely handled.
78    return true;
79  }
80  return it->second->IsSafeRedirectTarget(location);
81}
82
83}  // namespace net
84