[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 5 | #ifndef NET_PROXY_RESOLUTION_MULTI_THREADED_PROXY_RESOLVER_H_ |
| 6 | #define NET_PROXY_RESOLUTION_MULTI_THREADED_PROXY_RESOLVER_H_ |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 7 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
danakj | 8a98ca2 | 2016-04-16 02:47:36 | [diff] [blame] | 10 | #include <memory> |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 11 | #include <set> |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 12 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 14 | #include "net/base/completion_once_callback.h" |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 15 | #include "net/base/net_export.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 16 | #include "net/proxy_resolution/proxy_resolver_factory.h" |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 17 | |
| 18 | namespace net { |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 19 | class ProxyResolver; |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 20 | |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 21 | // MultiThreadedProxyResolverFactory creates instances of a ProxyResolver |
| 22 | // implementation that runs synchronous ProxyResolver implementations on worker |
| 23 | // threads. |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 24 | // |
| 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 | // |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 29 | // 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] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 31 | // this to lazily provision any new threads as needed. |
| 32 | // |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 33 | // 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] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 36 | // |
| 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! |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 52 | class NET_EXPORT_PRIVATE MultiThreadedProxyResolverFactory |
| 53 | : public ProxyResolverFactory { |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 54 | public: |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 55 | MultiThreadedProxyResolverFactory(size_t max_num_threads, |
| 56 | bool factory_expects_bytes); |
| 57 | ~MultiThreadedProxyResolverFactory() override; |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 58 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 59 | int CreateProxyResolver(const scoped_refptr<PacFileData>& pac_script, |
| 60 | std::unique_ptr<ProxyResolver>* resolver, |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 61 | CompletionOnceCallback callback, |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 62 | std::unique_ptr<Request>* request) override; |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 63 | |
| 64 | private: |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 65 | class Job; |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 66 | |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 67 | // Invoked to create a ProxyResolverFactory instance to pass to a |
| 68 | // MultiThreadedProxyResolver instance. |
danakj | 8a98ca2 | 2016-04-16 02:47:36 | [diff] [blame] | 69 | virtual std::unique_ptr<ProxyResolverFactory> |
| 70 | CreateProxyResolverFactory() = 0; |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 71 | |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 72 | void RemoveJob(Job* job); |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 73 | |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 74 | const size_t max_num_threads_; |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 75 | |
| 76 | std::set<Job*> jobs_; |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | } // namespace net |
| 80 | |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 81 | #endif // NET_PROXY_RESOLUTION_MULTI_THREADED_PROXY_RESOLVER_H_ |