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