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