Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 5 | #include "proxy_resolver_v8.h" |
| 6 | |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 7 | #include <algorithm> |
Ben Murdoch | e6933f5 | 2014-12-05 12:45:49 +0000 | [diff] [blame] | 8 | #include <cstdio> |
| 9 | #include <iostream> |
| 10 | #include <string> |
| 11 | #include <utils/String8.h> |
| 12 | #include <v8.h> |
Emily Bernier | 11e292b | 2015-06-23 16:53:06 -0400 | [diff] [blame^] | 13 | #include <libplatform/libplatform.h> |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Ben Murdoch | e6933f5 | 2014-12-05 12:45:49 +0000 | [diff] [blame] | 16 | #include "net_util.h" |
| 17 | #include "proxy_resolver_script.h" |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 18 | |
| 19 | // Notes on the javascript environment: |
| 20 | // |
| 21 | // For the majority of the PAC utility functions, we use the same code |
| 22 | // as Firefox. See the javascript library that proxy_resolver_scipt.h |
| 23 | // pulls in. |
| 24 | // |
| 25 | // In addition, we implement a subset of Microsoft's extensions to PAC. |
| 26 | // - myIpAddressEx() |
| 27 | // - dnsResolveEx() |
| 28 | // - isResolvableEx() |
| 29 | // - isInNetEx() |
| 30 | // - sortIpAddressList() |
| 31 | // |
| 32 | // It is worth noting that the original PAC specification does not describe |
| 33 | // the return values on failure. Consequently, there are compatibility |
| 34 | // differences between browsers on what to return on failure, which are |
| 35 | // illustrated below: |
| 36 | // |
| 37 | // --------------------+-------------+-------------------+-------------- |
| 38 | // | Firefox3 | InternetExplorer8 | --> Us <--- |
| 39 | // --------------------+-------------+-------------------+-------------- |
| 40 | // myIpAddress() | "127.0.0.1" | ??? | "127.0.0.1" |
| 41 | // dnsResolve() | null | false | null |
| 42 | // myIpAddressEx() | N/A | "" | "" |
| 43 | // sortIpAddressList() | N/A | false | false |
| 44 | // dnsResolveEx() | N/A | "" | "" |
| 45 | // isInNetEx() | N/A | false | false |
| 46 | // --------------------+-------------+-------------------+-------------- |
| 47 | // |
| 48 | // TODO: The cell above reading ??? means I didn't test it. |
| 49 | // |
| 50 | // Another difference is in how dnsResolve() and myIpAddress() are |
| 51 | // implemented -- whether they should restrict to IPv4 results, or |
| 52 | // include both IPv4 and IPv6. The following table illustrates the |
| 53 | // differences: |
| 54 | // |
| 55 | // --------------------+-------------+-------------------+-------------- |
| 56 | // | Firefox3 | InternetExplorer8 | --> Us <--- |
| 57 | // --------------------+-------------+-------------------+-------------- |
| 58 | // myIpAddress() | IPv4/IPv6 | IPv4 | IPv4 |
| 59 | // dnsResolve() | IPv4/IPv6 | IPv4 | IPv4 |
| 60 | // isResolvable() | IPv4/IPv6 | IPv4 | IPv4 |
| 61 | // myIpAddressEx() | N/A | IPv4/IPv6 | IPv4/IPv6 |
| 62 | // dnsResolveEx() | N/A | IPv4/IPv6 | IPv4/IPv6 |
| 63 | // sortIpAddressList() | N/A | IPv4/IPv6 | IPv4/IPv6 |
| 64 | // isResolvableEx() | N/A | IPv4/IPv6 | IPv4/IPv6 |
| 65 | // isInNetEx() | N/A | IPv4/IPv6 | IPv4/IPv6 |
| 66 | // -----------------+-------------+-------------------+-------------- |
| 67 | |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 68 | static bool DoIsStringASCII(const android::String16& str) { |
| 69 | for (size_t i = 0; i < str.size(); i++) { |
| 70 | unsigned short c = str.string()[i]; |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 71 | if (c > 0x7F) |
| 72 | return false; |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 77 | bool IsStringASCII(const android::String16& str) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 78 | return DoIsStringASCII(str); |
| 79 | } |
| 80 | |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 81 | namespace net { |
| 82 | |
| 83 | namespace { |
| 84 | |
| 85 | // Pseudo-name for the PAC script. |
| 86 | const char kPacResourceName[] = "proxy-pac-script.js"; |
| 87 | // Pseudo-name for the PAC utility script. |
| 88 | const char kPacUtilityResourceName[] = "proxy-pac-utility-script.js"; |
| 89 | |
| 90 | // External string wrapper so V8 can access the UTF16 string wrapped by |
| 91 | // ProxyResolverScriptData. |
| 92 | class V8ExternalStringFromScriptData |
| 93 | : public v8::String::ExternalStringResource { |
| 94 | public: |
| 95 | explicit V8ExternalStringFromScriptData( |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 96 | const android::String16& script_data) |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 97 | : script_data_(script_data) {} |
| 98 | |
| 99 | virtual const uint16_t* data() const { |
Dan Albert | d438de8 | 2014-11-20 11:20:39 -0800 | [diff] [blame] | 100 | return reinterpret_cast<const uint16_t*>(script_data_.string()); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | virtual size_t length() const { |
| 104 | return script_data_.size(); |
| 105 | } |
| 106 | |
| 107 | private: |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 108 | const android::String16& script_data_; |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 109 | // DISALLOW_COPY_AND_ASSIGN(V8ExternalStringFromScriptData); |
| 110 | }; |
| 111 | |
| 112 | // External string wrapper so V8 can access a string literal. |
Torne (Richard Coles) | 9ae4ac5 | 2014-10-22 11:19:47 +0100 | [diff] [blame] | 113 | class V8ExternalASCIILiteral |
| 114 | : public v8::String::ExternalOneByteStringResource { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 115 | public: |
| 116 | // |ascii| must be a NULL-terminated C string, and must remain valid |
| 117 | // throughout this object's lifetime. |
| 118 | V8ExternalASCIILiteral(const char* ascii, size_t length) |
| 119 | : ascii_(ascii), length_(length) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | virtual const char* data() const { |
| 123 | return ascii_; |
| 124 | } |
| 125 | |
| 126 | virtual size_t length() const { |
| 127 | return length_; |
| 128 | } |
| 129 | |
| 130 | private: |
| 131 | const char* ascii_; |
| 132 | size_t length_; |
| 133 | }; |
| 134 | |
| 135 | // When creating a v8::String from a C++ string we have two choices: create |
| 136 | // a copy, or create a wrapper that shares the same underlying storage. |
| 137 | // For small strings it is better to just make a copy, whereas for large |
| 138 | // strings there are savings by sharing the storage. This number identifies |
| 139 | // the cutoff length for when to start wrapping rather than creating copies. |
| 140 | const size_t kMaxStringBytesForCopy = 256; |
| 141 | |
| 142 | template <class string_type> |
| 143 | inline typename string_type::value_type* WriteInto(string_type* str, |
| 144 | size_t length_with_null) { |
| 145 | str->reserve(length_with_null); |
| 146 | str->resize(length_with_null - 1); |
| 147 | return &((*str)[0]); |
| 148 | } |
| 149 | |
| 150 | // Converts a V8 String to a UTF8 std::string. |
| 151 | std::string V8StringToUTF8(v8::Handle<v8::String> s) { |
| 152 | std::string result; |
| 153 | s->WriteUtf8(WriteInto(&result, s->Length() + 1)); |
| 154 | return result; |
| 155 | } |
| 156 | |
| 157 | // Converts a V8 String to a UTF16 string. |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 158 | android::String16 V8StringToUTF16(v8::Handle<v8::String> s) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 159 | int len = s->Length(); |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 160 | char16_t* buf = new char16_t[len + 1]; |
Dan Albert | d438de8 | 2014-11-20 11:20:39 -0800 | [diff] [blame] | 161 | s->Write(reinterpret_cast<uint16_t*>(buf), 0, len); |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 162 | android::String16 ret(buf, len); |
| 163 | delete buf; |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | std::string UTF16ToASCII(const android::String16& str) { |
Jason Monk | 40adcb5 | 2013-08-23 17:19:31 -0400 | [diff] [blame] | 168 | android::String8 rstr(str); |
| 169 | return std::string(rstr.string()); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // Converts an ASCII std::string to a V8 string. |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 173 | v8::Local<v8::String> ASCIIStringToV8String(v8::Isolate* isolate, const std::string& s) { |
| 174 | return v8::String::NewFromUtf8(isolate, s.data(), v8::String::kNormalString, s.size()); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 177 | v8::Local<v8::String> UTF16StringToV8String(v8::Isolate* isolate, const android::String16& s) { |
Dan Albert | d438de8 | 2014-11-20 11:20:39 -0800 | [diff] [blame] | 178 | return v8::String::NewFromTwoByte( |
| 179 | isolate, reinterpret_cast<const uint16_t*>(s.string()), |
| 180 | v8::String::kNormalString, s.size()); |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 183 | // Converts an ASCII string literal to a V8 string. |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 184 | v8::Local<v8::String> ASCIILiteralToV8String(v8::Isolate* isolate, const char* ascii) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 185 | // DCHECK(IsStringASCII(ascii)); |
| 186 | size_t length = strlen(ascii); |
| 187 | if (length <= kMaxStringBytesForCopy) |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 188 | return v8::String::NewFromUtf8(isolate, ascii, v8::String::kNormalString, length); |
| 189 | return v8::String::NewExternal(isolate, new V8ExternalASCIILiteral(ascii, length)); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // Stringizes a V8 object by calling its toString() method. Returns true |
| 193 | // on success. This may fail if the toString() throws an exception. |
| 194 | bool V8ObjectToUTF16String(v8::Handle<v8::Value> object, |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 195 | android::String16* utf16_result, |
| 196 | v8::Isolate* isolate) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 197 | if (object.IsEmpty()) |
| 198 | return false; |
| 199 | |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 200 | v8::HandleScope scope(isolate); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 201 | v8::Local<v8::String> str_object = object->ToString(); |
| 202 | if (str_object.IsEmpty()) |
| 203 | return false; |
| 204 | *utf16_result = V8StringToUTF16(str_object); |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | // Extracts an hostname argument from |args|. On success returns true |
| 209 | // and fills |*hostname| with the result. |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 210 | bool GetHostnameArgument(const v8::FunctionCallbackInfo<v8::Value>& args, std::string* hostname) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 211 | // The first argument should be a string. |
| 212 | if (args.Length() == 0 || args[0].IsEmpty() || !args[0]->IsString()) |
| 213 | return false; |
| 214 | |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 215 | const android::String16 hostname_utf16 = V8StringToUTF16(args[0]->ToString()); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 216 | |
| 217 | // If the hostname is already in ASCII, simply return it as is. |
| 218 | if (IsStringASCII(hostname_utf16)) { |
| 219 | *hostname = UTF16ToASCII(hostname_utf16); |
| 220 | return true; |
| 221 | } |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | // Wrapper for passing around IP address strings and IPAddressNumber objects. |
| 226 | struct IPAddress { |
| 227 | IPAddress(const std::string& ip_string, const IPAddressNumber& ip_number) |
| 228 | : string_value(ip_string), |
| 229 | ip_address_number(ip_number) { |
| 230 | } |
| 231 | |
| 232 | // Used for sorting IP addresses in ascending order in SortIpAddressList(). |
| 233 | // IP6 addresses are placed ahead of IPv4 addresses. |
| 234 | bool operator<(const IPAddress& rhs) const { |
| 235 | const IPAddressNumber& ip1 = this->ip_address_number; |
| 236 | const IPAddressNumber& ip2 = rhs.ip_address_number; |
| 237 | if (ip1.size() != ip2.size()) |
| 238 | return ip1.size() > ip2.size(); // IPv6 before IPv4. |
| 239 | return memcmp(&ip1[0], &ip2[0], ip1.size()) < 0; // Ascending order. |
| 240 | } |
| 241 | |
| 242 | std::string string_value; |
| 243 | IPAddressNumber ip_address_number; |
| 244 | }; |
| 245 | |
| 246 | template<typename STR> |
| 247 | bool RemoveCharsT(const STR& input, |
| 248 | const typename STR::value_type remove_chars[], |
| 249 | STR* output) { |
| 250 | bool removed = false; |
| 251 | size_t found; |
| 252 | |
| 253 | *output = input; |
| 254 | |
| 255 | found = output->find_first_of(remove_chars); |
| 256 | while (found != STR::npos) { |
| 257 | removed = true; |
| 258 | output->replace(found, 1, STR()); |
| 259 | found = output->find_first_of(remove_chars, found); |
| 260 | } |
| 261 | |
| 262 | return removed; |
| 263 | } |
| 264 | |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 265 | bool RemoveChars(const std::string& input, |
| 266 | const char remove_chars[], |
| 267 | std::string* output) { |
| 268 | return RemoveCharsT(input, remove_chars, output); |
| 269 | } |
| 270 | |
| 271 | // Handler for "sortIpAddressList(IpAddressList)". |ip_address_list| is a |
| 272 | // semi-colon delimited string containing IP addresses. |
| 273 | // |sorted_ip_address_list| is the resulting list of sorted semi-colon delimited |
| 274 | // IP addresses or an empty string if unable to sort the IP address list. |
| 275 | // Returns 'true' if the sorting was successful, and 'false' if the input was an |
| 276 | // empty string, a string of separators (";" in this case), or if any of the IP |
| 277 | // addresses in the input list failed to parse. |
| 278 | bool SortIpAddressList(const std::string& ip_address_list, |
| 279 | std::string* sorted_ip_address_list) { |
| 280 | sorted_ip_address_list->clear(); |
| 281 | |
| 282 | // Strip all whitespace (mimics IE behavior). |
| 283 | std::string cleaned_ip_address_list; |
| 284 | RemoveChars(ip_address_list, " \t", &cleaned_ip_address_list); |
| 285 | if (cleaned_ip_address_list.empty()) |
| 286 | return false; |
| 287 | |
| 288 | // Split-up IP addresses and store them in a vector. |
| 289 | std::vector<IPAddress> ip_vector; |
| 290 | IPAddressNumber ip_num; |
| 291 | char *tok_list = strtok((char *)cleaned_ip_address_list.c_str(), ";"); |
| 292 | while (tok_list != NULL) { |
| 293 | if (!ParseIPLiteralToNumber(tok_list, &ip_num)) |
| 294 | return false; |
| 295 | ip_vector.push_back(IPAddress(tok_list, ip_num)); |
Jason Monk | 86b75a5 | 2013-08-23 17:10:51 -0400 | [diff] [blame] | 296 | tok_list = strtok(NULL, ";"); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if (ip_vector.empty()) // Can happen if we have something like |
| 300 | return false; // sortIpAddressList(";") or sortIpAddressList("; ;") |
| 301 | |
| 302 | // Sort lists according to ascending numeric value. |
| 303 | if (ip_vector.size() > 1) |
| 304 | std::stable_sort(ip_vector.begin(), ip_vector.end()); |
| 305 | |
| 306 | // Return a semi-colon delimited list of sorted addresses (IPv6 followed by |
| 307 | // IPv4). |
| 308 | for (size_t i = 0; i < ip_vector.size(); ++i) { |
| 309 | if (i > 0) |
| 310 | *sorted_ip_address_list += ";"; |
| 311 | *sorted_ip_address_list += ip_vector[i].string_value; |
| 312 | } |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | // Handler for "isInNetEx(ip_address, ip_prefix)". |ip_address| is a string |
| 318 | // containing an IPv4/IPv6 address, and |ip_prefix| is a string containg a |
| 319 | // slash-delimited IP prefix with the top 'n' bits specified in the bit |
| 320 | // field. This returns 'true' if the address is in the same subnet, and |
| 321 | // 'false' otherwise. Also returns 'false' if the prefix is in an incorrect |
| 322 | // format, or if an address and prefix of different types are used (e.g. IPv6 |
| 323 | // address and IPv4 prefix). |
| 324 | bool IsInNetEx(const std::string& ip_address, const std::string& ip_prefix) { |
| 325 | IPAddressNumber address; |
Jason Monk | eafc0fa | 2013-08-23 17:05:52 -0400 | [diff] [blame] | 326 | std::string cleaned_ip_address; |
| 327 | if (RemoveChars(ip_address, " \t", &cleaned_ip_address)) |
| 328 | return false; |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 329 | if (!ParseIPLiteralToNumber(ip_address, &address)) |
| 330 | return false; |
| 331 | |
| 332 | IPAddressNumber prefix; |
| 333 | size_t prefix_length_in_bits; |
| 334 | if (!ParseCIDRBlock(ip_prefix, &prefix, &prefix_length_in_bits)) |
| 335 | return false; |
| 336 | |
| 337 | // Both |address| and |prefix| must be of the same type (IPv4 or IPv6). |
| 338 | if (address.size() != prefix.size()) |
| 339 | return false; |
| 340 | |
| 341 | return IPNumberMatchesPrefix(address, prefix, prefix_length_in_bits); |
| 342 | } |
| 343 | |
| 344 | } // namespace |
| 345 | |
| 346 | // ProxyResolverV8::Context --------------------------------------------------- |
| 347 | |
| 348 | class ProxyResolverV8::Context { |
| 349 | public: |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 350 | explicit Context(ProxyResolverJSBindings* js_bindings, |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 351 | ProxyErrorListener* error_listener, v8::Isolate* isolate) |
| 352 | : js_bindings_(js_bindings), error_listener_(error_listener), isolate_(isolate) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | ~Context() { |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 356 | v8::Locker locked(isolate_); |
| 357 | v8::Isolate::Scope isolate_scope(isolate_); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 358 | |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 359 | v8_this_.Reset(); |
| 360 | v8_context_.Reset(); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 361 | } |
| 362 | |
Jason Monk | 40adcb5 | 2013-08-23 17:19:31 -0400 | [diff] [blame] | 363 | int ResolveProxy(const android::String16 url, const android::String16 host, |
| 364 | android::String16* results) { |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 365 | v8::Locker locked(isolate_); |
| 366 | v8::Isolate::Scope isolate_scope(isolate_); |
| 367 | v8::HandleScope scope(isolate_); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 368 | |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 369 | v8::Local<v8::Context> context = |
| 370 | v8::Local<v8::Context>::New(isolate_, v8_context_); |
| 371 | v8::Context::Scope function_scope(context); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 372 | |
| 373 | v8::Local<v8::Value> function; |
| 374 | if (!GetFindProxyForURL(&function)) { |
Jason Monk | 40adcb5 | 2013-08-23 17:19:31 -0400 | [diff] [blame] | 375 | error_listener_->ErrorMessage( |
| 376 | android::String16("FindProxyForURL() is undefined")); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 377 | return ERR_PAC_SCRIPT_FAILED; |
| 378 | } |
| 379 | |
| 380 | v8::Handle<v8::Value> argv[] = { |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 381 | UTF16StringToV8String(isolate_, url), |
| 382 | UTF16StringToV8String(isolate_, host) }; |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 383 | |
| 384 | v8::TryCatch try_catch; |
| 385 | v8::Local<v8::Value> ret = v8::Function::Cast(*function)->Call( |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 386 | context->Global(), 2, argv); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 387 | |
| 388 | if (try_catch.HasCaught()) { |
Jason Monk | 40adcb5 | 2013-08-23 17:19:31 -0400 | [diff] [blame] | 389 | error_listener_->ErrorMessage( |
| 390 | V8StringToUTF16(try_catch.Message()->Get())); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 391 | return ERR_PAC_SCRIPT_FAILED; |
| 392 | } |
| 393 | |
| 394 | if (!ret->IsString()) { |
Jason Monk | 40adcb5 | 2013-08-23 17:19:31 -0400 | [diff] [blame] | 395 | error_listener_->ErrorMessage( |
| 396 | android::String16("FindProxyForURL() did not return a string.")); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 397 | return ERR_PAC_SCRIPT_FAILED; |
| 398 | } |
| 399 | |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 400 | *results = V8StringToUTF16(ret->ToString()); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 401 | |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 402 | if (!IsStringASCII(*results)) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 403 | // TODO: Rather than failing when a wide string is returned, we |
| 404 | // could extend the parsing to handle IDNA hostnames by |
| 405 | // converting them to ASCII punycode. |
| 406 | // crbug.com/47234 |
Jason Monk | 40adcb5 | 2013-08-23 17:19:31 -0400 | [diff] [blame] | 407 | error_listener_->ErrorMessage( |
| 408 | android::String16("FindProxyForURL() returned a non-ASCII string")); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 409 | return ERR_PAC_SCRIPT_FAILED; |
| 410 | } |
| 411 | |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 412 | return OK; |
| 413 | } |
| 414 | |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 415 | int InitV8(const android::String16& pac_script) { |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 416 | v8::Locker locked(isolate_); |
| 417 | v8::Isolate::Scope isolate_scope(isolate_); |
| 418 | v8::HandleScope scope(isolate_); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 419 | |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 420 | v8_this_.Reset(isolate_, v8::External::New(isolate_, this)); |
| 421 | v8::Local<v8::External> v8_this = |
| 422 | v8::Local<v8::External>::New(isolate_, v8_this_); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 423 | v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 424 | |
| 425 | // Attach the javascript bindings. |
| 426 | v8::Local<v8::FunctionTemplate> alert_template = |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 427 | v8::FunctionTemplate::New(isolate_, &AlertCallback, v8_this); |
| 428 | global_template->Set(ASCIILiteralToV8String(isolate_, "alert"), alert_template); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 429 | |
| 430 | v8::Local<v8::FunctionTemplate> my_ip_address_template = |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 431 | v8::FunctionTemplate::New(isolate_, &MyIpAddressCallback, v8_this); |
| 432 | global_template->Set(ASCIILiteralToV8String(isolate_, "myIpAddress"), |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 433 | my_ip_address_template); |
| 434 | |
| 435 | v8::Local<v8::FunctionTemplate> dns_resolve_template = |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 436 | v8::FunctionTemplate::New(isolate_, &DnsResolveCallback, v8_this); |
| 437 | global_template->Set(ASCIILiteralToV8String(isolate_, "dnsResolve"), |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 438 | dns_resolve_template); |
| 439 | |
| 440 | // Microsoft's PAC extensions: |
| 441 | |
| 442 | v8::Local<v8::FunctionTemplate> dns_resolve_ex_template = |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 443 | v8::FunctionTemplate::New(isolate_, &DnsResolveExCallback, v8_this); |
| 444 | global_template->Set(ASCIILiteralToV8String(isolate_, "dnsResolveEx"), |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 445 | dns_resolve_ex_template); |
| 446 | |
| 447 | v8::Local<v8::FunctionTemplate> my_ip_address_ex_template = |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 448 | v8::FunctionTemplate::New(isolate_, &MyIpAddressExCallback, v8_this); |
| 449 | global_template->Set(ASCIILiteralToV8String(isolate_, "myIpAddressEx"), |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 450 | my_ip_address_ex_template); |
| 451 | |
| 452 | v8::Local<v8::FunctionTemplate> sort_ip_address_list_template = |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 453 | v8::FunctionTemplate::New(isolate_, &SortIpAddressListCallback, v8_this); |
| 454 | global_template->Set(ASCIILiteralToV8String(isolate_, "sortIpAddressList"), |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 455 | sort_ip_address_list_template); |
| 456 | |
| 457 | v8::Local<v8::FunctionTemplate> is_in_net_ex_template = |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 458 | v8::FunctionTemplate::New(isolate_, &IsInNetExCallback, v8_this); |
| 459 | global_template->Set(ASCIILiteralToV8String(isolate_, "isInNetEx"), |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 460 | is_in_net_ex_template); |
| 461 | |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 462 | v8_context_.Reset( |
| 463 | isolate_, v8::Context::New(isolate_, NULL, global_template)); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 464 | |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 465 | v8::Local<v8::Context> context = |
| 466 | v8::Local<v8::Context>::New(isolate_, v8_context_); |
| 467 | v8::Context::Scope ctx(context); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 468 | |
| 469 | // Add the PAC utility functions to the environment. |
| 470 | // (This script should never fail, as it is a string literal!) |
| 471 | // Note that the two string literals are concatenated. |
| 472 | int rv = RunScript( |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 473 | ASCIILiteralToV8String(isolate_, |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 474 | PROXY_RESOLVER_SCRIPT |
| 475 | PROXY_RESOLVER_SCRIPT_EX), |
| 476 | kPacUtilityResourceName); |
| 477 | if (rv != OK) { |
| 478 | return rv; |
| 479 | } |
| 480 | |
| 481 | // Add the user's PAC code to the environment. |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 482 | rv = RunScript(UTF16StringToV8String(isolate_, pac_script), kPacResourceName); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 483 | if (rv != OK) { |
| 484 | return rv; |
| 485 | } |
| 486 | |
| 487 | // At a minimum, the FindProxyForURL() function must be defined for this |
| 488 | // to be a legitimiate PAC script. |
| 489 | v8::Local<v8::Value> function; |
| 490 | if (!GetFindProxyForURL(&function)) |
| 491 | return ERR_PAC_SCRIPT_FAILED; |
| 492 | |
| 493 | return OK; |
| 494 | } |
| 495 | |
| 496 | void PurgeMemory() { |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 497 | v8::Locker locked(isolate_); |
| 498 | v8::Isolate::Scope isolate_scope(isolate_); |
Ben Murdoch | e80b5fa | 2014-07-31 16:47:35 +0100 | [diff] [blame] | 499 | isolate_->LowMemoryNotification(); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | private: |
| 503 | bool GetFindProxyForURL(v8::Local<v8::Value>* function) { |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 504 | v8::Local<v8::Context> context = |
| 505 | v8::Local<v8::Context>::New(isolate_, v8_context_); |
| 506 | *function = context->Global()->Get( |
| 507 | ASCIILiteralToV8String(isolate_, "FindProxyForURL")); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 508 | return (*function)->IsFunction(); |
| 509 | } |
| 510 | |
| 511 | // Handle an exception thrown by V8. |
| 512 | void HandleError(v8::Handle<v8::Message> message) { |
| 513 | if (message.IsEmpty()) |
| 514 | return; |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 515 | error_listener_->ErrorMessage(V8StringToUTF16(message->Get())); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | // Compiles and runs |script| in the current V8 context. |
| 519 | // Returns OK on success, otherwise an error code. |
| 520 | int RunScript(v8::Handle<v8::String> script, const char* script_name) { |
| 521 | v8::TryCatch try_catch; |
| 522 | |
| 523 | // Compile the script. |
| 524 | v8::ScriptOrigin origin = |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 525 | v8::ScriptOrigin(ASCIILiteralToV8String(isolate_, script_name)); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 526 | v8::Local<v8::Script> code = v8::Script::Compile(script, &origin); |
| 527 | |
| 528 | // Execute. |
| 529 | if (!code.IsEmpty()) |
| 530 | code->Run(); |
| 531 | |
| 532 | // Check for errors. |
| 533 | if (try_catch.HasCaught()) { |
| 534 | HandleError(try_catch.Message()); |
| 535 | return ERR_PAC_SCRIPT_FAILED; |
| 536 | } |
| 537 | |
| 538 | return OK; |
| 539 | } |
| 540 | |
| 541 | // V8 callback for when "alert()" is invoked by the PAC script. |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 542 | static void AlertCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 543 | Context* context = |
| 544 | static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
| 545 | |
| 546 | // Like firefox we assume "undefined" if no argument was specified, and |
| 547 | // disregard any arguments beyond the first. |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 548 | android::String16 message; |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 549 | if (args.Length() == 0) { |
| 550 | std::string undef = "undefined"; |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 551 | android::String8 undef8(undef.c_str()); |
| 552 | android::String16 wundef(undef8); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 553 | message = wundef; |
| 554 | } else { |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 555 | if (!V8ObjectToUTF16String(args[0], &message, args.GetIsolate())) |
| 556 | return; // toString() threw an exception. |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 557 | } |
| 558 | |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 559 | context->error_listener_->AlertMessage(message); |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 560 | return; |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | // V8 callback for when "myIpAddress()" is invoked by the PAC script. |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 564 | static void MyIpAddressCallback( |
| 565 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 566 | Context* context = |
| 567 | static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
| 568 | |
| 569 | std::string result; |
| 570 | bool success; |
| 571 | |
| 572 | { |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 573 | v8::Unlocker unlocker(args.GetIsolate()); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 574 | |
| 575 | // We shouldn't be called with any arguments, but will not complain if |
| 576 | // we are. |
| 577 | success = context->js_bindings_->MyIpAddress(&result); |
| 578 | } |
| 579 | |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 580 | if (!success) { |
| 581 | args.GetReturnValue().Set(ASCIILiteralToV8String(args.GetIsolate(), "127.0.0.1")); |
| 582 | } else { |
| 583 | args.GetReturnValue().Set(ASCIIStringToV8String(args.GetIsolate(), result)); |
| 584 | } |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | // V8 callback for when "myIpAddressEx()" is invoked by the PAC script. |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 588 | static void MyIpAddressExCallback( |
| 589 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 590 | Context* context = |
| 591 | static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
| 592 | |
| 593 | std::string ip_address_list; |
| 594 | bool success; |
| 595 | |
| 596 | { |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 597 | v8::Unlocker unlocker(args.GetIsolate()); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 598 | |
| 599 | // We shouldn't be called with any arguments, but will not complain if |
| 600 | // we are. |
| 601 | success = context->js_bindings_->MyIpAddressEx(&ip_address_list); |
| 602 | } |
| 603 | |
| 604 | if (!success) |
| 605 | ip_address_list = std::string(); |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 606 | args.GetReturnValue().Set(ASCIIStringToV8String(args.GetIsolate(), ip_address_list)); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | // V8 callback for when "dnsResolve()" is invoked by the PAC script. |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 610 | static void DnsResolveCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 611 | Context* context = |
| 612 | static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
| 613 | |
| 614 | // We need at least one string argument. |
| 615 | std::string hostname; |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 616 | if (!GetHostnameArgument(args, &hostname)) { |
| 617 | return; |
| 618 | } |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 619 | |
| 620 | std::string ip_address; |
| 621 | bool success; |
| 622 | |
| 623 | { |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 624 | v8::Unlocker unlocker(args.GetIsolate()); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 625 | success = context->js_bindings_->DnsResolve(hostname, &ip_address); |
| 626 | } |
| 627 | |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 628 | if (success) { |
| 629 | args.GetReturnValue().Set(ASCIIStringToV8String(args.GetIsolate(), ip_address)); |
| 630 | } else { |
| 631 | args.GetReturnValue().SetNull(); |
| 632 | } |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | // V8 callback for when "dnsResolveEx()" is invoked by the PAC script. |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 636 | static void DnsResolveExCallback( |
| 637 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 638 | Context* context = |
| 639 | static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
| 640 | |
| 641 | // We need at least one string argument. |
| 642 | std::string hostname; |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 643 | if (!GetHostnameArgument(args, &hostname)) { |
| 644 | args.GetReturnValue().SetNull(); |
| 645 | return; |
| 646 | } |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 647 | |
| 648 | std::string ip_address_list; |
| 649 | bool success; |
| 650 | |
| 651 | { |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 652 | v8::Unlocker unlocker(args.GetIsolate()); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 653 | success = context->js_bindings_->DnsResolveEx(hostname, &ip_address_list); |
| 654 | } |
| 655 | |
| 656 | if (!success) |
| 657 | ip_address_list = std::string(); |
| 658 | |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 659 | args.GetReturnValue().Set(ASCIIStringToV8String(args.GetIsolate(), ip_address_list)); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | // V8 callback for when "sortIpAddressList()" is invoked by the PAC script. |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 663 | static void SortIpAddressListCallback( |
| 664 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 665 | // We need at least one string argument. |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 666 | if (args.Length() == 0 || args[0].IsEmpty() || !args[0]->IsString()) { |
| 667 | args.GetReturnValue().SetNull(); |
| 668 | return; |
| 669 | } |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 670 | |
| 671 | std::string ip_address_list = V8StringToUTF8(args[0]->ToString()); |
| 672 | std::string sorted_ip_address_list; |
| 673 | bool success = SortIpAddressList(ip_address_list, &sorted_ip_address_list); |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 674 | if (!success) { |
| 675 | args.GetReturnValue().Set(false); |
| 676 | return; |
| 677 | } |
| 678 | args.GetReturnValue().Set(ASCIIStringToV8String(args.GetIsolate(), sorted_ip_address_list)); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | // V8 callback for when "isInNetEx()" is invoked by the PAC script. |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 682 | static void IsInNetExCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 683 | // We need at least 2 string arguments. |
| 684 | if (args.Length() < 2 || args[0].IsEmpty() || !args[0]->IsString() || |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 685 | args[1].IsEmpty() || !args[1]->IsString()) { |
| 686 | args.GetReturnValue().SetNull(); |
| 687 | return; |
| 688 | } |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 689 | |
| 690 | std::string ip_address = V8StringToUTF8(args[0]->ToString()); |
| 691 | std::string ip_prefix = V8StringToUTF8(args[1]->ToString()); |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 692 | args.GetReturnValue().Set(IsInNetEx(ip_address, ip_prefix)); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | ProxyResolverJSBindings* js_bindings_; |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 696 | ProxyErrorListener* error_listener_; |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 697 | v8::Isolate* isolate_; |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 698 | v8::Persistent<v8::External> v8_this_; |
| 699 | v8::Persistent<v8::Context> v8_context_; |
| 700 | }; |
| 701 | |
| 702 | // ProxyResolverV8 ------------------------------------------------------------ |
| 703 | |
| 704 | ProxyResolverV8::ProxyResolverV8( |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 705 | ProxyResolverJSBindings* custom_js_bindings, |
| 706 | ProxyErrorListener* error_listener) |
| 707 | : context_(NULL), js_bindings_(custom_js_bindings), |
| 708 | error_listener_(error_listener) { |
Emily Bernier | 11e292b | 2015-06-23 16:53:06 -0400 | [diff] [blame^] | 709 | if (v8::V8::GetCurrentPlatform() == NULL) { |
| 710 | v8::Platform* platform = v8::platform::CreateDefaultPlatform(); |
| 711 | v8::V8::InitializePlatform(platform); |
| 712 | v8::V8::Initialize(); |
| 713 | } |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | ProxyResolverV8::~ProxyResolverV8() { |
Jason Monk | c06a8d6 | 2013-08-19 10:36:23 -0400 | [diff] [blame] | 717 | if (context_ != NULL) { |
| 718 | delete context_; |
| 719 | context_ = NULL; |
| 720 | } |
| 721 | if (js_bindings_ != NULL) { |
| 722 | delete js_bindings_; |
| 723 | } |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 724 | } |
| 725 | |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 726 | int ProxyResolverV8::GetProxyForURL(const android::String16 spec, const android::String16 host, |
| 727 | android::String16* results) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 728 | // If the V8 instance has not been initialized (either because |
| 729 | // SetPacScript() wasn't called yet, or because it failed. |
| 730 | if (context_ == NULL) |
| 731 | return ERR_FAILED; |
| 732 | |
| 733 | // Otherwise call into V8. |
| 734 | int rv = context_->ResolveProxy(spec, host, results); |
| 735 | |
| 736 | return rv; |
| 737 | } |
| 738 | |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 739 | void ProxyResolverV8::PurgeMemory() { |
| 740 | context_->PurgeMemory(); |
| 741 | } |
| 742 | |
Jason Monk | 40adcb5 | 2013-08-23 17:19:31 -0400 | [diff] [blame] | 743 | int ProxyResolverV8::SetPacScript(const android::String16& script_data) { |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 744 | if (context_ != NULL) { |
| 745 | delete context_; |
Jason Monk | c06a8d6 | 2013-08-19 10:36:23 -0400 | [diff] [blame] | 746 | context_ = NULL; |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 747 | } |
Jason Monk | f5cf6e3 | 2013-07-23 17:26:23 -0400 | [diff] [blame] | 748 | if (script_data.size() == 0) |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 749 | return ERR_PAC_SCRIPT_FAILED; |
| 750 | |
| 751 | // Try parsing the PAC script. |
Jason Monk | 9fd69be | 2014-03-18 11:55:59 -0400 | [diff] [blame] | 752 | context_ = new Context(js_bindings_, error_listener_, v8::Isolate::New()); |
Jason Monk | fc93418 | 2013-07-22 13:20:39 -0400 | [diff] [blame] | 753 | int rv; |
| 754 | if ((rv = context_->InitV8(script_data)) != OK) { |
| 755 | context_ = NULL; |
| 756 | } |
| 757 | if (rv != OK) |
| 758 | context_ = NULL; |
| 759 | return rv; |
| 760 | } |
| 761 | |
| 762 | } // namespace net |