proxy_string_util_mac.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 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. #include "net/base/proxy_string_util.h"
  5. #include <CoreFoundation/CoreFoundation.h>
  6. #include <string>
  7. #include "base/logging.h"
  8. #include "base/mac/foundation_util.h"
  9. #include "base/strings/sys_string_conversions.h"
  10. #include "net/base/host_port_pair.h"
  11. #include "net/base/proxy_server.h"
  12. namespace net {
  13. ProxyServer ProxyDictionaryToProxyServer(ProxyServer::Scheme scheme,
  14. CFDictionaryRef dict,
  15. CFStringRef host_key,
  16. CFStringRef port_key) {
  17. if (scheme == ProxyServer::SCHEME_INVALID ||
  18. scheme == ProxyServer::SCHEME_DIRECT) {
  19. // No hostname port to extract; we are done.
  20. return ProxyServer(scheme, HostPortPair());
  21. }
  22. CFStringRef host_ref =
  23. base::mac::GetValueFromDictionary<CFStringRef>(dict, host_key);
  24. if (!host_ref) {
  25. LOG(WARNING) << "Could not find expected key "
  26. << base::SysCFStringRefToUTF8(host_key)
  27. << " in the proxy dictionary";
  28. return ProxyServer(); // Invalid.
  29. }
  30. std::string host = base::SysCFStringRefToUTF8(host_ref);
  31. CFNumberRef port_ref =
  32. base::mac::GetValueFromDictionary<CFNumberRef>(dict, port_key);
  33. int port;
  34. if (port_ref) {
  35. CFNumberGetValue(port_ref, kCFNumberIntType, &port);
  36. } else {
  37. port = ProxyServer::GetDefaultPortForScheme(scheme);
  38. }
  39. return ProxyServer(scheme, HostPortPair(host, port));
  40. }
  41. } // namespace net