Revert "Update chromium-libpac to use chromium_org/v8"

This reverts commit 2077bd69a8f7657d2615ff4f93cfa6e843c998c8.

Change-Id: I2bcd5505ff43466094bc62bf6b06e77b66b17b4e
diff --git a/src/proxy_resolver_v8.cc b/src/proxy_resolver_v8.cc
index 7b86ae8..285091d 100644
--- a/src/proxy_resolver_v8.cc
+++ b/src/proxy_resolver_v8.cc
@@ -179,12 +179,12 @@
   return v8::String::New(s.data(), s.size());
 }
 
-v8::Local<v8::String> UTF16StringToV8String(v8::Isolate* isolate, const android::String16& s) {
+v8::Local<v8::String> UTF16StringToV8String(const android::String16& s) {
   return v8::String::New(s.string(), s.size());
 }
 
 // Converts an ASCII string literal to a V8 string.
-v8::Local<v8::String> ASCIILiteralToV8String(v8::Isolate* isolate, const char* ascii) {
+v8::Local<v8::String> ASCIILiteralToV8String(const char* ascii) {
 //  DCHECK(IsStringASCII(ascii));
   size_t length = strlen(ascii);
   if (length <= kMaxStringBytesForCopy)
@@ -195,12 +195,11 @@
 // Stringizes a V8 object by calling its toString() method. Returns true
 // on success. This may fail if the toString() throws an exception.
 bool V8ObjectToUTF16String(v8::Handle<v8::Value> object,
-                           android::String16* utf16_result,
-                           v8::Isolate* isolate) {
+                           android::String16* utf16_result) {
   if (object.IsEmpty())
     return false;
 
-  v8::HandleScope scope(isolate);
+  v8::HandleScope scope;
   v8::Local<v8::String> str_object = object->ToString();
   if (str_object.IsEmpty())
     return false;
@@ -210,7 +209,7 @@
 
 // Extracts an hostname argument from |args|. On success returns true
 // and fills |*hostname| with the result.
-bool GetHostnameArgument(const v8::FunctionCallbackInfo<v8::Value>& args, std::string* hostname) {
+bool GetHostnameArgument(const v8::Arguments& args, std::string* hostname) {
   // The first argument should be a string.
   if (args.Length() == 0 || args[0].IsEmpty() || !args[0]->IsString())
     return false;
@@ -351,27 +350,29 @@
 class ProxyResolverV8::Context {
  public:
   explicit Context(ProxyResolverJSBindings* js_bindings,
-          ProxyErrorListener* error_listener, v8::Isolate* isolate)
-      : js_bindings_(js_bindings), error_listener_(error_listener), isolate_(isolate) {
+          ProxyErrorListener* error_listener)
+      : js_bindings_(js_bindings), error_listener_(error_listener) {
   }
 
   ~Context() {
-    v8::Locker locked(isolate_);
-    v8::Isolate::Scope isolate_scope(isolate_);
+    v8::Locker locked;
 
-    v8_this_.Reset();
-    v8_context_.Reset();
+    v8_this_.Dispose();
+    v8_context_.Dispose();
+
+    // Run the V8 garbage collector. We do this to be sure the
+    // ExternalStringResource objects we allocated get properly disposed.
+    // Otherwise when running the unit-tests they may get leaked.
+    // See crbug.com/48145.
+    PurgeMemory();
   }
 
   int ResolveProxy(const android::String16 url, const android::String16 host,
         android::String16* results) {
-    v8::Locker locked(isolate_);
-    v8::Isolate::Scope isolate_scope(isolate_);
-    v8::HandleScope scope(isolate_);
+    v8::Locker locked;
+    v8::HandleScope scope;
 
-    v8::Local<v8::Context> context =
-        v8::Local<v8::Context>::New(isolate_, v8_context_);
-    v8::Context::Scope function_scope(context);
+    v8::Context::Scope function_scope(v8_context_);
 
     v8::Local<v8::Value> function;
     if (!GetFindProxyForURL(&function)) {
@@ -381,12 +382,12 @@
     }
 
     v8::Handle<v8::Value> argv[] = {
-        UTF16StringToV8String(isolate_, url),
-        UTF16StringToV8String(isolate_, host) };
+        UTF16StringToV8String(url),
+        UTF16StringToV8String(host) };
 
     v8::TryCatch try_catch;
     v8::Local<v8::Value> ret = v8::Function::Cast(*function)->Call(
-        context->Global(), 2, argv);
+        v8_context_->Global(), 2, argv);
 
     if (try_catch.HasCaught()) {
       error_listener_->ErrorMessage(
@@ -416,64 +417,58 @@
   }
 
   int InitV8(const android::String16& pac_script) {
-    v8::Locker locked(isolate_);
-    v8::Isolate::Scope isolate_scope(isolate_);
-    v8::HandleScope scope(isolate_);
+    v8::Locker locked;
+    v8::HandleScope scope;
 
-    v8_this_.Reset(isolate_, v8::External::New(isolate_, this));
-    v8::Local<v8::External> v8_this =
-        v8::Local<v8::External>::New(isolate_, v8_this_);
+    v8_this_ = v8::Persistent<v8::External>::New(v8::External::New(this));
     v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
 
     // Attach the javascript bindings.
     v8::Local<v8::FunctionTemplate> alert_template =
-        v8::FunctionTemplate::New(isolate_, &AlertCallback, v8_this);
-    global_template->Set(ASCIILiteralToV8String(isolate_, "alert"), alert_template);
+        v8::FunctionTemplate::New(&AlertCallback, v8_this_);
+    global_template->Set(ASCIILiteralToV8String("alert"), alert_template);
 
     v8::Local<v8::FunctionTemplate> my_ip_address_template =
-        v8::FunctionTemplate::New(isolate_, &MyIpAddressCallback, v8_this);
-    global_template->Set(ASCIILiteralToV8String(isolate_, "myIpAddress"),
+        v8::FunctionTemplate::New(&MyIpAddressCallback, v8_this_);
+    global_template->Set(ASCIILiteralToV8String("myIpAddress"),
         my_ip_address_template);
 
     v8::Local<v8::FunctionTemplate> dns_resolve_template =
-        v8::FunctionTemplate::New(isolate_, &DnsResolveCallback, v8_this);
-    global_template->Set(ASCIILiteralToV8String(isolate_, "dnsResolve"),
+        v8::FunctionTemplate::New(&DnsResolveCallback, v8_this_);
+    global_template->Set(ASCIILiteralToV8String("dnsResolve"),
         dns_resolve_template);
 
     // Microsoft's PAC extensions:
 
     v8::Local<v8::FunctionTemplate> dns_resolve_ex_template =
-        v8::FunctionTemplate::New(isolate_, &DnsResolveExCallback, v8_this);
-    global_template->Set(ASCIILiteralToV8String(isolate_, "dnsResolveEx"),
+        v8::FunctionTemplate::New(&DnsResolveExCallback, v8_this_);
+    global_template->Set(ASCIILiteralToV8String("dnsResolveEx"),
                          dns_resolve_ex_template);
 
     v8::Local<v8::FunctionTemplate> my_ip_address_ex_template =
-        v8::FunctionTemplate::New(isolate_, &MyIpAddressExCallback, v8_this);
-    global_template->Set(ASCIILiteralToV8String(isolate_, "myIpAddressEx"),
+        v8::FunctionTemplate::New(&MyIpAddressExCallback, v8_this_);
+    global_template->Set(ASCIILiteralToV8String("myIpAddressEx"),
                          my_ip_address_ex_template);
 
     v8::Local<v8::FunctionTemplate> sort_ip_address_list_template =
-        v8::FunctionTemplate::New(isolate_, &SortIpAddressListCallback, v8_this);
-    global_template->Set(ASCIILiteralToV8String(isolate_, "sortIpAddressList"),
+        v8::FunctionTemplate::New(&SortIpAddressListCallback, v8_this_);
+    global_template->Set(ASCIILiteralToV8String("sortIpAddressList"),
                          sort_ip_address_list_template);
 
     v8::Local<v8::FunctionTemplate> is_in_net_ex_template =
-        v8::FunctionTemplate::New(isolate_, &IsInNetExCallback, v8_this);
-    global_template->Set(ASCIILiteralToV8String(isolate_, "isInNetEx"),
+        v8::FunctionTemplate::New(&IsInNetExCallback, v8_this_);
+    global_template->Set(ASCIILiteralToV8String("isInNetEx"),
                          is_in_net_ex_template);
 
-    v8_context_.Reset(
-        isolate_, v8::Context::New(isolate_, NULL, global_template));
+    v8_context_ = v8::Context::New(NULL, global_template);
 
-    v8::Local<v8::Context> context =
-        v8::Local<v8::Context>::New(isolate_, v8_context_);
-    v8::Context::Scope ctx(context);
+    v8::Context::Scope ctx(v8_context_);
 
     // Add the PAC utility functions to the environment.
     // (This script should never fail, as it is a string literal!)
     // Note that the two string literals are concatenated.
     int rv = RunScript(
-        ASCIILiteralToV8String(isolate_,
+        ASCIILiteralToV8String(
             PROXY_RESOLVER_SCRIPT
             PROXY_RESOLVER_SCRIPT_EX),
         kPacUtilityResourceName);
@@ -482,7 +477,7 @@
     }
 
     // Add the user's PAC code to the environment.
-    rv = RunScript(UTF16StringToV8String(isolate_, pac_script), kPacResourceName);
+    rv = RunScript(UTF16StringToV8String(pac_script), kPacResourceName);
     if (rv != OK) {
       return rv;
     }
@@ -497,17 +492,19 @@
   }
 
   void PurgeMemory() {
-    v8::Locker locked(isolate_);
-    v8::Isolate::Scope isolate_scope(isolate_);
-    v8::V8::LowMemoryNotification();
+    v8::Locker locked;
+    // Repeatedly call the V8 idle notification until it returns true ("nothing
+    // more to free").  Note that it makes more sense to do this than to
+    // implement a new "delete everything" pass because object references make
+    // it difficult to free everything possible in just one pass.
+    while (!v8::V8::IdleNotification())
+      ;
   }
 
  private:
   bool GetFindProxyForURL(v8::Local<v8::Value>* function) {
-    v8::Local<v8::Context> context =
-        v8::Local<v8::Context>::New(isolate_, v8_context_);
-    *function = context->Global()->Get(
-        ASCIILiteralToV8String(isolate_, "FindProxyForURL"));
+    *function = v8_context_->Global()->Get(
+        ASCIILiteralToV8String("FindProxyForURL"));
     return (*function)->IsFunction();
   }
 
@@ -525,7 +522,7 @@
 
     // Compile the script.
     v8::ScriptOrigin origin =
-        v8::ScriptOrigin(ASCIILiteralToV8String(isolate_, script_name));
+        v8::ScriptOrigin(ASCIILiteralToV8String(script_name));
     v8::Local<v8::Script> code = v8::Script::Compile(script, &origin);
 
     // Execute.
@@ -542,7 +539,7 @@
   }
 
   // V8 callback for when "alert()" is invoked by the PAC script.
-  static void AlertCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
+  static v8::Handle<v8::Value> AlertCallback(const v8::Arguments& args) {
     Context* context =
         static_cast<Context*>(v8::External::Cast(*args.Data())->Value());
 
@@ -555,17 +552,16 @@
       android::String16 wundef(undef8);
       message = wundef;
     } else {
-      if (!V8ObjectToUTF16String(args[0], &message, args.GetIsolate()))
-        return;  // toString() threw an exception.
+      if (!V8ObjectToUTF16String(args[0], &message))
+        return v8::Undefined();  // toString() threw an exception.
     }
 
     context->error_listener_->AlertMessage(message);
-    return;
+    return v8::Undefined();
   }
 
   // V8 callback for when "myIpAddress()" is invoked by the PAC script.
-  static void MyIpAddressCallback(
-      const v8::FunctionCallbackInfo<v8::Value>& args) {
+  static v8::Handle<v8::Value> MyIpAddressCallback(const v8::Arguments& args) {
     Context* context =
         static_cast<Context*>(v8::External::Cast(*args.Data())->Value());
 
@@ -573,23 +569,21 @@
     bool success;
 
     {
-      v8::Unlocker unlocker(args.GetIsolate());
+      v8::Unlocker unlocker;
 
       // We shouldn't be called with any arguments, but will not complain if
       // we are.
       success = context->js_bindings_->MyIpAddress(&result);
     }
 
-    if (!success) {
-      args.GetReturnValue().Set(ASCIILiteralToV8String(args.GetIsolate(), "127.0.0.1"));
-    } else {
-      args.GetReturnValue().Set(ASCIIStringToV8String(result));
-    }
+    if (!success)
+      return ASCIILiteralToV8String("127.0.0.1");
+    return ASCIIStringToV8String(result);
   }
 
   // V8 callback for when "myIpAddressEx()" is invoked by the PAC script.
-  static void MyIpAddressExCallback(
-      const v8::FunctionCallbackInfo<v8::Value>& args) {
+  static v8::Handle<v8::Value> MyIpAddressExCallback(
+      const v8::Arguments& args) {
     Context* context =
         static_cast<Context*>(v8::External::Cast(*args.Data())->Value());
 
@@ -597,7 +591,7 @@
     bool success;
 
     {
-      v8::Unlocker unlocker(args.GetIsolate());
+      v8::Unlocker unlocker;
 
       // We shouldn't be called with any arguments, but will not complain if
       // we are.
@@ -606,98 +600,83 @@
 
     if (!success)
       ip_address_list = std::string();
-    args.GetReturnValue().Set(ASCIIStringToV8String(ip_address_list));
+    return ASCIIStringToV8String(ip_address_list);
   }
 
   // V8 callback for when "dnsResolve()" is invoked by the PAC script.
-  static void DnsResolveCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
+  static v8::Handle<v8::Value> DnsResolveCallback(const v8::Arguments& args) {
     Context* context =
         static_cast<Context*>(v8::External::Cast(*args.Data())->Value());
 
     // We need at least one string argument.
     std::string hostname;
-    if (!GetHostnameArgument(args, &hostname)) {
-      return;
-    }
+    if (!GetHostnameArgument(args, &hostname))
+      return v8::Null();
 
     std::string ip_address;
     bool success;
 
     {
-      v8::Unlocker unlocker(args.GetIsolate());
+      v8::Unlocker unlocker;
       success = context->js_bindings_->DnsResolve(hostname, &ip_address);
     }
 
-    if (success) {
-      args.GetReturnValue().Set(ASCIIStringToV8String(ip_address));
-    } else {
-      args.GetReturnValue().SetNull();
-    }
+    return success ? ASCIIStringToV8String(ip_address) : v8::Null();
   }
 
   // V8 callback for when "dnsResolveEx()" is invoked by the PAC script.
-  static void DnsResolveExCallback(
-      const v8::FunctionCallbackInfo<v8::Value>& args) {
+  static v8::Handle<v8::Value> DnsResolveExCallback(const v8::Arguments& args) {
     Context* context =
         static_cast<Context*>(v8::External::Cast(*args.Data())->Value());
 
     // We need at least one string argument.
     std::string hostname;
-    if (!GetHostnameArgument(args, &hostname)) {
-      args.GetReturnValue().SetNull();
-      return;
-    }
+    if (!GetHostnameArgument(args, &hostname))
+      return v8::Undefined();
 
     std::string ip_address_list;
     bool success;
 
     {
-      v8::Unlocker unlocker(args.GetIsolate());
+      v8::Unlocker unlocker;
       success = context->js_bindings_->DnsResolveEx(hostname, &ip_address_list);
     }
 
     if (!success)
       ip_address_list = std::string();
 
-    args.GetReturnValue().Set(ASCIIStringToV8String(ip_address_list));
+    return ASCIIStringToV8String(ip_address_list);
   }
 
   // V8 callback for when "sortIpAddressList()" is invoked by the PAC script.
-  static void SortIpAddressListCallback(
-      const v8::FunctionCallbackInfo<v8::Value>& args) {
+  static v8::Handle<v8::Value> SortIpAddressListCallback(
+      const v8::Arguments& args) {
     // We need at least one string argument.
-    if (args.Length() == 0 || args[0].IsEmpty() || !args[0]->IsString()) {
-      args.GetReturnValue().SetNull();
-      return;
-    }
+    if (args.Length() == 0 || args[0].IsEmpty() || !args[0]->IsString())
+      return v8::Null();
 
     std::string ip_address_list = V8StringToUTF8(args[0]->ToString());
     std::string sorted_ip_address_list;
     bool success = SortIpAddressList(ip_address_list, &sorted_ip_address_list);
-    if (!success) {
-      args.GetReturnValue().Set(false);
-      return;
-    }
-    args.GetReturnValue().Set(ASCIIStringToV8String(sorted_ip_address_list));
+    if (!success)
+      return v8::False();
+    return ASCIIStringToV8String(sorted_ip_address_list);
   }
 
   // V8 callback for when "isInNetEx()" is invoked by the PAC script.
-  static void IsInNetExCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
+  static v8::Handle<v8::Value> IsInNetExCallback(const v8::Arguments& args) {
     // We need at least 2 string arguments.
     if (args.Length() < 2 || args[0].IsEmpty() || !args[0]->IsString() ||
-        args[1].IsEmpty() || !args[1]->IsString()) {
-      args.GetReturnValue().SetNull();
-      return;
-    }
+        args[1].IsEmpty() || !args[1]->IsString())
+      return v8::Null();
 
     std::string ip_address = V8StringToUTF8(args[0]->ToString());
     std::string ip_prefix = V8StringToUTF8(args[1]->ToString());
-    args.GetReturnValue().Set(IsInNetEx(ip_address, ip_prefix));
+    return IsInNetEx(ip_address, ip_prefix) ? v8::True() : v8::False();
   }
 
   ProxyResolverJSBindings* js_bindings_;
   ProxyErrorListener* error_listener_;
-  v8::Isolate* isolate_;
   v8::Persistent<v8::External> v8_this_;
   v8::Persistent<v8::Context> v8_context_;
 };
@@ -748,7 +727,7 @@
     return ERR_PAC_SCRIPT_FAILED;
 
   // Try parsing the PAC script.
-  context_ = new Context(js_bindings_, error_listener_, v8::Isolate::New());
+  context_ = new Context(js_bindings_, error_listener_);
   int rv;
   if ((rv = context_->InitV8(script_data)) != OK) {
     context_ = NULL;