proxy_resolver_v8.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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
13namespace v8 {
14class HeapStatistics;
15class Isolate;
16}  // namespace v8
17
18namespace 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.
38class 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
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:
69    virtual ~JSBindings() {}
70  };
71
72  // Constructs a ProxyResolverV8.
73  ProxyResolverV8();
74
75  virtual ~ProxyResolverV8();
76
77  JSBindings* js_bindings() const { return 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  static v8::Isolate* GetDefaultIsolate();
98
99  // Get total/ued heap memory usage of all v8 instances used by the proxy
100  // resolver.
101  static size_t GetTotalHeapSize();
102  static size_t GetUsedHeapSize();
103
104 private:
105  static v8::Isolate* g_default_isolate_;
106
107  // Context holds the Javascript state for the most recently loaded PAC
108  // script. It corresponds with the data from the last call to
109  // SetPacScript().
110  class Context;
111
112  scoped_ptr<Context> context_;
113
114  JSBindings* js_bindings_;
115
116  DISALLOW_COPY_AND_ASSIGN(ProxyResolverV8);
117};
118
119}  // namespace net
120
121#endif  // NET_PROXY_PROXY_RESOLVER_V8_H_
122