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 <utils/String16.h>
10
11#include "proxy_resolver_js_bindings.h"
12
13namespace 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
22class ProxyErrorListener {
23protected:
24  virtual ~ProxyErrorListener() {}
25public:
26  virtual void AlertMessage(android::String16 message) = 0;
27  virtual void ErrorMessage(android::String16 error) = 0;
28};
29
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.
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.
53  explicit ProxyResolverV8(ProxyResolverJSBindings* custom_js_bindings,
54          ProxyErrorListener* error_listener);
55
56  virtual ~ProxyResolverV8();
57
58  ProxyResolverJSBindings* js_bindings() { return js_bindings_; }
59
60  virtual int GetProxyForURL(const android::String16 spec, const android::String16 host,
61                             android::String16* results);
62  virtual void PurgeMemory();
63  virtual int SetPacScript(const android::String16& script_data);
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_;
73  ProxyErrorListener* error_listener_;
74  static bool initialized_for_this_process_;
75};
76
77}  // namespace net
78
79#endif  // NET_PROXY_PROXY_RESOLVER_V8_H_
80