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
3 // found in the LICENSE file.
5 #include "net/proxy/proxy_service_v8.h"
7 #include "base/logging.h"
8 #include "net/proxy/multi_threaded_proxy_resolver.h"
9 #include "net/proxy/network_delegate_error_observer.h"
10 #include "net/proxy/proxy_resolver.h"
11 #include "net/proxy/proxy_resolver_js_bindings.h"
12 #include "net/proxy/proxy_resolver_v8.h"
13 #include "net/proxy/proxy_service.h"
14 #include "net/proxy/sync_host_resolver_bridge.h"
19 // This factory creates V8ProxyResolvers with appropriate javascript bindings.
20 class ProxyResolverFactoryForV8
: public ProxyResolverFactory
{
22 // |async_host_resolver|, |io_loop| and |net_log| must remain
23 // valid for the duration of our lifetime.
24 // |async_host_resolver| will only be operated on |io_loop|.
25 // TODO(willchan): remove io_loop and replace it with origin_loop.
26 ProxyResolverFactoryForV8(HostResolver
* async_host_resolver
,
28 base::MessageLoopProxy
* origin_loop
,
30 NetworkDelegate
* network_delegate
)
31 : ProxyResolverFactory(true /*expects_pac_bytes*/),
32 async_host_resolver_(async_host_resolver
),
34 origin_loop_(origin_loop
),
36 network_delegate_(network_delegate
) {
39 virtual ProxyResolver
* CreateProxyResolver() OVERRIDE
{
40 // Create a synchronous host resolver wrapper that operates
41 // |async_host_resolver_| on |io_loop_|.
42 SyncHostResolverBridge
* sync_host_resolver
=
43 new SyncHostResolverBridge(async_host_resolver_
, io_loop_
);
45 NetworkDelegateErrorObserver
* error_observer
=
46 new NetworkDelegateErrorObserver(
47 network_delegate_
, origin_loop_
.get());
49 // ProxyResolverJSBindings takes ownership of |error_observer| and
50 // |sync_host_resolver|.
51 ProxyResolverJSBindings
* js_bindings
=
52 ProxyResolverJSBindings::CreateDefault(
53 sync_host_resolver
, net_log_
, error_observer
);
55 // ProxyResolverV8 takes ownership of |js_bindings|.
56 return new ProxyResolverV8(js_bindings
);
60 HostResolver
* const async_host_resolver_
;
61 MessageLoop
* io_loop_
;
62 scoped_refptr
<base::MessageLoopProxy
> origin_loop_
;
64 NetworkDelegate
* network_delegate_
;
70 ProxyService
* CreateProxyServiceUsingV8ProxyResolver(
71 ProxyConfigService
* proxy_config_service
,
72 size_t num_pac_threads
,
73 ProxyScriptFetcher
* proxy_script_fetcher
,
74 DhcpProxyScriptFetcher
* dhcp_proxy_script_fetcher
,
75 HostResolver
* host_resolver
,
77 NetworkDelegate
* network_delegate
) {
78 DCHECK(proxy_config_service
);
79 DCHECK(proxy_script_fetcher
);
80 DCHECK(dhcp_proxy_script_fetcher
);
81 DCHECK(host_resolver
);
83 if (num_pac_threads
== 0)
84 num_pac_threads
= ProxyService::kDefaultNumPacThreads
;
86 ProxyResolverFactory
* sync_resolver_factory
=
87 new ProxyResolverFactoryForV8(
89 MessageLoop::current(),
90 base::MessageLoopProxy::current(),
94 ProxyResolver
* proxy_resolver
=
95 new MultiThreadedProxyResolver(sync_resolver_factory
, num_pac_threads
);
97 ProxyService
* proxy_service
=
98 new ProxyService(proxy_config_service
, proxy_resolver
, net_log
);
100 // Configure fetchers to use for PAC script downloads and auto-detect.
101 proxy_service
->SetProxyScriptFetchers(proxy_script_fetcher
,
102 dhcp_proxy_script_fetcher
);
104 return proxy_service
;