| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/proxy/proxy_resolver_js_bindings.h" | 5 #include "net/proxy/proxy_resolver_js_bindings.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 7 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 9 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 10 #include "base/values.h" | 11 #include "base/values.h" |
| 11 #include "net/base/address_list.h" | 12 #include "net/base/address_list.h" |
| 12 #include "net/base/host_cache.h" | 13 #include "net/base/host_cache.h" |
| 13 #include "net/base/host_resolver.h" | 14 #include "net/base/host_resolver.h" |
| 14 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 15 #include "net/base/net_log.h" | 16 #include "net/base/net_log.h" |
| 16 #include "net/base/net_util.h" | 17 #include "net/base/net_util.h" |
| 17 #include "net/proxy/proxy_resolver_error_observer.h" | 18 #include "net/proxy/proxy_resolver_error_observer.h" |
| 18 #include "net/proxy/proxy_resolver_request_context.h" | 19 #include "net/proxy/proxy_resolver_request_context.h" |
| 19 #include "net/proxy/sync_host_resolver.h" | 20 #include "net/proxy/sync_host_resolver.h" |
| 20 | 21 |
| 21 namespace net { | 22 namespace net { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // TTL for the per-request DNS cache. Applies to both successful and failed | 26 // TTL for the per-request DNS cache. Applies to both successful and failed |
| 26 // DNS resolutions. | 27 // DNS resolutions. |
| 27 const unsigned kCacheEntryTTLSeconds = 5 * 60; | 28 const unsigned kCacheEntryTTLSeconds = 5 * 60; |
| 28 | 29 |
| 29 // Event parameters for a PAC error message (line number + message). | 30 // Returns event parameters for a PAC error message (line number + message). |
| 30 class ErrorNetlogParams : public NetLog::EventParameters { | 31 Value* NetLogErrorCallback(int line_number, |
| 31 public: | 32 const string16* message, |
| 32 ErrorNetlogParams(int line_number, | 33 NetLog::LogLevel /* log_level */) { |
| 33 const string16& message) | 34 DictionaryValue* dict = new DictionaryValue(); |
| 34 : line_number_(line_number), | 35 dict->SetInteger("line_number", line_number); |
| 35 message_(message) { | 36 dict->SetString("message", *message); |
| 36 } | 37 return dict; |
| 37 | 38 } |
| 38 virtual Value* ToValue() const OVERRIDE { | |
| 39 DictionaryValue* dict = new DictionaryValue(); | |
| 40 dict->SetInteger("line_number", line_number_); | |
| 41 dict->SetString("message", message_); | |
| 42 return dict; | |
| 43 } | |
| 44 | |
| 45 protected: | |
| 46 virtual ~ErrorNetlogParams() {} | |
| 47 | |
| 48 private: | |
| 49 const int line_number_; | |
| 50 const string16 message_; | |
| 51 | |
| 52 DISALLOW_COPY_AND_ASSIGN(ErrorNetlogParams); | |
| 53 }; | |
| 54 | |
| 55 // Event parameters for a PAC alert(). | |
| 56 class AlertNetlogParams : public NetLog::EventParameters { | |
| 57 public: | |
| 58 explicit AlertNetlogParams(const string16& message) : message_(message) {} | |
| 59 | |
| 60 virtual Value* ToValue() const OVERRIDE { | |
| 61 DictionaryValue* dict = new DictionaryValue(); | |
| 62 dict->SetString("message", message_); | |
| 63 return dict; | |
| 64 } | |
| 65 | |
| 66 protected: | |
| 67 virtual ~AlertNetlogParams() {} | |
| 68 | |
| 69 private: | |
| 70 const string16 message_; | |
| 71 | |
| 72 DISALLOW_COPY_AND_ASSIGN(AlertNetlogParams); | |
| 73 }; | |
| 74 | 39 |
| 75 // ProxyResolverJSBindings implementation. | 40 // ProxyResolverJSBindings implementation. |
| 76 class DefaultJSBindings : public ProxyResolverJSBindings { | 41 class DefaultJSBindings : public ProxyResolverJSBindings { |
| 77 public: | 42 public: |
| 78 DefaultJSBindings(SyncHostResolver* host_resolver, | 43 DefaultJSBindings(SyncHostResolver* host_resolver, |
| 79 NetLog* net_log, | 44 NetLog* net_log, |
| 80 ProxyResolverErrorObserver* error_observer) | 45 ProxyResolverErrorObserver* error_observer) |
| 81 : host_resolver_(host_resolver), | 46 : host_resolver_(host_resolver), |
| 82 net_log_(net_log), | 47 net_log_(net_log), |
| 83 error_observer_(error_observer) { | 48 error_observer_(error_observer) { |
| 84 } | 49 } |
| 85 | 50 |
| 86 // Handler for "alert(message)". | 51 // Handler for "alert(message)". |
| 87 virtual void Alert(const string16& message) OVERRIDE { | 52 virtual void Alert(const string16& message) OVERRIDE { |
| 88 VLOG(1) << "PAC-alert: " << message; | 53 VLOG(1) << "PAC-alert: " << message; |
| 89 | 54 |
| 90 // Send to the NetLog. | 55 // Send to the NetLog. |
| 91 LogEventToCurrentRequestAndGlobally(NetLog::TYPE_PAC_JAVASCRIPT_ALERT, | 56 LogEventToCurrentRequestAndGlobally( |
| 92 new AlertNetlogParams(message)); | 57 NetLog::TYPE_PAC_JAVASCRIPT_ALERT, |
| 58 NetLog::StringCallback("message", &message)); |
| 93 } | 59 } |
| 94 | 60 |
| 95 // Handler for "myIpAddress()". | 61 // Handler for "myIpAddress()". |
| 96 // TODO(eroman): Perhaps enumerate the interfaces directly, using | 62 // TODO(eroman): Perhaps enumerate the interfaces directly, using |
| 97 // getifaddrs(). | 63 // getifaddrs(). |
| 98 virtual bool MyIpAddress(std::string* first_ip_address) OVERRIDE { | 64 virtual bool MyIpAddress(std::string* first_ip_address) OVERRIDE { |
| 99 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, | 65 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, |
| 100 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS, | 66 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS); |
| 101 NULL); | |
| 102 | 67 |
| 103 bool ok = MyIpAddressImpl(first_ip_address); | 68 bool ok = MyIpAddressImpl(first_ip_address); |
| 104 | 69 |
| 105 LogEventToCurrentRequest(NetLog::PHASE_END, | 70 LogEventToCurrentRequest(NetLog::PHASE_END, |
| 106 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS, | 71 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS); |
| 107 NULL); | |
| 108 return ok; | 72 return ok; |
| 109 } | 73 } |
| 110 | 74 |
| 111 // Handler for "myIpAddressEx()". | 75 // Handler for "myIpAddressEx()". |
| 112 virtual bool MyIpAddressEx(std::string* ip_address_list) OVERRIDE { | 76 virtual bool MyIpAddressEx(std::string* ip_address_list) OVERRIDE { |
| 113 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, | 77 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, |
| 114 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS_EX, | 78 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS_EX); |
| 115 NULL); | |
| 116 | 79 |
| 117 bool ok = MyIpAddressExImpl(ip_address_list); | 80 bool ok = MyIpAddressExImpl(ip_address_list); |
| 118 | 81 |
| 119 LogEventToCurrentRequest(NetLog::PHASE_END, | 82 LogEventToCurrentRequest(NetLog::PHASE_END, |
| 120 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS_EX, | 83 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS_EX); |
| 121 NULL); | |
| 122 return ok; | 84 return ok; |
| 123 } | 85 } |
| 124 | 86 |
| 125 // Handler for "dnsResolve(host)". | 87 // Handler for "dnsResolve(host)". |
| 126 virtual bool DnsResolve(const std::string& host, | 88 virtual bool DnsResolve(const std::string& host, |
| 127 std::string* first_ip_address) OVERRIDE { | 89 std::string* first_ip_address) OVERRIDE { |
| 128 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, | 90 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, |
| 129 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE, | 91 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE); |
| 130 NULL); | |
| 131 | 92 |
| 132 bool ok = DnsResolveImpl(host, first_ip_address); | 93 bool ok = DnsResolveImpl(host, first_ip_address); |
| 133 | 94 |
| 134 LogEventToCurrentRequest(NetLog::PHASE_END, | 95 LogEventToCurrentRequest(NetLog::PHASE_END, |
| 135 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE, | 96 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE); |
| 136 NULL); | |
| 137 return ok; | 97 return ok; |
| 138 } | 98 } |
| 139 | 99 |
| 140 // Handler for "dnsResolveEx(host)". | 100 // Handler for "dnsResolveEx(host)". |
| 141 virtual bool DnsResolveEx(const std::string& host, | 101 virtual bool DnsResolveEx(const std::string& host, |
| 142 std::string* ip_address_list) OVERRIDE { | 102 std::string* ip_address_list) OVERRIDE { |
| 143 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, | 103 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, |
| 144 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE_EX, | 104 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE_EX); |
| 145 NULL); | |
| 146 | 105 |
| 147 bool ok = DnsResolveExImpl(host, ip_address_list); | 106 bool ok = DnsResolveExImpl(host, ip_address_list); |
| 148 | 107 |
| 149 LogEventToCurrentRequest(NetLog::PHASE_END, | 108 LogEventToCurrentRequest(NetLog::PHASE_END, |
| 150 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE_EX, | 109 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE_EX); |
| 151 NULL); | |
| 152 return ok; | 110 return ok; |
| 153 } | 111 } |
| 154 | 112 |
| 155 // Handler for when an error is encountered. |line_number| may be -1. | 113 // Handler for when an error is encountered. |line_number| may be -1. |
| 156 virtual void OnError(int line_number, const string16& message) OVERRIDE { | 114 virtual void OnError(int line_number, const string16& message) OVERRIDE { |
| 157 // Send to the chrome log. | 115 // Send to the chrome log. |
| 158 if (line_number == -1) | 116 if (line_number == -1) |
| 159 VLOG(1) << "PAC-error: " << message; | 117 VLOG(1) << "PAC-error: " << message; |
| 160 else | 118 else |
| 161 VLOG(1) << "PAC-error: " << "line: " << line_number << ": " << message; | 119 VLOG(1) << "PAC-error: " << "line: " << line_number << ": " << message; |
| 162 | 120 |
| 163 // Send the error to the NetLog. | 121 // Send the error to the NetLog. |
| 164 LogEventToCurrentRequestAndGlobally( | 122 LogEventToCurrentRequestAndGlobally( |
| 165 NetLog::TYPE_PAC_JAVASCRIPT_ERROR, | 123 NetLog::TYPE_PAC_JAVASCRIPT_ERROR, |
| 166 new ErrorNetlogParams(line_number, message)); | 124 base::Bind(&NetLogErrorCallback, line_number, &message)); |
| 167 | 125 |
| 168 if (error_observer_.get()) | 126 if (error_observer_.get()) |
| 169 error_observer_->OnPACScriptError(line_number, message); | 127 error_observer_->OnPACScriptError(line_number, message); |
| 170 } | 128 } |
| 171 | 129 |
| 172 virtual void Shutdown() OVERRIDE { | 130 virtual void Shutdown() OVERRIDE { |
| 173 host_resolver_->Shutdown(); | 131 host_resolver_->Shutdown(); |
| 174 } | 132 } |
| 175 | 133 |
| 176 private: | 134 private: |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 | 240 |
| 283 // May return NULL. | 241 // May return NULL. |
| 284 const BoundNetLog* GetNetLogForCurrentRequest() { | 242 const BoundNetLog* GetNetLogForCurrentRequest() { |
| 285 if (!current_request_context()) | 243 if (!current_request_context()) |
| 286 return NULL; | 244 return NULL; |
| 287 return current_request_context()->net_log; | 245 return current_request_context()->net_log; |
| 288 } | 246 } |
| 289 | 247 |
| 290 void LogEventToCurrentRequest( | 248 void LogEventToCurrentRequest( |
| 291 NetLog::EventPhase phase, | 249 NetLog::EventPhase phase, |
| 250 NetLog::EventType type) { |
| 251 const BoundNetLog* net_log = GetNetLogForCurrentRequest(); |
| 252 if (net_log) |
| 253 net_log->AddEntry(type, phase); |
| 254 } |
| 255 |
| 256 void LogEventToCurrentRequest( |
| 257 NetLog::EventPhase phase, |
| 292 NetLog::EventType type, | 258 NetLog::EventType type, |
| 293 scoped_refptr<NetLog::EventParameters> params) { | 259 const NetLog::ParametersCallback& parameters_callback) { |
| 294 const BoundNetLog* net_log = GetNetLogForCurrentRequest(); | 260 const BoundNetLog* net_log = GetNetLogForCurrentRequest(); |
| 295 if (net_log) | 261 if (net_log) |
| 296 net_log->AddEntry(type, phase, params); | 262 net_log->AddEntry(type, phase, parameters_callback); |
| 297 } | 263 } |
| 298 | 264 |
| 299 void LogEventToCurrentRequestAndGlobally( | 265 void LogEventToCurrentRequestAndGlobally( |
| 300 NetLog::EventType type, | 266 NetLog::EventType type, |
| 301 scoped_refptr<NetLog::EventParameters> params) { | 267 const NetLog::ParametersCallback& parameters_callback) { |
| 302 LogEventToCurrentRequest(NetLog::PHASE_NONE, type, params); | 268 LogEventToCurrentRequest(NetLog::PHASE_NONE, type, parameters_callback); |
| 303 | 269 |
| 304 // Emit to the global NetLog event stream. | 270 // Emit to the global NetLog event stream. |
| 305 if (net_log_) | 271 if (net_log_) |
| 306 net_log_->AddGlobalEntry(type, params); | 272 net_log_->AddGlobalEntry(type, parameters_callback); |
| 307 } | 273 } |
| 308 | 274 |
| 309 scoped_ptr<SyncHostResolver> host_resolver_; | 275 scoped_ptr<SyncHostResolver> host_resolver_; |
| 310 NetLog* net_log_; | 276 NetLog* net_log_; |
| 311 scoped_ptr<ProxyResolverErrorObserver> error_observer_; | 277 scoped_ptr<ProxyResolverErrorObserver> error_observer_; |
| 312 DISALLOW_COPY_AND_ASSIGN(DefaultJSBindings); | 278 DISALLOW_COPY_AND_ASSIGN(DefaultJSBindings); |
| 313 }; | 279 }; |
| 314 | 280 |
| 315 } // namespace | 281 } // namespace |
| 316 | 282 |
| 317 // static | 283 // static |
| 318 ProxyResolverJSBindings* ProxyResolverJSBindings::CreateDefault( | 284 ProxyResolverJSBindings* ProxyResolverJSBindings::CreateDefault( |
| 319 SyncHostResolver* host_resolver, | 285 SyncHostResolver* host_resolver, |
| 320 NetLog* net_log, | 286 NetLog* net_log, |
| 321 ProxyResolverErrorObserver* error_observer) { | 287 ProxyResolverErrorObserver* error_observer) { |
| 322 return new DefaultJSBindings(host_resolver, net_log, error_observer); | 288 return new DefaultJSBindings(host_resolver, net_log, error_observer); |
| 323 } | 289 } |
| 324 | 290 |
| 325 } // namespace net | 291 } // namespace net |
| OLD | NEW |