[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 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 | |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 8 | #include "base/compiler_specific.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 9 | #include "base/memory/scoped_ptr.h" |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 10 | #include "net/base/net_export.h" |
[email protected] | 7dc52f2 | 2009-03-02 22:37:18 | [diff] [blame] | 11 | #include "net/proxy/proxy_resolver.h" |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 12 | |
| 13 | namespace net { |
| 14 | |
| 15 | // Implementation of ProxyResolver that uses V8 to evaluate PAC scripts. |
| 16 | // |
| 17 | // ---------------------------------------------------------------------------- |
| 18 | // !!! Important note on threading model: |
| 19 | // ---------------------------------------------------------------------------- |
| 20 | // There can be only one instance of V8 running at a time. To enforce this |
| 21 | // constraint, ProxyResolverV8 holds a v8::Locker during execution. Therefore |
| 22 | // it is OK to run multiple instances of ProxyResolverV8 on different threads, |
| 23 | // since only one will be running inside V8 at a time. |
| 24 | // |
| 25 | // It is important that *ALL* instances of V8 in the process be using |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 26 | // v8::Locker. If not there can be race conditions between the non-locked V8 |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 27 | // instances and the locked V8 instances used by ProxyResolverV8 (assuming they |
| 28 | // run on different threads). |
| 29 | // |
| 30 | // This is the case with the V8 instance used by chromium's renderer -- it runs |
| 31 | // on a different thread from ProxyResolver (renderer thread vs PAC thread), |
| 32 | // and does not use locking since it expects to be alone. |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 33 | class NET_EXPORT_PRIVATE ProxyResolverV8 : public ProxyResolver { |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 34 | public: |
[email protected] | 501e2d4 | 2013-01-30 22:30:49 | [diff] [blame^] | 35 | // Interface for the javascript bindings. |
| 36 | class NET_EXPORT_PRIVATE JSBindings { |
| 37 | public: |
| 38 | enum ResolveDnsOperation { |
| 39 | DNS_RESOLVE, |
| 40 | DNS_RESOLVE_EX, |
| 41 | MY_IP_ADDRESS, |
| 42 | MY_IP_ADDRESS_EX, |
| 43 | NUM_DNS_OPERATIONS, |
| 44 | }; |
| 45 | |
| 46 | JSBindings() {} |
| 47 | |
| 48 | virtual ~JSBindings() {} |
| 49 | |
| 50 | // Handler for "dnsResolve()", "dnsResolveEx()", "myIpAddress()", |
| 51 | // "myIpAddressEx()". Returns true on success and fills |*output| with the |
| 52 | // result. |
| 53 | virtual bool ResolveDns(const std::string& host, |
| 54 | ResolveDnsOperation op, |
| 55 | std::string* output) = 0; |
| 56 | |
| 57 | // Handler for "alert(message)" |
| 58 | virtual void Alert(const string16& message) = 0; |
| 59 | |
| 60 | // Handler for when an error is encountered. |line_number| may be -1 |
| 61 | // if a line number is not applicable to this error. |
| 62 | virtual void OnError(int line_number, const string16& error) = 0; |
| 63 | }; |
| 64 | |
| 65 | // Constructs a ProxyResolverV8. |
| 66 | ProxyResolverV8(); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 67 | |
[email protected] | 775fd9e | 2009-07-26 21:12:20 | [diff] [blame] | 68 | virtual ~ProxyResolverV8(); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 69 | |
[email protected] | 501e2d4 | 2013-01-30 22:30:49 | [diff] [blame^] | 70 | JSBindings* js_bindings() const { return js_bindings_; } |
| 71 | void set_js_bindings(JSBindings* js_bindings) { js_bindings_ = js_bindings; } |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 72 | |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 73 | // ProxyResolver implementation: |
[email protected] | 775fd9e | 2009-07-26 21:12:20 | [diff] [blame] | 74 | virtual int GetProxyForURL(const GURL& url, |
| 75 | ProxyInfo* results, |
[email protected] | 23578681 | 2011-12-20 02:15:31 | [diff] [blame] | 76 | const net::CompletionCallback& /*callback*/, |
[email protected] | 52ae80c | 2009-09-10 21:27:00 | [diff] [blame] | 77 | RequestHandle* /*request*/, |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 78 | const BoundNetLog& net_log) OVERRIDE; |
| 79 | virtual void CancelRequest(RequestHandle request) OVERRIDE; |
[email protected] | f2c971f | 2011-11-08 00:33:17 | [diff] [blame] | 80 | virtual LoadState GetLoadState(RequestHandle request) const OVERRIDE; |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 81 | virtual void CancelSetPacScript() OVERRIDE; |
| 82 | virtual void PurgeMemory() OVERRIDE; |
[email protected] | 2447640 | 2010-07-20 20:55:17 | [diff] [blame] | 83 | virtual int SetPacScript( |
| 84 | const scoped_refptr<ProxyResolverScriptData>& script_data, |
[email protected] | 23578681 | 2011-12-20 02:15:31 | [diff] [blame] | 85 | const net::CompletionCallback& /*callback*/) OVERRIDE; |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 86 | |
| 87 | private: |
| 88 | // Context holds the Javascript state for the most recently loaded PAC |
| 89 | // script. It corresponds with the data from the last call to |
[email protected] | 620f571 | 2009-08-04 22:43:12 | [diff] [blame] | 90 | // SetPacScript(). |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 91 | class Context; |
[email protected] | 501e2d4 | 2013-01-30 22:30:49 | [diff] [blame^] | 92 | |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 93 | scoped_ptr<Context> context_; |
| 94 | |
[email protected] | 501e2d4 | 2013-01-30 22:30:49 | [diff] [blame^] | 95 | JSBindings* js_bindings_; |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 96 | |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 97 | DISALLOW_COPY_AND_ASSIGN(ProxyResolverV8); |
| 98 | }; |
| 99 | |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 100 | } // namespace net |
| 101 | |
| 102 | #endif // NET_PROXY_PROXY_RESOLVER_V8_H_ |