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