• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef NET_PROXY_PROXY_RESOLVER_V8_H_
6 #define NET_PROXY_PROXY_RESOLVER_V8_H_
7 
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "net/base/net_export.h"
11 #include "net/proxy/proxy_resolver.h"
12 
13 namespace v8 {
14 class HeapStatistics;
15 class Isolate;
16 }  // namespace v8
17 
18 namespace net {
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 between 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 NET_EXPORT_PRIVATE ProxyResolverV8 : public ProxyResolver {
39  public:
40   // Interface for the javascript bindings.
41   class NET_EXPORT_PRIVATE JSBindings {
42    public:
43     enum ResolveDnsOperation {
44       DNS_RESOLVE,
45       DNS_RESOLVE_EX,
46       MY_IP_ADDRESS,
47       MY_IP_ADDRESS_EX,
48     };
49 
JSBindings()50     JSBindings() {}
51 
52     // Handler for "dnsResolve()", "dnsResolveEx()", "myIpAddress()",
53     // "myIpAddressEx()". Returns true on success and fills |*output| with the
54     // result. If |*terminate| is set to true, then the script execution will
55     // be aborted. Note that termination may not happen right away.
56     virtual bool ResolveDns(const std::string& host,
57                             ResolveDnsOperation op,
58                             std::string* output,
59                             bool* terminate) = 0;
60 
61     // Handler for "alert(message)"
62     virtual void Alert(const base::string16& message) = 0;
63 
64     // Handler for when an error is encountered. |line_number| may be -1
65     // if a line number is not applicable to this error.
66     virtual void OnError(int line_number, const base::string16& error) = 0;
67 
68    protected:
~JSBindings()69     virtual ~JSBindings() {}
70   };
71 
72   // Constructs a ProxyResolverV8.
73   ProxyResolverV8();
74 
75   virtual ~ProxyResolverV8();
76 
js_bindings()77   JSBindings* js_bindings() const { return js_bindings_; }
set_js_bindings(JSBindings * js_bindings)78   void set_js_bindings(JSBindings* js_bindings) { js_bindings_ = js_bindings; }
79 
80   // ProxyResolver implementation:
81   virtual int GetProxyForURL(const GURL& url,
82                              ProxyInfo* results,
83                              const net::CompletionCallback& /*callback*/,
84                              RequestHandle* /*request*/,
85                              const BoundNetLog& net_log) OVERRIDE;
86   virtual void CancelRequest(RequestHandle request) OVERRIDE;
87   virtual LoadState GetLoadState(RequestHandle request) const OVERRIDE;
88   virtual void CancelSetPacScript() OVERRIDE;
89   virtual void PurgeMemory() OVERRIDE;
90   virtual int SetPacScript(
91       const scoped_refptr<ProxyResolverScriptData>& script_data,
92       const net::CompletionCallback& /*callback*/) OVERRIDE;
93 
94   // Remember the default Isolate, must be called from the main thread. This
95   // hack can be removed when the "default Isolate" concept is gone.
96   static void RememberDefaultIsolate();
97 
98 #if defined(OS_WIN)
99   // Create an isolate to use for the proxy resolver. Until the "default
100   // Isolate" concept is gone, it is preferable to invoke
101   // RememberDefaultIsolate() as creating a new Isolate in additional to the
102   // default Isolate will waste a few MB of memory and the runtime it took to
103   // create the default Isolate.
104   static void CreateIsolate();
105 #endif
106 
107   static v8::Isolate* GetDefaultIsolate();
108 
109   // Get total/ued heap memory usage of all v8 instances used by the proxy
110   // resolver.
111   static size_t GetTotalHeapSize();
112   static size_t GetUsedHeapSize();
113 
114  private:
115   static v8::Isolate* g_default_isolate_;
116 
117   // Context holds the Javascript state for the most recently loaded PAC
118   // script. It corresponds with the data from the last call to
119   // SetPacScript().
120   class Context;
121 
122   scoped_ptr<Context> context_;
123 
124   JSBindings* js_bindings_;
125 
126   DISALLOW_COPY_AND_ASSIGN(ProxyResolverV8);
127 };
128 
129 }  // namespace net
130 
131 #endif  // NET_PROXY_PROXY_RESOLVER_V8_H_
132