OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 7 #include <algorithm> |
8 #include <cstdio> | 8 #include <cstdio> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 }; | 126 }; |
127 | 127 |
128 // When creating a v8::String from a C++ string we have two choices: create | 128 // When creating a v8::String from a C++ string we have two choices: create |
129 // a copy, or create a wrapper that shares the same underlying storage. | 129 // a copy, or create a wrapper that shares the same underlying storage. |
130 // For small strings it is better to just make a copy, whereas for large | 130 // For small strings it is better to just make a copy, whereas for large |
131 // strings there are savings by sharing the storage. This number identifies | 131 // strings there are savings by sharing the storage. This number identifies |
132 // the cutoff length for when to start wrapping rather than creating copies. | 132 // the cutoff length for when to start wrapping rather than creating copies. |
133 const size_t kMaxStringBytesForCopy = 256; | 133 const size_t kMaxStringBytesForCopy = 256; |
134 | 134 |
135 // Converts a V8 String to a UTF8 std::string. | 135 // Converts a V8 String to a UTF8 std::string. |
136 std::string V8StringToUTF8(v8::Handle<v8::String> s) { | 136 std::string V8StringToUTF8(v8::Local<v8::String> s) { |
137 int len = s->Length(); | 137 int len = s->Length(); |
138 std::string result; | 138 std::string result; |
139 if (len > 0) | 139 if (len > 0) |
140 s->WriteUtf8(WriteInto(&result, len + 1)); | 140 s->WriteUtf8(WriteInto(&result, len + 1)); |
141 return result; | 141 return result; |
142 } | 142 } |
143 | 143 |
144 // Converts a V8 String to a UTF16 base::string16. | 144 // Converts a V8 String to a UTF16 base::string16. |
145 base::string16 V8StringToUTF16(v8::Handle<v8::String> s) { | 145 base::string16 V8StringToUTF16(v8::Local<v8::String> s) { |
146 int len = s->Length(); | 146 int len = s->Length(); |
147 base::string16 result; | 147 base::string16 result; |
148 // Note that the reinterpret cast is because on Windows string16 is an alias | 148 // Note that the reinterpret cast is because on Windows string16 is an alias |
149 // to wstring, and hence has character type wchar_t not uint16_t. | 149 // to wstring, and hence has character type wchar_t not uint16_t. |
150 if (len > 0) | 150 if (len > 0) |
151 s->Write(reinterpret_cast<uint16_t*>(WriteInto(&result, len + 1)), 0, len); | 151 s->Write(reinterpret_cast<uint16_t*>(WriteInto(&result, len + 1)), 0, len); |
152 return result; | 152 return result; |
153 } | 153 } |
154 | 154 |
155 // Converts an ASCII std::string to a V8 string. | 155 // Converts an ASCII std::string to a V8 string. |
(...skipping 26 matching lines...) Expand all Loading... |
182 size_t length = strlen(ascii); | 182 size_t length = strlen(ascii); |
183 if (length <= kMaxStringBytesForCopy) | 183 if (length <= kMaxStringBytesForCopy) |
184 return v8::String::NewFromUtf8(isolate, ascii, v8::String::kNormalString, | 184 return v8::String::NewFromUtf8(isolate, ascii, v8::String::kNormalString, |
185 length); | 185 length); |
186 return v8::String::NewExternal(isolate, | 186 return v8::String::NewExternal(isolate, |
187 new V8ExternalASCIILiteral(ascii, length)); | 187 new V8ExternalASCIILiteral(ascii, length)); |
188 } | 188 } |
189 | 189 |
190 // Stringizes a V8 object by calling its toString() method. Returns true | 190 // Stringizes a V8 object by calling its toString() method. Returns true |
191 // on success. This may fail if the toString() throws an exception. | 191 // on success. This may fail if the toString() throws an exception. |
192 bool V8ObjectToUTF16String(v8::Handle<v8::Value> object, | 192 bool V8ObjectToUTF16String(v8::Local<v8::Value> object, |
193 base::string16* utf16_result, | 193 base::string16* utf16_result, |
194 v8::Isolate* isolate) { | 194 v8::Isolate* isolate) { |
195 if (object.IsEmpty()) | 195 if (object.IsEmpty()) |
196 return false; | 196 return false; |
197 | 197 |
198 v8::HandleScope scope(isolate); | 198 v8::HandleScope scope(isolate); |
199 v8::Local<v8::String> str_object = object->ToString(); | 199 v8::Local<v8::String> str_object = object->ToString(); |
200 if (str_object.IsEmpty()) | 200 if (str_object.IsEmpty()) |
201 return false; | 201 return false; |
202 *utf16_result = V8StringToUTF16(str_object); | 202 *utf16_result = V8StringToUTF16(str_object); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 v8::Local<v8::Context>::New(isolate_, v8_context_); | 366 v8::Local<v8::Context>::New(isolate_, v8_context_); |
367 v8::Context::Scope function_scope(context); | 367 v8::Context::Scope function_scope(context); |
368 | 368 |
369 v8::Local<v8::Value> function; | 369 v8::Local<v8::Value> function; |
370 if (!GetFindProxyForURL(&function)) { | 370 if (!GetFindProxyForURL(&function)) { |
371 js_bindings()->OnError( | 371 js_bindings()->OnError( |
372 -1, base::ASCIIToUTF16("FindProxyForURL() is undefined.")); | 372 -1, base::ASCIIToUTF16("FindProxyForURL() is undefined.")); |
373 return ERR_PAC_SCRIPT_FAILED; | 373 return ERR_PAC_SCRIPT_FAILED; |
374 } | 374 } |
375 | 375 |
376 v8::Handle<v8::Value> argv[] = { | 376 v8::Local<v8::Value> argv[] = { |
377 ASCIIStringToV8String(isolate_, query_url.spec()), | 377 ASCIIStringToV8String(isolate_, query_url.spec()), |
378 ASCIIStringToV8String(isolate_, query_url.HostNoBrackets()), | 378 ASCIIStringToV8String(isolate_, query_url.HostNoBrackets()), |
379 }; | 379 }; |
380 | 380 |
381 v8::TryCatch try_catch; | 381 v8::TryCatch try_catch; |
382 v8::Local<v8::Value> ret = v8::Function::Cast(*function)->Call( | 382 v8::Local<v8::Value> ret = v8::Function::Cast(*function)->Call( |
383 context->Global(), arraysize(argv), argv); | 383 context->Global(), arraysize(argv), argv); |
384 | 384 |
385 if (try_catch.HasCaught()) { | 385 if (try_catch.HasCaught()) { |
386 HandleError(try_catch.Message()); | 386 HandleError(try_catch.Message()); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 bool GetFindProxyForURL(v8::Local<v8::Value>* function) { | 505 bool GetFindProxyForURL(v8::Local<v8::Value>* function) { |
506 v8::Local<v8::Context> context = | 506 v8::Local<v8::Context> context = |
507 v8::Local<v8::Context>::New(isolate_, v8_context_); | 507 v8::Local<v8::Context>::New(isolate_, v8_context_); |
508 *function = | 508 *function = |
509 context->Global()->Get( | 509 context->Global()->Get( |
510 ASCIILiteralToV8String(isolate_, "FindProxyForURL")); | 510 ASCIILiteralToV8String(isolate_, "FindProxyForURL")); |
511 return (*function)->IsFunction(); | 511 return (*function)->IsFunction(); |
512 } | 512 } |
513 | 513 |
514 // Handle an exception thrown by V8. | 514 // Handle an exception thrown by V8. |
515 void HandleError(v8::Handle<v8::Message> message) { | 515 void HandleError(v8::Local<v8::Message> message) { |
516 base::string16 error_message; | 516 base::string16 error_message; |
517 int line_number = -1; | 517 int line_number = -1; |
518 | 518 |
519 if (!message.IsEmpty()) { | 519 if (!message.IsEmpty()) { |
520 line_number = message->GetLineNumber(); | 520 line_number = message->GetLineNumber(); |
521 V8ObjectToUTF16String(message->Get(), &error_message, isolate_); | 521 V8ObjectToUTF16String(message->Get(), &error_message, isolate_); |
522 } | 522 } |
523 | 523 |
524 js_bindings()->OnError(line_number, error_message); | 524 js_bindings()->OnError(line_number, error_message); |
525 } | 525 } |
526 | 526 |
527 // Compiles and runs |script| in the current V8 context. | 527 // Compiles and runs |script| in the current V8 context. |
528 // Returns OK on success, otherwise an error code. | 528 // Returns OK on success, otherwise an error code. |
529 int RunScript(v8::Handle<v8::String> script, const char* script_name) { | 529 int RunScript(v8::Local<v8::String> script, const char* script_name) { |
530 v8::TryCatch try_catch; | 530 v8::TryCatch try_catch; |
531 | 531 |
532 // Compile the script. | 532 // Compile the script. |
533 v8::ScriptOrigin origin = | 533 v8::ScriptOrigin origin = |
534 v8::ScriptOrigin(ASCIILiteralToV8String(isolate_, script_name)); | 534 v8::ScriptOrigin(ASCIILiteralToV8String(isolate_, script_name)); |
535 v8::Local<v8::Script> code = v8::Script::Compile(script, &origin); | 535 v8::Local<v8::Script> code = v8::Script::Compile(script, &origin); |
536 | 536 |
537 // Execute. | 537 // Execute. |
538 if (!code.IsEmpty()) | 538 if (!code.IsEmpty()) |
539 code->Run(); | 539 code->Run(); |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 return 0; | 797 return 0; |
798 | 798 |
799 v8::Locker locked(g_proxy_resolver_isolate_->isolate()); | 799 v8::Locker locked(g_proxy_resolver_isolate_->isolate()); |
800 v8::Isolate::Scope isolate_scope(g_proxy_resolver_isolate_->isolate()); | 800 v8::Isolate::Scope isolate_scope(g_proxy_resolver_isolate_->isolate()); |
801 v8::HeapStatistics heap_statistics; | 801 v8::HeapStatistics heap_statistics; |
802 g_proxy_resolver_isolate_->isolate()->GetHeapStatistics(&heap_statistics); | 802 g_proxy_resolver_isolate_->isolate()->GetHeapStatistics(&heap_statistics); |
803 return heap_statistics.used_heap_size(); | 803 return heap_statistics.used_heap_size(); |
804 } | 804 } |
805 | 805 |
806 } // namespace net | 806 } // namespace net |
OLD | NEW |