Add the first GYP-based bots to MB (blink android trybots).
[chromium-blink-merge.git] / net / proxy / multi_threaded_proxy_resolver.h
bloba83c2736b4e899df9f5c28da9050778c3d22f3f8
1 // Copyright (c) 2011 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 #ifndef NET_PROXY_MULTI_THREADED_PROXY_RESOLVER_H_
6 #define NET_PROXY_MULTI_THREADED_PROXY_RESOLVER_H_
8 #include <deque>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "net/base/net_export.h"
16 #include "net/proxy/proxy_resolver.h"
18 namespace base {
19 class Thread;
20 } // namespace base
22 namespace net {
23 class LegacyProxyResolverFactory;
25 // MultiThreadedProxyResolver is a ProxyResolver implementation that runs
26 // synchronous ProxyResolver implementations on worker threads.
28 // Threads are created lazily on demand, up to a maximum total. The advantage
29 // of having a pool of threads, is faster performance. In particular, being
30 // able to keep servicing PAC requests even if one blocks its execution.
32 // During initialization (SetPacScript), a single thread is spun up to test
33 // the script. If this succeeds, we cache the input script, and will re-use
34 // this to lazily provision any new threads as needed.
36 // For each new thread that we spawn, a corresponding new ProxyResolver is
37 // created using ProxyResolverFactory.
39 // Because we are creating multiple ProxyResolver instances, this means we
40 // are duplicating script contexts for what is ordinarily seen as being a
41 // single script. This can affect compatibility on some classes of PAC
42 // script:
44 // (a) Scripts whose initialization has external dependencies on network or
45 // time may end up successfully initializing on some threads, but not
46 // others. So depending on what thread services the request, the result
47 // may jump between several possibilities.
49 // (b) Scripts whose FindProxyForURL() depends on side-effects may now
50 // work differently. For example, a PAC script which was incrementing
51 // a global counter and using that to make a decision. In the
52 // multi-threaded model, each thread may have a different value for this
53 // counter, so it won't globally be seen as monotonically increasing!
54 class NET_EXPORT_PRIVATE MultiThreadedProxyResolver
55 : public ProxyResolver,
56 NON_EXPORTED_BASE(public base::NonThreadSafe) {
57 public:
58 // Creates an asynchronous ProxyResolver that runs requests on up to
59 // |max_num_threads|.
61 // For each thread that is created, an accompanying synchronous ProxyResolver
62 // will be provisioned using |resolver_factory|. All methods on these
63 // ProxyResolvers will be called on the one thread, with the exception of
64 // ProxyResolver::Shutdown() which will be called from the origin thread
65 // prior to destruction.
67 // The constructor takes ownership of |resolver_factory|.
68 MultiThreadedProxyResolver(LegacyProxyResolverFactory* resolver_factory,
69 size_t max_num_threads);
71 ~MultiThreadedProxyResolver() override;
73 // ProxyResolver implementation:
74 int GetProxyForURL(const GURL& url,
75 ProxyInfo* results,
76 const CompletionCallback& callback,
77 RequestHandle* request,
78 const BoundNetLog& net_log) override;
79 void CancelRequest(RequestHandle request) override;
80 LoadState GetLoadState(RequestHandle request) const override;
81 void CancelSetPacScript() override;
82 int SetPacScript(const scoped_refptr<ProxyResolverScriptData>& script_data,
83 const CompletionCallback& callback) override;
85 private:
86 class Executor;
87 class Job;
88 class SetPacScriptJob;
89 class GetProxyForURLJob;
90 // FIFO queue of pending jobs waiting to be started.
91 // TODO(eroman): Make this priority queue.
92 typedef std::deque<scoped_refptr<Job> > PendingJobsQueue;
93 typedef std::vector<scoped_refptr<Executor> > ExecutorList;
95 // Asserts that there are no outstanding user-initiated jobs on any of the
96 // worker threads.
97 void CheckNoOutstandingUserRequests() const;
99 // Stops and deletes all of the worker threads.
100 void ReleaseAllExecutors();
102 // Returns an idle worker thread which is ready to receive GetProxyForURL()
103 // requests. If all threads are occupied, returns NULL.
104 Executor* FindIdleExecutor();
106 // Creates a new worker thread, and appends it to |executors_|.
107 Executor* AddNewExecutor();
109 // Starts the next job from |pending_jobs_| if possible.
110 void OnExecutorReady(Executor* executor);
112 const scoped_ptr<LegacyProxyResolverFactory> resolver_factory_;
113 const size_t max_num_threads_;
114 PendingJobsQueue pending_jobs_;
115 ExecutorList executors_;
116 scoped_refptr<ProxyResolverScriptData> current_script_data_;
119 } // namespace net
121 #endif // NET_PROXY_MULTI_THREADED_PROXY_RESOLVER_H_