1 // Copyright 2013 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 "remoting/base/url_request_context.h" 6 7 #include "base/message_loop/message_loop_proxy.h" 8 #include "net/cert/cert_verifier.h" 9 #include "net/dns/host_resolver.h" 10 #include "net/http/http_auth_handler_factory.h" 11 #include "net/http/http_network_layer.h" 12 #include "net/http/http_network_session.h" 13 #include "net/http/http_server_properties_impl.h" 14 #include "net/http/transport_security_state.h" 15 #include "net/proxy/proxy_config_service.h" 16 #include "net/proxy/proxy_service.h" 17 #include "net/ssl/ssl_config_service_defaults.h" 18 #include "remoting/base/vlog_net_log.h" 19 20 #if defined(OS_WIN) 21 #include "net/proxy/proxy_config_service_win.h" 22 #elif defined(OS_IOS) 23 #include "net/proxy/proxy_config_service_ios.h" 24 #elif defined(OS_MACOSX) 25 #include "net/proxy/proxy_config_service_mac.h" 26 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS) 27 #include "net/proxy/proxy_config_service_linux.h" 28 #endif 29 30 namespace remoting { 31 32 namespace { 33 34 // Config getter that always returns direct settings. 35 class ProxyConfigServiceDirect : public net::ProxyConfigService { 36 public: 37 // ProxyConfigService implementation: AddObserver(Observer * observer)38 virtual void AddObserver(Observer* observer) OVERRIDE {} RemoveObserver(Observer * observer)39 virtual void RemoveObserver(Observer* observer) OVERRIDE {} GetLatestProxyConfig(net::ProxyConfig * config)40 virtual ConfigAvailability GetLatestProxyConfig( 41 net::ProxyConfig* config) OVERRIDE { 42 *config = net::ProxyConfig::CreateDirect(); 43 return CONFIG_VALID; 44 } 45 }; 46 CreateSystemProxyConfigService(base::SingleThreadTaskRunner * io_thread_task_runner)47net::ProxyConfigService* CreateSystemProxyConfigService( 48 base::SingleThreadTaskRunner* io_thread_task_runner) { 49 #if defined(OS_WIN) 50 return new net::ProxyConfigServiceWin(); 51 #elif defined(OS_IOS) 52 return new net::ProxyConfigServiceIOS(); 53 #elif defined(OS_MACOSX) 54 return new net::ProxyConfigServiceMac(io_thread_task_runner); 55 #elif defined(OS_CHROMEOS) 56 NOTREACHED() << "ChromeOS is not a supported target for Chromoting host"; 57 return NULL; 58 #elif defined(OS_LINUX) 59 // TODO(sergeyu): Currently ProxyConfigServiceLinux depends on 60 // base::OneShotTimer that doesn't work properly on main NPAPI 61 // thread. Fix that and uncomment the code below. 62 // 63 // net::ProxyConfigServiceLinux* linux_config_service = 64 // new net::ProxyConfigServiceLinux(); 65 // linux_config_service->SetupAndFetchInitialConfig( 66 // ui_message_loop_, io_message_loop->message_loop_proxy(), 67 // file_message_loop); 68 69 // return linux_config_service; 70 return new ProxyConfigServiceDirect(); 71 #else 72 LOG(WARNING) << "Failed to choose a system proxy settings fetcher " 73 "for this platform."; 74 return new ProxyConfigServiceDirect(); 75 #endif 76 } 77 78 } // namespace 79 80 // TODO(willchan): This is largely copied from service_url_request_context.cc, 81 // which is in turn copied from some test code. Move it somewhere reusable. URLRequestContext(scoped_ptr<net::ProxyConfigService> proxy_config_service)82URLRequestContext::URLRequestContext( 83 scoped_ptr<net::ProxyConfigService> proxy_config_service) 84 : storage_(this) { 85 scoped_ptr<VlogNetLog> net_log(new VlogNetLog()); 86 storage_.set_host_resolver( 87 net::HostResolver::CreateDefaultResolver(net_log.get())); 88 storage_.set_proxy_service(net::ProxyService::CreateUsingSystemProxyResolver( 89 proxy_config_service.release(), 0u, net_log.get())); 90 storage_.set_cert_verifier(net::CertVerifier::CreateDefault()); 91 storage_.set_ssl_config_service(new net::SSLConfigServiceDefaults); 92 storage_.set_http_auth_handler_factory( 93 net::HttpAuthHandlerFactory::CreateDefault(host_resolver())); 94 storage_.set_http_server_properties( 95 scoped_ptr<net::HttpServerProperties>( 96 new net::HttpServerPropertiesImpl())); 97 storage_.set_transport_security_state(new net::TransportSecurityState); 98 99 net::HttpNetworkSession::Params session_params; 100 session_params.host_resolver = host_resolver(); 101 session_params.cert_verifier = cert_verifier(); 102 session_params.transport_security_state = transport_security_state(); 103 session_params.proxy_service = proxy_service(); 104 session_params.ssl_config_service = ssl_config_service(); 105 session_params.http_auth_handler_factory = http_auth_handler_factory(); 106 session_params.http_server_properties = http_server_properties(); 107 session_params.net_log = net_log.get(); 108 scoped_refptr<net::HttpNetworkSession> network_session( 109 new net::HttpNetworkSession(session_params)); 110 storage_.set_http_transaction_factory( 111 new net::HttpNetworkLayer(network_session.get())); 112 storage_.set_net_log(net_log.release()); 113 } 114 ~URLRequestContext()115URLRequestContext::~URLRequestContext() { 116 } 117 URLRequestContextGetter(scoped_refptr<base::SingleThreadTaskRunner> network_task_runner)118URLRequestContextGetter::URLRequestContextGetter( 119 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner) 120 : network_task_runner_(network_task_runner) { 121 proxy_config_service_.reset(CreateSystemProxyConfigService( 122 network_task_runner_.get())); 123 } 124 GetURLRequestContext()125net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { 126 if (!url_request_context_.get()) { 127 url_request_context_.reset( 128 new URLRequestContext(proxy_config_service_.Pass())); 129 } 130 return url_request_context_.get(); 131 } 132 133 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() const134URLRequestContextGetter::GetNetworkTaskRunner() const { 135 return network_task_runner_; 136 } 137 ~URLRequestContextGetter()138URLRequestContextGetter::~URLRequestContextGetter() { 139 } 140 141 } // namespace remoting 142