proxy_resolver_v8.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file.
490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef NET_PROXY_PROXY_RESOLVER_V8_H_
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#define NET_PROXY_PROXY_RESOLVER_V8_H_
790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/compiler_specific.h"
990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "net/base/net_export.h"
11f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "net/proxy/proxy_resolver.h"
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace net {
1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class ProxyResolverJSBindings;
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Implementation of ProxyResolver that uses V8 to evaluate PAC scripts.
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//
1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// ----------------------------------------------------------------------------
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// !!! Important note on threading model:
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// ----------------------------------------------------------------------------
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// There can be only one instance of V8 running at a time. To enforce this
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// constraint, ProxyResolverV8 holds a v8::Locker during execution. Therefore
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// it is OK to run multiple instances of ProxyResolverV8 on different threads,
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// since only one will be running inside V8 at a time.
2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//
2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// It is important that *ALL* instances of V8 in the process be using
2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// v8::Locker. If not there can be race conditions between the non-locked V8
2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// instances and the locked V8 instances used by ProxyResolverV8 (assuming they
3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// run on different threads).
3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//
3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This is the case with the V8 instance used by chromium's renderer -- it runs
3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// on a different thread from ProxyResolver (renderer thread vs PAC thread),
3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// and does not use locking since it expects to be alone.
3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class NET_EXPORT_PRIVATE ProxyResolverV8 : public ProxyResolver {
3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public:
3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Constructs a ProxyResolverV8 with custom bindings. ProxyResolverV8 takes
3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // ownership of |custom_js_bindings| and deletes it when ProxyResolverV8
3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // is destroyed.
4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  explicit ProxyResolverV8(ProxyResolverJSBindings* custom_js_bindings);
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual ~ProxyResolverV8();
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ProxyResolverJSBindings* js_bindings() const { return js_bindings_.get(); }
4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // ProxyResolver implementation:
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual int GetProxyForURL(const GURL& url,
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                             ProxyInfo* results,
49                             const net::CompletionCallback& /*callback*/,
50                             RequestHandle* /*request*/,
51                             const BoundNetLog& net_log) OVERRIDE;
52  virtual void CancelRequest(RequestHandle request) OVERRIDE;
53  virtual LoadState GetLoadState(RequestHandle request) const OVERRIDE;
54  virtual LoadState GetLoadStateThreadSafe(
55      RequestHandle request) const OVERRIDE;
56  virtual void CancelSetPacScript() OVERRIDE;
57  virtual void PurgeMemory() OVERRIDE;
58  virtual void Shutdown() OVERRIDE;
59  virtual int SetPacScript(
60      const scoped_refptr<ProxyResolverScriptData>& script_data,
61      const net::CompletionCallback& /*callback*/) OVERRIDE;
62
63 private:
64  // Context holds the Javascript state for the most recently loaded PAC
65  // script. It corresponds with the data from the last call to
66  // SetPacScript().
67  class Context;
68  scoped_ptr<Context> context_;
69
70  scoped_ptr<ProxyResolverJSBindings> js_bindings_;
71
72  DISALLOW_COPY_AND_ASSIGN(ProxyResolverV8);
73};
74
75}  // namespace net
76
77#endif  // NET_PROXY_PROXY_RESOLVER_V8_H_
78