[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" |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 14 | #include "net/base/net_export.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame^] | 15 | #include "net/proxy_resolution/proxy_resolver_factory.h" |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 16 | |
| 17 | namespace net { |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 18 | class ProxyResolver; |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 19 | |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 20 | // MultiThreadedProxyResolverFactory creates instances of a ProxyResolver |
| 21 | // implementation that runs synchronous ProxyResolver implementations on worker |
| 22 | // threads. |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 23 | // |
| 24 | // Threads are created lazily on demand, up to a maximum total. The advantage |
| 25 | // of having a pool of threads, is faster performance. In particular, being |
| 26 | // able to keep servicing PAC requests even if one blocks its execution. |
| 27 | // |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 28 | // During initialization (CreateProxyResolver), a single thread is spun up to |
| 29 | // 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] | 30 | // this to lazily provision any new threads as needed. |
| 31 | // |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 32 | // For each new thread that we spawn in a particular MultiThreadedProxyResolver |
| 33 | // instance, a corresponding new ProxyResolver is created using the |
| 34 | // ProxyResolverFactory returned by CreateProxyResolverFactory(). |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 35 | // |
| 36 | // Because we are creating multiple ProxyResolver instances, this means we |
| 37 | // are duplicating script contexts for what is ordinarily seen as being a |
| 38 | // single script. This can affect compatibility on some classes of PAC |
| 39 | // script: |
| 40 | // |
| 41 | // (a) Scripts whose initialization has external dependencies on network or |
| 42 | // time may end up successfully initializing on some threads, but not |
| 43 | // others. So depending on what thread services the request, the result |
| 44 | // may jump between several possibilities. |
| 45 | // |
| 46 | // (b) Scripts whose FindProxyForURL() depends on side-effects may now |
| 47 | // work differently. For example, a PAC script which was incrementing |
| 48 | // a global counter and using that to make a decision. In the |
| 49 | // multi-threaded model, each thread may have a different value for this |
| 50 | // counter, so it won't globally be seen as monotonically increasing! |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 51 | class NET_EXPORT_PRIVATE MultiThreadedProxyResolverFactory |
| 52 | : public ProxyResolverFactory { |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 53 | public: |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 54 | MultiThreadedProxyResolverFactory(size_t max_num_threads, |
| 55 | bool factory_expects_bytes); |
| 56 | ~MultiThreadedProxyResolverFactory() override; |
[email protected] | 61b84d5 | 2010-07-09 03:32:00 | [diff] [blame] | 57 | |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 58 | int CreateProxyResolver( |
| 59 | const scoped_refptr<ProxyResolverScriptData>& pac_script, |
danakj | 8a98ca2 | 2016-04-16 02:47:36 | [diff] [blame] | 60 | std::unique_ptr<ProxyResolver>* resolver, |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 61 | const CompletionCallback& callback, |
danakj | 8a98ca2 | 2016-04-16 02:47:36 | [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_ |