Chromium Code Reviews| 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; |
| 38 } | |
| 37 | 39 |
| 38 virtual Value* ToValue() const OVERRIDE { | 40 // Returns event parameters for a PAC alert(). |
| 39 DictionaryValue* dict = new DictionaryValue(); | 41 Value* NetLogAlertCallback(const string16* message, |
| 40 dict->SetInteger("line_number", line_number_); | 42 NetLog::LogLevel /* log_level */) { |
| 41 dict->SetString("message", message_); | 43 DictionaryValue* dict = new DictionaryValue(); |
| 42 return dict; | 44 dict->SetString("message", *message); |
| 43 } | 45 return dict; |
| 44 | 46 } |
| 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 | 47 |
| 75 // ProxyResolverJSBindings implementation. | 48 // ProxyResolverJSBindings implementation. |
| 76 class DefaultJSBindings : public ProxyResolverJSBindings { | 49 class DefaultJSBindings : public ProxyResolverJSBindings { |
| 77 public: | 50 public: |
| 78 DefaultJSBindings(SyncHostResolver* host_resolver, | 51 DefaultJSBindings(SyncHostResolver* host_resolver, |
| 79 NetLog* net_log, | 52 NetLog* net_log, |
| 80 ProxyResolverErrorObserver* error_observer) | 53 ProxyResolverErrorObserver* error_observer) |
| 81 : host_resolver_(host_resolver), | 54 : host_resolver_(host_resolver), |
| 82 net_log_(net_log), | 55 net_log_(net_log), |
| 83 error_observer_(error_observer) { | 56 error_observer_(error_observer) { |
| 84 } | 57 } |
| 85 | 58 |
| 86 // Handler for "alert(message)". | 59 // Handler for "alert(message)". |
| 87 virtual void Alert(const string16& message) OVERRIDE { | 60 virtual void Alert(const string16& message) OVERRIDE { |
| 88 VLOG(1) << "PAC-alert: " << message; | 61 VLOG(1) << "PAC-alert: " << message; |
| 89 | 62 |
| 90 // Send to the NetLog. | 63 // Send to the NetLog. |
| 91 LogEventToCurrentRequestAndGlobally(NetLog::TYPE_PAC_JAVASCRIPT_ALERT, | 64 LogEventToCurrentRequestAndGlobally( |
| 92 new AlertNetlogParams(message)); | 65 NetLog::TYPE_PAC_JAVASCRIPT_ALERT, |
| 66 base::Bind(&NetLogAlertCallback, &message)); | |
|
eroman
2012/06/13 18:55:33
Perhaps this can just use a generic NetLogString16
mmenke
2012/06/13 20:27:58
Done.
| |
| 93 } | 67 } |
| 94 | 68 |
| 95 // Handler for "myIpAddress()". | 69 // Handler for "myIpAddress()". |
| 96 // TODO(eroman): Perhaps enumerate the interfaces directly, using | 70 // TODO(eroman): Perhaps enumerate the interfaces directly, using |
| 97 // getifaddrs(). | 71 // getifaddrs(). |
| 98 virtual bool MyIpAddress(std::string* first_ip_address) OVERRIDE { | 72 virtual bool MyIpAddress(std::string* first_ip_address) OVERRIDE { |
| 99 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, | 73 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, |
| 100 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS, | 74 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS); |
| 101 NULL); | |
| 102 | 75 |
| 103 bool ok = MyIpAddressImpl(first_ip_address); | 76 bool ok = MyIpAddressImpl(first_ip_address); |
| 104 | 77 |
| 105 LogEventToCurrentRequest(NetLog::PHASE_END, | 78 LogEventToCurrentRequest(NetLog::PHASE_END, |
| 106 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS, | 79 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS); |
| 107 NULL); | |
| 108 return ok; | 80 return ok; |
| 109 } | 81 } |
| 110 | 82 |
| 111 // Handler for "myIpAddressEx()". | 83 // Handler for "myIpAddressEx()". |
| 112 virtual bool MyIpAddressEx(std::string* ip_address_list) OVERRIDE { | 84 virtual bool MyIpAddressEx(std::string* ip_address_list) OVERRIDE { |
| 113 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, | 85 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, |
| 114 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS_EX, | 86 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS_EX); |
| 115 NULL); | |
| 116 | 87 |
| 117 bool ok = MyIpAddressExImpl(ip_address_list); | 88 bool ok = MyIpAddressExImpl(ip_address_list); |
| 118 | 89 |
| 119 LogEventToCurrentRequest(NetLog::PHASE_END, | 90 LogEventToCurrentRequest(NetLog::PHASE_END, |
| 120 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS_EX, | 91 NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS_EX); |
| 121 NULL); | |
| 122 return ok; | 92 return ok; |
| 123 } | 93 } |
| 124 | 94 |
| 125 // Handler for "dnsResolve(host)". | 95 // Handler for "dnsResolve(host)". |
| 126 virtual bool DnsResolve(const std::string& host, | 96 virtual bool DnsResolve(const std::string& host, |
| 127 std::string* first_ip_address) OVERRIDE { | 97 std::string* first_ip_address) OVERRIDE { |
| 128 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, | 98 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, |
| 129 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE, | 99 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE); |
| 130 NULL); | |
| 131 | 100 |
| 132 bool ok = DnsResolveImpl(host, first_ip_address); | 101 bool ok = DnsResolveImpl(host, first_ip_address); |
| 133 | 102 |
| 134 LogEventToCurrentRequest(NetLog::PHASE_END, | 103 LogEventToCurrentRequest(NetLog::PHASE_END, |
| 135 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE, | 104 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE); |
| 136 NULL); | |
| 137 return ok; | 105 return ok; |
| 138 } | 106 } |
| 139 | 107 |
| 140 // Handler for "dnsResolveEx(host)". | 108 // Handler for "dnsResolveEx(host)". |
| 141 virtual bool DnsResolveEx(const std::string& host, | 109 virtual bool DnsResolveEx(const std::string& host, |
| 142 std::string* ip_address_list) OVERRIDE { | 110 std::string* ip_address_list) OVERRIDE { |
| 143 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, | 111 LogEventToCurrentRequest(NetLog::PHASE_BEGIN, |
| 144 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE_EX, | 112 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE_EX); |
| 145 NULL); | |
| 146 | 113 |
| 147 bool ok = DnsResolveExImpl(host, ip_address_list); | 114 bool ok = DnsResolveExImpl(host, ip_address_list); |
| 148 | 115 |
| 149 LogEventToCurrentRequest(NetLog::PHASE_END, | 116 LogEventToCurrentRequest(NetLog::PHASE_END, |
| 150 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE_EX, | 117 NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE_EX); |
| 151 NULL); | |
| 152 return ok; | 118 return ok; |
| 153 } | 119 } |
| 154 | 120 |
| 155 // Handler for when an error is encountered. |line_number| may be -1. | 121 // Handler for when an error is encountered. |line_number| may be -1. |
| 156 virtual void OnError(int line_number, const string16& message) OVERRIDE { | 122 virtual void OnError(int line_number, const string16& message) OVERRIDE { |
| 157 // Send to the chrome log. | 123 // Send to the chrome log. |
| 158 if (line_number == -1) | 124 if (line_number == -1) |
| 159 VLOG(1) << "PAC-error: " << message; | 125 VLOG(1) << "PAC-error: " << message; |
| 160 else | 126 else |
| 161 VLOG(1) << "PAC-error: " << "line: " << line_number << ": " << message; | 127 VLOG(1) << "PAC-error: " << "line: " << line_number << ": " << message; |
| 162 | 128 |
| 163 // Send the error to the NetLog. | 129 // Send the error to the NetLog. |
| 164 LogEventToCurrentRequestAndGlobally( | 130 LogEventToCurrentRequestAndGlobally( |
| 165 NetLog::TYPE_PAC_JAVASCRIPT_ERROR, | 131 NetLog::TYPE_PAC_JAVASCRIPT_ERROR, |
| 166 new ErrorNetlogParams(line_number, message)); | 132 base::Bind(&NetLogErrorCallback, line_number, &message)); |
| 167 | 133 |
| 168 if (error_observer_.get()) | 134 if (error_observer_.get()) |
| 169 error_observer_->OnPACScriptError(line_number, message); | 135 error_observer_->OnPACScriptError(line_number, message); |
| 170 } | 136 } |
| 171 | 137 |
| 172 virtual void Shutdown() OVERRIDE { | 138 virtual void Shutdown() OVERRIDE { |
| 173 host_resolver_->Shutdown(); | 139 host_resolver_->Shutdown(); |
| 174 } | 140 } |
| 175 | 141 |
| 176 private: | 142 private: |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 | 248 |
| 283 // May return NULL. | 249 // May return NULL. |
| 284 const BoundNetLog* GetNetLogForCurrentRequest() { | 250 const BoundNetLog* GetNetLogForCurrentRequest() { |
| 285 if (!current_request_context()) | 251 if (!current_request_context()) |
| 286 return NULL; | 252 return NULL; |
| 287 return current_request_context()->net_log; | 253 return current_request_context()->net_log; |
| 288 } | 254 } |
| 289 | 255 |
| 290 void LogEventToCurrentRequest( | 256 void LogEventToCurrentRequest( |
| 291 NetLog::EventPhase phase, | 257 NetLog::EventPhase phase, |
| 258 NetLog::EventType type) { | |
| 259 const BoundNetLog* net_log = GetNetLogForCurrentRequest(); | |
| 260 if (net_log) | |
| 261 net_log->AddEntry(type, phase); | |
| 262 } | |
| 263 | |
| 264 void LogEventToCurrentRequest( | |
| 265 NetLog::EventPhase phase, | |
| 292 NetLog::EventType type, | 266 NetLog::EventType type, |
| 293 scoped_refptr<NetLog::EventParameters> params) { | 267 const NetLog::ParametersCallback& parameters_callback) { |
| 294 const BoundNetLog* net_log = GetNetLogForCurrentRequest(); | 268 const BoundNetLog* net_log = GetNetLogForCurrentRequest(); |
| 295 if (net_log) | 269 if (net_log) |
| 296 net_log->AddEntry(type, phase, params); | 270 net_log->AddEntry(type, phase, parameters_callback); |
| 297 } | 271 } |
| 298 | 272 |
| 299 void LogEventToCurrentRequestAndGlobally( | 273 void LogEventToCurrentRequestAndGlobally( |
| 300 NetLog::EventType type, | 274 NetLog::EventType type, |
| 301 scoped_refptr<NetLog::EventParameters> params) { | 275 const NetLog::ParametersCallback& parameters_callback) { |
| 302 LogEventToCurrentRequest(NetLog::PHASE_NONE, type, params); | 276 LogEventToCurrentRequest(NetLog::PHASE_NONE, type, parameters_callback); |
| 303 | 277 |
| 304 // Emit to the global NetLog event stream. | 278 // Emit to the global NetLog event stream. |
| 305 if (net_log_) | 279 if (net_log_) |
| 306 net_log_->AddGlobalEntry(type, params); | 280 net_log_->AddGlobalEntry(type, parameters_callback); |
| 307 } | 281 } |
| 308 | 282 |
| 309 scoped_ptr<SyncHostResolver> host_resolver_; | 283 scoped_ptr<SyncHostResolver> host_resolver_; |
| 310 NetLog* net_log_; | 284 NetLog* net_log_; |
| 311 scoped_ptr<ProxyResolverErrorObserver> error_observer_; | 285 scoped_ptr<ProxyResolverErrorObserver> error_observer_; |
| 312 DISALLOW_COPY_AND_ASSIGN(DefaultJSBindings); | 286 DISALLOW_COPY_AND_ASSIGN(DefaultJSBindings); |
| 313 }; | 287 }; |
| 314 | 288 |
| 315 } // namespace | 289 } // namespace |
| 316 | 290 |
| 317 // static | 291 // static |
| 318 ProxyResolverJSBindings* ProxyResolverJSBindings::CreateDefault( | 292 ProxyResolverJSBindings* ProxyResolverJSBindings::CreateDefault( |
| 319 SyncHostResolver* host_resolver, | 293 SyncHostResolver* host_resolver, |
| 320 NetLog* net_log, | 294 NetLog* net_log, |
| 321 ProxyResolverErrorObserver* error_observer) { | 295 ProxyResolverErrorObserver* error_observer) { |
| 322 return new DefaultJSBindings(host_resolver, net_log, error_observer); | 296 return new DefaultJSBindings(host_resolver, net_log, error_observer); |
| 323 } | 297 } |
| 324 | 298 |
| 325 } // namespace net | 299 } // namespace net |
| OLD | NEW |