blob: 9b74e297ebd73e9474caddd5440a0bd8410793a5 [file] [log] [blame]
Jason Monkfc934182013-07-22 13:20:39 -04001// 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 Monkfc934182013-07-22 13:20:39 -04008
Jason Monkf5cf6e32013-07-23 17:26:23 -04009#include <utils/String16.h>
10
Ben Murdoche6933f52014-12-05 12:45:49 +000011#include "proxy_resolver_js_bindings.h"
12
Jason Monkfc934182013-07-22 13:20:39 -040013namespace net {
14
15typedef void* RequestHandle;
16typedef void* CompletionCallback;
17
18#define OK 0
19#define ERR_PAC_SCRIPT_FAILED -1
20#define ERR_FAILED -2
21
Jason Monkf5cf6e32013-07-23 17:26:23 -040022class ProxyErrorListener {
23protected:
24 virtual ~ProxyErrorListener() {}
25public:
26 virtual void AlertMessage(android::String16 message) = 0;
27 virtual void ErrorMessage(android::String16 error) = 0;
28};
29
Jason Monkfc934182013-07-22 13:20:39 -040030// 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.
48class 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 Monkf5cf6e32013-07-23 17:26:23 -040053 explicit ProxyResolverV8(ProxyResolverJSBindings* custom_js_bindings,
54 ProxyErrorListener* error_listener);
Jason Monkfc934182013-07-22 13:20:39 -040055
56 virtual ~ProxyResolverV8();
57
58 ProxyResolverJSBindings* js_bindings() { return js_bindings_; }
59
Jason Monkf5cf6e32013-07-23 17:26:23 -040060 virtual int GetProxyForURL(const android::String16 spec, const android::String16 host,
61 android::String16* results);
Jason Monkfc934182013-07-22 13:20:39 -040062 virtual void PurgeMemory();
Jason Monk40adcb52013-08-23 17:19:31 -040063 virtual int SetPacScript(const android::String16& script_data);
Jason Monkfc934182013-07-22 13:20:39 -040064
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 Monkf5cf6e32013-07-23 17:26:23 -040073 ProxyErrorListener* error_listener_;
Ben Murdochab8377a2016-03-24 15:59:32 +000074 static bool initialized_for_this_process_;
Jason Monkfc934182013-07-22 13:20:39 -040075};
76
77} // namespace net
78
79#endif // NET_PROXY_PROXY_RESOLVER_V8_H_