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