blob: afaa419ae9a33d44e01d8f5e590340dead253c04 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]61b84d52010-07-09 03:32:002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Lily Houghton582d4622018-01-22 22:43:405#ifndef NET_PROXY_RESOLUTION_MULTI_THREADED_PROXY_RESOLVER_H_
6#define NET_PROXY_RESOLUTION_MULTI_THREADED_PROXY_RESOLVER_H_
[email protected]61b84d52010-07-09 03:32:007
Avi Drissman13fc8932015-12-20 04:40:468#include <stddef.h>
9
danakj8a98ca22016-04-16 02:47:3610#include <memory>
sammc514748c2015-05-01 06:15:0411#include <set>
[email protected]61b84d52010-07-09 03:32:0012
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/ref_counted.h"
Bence Békycc5b88a2018-05-25 20:24:1714#include "net/base/completion_once_callback.h"
[email protected]172da1b2011-08-12 15:52:2615#include "net/base/net_export.h"
Lily Houghton582d4622018-01-22 22:43:4016#include "net/proxy_resolution/proxy_resolver_factory.h"
[email protected]61b84d52010-07-09 03:32:0017
18namespace net {
sammc514748c2015-05-01 06:15:0419class ProxyResolver;
[email protected]61b84d52010-07-09 03:32:0020
sammc514748c2015-05-01 06:15:0421// MultiThreadedProxyResolverFactory creates instances of a ProxyResolver
22// implementation that runs synchronous ProxyResolver implementations on worker
23// threads.
[email protected]61b84d52010-07-09 03:32:0024//
25// Threads are created lazily on demand, up to a maximum total. The advantage
26// of having a pool of threads, is faster performance. In particular, being
27// able to keep servicing PAC requests even if one blocks its execution.
28//
sammc514748c2015-05-01 06:15:0429// During initialization (CreateProxyResolver), a single thread is spun up to
30// test the script. If this succeeds, we cache the input script, and will re-use
[email protected]61b84d52010-07-09 03:32:0031// this to lazily provision any new threads as needed.
32//
sammc514748c2015-05-01 06:15:0433// For each new thread that we spawn in a particular MultiThreadedProxyResolver
34// instance, a corresponding new ProxyResolver is created using the
35// ProxyResolverFactory returned by CreateProxyResolverFactory().
[email protected]61b84d52010-07-09 03:32:0036//
37// Because we are creating multiple ProxyResolver instances, this means we
38// are duplicating script contexts for what is ordinarily seen as being a
39// single script. This can affect compatibility on some classes of PAC
40// script:
41//
42// (a) Scripts whose initialization has external dependencies on network or
43// time may end up successfully initializing on some threads, but not
44// others. So depending on what thread services the request, the result
45// may jump between several possibilities.
46//
47// (b) Scripts whose FindProxyForURL() depends on side-effects may now
48// work differently. For example, a PAC script which was incrementing
49// a global counter and using that to make a decision. In the
50// multi-threaded model, each thread may have a different value for this
51// counter, so it won't globally be seen as monotonically increasing!
sammc514748c2015-05-01 06:15:0452class NET_EXPORT_PRIVATE MultiThreadedProxyResolverFactory
53 : public ProxyResolverFactory {
[email protected]61b84d52010-07-09 03:32:0054 public:
sammc514748c2015-05-01 06:15:0455 MultiThreadedProxyResolverFactory(size_t max_num_threads,
56 bool factory_expects_bytes);
57 ~MultiThreadedProxyResolverFactory() override;
[email protected]61b84d52010-07-09 03:32:0058
Lily Houghton99597862018-03-07 16:40:4259 int CreateProxyResolver(const scoped_refptr<PacFileData>& pac_script,
60 std::unique_ptr<ProxyResolver>* resolver,
Bence Békycc5b88a2018-05-25 20:24:1761 CompletionOnceCallback callback,
Lily Houghton99597862018-03-07 16:40:4262 std::unique_ptr<Request>* request) override;
[email protected]61b84d52010-07-09 03:32:0063
64 private:
[email protected]61b84d52010-07-09 03:32:0065 class Job;
[email protected]61b84d52010-07-09 03:32:0066
sammc514748c2015-05-01 06:15:0467 // Invoked to create a ProxyResolverFactory instance to pass to a
68 // MultiThreadedProxyResolver instance.
danakj8a98ca22016-04-16 02:47:3669 virtual std::unique_ptr<ProxyResolverFactory>
70 CreateProxyResolverFactory() = 0;
[email protected]61b84d52010-07-09 03:32:0071
sammc514748c2015-05-01 06:15:0472 void RemoveJob(Job* job);
[email protected]61b84d52010-07-09 03:32:0073
[email protected]61b84d52010-07-09 03:32:0074 const size_t max_num_threads_;
sammc514748c2015-05-01 06:15:0475
76 std::set<Job*> jobs_;
[email protected]61b84d52010-07-09 03:32:0077};
78
79} // namespace net
80
Lily Houghton582d4622018-01-22 22:43:4081#endif // NET_PROXY_RESOLUTION_MULTI_THREADED_PROXY_RESOLVER_H_