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