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