blob: 3adc672a1ffebdb9db891e255f06fd25c704406a [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]943c8082009-02-23 19:10:452// 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
[email protected]c4c1b482011-07-22 17:24:268#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:159#include "base/memory/scoped_ptr.h"
[email protected]172da1b2011-08-12 15:52:2610#include "net/base/net_export.h"
[email protected]7dc52f22009-03-02 22:37:1811#include "net/proxy/proxy_resolver.h"
[email protected]943c8082009-02-23 19:10:4512
[email protected]a2c1b132013-02-01 12:28:4713namespace v8 {
14class Isolate;
15} // namespace v8
16
[email protected]943c8082009-02-23 19:10:4517namespace net {
18
19// Implementation of ProxyResolver that uses V8 to evaluate PAC scripts.
20//
21// ----------------------------------------------------------------------------
22// !!! Important note on threading model:
23// ----------------------------------------------------------------------------
24// There can be only one instance of V8 running at a time. To enforce this
25// constraint, ProxyResolverV8 holds a v8::Locker during execution. Therefore
26// it is OK to run multiple instances of ProxyResolverV8 on different threads,
27// since only one will be running inside V8 at a time.
28//
29// It is important that *ALL* instances of V8 in the process be using
[email protected]49639fa2011-12-20 23:22:4130// v8::Locker. If not there can be race conditions between the non-locked V8
[email protected]943c8082009-02-23 19:10:4531// instances and the locked V8 instances used by ProxyResolverV8 (assuming they
32// run on different threads).
33//
34// This is the case with the V8 instance used by chromium's renderer -- it runs
35// on a different thread from ProxyResolver (renderer thread vs PAC thread),
36// and does not use locking since it expects to be alone.
[email protected]172da1b2011-08-12 15:52:2637class NET_EXPORT_PRIVATE ProxyResolverV8 : public ProxyResolver {
[email protected]943c8082009-02-23 19:10:4538 public:
[email protected]501e2d42013-01-30 22:30:4939 // Interface for the javascript bindings.
40 class NET_EXPORT_PRIVATE JSBindings {
41 public:
42 enum ResolveDnsOperation {
43 DNS_RESOLVE,
44 DNS_RESOLVE_EX,
45 MY_IP_ADDRESS,
46 MY_IP_ADDRESS_EX,
[email protected]501e2d42013-01-30 22:30:4947 };
48
49 JSBindings() {}
50
[email protected]501e2d42013-01-30 22:30:4951 // Handler for "dnsResolve()", "dnsResolveEx()", "myIpAddress()",
52 // "myIpAddressEx()". Returns true on success and fills |*output| with the
[email protected]8cdc8812013-02-21 05:29:4953 // result. If |*terminate| is set to true, then the script execution will
54 // be aborted. Note that termination may not happen right away.
[email protected]501e2d42013-01-30 22:30:4955 virtual bool ResolveDns(const std::string& host,
56 ResolveDnsOperation op,
[email protected]8cdc8812013-02-21 05:29:4957 std::string* output,
58 bool* terminate) = 0;
[email protected]501e2d42013-01-30 22:30:4959
60 // Handler for "alert(message)"
61 virtual void Alert(const string16& message) = 0;
62
63 // Handler for when an error is encountered. |line_number| may be -1
64 // if a line number is not applicable to this error.
65 virtual void OnError(int line_number, const string16& error) = 0;
[email protected]8c49a502013-02-01 03:54:1166
67 protected:
68 virtual ~JSBindings() {}
[email protected]501e2d42013-01-30 22:30:4969 };
70
71 // Constructs a ProxyResolverV8.
72 ProxyResolverV8();
[email protected]50d7d7282009-03-02 21:45:1873
[email protected]775fd9e2009-07-26 21:12:2074 virtual ~ProxyResolverV8();
[email protected]943c8082009-02-23 19:10:4575
[email protected]501e2d42013-01-30 22:30:4976 JSBindings* js_bindings() const { return js_bindings_; }
77 void set_js_bindings(JSBindings* js_bindings) { js_bindings_ = js_bindings; }
[email protected]7aefb152011-01-21 23:46:4978
[email protected]943c8082009-02-23 19:10:4579 // ProxyResolver implementation:
[email protected]775fd9e2009-07-26 21:12:2080 virtual int GetProxyForURL(const GURL& url,
81 ProxyInfo* results,
[email protected]235786812011-12-20 02:15:3182 const net::CompletionCallback& /*callback*/,
[email protected]52ae80c2009-09-10 21:27:0083 RequestHandle* /*request*/,
[email protected]c4c1b482011-07-22 17:24:2684 const BoundNetLog& net_log) OVERRIDE;
85 virtual void CancelRequest(RequestHandle request) OVERRIDE;
[email protected]f2c971f2011-11-08 00:33:1786 virtual LoadState GetLoadState(RequestHandle request) const OVERRIDE;
[email protected]c4c1b482011-07-22 17:24:2687 virtual void CancelSetPacScript() OVERRIDE;
88 virtual void PurgeMemory() OVERRIDE;
[email protected]24476402010-07-20 20:55:1789 virtual int SetPacScript(
90 const scoped_refptr<ProxyResolverScriptData>& script_data,
[email protected]235786812011-12-20 02:15:3191 const net::CompletionCallback& /*callback*/) OVERRIDE;
[email protected]943c8082009-02-23 19:10:4592
[email protected]a2c1b132013-02-01 12:28:4793 // Remember the default Isolate, must be called from the main thread. This
94 // hack can be removed when the "default Isolate" concept is gone.
95 static void RememberDefaultIsolate();
96 static v8::Isolate* GetDefaultIsolate();
97
[email protected]943c8082009-02-23 19:10:4598 private:
[email protected]a2c1b132013-02-01 12:28:4799 static v8::Isolate* g_default_isolate_;
100
[email protected]943c8082009-02-23 19:10:45101 // Context holds the Javascript state for the most recently loaded PAC
102 // script. It corresponds with the data from the last call to
[email protected]620f5712009-08-04 22:43:12103 // SetPacScript().
[email protected]943c8082009-02-23 19:10:45104 class Context;
[email protected]501e2d42013-01-30 22:30:49105
[email protected]943c8082009-02-23 19:10:45106 scoped_ptr<Context> context_;
107
[email protected]501e2d42013-01-30 22:30:49108 JSBindings* js_bindings_;
[email protected]50d7d7282009-03-02 21:45:18109
[email protected]943c8082009-02-23 19:10:45110 DISALLOW_COPY_AND_ASSIGN(ProxyResolverV8);
111};
112
[email protected]943c8082009-02-23 19:10:45113} // namespace net
114
115#endif // NET_PROXY_PROXY_RESOLVER_V8_H_