am 548e12e1: Merge \\\"Update proxy resolver and tests for V8 API changes.\\\" am: 3451232e59 am: 0cfe31b2d0
am: 5c19a4c5d8

Change-Id: I5c7f3a2a60f74ef9787d06b875f92407680545c8
diff --git a/Android.mk b/Android.mk
index d25755f..e8a2b26 100644
--- a/Android.mk
+++ b/Android.mk
@@ -23,7 +23,7 @@
 
 LOCAL_STATIC_LIBRARIES := libv8
 
-LOCAL_SHARED_LIBRARIES := libutils liblog
+LOCAL_SHARED_LIBRARIES := libutils liblog libicuuc libicui18n
 
 LOCAL_CXX_STL := libc++
 
diff --git a/src/proxy_resolver_js_bindings.cc b/src/proxy_resolver_js_bindings.cc
index 897fde4..0686f23 100644
--- a/src/proxy_resolver_js_bindings.cc
+++ b/src/proxy_resolver_js_bindings.cc
@@ -5,6 +5,7 @@
 #include "proxy_resolver_js_bindings.h"
 #include "proxy_resolver_v8.h"
 
+#include <arpa/inet.h>
 #include <netdb.h>
 #include <unistd.h>
 #include <cstddef>
@@ -64,10 +65,16 @@
                       std::string* first_ip_address) {
     struct hostent* he = gethostbyname(host.c_str());
 
-    if (he == NULL) {
+    if (he == NULL || he->h_addr == NULL || he->h_addrtype != AF_INET) {
       return false;
     }
-    *first_ip_address = std::string(he->h_addr);
+
+    char tmp[INET_ADDRSTRLEN];
+    if (inet_ntop(he->h_addrtype, he->h_addr, tmp, sizeof(tmp)) == NULL) {
+        return false;
+    }
+
+    *first_ip_address = std::string(tmp);
     return true;
   }
 
diff --git a/src/proxy_resolver_v8.cc b/src/proxy_resolver_v8.cc
index 533ef88..f978694 100644
--- a/src/proxy_resolver_v8.cc
+++ b/src/proxy_resolver_v8.cc
@@ -160,7 +160,7 @@
   char16_t* buf = new char16_t[len + 1];
   s->Write(reinterpret_cast<uint16_t*>(buf), 0, len);
   android::String16 ret(buf, len);
-  delete buf;
+  delete[] buf;
   return ret;
 }
 
@@ -343,6 +343,17 @@
 
 }  // namespace
 
+class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
+  public:
+   virtual void* Allocate(size_t length) {
+     void* data = AllocateUninitialized(length);
+     return data == NULL ? data : memset(data, 0, length);
+   }
+   virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
+   virtual void Free(void* data, size_t) { free(data); }
+};
+
+
 // ProxyResolverV8::Context ---------------------------------------------------
 
 class ProxyResolverV8::Context {
@@ -701,15 +712,18 @@
 
 // ProxyResolverV8 ------------------------------------------------------------
 
+bool ProxyResolverV8::initialized_for_this_process_ = false;
+
 ProxyResolverV8::ProxyResolverV8(
     ProxyResolverJSBindings* custom_js_bindings,
     ProxyErrorListener* error_listener)
     : context_(NULL), js_bindings_(custom_js_bindings),
       error_listener_(error_listener) {
-  if (v8::V8::GetCurrentPlatform() == NULL) {
+  if (!initialized_for_this_process_) {
     v8::Platform* platform = v8::platform::CreateDefaultPlatform();
     v8::V8::InitializePlatform(platform);
     v8::V8::Initialize();
+    initialized_for_this_process_ = true;
   }
 }
 
@@ -749,7 +763,11 @@
     return ERR_PAC_SCRIPT_FAILED;
 
   // Try parsing the PAC script.
-  context_ = new Context(js_bindings_, error_listener_, v8::Isolate::New());
+  ArrayBufferAllocator allocator;
+  v8::Isolate::CreateParams create_params;
+  create_params.array_buffer_allocator = &allocator;
+
+  context_ = new Context(js_bindings_, error_listener_, v8::Isolate::New(create_params));
   int rv;
   if ((rv = context_->InitV8(script_data)) != OK) {
     context_ = NULL;
diff --git a/src/proxy_resolver_v8.h b/src/proxy_resolver_v8.h
index 13a1bd8..9b74e29 100644
--- a/src/proxy_resolver_v8.h
+++ b/src/proxy_resolver_v8.h
@@ -71,6 +71,7 @@
 
   ProxyResolverJSBindings* js_bindings_;
   ProxyErrorListener* error_listener_;
+  static bool initialized_for_this_process_;
 };
 
 }  // namespace net