Merge "Fixup libpac for V8 4.9.385.28" am: 83f3d60 am: 35a6630
am: 2ff2dd1

* commit '2ff2dd13fb349015dc326d7e54df145e48b4aed6':
  Fixup libpac for V8 4.9.385.28

Change-Id: I387a1390b9e39783f8edf58936b01ebd3a511956
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_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