• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2009 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.
4 
5 #ifndef NET_PROXY_PROXY_RESOLVER_V8_H_
6 #define NET_PROXY_PROXY_RESOLVER_V8_H_
7 
8 #include <string>
9 
10 #include "base/scoped_ptr.h"
11 #include "net/proxy/proxy_resolver.h"
12 
13 class MessageLoop;
14 
15 namespace net {
16 
17 class HostResolver;
18 class ProxyResolverJSBindings;
19 
20 // Implementation of ProxyResolver that uses V8 to evaluate PAC scripts.
21 //
22 // ----------------------------------------------------------------------------
23 // !!! Important note on threading model:
24 // ----------------------------------------------------------------------------
25 // There can be only one instance of V8 running at a time. To enforce this
26 // constraint, ProxyResolverV8 holds a v8::Locker during execution. Therefore
27 // it is OK to run multiple instances of ProxyResolverV8 on different threads,
28 // since only one will be running inside V8 at a time.
29 //
30 // It is important that *ALL* instances of V8 in the process be using
31 // v8::Locker. If not there can be race conditions beween the non-locked V8
32 // instances and the locked V8 instances used by ProxyResolverV8 (assuming they
33 // run on different threads).
34 //
35 // This is the case with the V8 instance used by chromium's renderer -- it runs
36 // on a different thread from ProxyResolver (renderer thread vs PAC thread),
37 // and does not use locking since it expects to be alone.
38 class ProxyResolverV8 : public ProxyResolver {
39  public:
40   // Constructs a ProxyResolverV8 with custom bindings. ProxyResolverV8 takes
41   // ownership of |custom_js_bindings| and deletes it when ProxyResolverV8
42   // is destroyed.
43   explicit ProxyResolverV8(ProxyResolverJSBindings* custom_js_bindings);
44 
45   virtual ~ProxyResolverV8();
46 
47   // ProxyResolver implementation:
48   virtual int GetProxyForURL(const GURL& url,
49                              ProxyInfo* results,
50                              CompletionCallback* /*callback*/,
51                              RequestHandle* /*request*/,
52                              LoadLog* load_log);
53   virtual void CancelRequest(RequestHandle request);
54   virtual void PurgeMemory();
55 
js_bindings()56   ProxyResolverJSBindings* js_bindings() const { return js_bindings_.get(); }
57 
58  private:
59   // Context holds the Javascript state for the most recently loaded PAC
60   // script. It corresponds with the data from the last call to
61   // SetPacScript().
62   class Context;
63 
64   // ProxyResolver implementation:
65   virtual int SetPacScript(const GURL& /*pac_url*/,
66                            const std::string& bytes_utf8,
67                            CompletionCallback* /*callback*/);
68   scoped_ptr<Context> context_;
69 
70   scoped_ptr<ProxyResolverJSBindings> js_bindings_;
71 
72   DISALLOW_COPY_AND_ASSIGN(ProxyResolverV8);
73 };
74 
75 }  // namespace net
76 
77 #endif  // NET_PROXY_PROXY_RESOLVER_V8_H_
78