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