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