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 #pragma once
8 
9 #include <string>
10 
11 #include "proxy_resolver_js_bindings.h"
12 #include "proxy_resolver_v8_wrapper.h"
13 
14 namespace net {
15 
16 class ProxyErrorListener {
17 protected:
~ProxyErrorListener()18   virtual ~ProxyErrorListener() {}
19 public:
20   virtual void AlertMessage(const std::string& message) = 0;
21   virtual void ErrorMessage(const std::string& error) = 0;
22 };
23 
24 // Implementation of ProxyResolver that uses V8 to evaluate PAC scripts.
25 //
26 // ----------------------------------------------------------------------------
27 // !!! Important note on threading model:
28 // ----------------------------------------------------------------------------
29 // There can be only one instance of V8 running at a time. To enforce this
30 // constraint, ProxyResolverV8 holds a v8::Locker during execution. Therefore
31 // it is OK to run multiple instances of ProxyResolverV8 on different threads,
32 // since only one will be running inside V8 at a time.
33 //
34 // It is important that *ALL* instances of V8 in the process be using
35 // v8::Locker. If not there can be race conditions beween the non-locked V8
36 // instances and the locked V8 instances used by ProxyResolverV8 (assuming they
37 // run on different threads).
38 //
39 // This is the case with the V8 instance used by chromium's renderer -- it runs
40 // on a different thread from ProxyResolver (renderer thread vs PAC thread),
41 // and does not use locking since it expects to be alone.
42 class ProxyResolverV8 {
43  public:
44   // Constructs a ProxyResolverV8 with custom bindings. ProxyResolverV8 takes
45   // ownership of |custom_js_bindings| and deletes it when ProxyResolverV8
46   // is destroyed.
47   explicit ProxyResolverV8(ProxyResolverJSBindings* custom_js_bindings);
48   // This constructor should only be used for test.
49   explicit ProxyResolverV8(ProxyResolverJSBindings* custom_js_bindings,
50           ProxyErrorListener* error_listener);
51 
52   virtual ~ProxyResolverV8();
53 
js_bindings()54   ProxyResolverJSBindings* js_bindings() { return js_bindings_; }
55 
56   virtual int GetProxyForURL(const std::u16string& spec, const std::u16string& host,
57                              std::u16string* results);
58   virtual void PurgeMemory();
59   virtual int SetPacScript(const std::u16string& script_data);
60 
61  private:
62   // Context holds the Javascript state for the most recently loaded PAC
63   // script. It corresponds with the data from the last call to
64   // SetPacScript().
65   class Context;
66   Context* context_;
67 
68   ProxyResolverJSBindings* js_bindings_;
69   ProxyErrorListener* error_listener_;
70   static bool initialized_for_this_process_;
71 };
72 
73 }  // namespace net
74 
75 #endif  // NET_PROXY_PROXY_RESOLVER_V8_H_
76