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