OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/proxy/proxy_resolver_v8.h" | 5 #include "net/proxy/proxy_resolver_v8.h" |
6 | 6 |
| 7 #include "base/basictypes.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/string_util.h" | 9 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
10 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
11 #include "googleurl/src/url_canon.h" | 12 #include "googleurl/src/url_canon.h" |
12 #include "net/base/host_cache.h" | 13 #include "net/base/host_cache.h" |
13 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
14 #include "net/base/net_log.h" | 15 #include "net/base/net_log.h" |
15 #include "net/proxy/proxy_info.h" | 16 #include "net/proxy/proxy_info.h" |
16 #include "net/proxy/proxy_resolver_js_bindings.h" | 17 #include "net/proxy/proxy_resolver_js_bindings.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 66 |
66 namespace net { | 67 namespace net { |
67 | 68 |
68 namespace { | 69 namespace { |
69 | 70 |
70 // Pseudo-name for the PAC script. | 71 // Pseudo-name for the PAC script. |
71 const char kPacResourceName[] = "proxy-pac-script.js"; | 72 const char kPacResourceName[] = "proxy-pac-script.js"; |
72 // Pseudo-name for the PAC utility script. | 73 // Pseudo-name for the PAC utility script. |
73 const char kPacUtilityResourceName[] = "proxy-pac-utility-script.js"; | 74 const char kPacUtilityResourceName[] = "proxy-pac-utility-script.js"; |
74 | 75 |
| 76 // External string wrapper so V8 can access a string16. |
| 77 class V8ExternalString16 : public v8::String::ExternalStringResource { |
| 78 public: |
| 79 explicit V8ExternalString16(const string16& string) : string_(string) {} |
| 80 |
| 81 virtual const uint16_t* data() const { |
| 82 return reinterpret_cast<const uint16*>(string_.data()); |
| 83 } |
| 84 |
| 85 virtual size_t length() const { |
| 86 return string_.size(); |
| 87 } |
| 88 |
| 89 private: |
| 90 const string16 string_; |
| 91 DISALLOW_COPY_AND_ASSIGN(V8ExternalString16); |
| 92 }; |
| 93 |
| 94 // External string wrapper so V8 can access a string literal. |
| 95 class V8ExternalASCIILiteral : public v8::String::ExternalAsciiStringResource { |
| 96 public: |
| 97 // |ascii| must be a NULL-terminated C string, and must remain valid |
| 98 // throughout this object's lifetime. |
| 99 V8ExternalASCIILiteral(const char* ascii, size_t length) |
| 100 : ascii_(ascii), length_(length) { |
| 101 DCHECK(IsStringASCII(ascii)); |
| 102 } |
| 103 |
| 104 virtual const char* data() const { |
| 105 return ascii_; |
| 106 } |
| 107 |
| 108 virtual size_t length() const { |
| 109 return length_; |
| 110 } |
| 111 |
| 112 private: |
| 113 const char* ascii_; |
| 114 size_t length_; |
| 115 DISALLOW_COPY_AND_ASSIGN(V8ExternalASCIILiteral); |
| 116 }; |
| 117 |
| 118 // External string wrapper so V8 can access a std::string. |
| 119 class V8ExternalASCIIString : public v8::String::ExternalAsciiStringResource { |
| 120 public: |
| 121 explicit V8ExternalASCIIString(const std::string& ascii) |
| 122 : ascii_(ascii) { |
| 123 DCHECK(IsStringASCII(ascii)); |
| 124 } |
| 125 |
| 126 virtual const char* data() const { |
| 127 return ascii_.data(); |
| 128 } |
| 129 |
| 130 virtual size_t length() const { |
| 131 return ascii_.size(); |
| 132 } |
| 133 |
| 134 private: |
| 135 const std::string ascii_; |
| 136 DISALLOW_COPY_AND_ASSIGN(V8ExternalASCIIString); |
| 137 }; |
| 138 |
| 139 // When creating a v8::String from a C++ string we have two choices: create |
| 140 // a copy, or create a wrapper that shares the same underlying storage. |
| 141 // For small strings it is better to just make a copy, whereas for large |
| 142 // strings there are savings by sharing the storage. This number identifies |
| 143 // the cutoff length for when to start wrapping rather than creating copies. |
| 144 const size_t kMaxStringBytesForCopy = 256; |
| 145 |
75 // Converts a V8 String to a UTF16 string16. | 146 // Converts a V8 String to a UTF16 string16. |
76 string16 V8StringToUTF16(v8::Handle<v8::String> s) { | 147 string16 V8StringToUTF16(v8::Handle<v8::String> s) { |
77 int len = s->Length(); | 148 int len = s->Length(); |
78 string16 result; | 149 string16 result; |
79 // Note that the reinterpret cast is because on Windows string16 is an alias | 150 // Note that the reinterpret cast is because on Windows string16 is an alias |
80 // to wstring, and hence has character type wchar_t not uint16_t. | 151 // to wstring, and hence has character type wchar_t not uint16_t. |
81 s->Write(reinterpret_cast<uint16_t*>(WriteInto(&result, len + 1)), 0, len); | 152 s->Write(reinterpret_cast<uint16_t*>(WriteInto(&result, len + 1)), 0, len); |
82 return result; | 153 return result; |
83 } | 154 } |
84 | 155 |
85 // Converts a std::string (UTF8) to a V8 string. | 156 // Converts an ASCII std::string to a V8 string. |
86 v8::Local<v8::String> UTF8StdStringToV8String(const std::string& s) { | 157 v8::Local<v8::String> ASCIIStringToV8String(const std::string& s) { |
87 return v8::String::New(s.data(), s.size()); | 158 DCHECK(IsStringASCII(s)); |
| 159 if (s.size() <= kMaxStringBytesForCopy) |
| 160 return v8::String::New(s.data(), s.size()); |
| 161 return v8::String::NewExternal(new V8ExternalASCIIString(s)); |
| 162 } |
| 163 |
| 164 // Converts a UTF16 string16 to a V8 string. |
| 165 v8::Local<v8::String> UTF16StringToV8String(const string16& s) { |
| 166 if (s.size() * 2 <= kMaxStringBytesForCopy) { |
| 167 return v8::String::New( |
| 168 reinterpret_cast<const uint16_t*>(s.data()), s.size()); |
| 169 } |
| 170 return v8::String::NewExternal(new V8ExternalString16(s)); |
| 171 } |
| 172 |
| 173 // Converts an ASCII string literal to a V8 string. |
| 174 v8::Local<v8::String> ASCIILiteralToV8String(const char* ascii) { |
| 175 DCHECK(IsStringASCII(ascii)); |
| 176 size_t length = strlen(ascii); |
| 177 if (length <= kMaxStringBytesForCopy) |
| 178 return v8::String::New(ascii, length); |
| 179 return v8::String::NewExternal(new V8ExternalASCIILiteral(ascii, length)); |
88 } | 180 } |
89 | 181 |
90 // Stringizes a V8 object by calling its toString() method. Returns true | 182 // Stringizes a V8 object by calling its toString() method. Returns true |
91 // on success. This may fail if the toString() throws an exception. | 183 // on success. This may fail if the toString() throws an exception. |
92 bool V8ObjectToUTF16String(v8::Handle<v8::Value> object, | 184 bool V8ObjectToUTF16String(v8::Handle<v8::Value> object, |
93 string16* utf16_result) { | 185 string16* utf16_result) { |
94 if (object.IsEmpty()) | 186 if (object.IsEmpty()) |
95 return false; | 187 return false; |
96 | 188 |
97 v8::HandleScope scope; | 189 v8::HandleScope scope; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 v8::Context::Scope function_scope(v8_context_); | 254 v8::Context::Scope function_scope(v8_context_); |
163 | 255 |
164 v8::Local<v8::Value> function; | 256 v8::Local<v8::Value> function; |
165 if (!GetFindProxyForURL(&function)) { | 257 if (!GetFindProxyForURL(&function)) { |
166 js_bindings_->OnError( | 258 js_bindings_->OnError( |
167 -1, ASCIIToUTF16("FindProxyForURL() is undefined.")); | 259 -1, ASCIIToUTF16("FindProxyForURL() is undefined.")); |
168 return ERR_PAC_SCRIPT_FAILED; | 260 return ERR_PAC_SCRIPT_FAILED; |
169 } | 261 } |
170 | 262 |
171 v8::Handle<v8::Value> argv[] = { | 263 v8::Handle<v8::Value> argv[] = { |
172 UTF8StdStringToV8String(query_url.spec()), | 264 ASCIIStringToV8String(query_url.spec()), |
173 UTF8StdStringToV8String(query_url.host()), | 265 ASCIIStringToV8String(query_url.host()), |
174 }; | 266 }; |
175 | 267 |
176 v8::TryCatch try_catch; | 268 v8::TryCatch try_catch; |
177 v8::Local<v8::Value> ret = v8::Function::Cast(*function)->Call( | 269 v8::Local<v8::Value> ret = v8::Function::Cast(*function)->Call( |
178 v8_context_->Global(), arraysize(argv), argv); | 270 v8_context_->Global(), arraysize(argv), argv); |
179 | 271 |
180 if (try_catch.HasCaught()) { | 272 if (try_catch.HasCaught()) { |
181 HandleError(try_catch.Message()); | 273 HandleError(try_catch.Message()); |
182 return ERR_PAC_SCRIPT_FAILED; | 274 return ERR_PAC_SCRIPT_FAILED; |
183 } | 275 } |
(...skipping 15 matching lines...) Expand all Loading... |
199 ASCIIToUTF16("FindProxyForURL() returned a non-ASCII string " | 291 ASCIIToUTF16("FindProxyForURL() returned a non-ASCII string " |
200 "(crbug.com/47234): ") + ret_str; | 292 "(crbug.com/47234): ") + ret_str; |
201 js_bindings_->OnError(-1, error_message); | 293 js_bindings_->OnError(-1, error_message); |
202 return ERR_PAC_SCRIPT_FAILED; | 294 return ERR_PAC_SCRIPT_FAILED; |
203 } | 295 } |
204 | 296 |
205 results->UsePacString(UTF16ToASCII(ret_str)); | 297 results->UsePacString(UTF16ToASCII(ret_str)); |
206 return OK; | 298 return OK; |
207 } | 299 } |
208 | 300 |
209 int InitV8(const std::string& pac_data_utf8) { | 301 int InitV8(const string16& pac_script) { |
210 v8::Locker locked; | 302 v8::Locker locked; |
211 v8::HandleScope scope; | 303 v8::HandleScope scope; |
212 | 304 |
213 v8_this_ = v8::Persistent<v8::External>::New(v8::External::New(this)); | 305 v8_this_ = v8::Persistent<v8::External>::New(v8::External::New(this)); |
214 v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); | 306 v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
215 | 307 |
216 // Attach the javascript bindings. | 308 // Attach the javascript bindings. |
217 v8::Local<v8::FunctionTemplate> alert_template = | 309 v8::Local<v8::FunctionTemplate> alert_template = |
218 v8::FunctionTemplate::New(&AlertCallback, v8_this_); | 310 v8::FunctionTemplate::New(&AlertCallback, v8_this_); |
219 global_template->Set(v8::String::New("alert"), alert_template); | 311 global_template->Set(ASCIILiteralToV8String("alert"), alert_template); |
220 | 312 |
221 v8::Local<v8::FunctionTemplate> my_ip_address_template = | 313 v8::Local<v8::FunctionTemplate> my_ip_address_template = |
222 v8::FunctionTemplate::New(&MyIpAddressCallback, v8_this_); | 314 v8::FunctionTemplate::New(&MyIpAddressCallback, v8_this_); |
223 global_template->Set(v8::String::New("myIpAddress"), | 315 global_template->Set(ASCIILiteralToV8String("myIpAddress"), |
224 my_ip_address_template); | 316 my_ip_address_template); |
225 | 317 |
226 v8::Local<v8::FunctionTemplate> dns_resolve_template = | 318 v8::Local<v8::FunctionTemplate> dns_resolve_template = |
227 v8::FunctionTemplate::New(&DnsResolveCallback, v8_this_); | 319 v8::FunctionTemplate::New(&DnsResolveCallback, v8_this_); |
228 global_template->Set(v8::String::New("dnsResolve"), | 320 global_template->Set(ASCIILiteralToV8String("dnsResolve"), |
229 dns_resolve_template); | 321 dns_resolve_template); |
230 | 322 |
231 // Microsoft's PAC extensions (incomplete): | 323 // Microsoft's PAC extensions (incomplete): |
232 | 324 |
233 v8::Local<v8::FunctionTemplate> dns_resolve_ex_template = | 325 v8::Local<v8::FunctionTemplate> dns_resolve_ex_template = |
234 v8::FunctionTemplate::New(&DnsResolveExCallback, v8_this_); | 326 v8::FunctionTemplate::New(&DnsResolveExCallback, v8_this_); |
235 global_template->Set(v8::String::New("dnsResolveEx"), | 327 global_template->Set(ASCIILiteralToV8String("dnsResolveEx"), |
236 dns_resolve_ex_template); | 328 dns_resolve_ex_template); |
237 | 329 |
238 v8::Local<v8::FunctionTemplate> my_ip_address_ex_template = | 330 v8::Local<v8::FunctionTemplate> my_ip_address_ex_template = |
239 v8::FunctionTemplate::New(&MyIpAddressExCallback, v8_this_); | 331 v8::FunctionTemplate::New(&MyIpAddressExCallback, v8_this_); |
240 global_template->Set(v8::String::New("myIpAddressEx"), | 332 global_template->Set(ASCIILiteralToV8String("myIpAddressEx"), |
241 my_ip_address_ex_template); | 333 my_ip_address_ex_template); |
242 | 334 |
243 v8_context_ = v8::Context::New(NULL, global_template); | 335 v8_context_ = v8::Context::New(NULL, global_template); |
244 | 336 |
245 v8::Context::Scope ctx(v8_context_); | 337 v8::Context::Scope ctx(v8_context_); |
246 | 338 |
247 // Add the PAC utility functions to the environment. | 339 // Add the PAC utility functions to the environment. |
248 // (This script should never fail, as it is a string literal!) | 340 // (This script should never fail, as it is a string literal!) |
249 // Note that the two string literals are concatenated. | 341 // Note that the two string literals are concatenated. |
250 int rv = RunScript(PROXY_RESOLVER_SCRIPT | 342 int rv = RunScript( |
251 PROXY_RESOLVER_SCRIPT_EX, | 343 ASCIILiteralToV8String( |
252 kPacUtilityResourceName); | 344 PROXY_RESOLVER_SCRIPT |
| 345 PROXY_RESOLVER_SCRIPT_EX), |
| 346 kPacUtilityResourceName); |
253 if (rv != OK) { | 347 if (rv != OK) { |
254 NOTREACHED(); | 348 NOTREACHED(); |
255 return rv; | 349 return rv; |
256 } | 350 } |
257 | 351 |
258 // Add the user's PAC code to the environment. | 352 // Add the user's PAC code to the environment. |
259 rv = RunScript(pac_data_utf8, kPacResourceName); | 353 rv = RunScript(UTF16StringToV8String(pac_script), kPacResourceName); |
260 if (rv != OK) | 354 if (rv != OK) |
261 return rv; | 355 return rv; |
262 | 356 |
263 // At a minimum, the FindProxyForURL() function must be defined for this | 357 // At a minimum, the FindProxyForURL() function must be defined for this |
264 // to be a legitimiate PAC script. | 358 // to be a legitimiate PAC script. |
265 v8::Local<v8::Value> function; | 359 v8::Local<v8::Value> function; |
266 if (!GetFindProxyForURL(&function)) | 360 if (!GetFindProxyForURL(&function)) |
267 return ERR_PAC_SCRIPT_FAILED; | 361 return ERR_PAC_SCRIPT_FAILED; |
268 | 362 |
269 return OK; | 363 return OK; |
270 } | 364 } |
271 | 365 |
272 void SetCurrentRequestContext(ProxyResolverRequestContext* context) { | 366 void SetCurrentRequestContext(ProxyResolverRequestContext* context) { |
273 js_bindings_->set_current_request_context(context); | 367 js_bindings_->set_current_request_context(context); |
274 } | 368 } |
275 | 369 |
276 void PurgeMemory() { | 370 void PurgeMemory() { |
277 v8::Locker locked; | 371 v8::Locker locked; |
278 // Repeatedly call the V8 idle notification until it returns true ("nothing | 372 // Repeatedly call the V8 idle notification until it returns true ("nothing |
279 // more to free"). Note that it makes more sense to do this than to | 373 // more to free"). Note that it makes more sense to do this than to |
280 // implement a new "delete everything" pass because object references make | 374 // implement a new "delete everything" pass because object references make |
281 // it difficult to free everything possible in just one pass. | 375 // it difficult to free everything possible in just one pass. |
282 while (!v8::V8::IdleNotification()) | 376 while (!v8::V8::IdleNotification()) |
283 ; | 377 ; |
284 } | 378 } |
285 | 379 |
286 private: | 380 private: |
287 bool GetFindProxyForURL(v8::Local<v8::Value>* function) { | 381 bool GetFindProxyForURL(v8::Local<v8::Value>* function) { |
288 *function = v8_context_->Global()->Get(v8::String::New("FindProxyForURL")); | 382 *function = v8_context_->Global()->Get( |
| 383 ASCIILiteralToV8String("FindProxyForURL")); |
289 return (*function)->IsFunction(); | 384 return (*function)->IsFunction(); |
290 } | 385 } |
291 | 386 |
292 // Handle an exception thrown by V8. | 387 // Handle an exception thrown by V8. |
293 void HandleError(v8::Handle<v8::Message> message) { | 388 void HandleError(v8::Handle<v8::Message> message) { |
294 if (message.IsEmpty()) | 389 if (message.IsEmpty()) |
295 return; | 390 return; |
296 | 391 |
297 // Otherwise dispatch to the bindings. | 392 // Otherwise dispatch to the bindings. |
298 int line_number = message->GetLineNumber(); | 393 int line_number = message->GetLineNumber(); |
299 string16 error_message; | 394 string16 error_message; |
300 V8ObjectToUTF16String(message->Get(), &error_message); | 395 V8ObjectToUTF16String(message->Get(), &error_message); |
301 js_bindings_->OnError(line_number, error_message); | 396 js_bindings_->OnError(line_number, error_message); |
302 } | 397 } |
303 | 398 |
304 // Compiles and runs |script_utf8| in the current V8 context. | 399 // Compiles and runs |script| in the current V8 context. |
305 // Returns OK on success, otherwise an error code. | 400 // Returns OK on success, otherwise an error code. |
306 int RunScript(const std::string& script_utf8, const char* script_name) { | 401 int RunScript(v8::Handle<v8::String> script, const char* script_name) { |
307 v8::TryCatch try_catch; | 402 v8::TryCatch try_catch; |
308 | 403 |
309 // Compile the script. | 404 // Compile the script. |
310 v8::Local<v8::String> text = UTF8StdStringToV8String(script_utf8); | 405 v8::ScriptOrigin origin = |
311 v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New(script_name)); | 406 v8::ScriptOrigin(ASCIILiteralToV8String(script_name)); |
312 v8::Local<v8::Script> code = v8::Script::Compile(text, &origin); | 407 v8::Local<v8::Script> code = v8::Script::Compile(script, &origin); |
313 | 408 |
314 // Execute. | 409 // Execute. |
315 if (!code.IsEmpty()) | 410 if (!code.IsEmpty()) |
316 code->Run(); | 411 code->Run(); |
317 | 412 |
318 // Check for errors. | 413 // Check for errors. |
319 if (try_catch.HasCaught()) { | 414 if (try_catch.HasCaught()) { |
320 HandleError(try_catch.Message()); | 415 HandleError(try_catch.Message()); |
321 return ERR_PAC_SCRIPT_FAILED; | 416 return ERR_PAC_SCRIPT_FAILED; |
322 } | 417 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 // we are. | 458 // we are. |
364 success = context->js_bindings_->MyIpAddress(&result); | 459 success = context->js_bindings_->MyIpAddress(&result); |
365 | 460 |
366 LogEventToCurrentRequest(context, | 461 LogEventToCurrentRequest(context, |
367 NetLog::PHASE_END, | 462 NetLog::PHASE_END, |
368 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS, | 463 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS, |
369 NULL); | 464 NULL); |
370 } | 465 } |
371 | 466 |
372 if (!success) | 467 if (!success) |
373 result = "127.0.0.1"; | 468 return ASCIILiteralToV8String("127.0.0.1"); |
374 return UTF8StdStringToV8String(result); | 469 return ASCIIStringToV8String(result); |
375 } | 470 } |
376 | 471 |
377 // V8 callback for when "myIpAddressEx()" is invoked by the PAC script. | 472 // V8 callback for when "myIpAddressEx()" is invoked by the PAC script. |
378 static v8::Handle<v8::Value> MyIpAddressExCallback( | 473 static v8::Handle<v8::Value> MyIpAddressExCallback( |
379 const v8::Arguments& args) { | 474 const v8::Arguments& args) { |
380 Context* context = | 475 Context* context = |
381 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); | 476 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
382 | 477 |
383 std::string ip_address_list; | 478 std::string ip_address_list; |
384 bool success; | 479 bool success; |
(...skipping 11 matching lines...) Expand all Loading... |
396 success = context->js_bindings_->MyIpAddressEx(&ip_address_list); | 491 success = context->js_bindings_->MyIpAddressEx(&ip_address_list); |
397 | 492 |
398 LogEventToCurrentRequest(context, | 493 LogEventToCurrentRequest(context, |
399 NetLog::PHASE_END, | 494 NetLog::PHASE_END, |
400 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS_EX, | 495 NetLog::TYPE_PROXY_RESOLVER_V8_MY_IP_ADDRESS_EX, |
401 NULL); | 496 NULL); |
402 } | 497 } |
403 | 498 |
404 if (!success) | 499 if (!success) |
405 ip_address_list = std::string(); | 500 ip_address_list = std::string(); |
406 return UTF8StdStringToV8String(ip_address_list); | 501 return ASCIIStringToV8String(ip_address_list); |
407 } | 502 } |
408 | 503 |
409 // V8 callback for when "dnsResolve()" is invoked by the PAC script. | 504 // V8 callback for when "dnsResolve()" is invoked by the PAC script. |
410 static v8::Handle<v8::Value> DnsResolveCallback(const v8::Arguments& args) { | 505 static v8::Handle<v8::Value> DnsResolveCallback(const v8::Arguments& args) { |
411 Context* context = | 506 Context* context = |
412 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); | 507 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
413 | 508 |
414 // We need at least one string argument. | 509 // We need at least one string argument. |
415 std::string hostname; | 510 std::string hostname; |
416 if (!GetHostnameArgument(args, &hostname)) | 511 if (!GetHostnameArgument(args, &hostname)) |
(...skipping 11 matching lines...) Expand all Loading... |
428 NULL); | 523 NULL); |
429 | 524 |
430 success = context->js_bindings_->DnsResolve(hostname, &ip_address); | 525 success = context->js_bindings_->DnsResolve(hostname, &ip_address); |
431 | 526 |
432 LogEventToCurrentRequest(context, | 527 LogEventToCurrentRequest(context, |
433 NetLog::PHASE_END, | 528 NetLog::PHASE_END, |
434 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE, | 529 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE, |
435 NULL); | 530 NULL); |
436 } | 531 } |
437 | 532 |
438 return success ? UTF8StdStringToV8String(ip_address) : v8::Null(); | 533 return success ? ASCIIStringToV8String(ip_address) : v8::Null(); |
439 } | 534 } |
440 | 535 |
441 // V8 callback for when "dnsResolveEx()" is invoked by the PAC script. | 536 // V8 callback for when "dnsResolveEx()" is invoked by the PAC script. |
442 static v8::Handle<v8::Value> DnsResolveExCallback(const v8::Arguments& args) { | 537 static v8::Handle<v8::Value> DnsResolveExCallback(const v8::Arguments& args) { |
443 Context* context = | 538 Context* context = |
444 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); | 539 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); |
445 | 540 |
446 // We need at least one string argument. | 541 // We need at least one string argument. |
447 std::string hostname; | 542 std::string hostname; |
448 if (!GetHostnameArgument(args, &hostname)) | 543 if (!GetHostnameArgument(args, &hostname)) |
(...skipping 15 matching lines...) Expand all Loading... |
464 | 559 |
465 LogEventToCurrentRequest(context, | 560 LogEventToCurrentRequest(context, |
466 NetLog::PHASE_END, | 561 NetLog::PHASE_END, |
467 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE_EX, | 562 NetLog::TYPE_PROXY_RESOLVER_V8_DNS_RESOLVE_EX, |
468 NULL); | 563 NULL); |
469 } | 564 } |
470 | 565 |
471 if (!success) | 566 if (!success) |
472 ip_address_list = std::string(); | 567 ip_address_list = std::string(); |
473 | 568 |
474 return UTF8StdStringToV8String(ip_address_list); | 569 return ASCIIStringToV8String(ip_address_list); |
475 } | 570 } |
476 | 571 |
477 static void LogEventToCurrentRequest(Context* context, | 572 static void LogEventToCurrentRequest(Context* context, |
478 NetLog::EventPhase phase, | 573 NetLog::EventPhase phase, |
479 NetLog::EventType type, | 574 NetLog::EventType type, |
480 NetLog::EventParameters* params) { | 575 NetLog::EventParameters* params) { |
481 if (context->js_bindings_->current_request_context()) { | 576 if (context->js_bindings_->current_request_context()) { |
482 context->js_bindings_->current_request_context()->net_log->AddEntry( | 577 context->js_bindings_->current_request_context()->net_log->AddEntry( |
483 type, phase, params); | 578 type, phase, params); |
484 } | 579 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 void ProxyResolverV8::CancelRequest(RequestHandle request) { | 628 void ProxyResolverV8::CancelRequest(RequestHandle request) { |
534 // This is a synchronous ProxyResolver; no possibility for async requests. | 629 // This is a synchronous ProxyResolver; no possibility for async requests. |
535 NOTREACHED(); | 630 NOTREACHED(); |
536 } | 631 } |
537 | 632 |
538 void ProxyResolverV8::PurgeMemory() { | 633 void ProxyResolverV8::PurgeMemory() { |
539 context_->PurgeMemory(); | 634 context_->PurgeMemory(); |
540 } | 635 } |
541 | 636 |
542 int ProxyResolverV8::SetPacScript(const GURL& /*url*/, | 637 int ProxyResolverV8::SetPacScript(const GURL& /*url*/, |
543 const std::string& bytes_utf8, | 638 const string16& pac_script, |
544 CompletionCallback* /*callback*/) { | 639 CompletionCallback* /*callback*/) { |
545 context_.reset(); | 640 context_.reset(); |
546 if (bytes_utf8.empty()) | 641 if (pac_script.empty()) |
547 return ERR_PAC_SCRIPT_FAILED; | 642 return ERR_PAC_SCRIPT_FAILED; |
548 | 643 |
549 // Try parsing the PAC script. | 644 // Try parsing the PAC script. |
550 scoped_ptr<Context> context(new Context(js_bindings_.get())); | 645 scoped_ptr<Context> context(new Context(js_bindings_.get())); |
551 int rv = context->InitV8(bytes_utf8); | 646 int rv = context->InitV8(pac_script); |
552 if (rv == OK) | 647 if (rv == OK) |
553 context_.reset(context.release()); | 648 context_.reset(context.release()); |
554 return rv; | 649 return rv; |
555 } | 650 } |
556 | 651 |
557 } // namespace net | 652 } // namespace net |
OLD | NEW |