OLD | NEW |
| (Empty) |
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_interceptor.h" | |
10 #include "net/url_request/url_request_job_manager.h" | |
11 #include "url/gurl.h" | |
12 | |
13 namespace net { | |
14 | |
15 namespace { | |
16 | |
17 URLRequestInterceptor* g_interceptor_for_testing = NULL; | |
18 | |
19 } // namespace | |
20 | |
21 URLRequestJobFactoryImpl::URLRequestJobFactoryImpl() {} | |
22 | |
23 URLRequestJobFactoryImpl::~URLRequestJobFactoryImpl() { | |
24 STLDeleteValues(&protocol_handler_map_); | |
25 } | |
26 | |
27 bool URLRequestJobFactoryImpl::SetProtocolHandler( | |
28 const std::string& scheme, | |
29 ProtocolHandler* protocol_handler) { | |
30 DCHECK(CalledOnValidThread()); | |
31 | |
32 if (!protocol_handler) { | |
33 ProtocolHandlerMap::iterator it = protocol_handler_map_.find(scheme); | |
34 if (it == protocol_handler_map_.end()) | |
35 return false; | |
36 | |
37 delete it->second; | |
38 protocol_handler_map_.erase(it); | |
39 return true; | |
40 } | |
41 | |
42 if (ContainsKey(protocol_handler_map_, scheme)) | |
43 return false; | |
44 protocol_handler_map_[scheme] = protocol_handler; | |
45 return true; | |
46 } | |
47 | |
48 URLRequestJob* URLRequestJobFactoryImpl::MaybeCreateJobWithProtocolHandler( | |
49 const std::string& scheme, | |
50 URLRequest* request, | |
51 NetworkDelegate* network_delegate) const { | |
52 DCHECK(CalledOnValidThread()); | |
53 if (g_interceptor_for_testing) { | |
54 URLRequestJob* job = g_interceptor_for_testing->MaybeInterceptRequest( | |
55 request, network_delegate); | |
56 if (job) | |
57 return job; | |
58 } | |
59 | |
60 ProtocolHandlerMap::const_iterator it = protocol_handler_map_.find(scheme); | |
61 if (it == protocol_handler_map_.end()) | |
62 return NULL; | |
63 return it->second->MaybeCreateJob(request, network_delegate); | |
64 } | |
65 | |
66 URLRequestJob* URLRequestJobFactoryImpl::MaybeInterceptRedirect( | |
67 URLRequest* request, | |
68 NetworkDelegate* network_delegate, | |
69 const GURL& location) const { | |
70 return nullptr; | |
71 } | |
72 | |
73 URLRequestJob* URLRequestJobFactoryImpl::MaybeInterceptResponse( | |
74 URLRequest* request, | |
75 NetworkDelegate* network_delegate) const { | |
76 return nullptr; | |
77 } | |
78 | |
79 bool URLRequestJobFactoryImpl::IsHandledProtocol( | |
80 const std::string& scheme) const { | |
81 DCHECK(CalledOnValidThread()); | |
82 return ContainsKey(protocol_handler_map_, scheme) || | |
83 URLRequestJobManager::GetInstance()->SupportsScheme(scheme); | |
84 } | |
85 | |
86 bool URLRequestJobFactoryImpl::IsHandledURL(const GURL& url) const { | |
87 if (!url.is_valid()) { | |
88 // We handle error cases. | |
89 return true; | |
90 } | |
91 return IsHandledProtocol(url.scheme()); | |
92 } | |
93 | |
94 bool URLRequestJobFactoryImpl::IsSafeRedirectTarget( | |
95 const GURL& location) const { | |
96 DCHECK(CalledOnValidThread()); | |
97 if (!location.is_valid()) { | |
98 // Error cases are safely handled. | |
99 return true; | |
100 } | |
101 ProtocolHandlerMap::const_iterator it = protocol_handler_map_.find( | |
102 location.scheme()); | |
103 if (it == protocol_handler_map_.end()) { | |
104 // Unhandled cases are safely handled. | |
105 return true; | |
106 } | |
107 return it->second->IsSafeRedirectTarget(location); | |
108 } | |
109 | |
110 // static | |
111 void URLRequestJobFactoryImpl::SetInterceptorForTesting( | |
112 URLRequestInterceptor* interceptor) { | |
113 DCHECK(!interceptor || !g_interceptor_for_testing); | |
114 | |
115 g_interceptor_for_testing = interceptor; | |
116 } | |
117 | |
118 } // namespace net | |
OLD | NEW |