| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. | 3 // 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/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
| 10 #include "net/base/load_log.h" | 10 #include "net/base/load_log.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 const char kPacResourceName[] = "proxy-pac-script.js"; | 22 const char kPacResourceName[] = "proxy-pac-script.js"; |
| 23 | 23 |
| 24 // Convert a V8 String to a std::string. | 24 // Convert a V8 String to a std::string. |
| 25 std::string V8StringToStdString(v8::Handle<v8::String> s) { | 25 std::string V8StringToStdString(v8::Handle<v8::String> s) { |
| 26 int len = s->Utf8Length(); | 26 int len = s->Utf8Length(); |
| 27 std::string result; | 27 std::string result; |
| 28 s->WriteUtf8(WriteInto(&result, len + 1), len); | 28 s->WriteUtf8(WriteInto(&result, len + 1), len); |
| 29 return result; | 29 return result; |
| 30 } | 30 } |
| 31 | 31 |
| 32 // Convert a std::string to a V8 string. | 32 // Convert a std::string (UTF8) to a V8 string. |
| 33 v8::Local<v8::String> StdStringToV8String(const std::string& s) { | 33 v8::Local<v8::String> StdStringToV8String(const std::string& s) { |
| 34 return v8::String::New(s.data(), s.size()); | 34 return v8::String::New(s.data(), s.size()); |
| 35 } | 35 } |
| 36 | 36 |
| 37 // String-ize a V8 object by calling its toString() method. Returns true | 37 // String-ize a V8 object by calling its toString() method. Returns true |
| 38 // on success. This may fail if the toString() throws an exception. | 38 // on success. This may fail if the toString() throws an exception. |
| 39 bool V8ObjectToString(v8::Handle<v8::Value> object, std::string* result) { | 39 bool V8ObjectToString(v8::Handle<v8::Value> object, std::string* result) { |
| 40 if (object.IsEmpty()) | 40 if (object.IsEmpty()) |
| 41 return false; | 41 return false; |
| 42 | 42 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 return ERR_PAC_SCRIPT_FAILED; | 97 return ERR_PAC_SCRIPT_FAILED; |
| 98 } | 98 } |
| 99 | 99 |
| 100 std::string ret_str = V8StringToStdString(ret->ToString()); | 100 std::string ret_str = V8StringToStdString(ret->ToString()); |
| 101 | 101 |
| 102 results->UsePacString(ret_str); | 102 results->UsePacString(ret_str); |
| 103 | 103 |
| 104 return OK; | 104 return OK; |
| 105 } | 105 } |
| 106 | 106 |
| 107 int InitV8(const std::string& pac_data) { | 107 int InitV8(const std::string& pac_data_utf8) { |
| 108 v8::Locker locked; | 108 v8::Locker locked; |
| 109 v8::HandleScope scope; | 109 v8::HandleScope scope; |
| 110 | 110 |
| 111 v8_this_ = v8::Persistent<v8::External>::New(v8::External::New(this)); | 111 v8_this_ = v8::Persistent<v8::External>::New(v8::External::New(this)); |
| 112 v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); | 112 v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 113 | 113 |
| 114 // Attach the javascript bindings. | 114 // Attach the javascript bindings. |
| 115 v8::Local<v8::FunctionTemplate> alert_template = | 115 v8::Local<v8::FunctionTemplate> alert_template = |
| 116 v8::FunctionTemplate::New(&AlertCallback, v8_this_); | 116 v8::FunctionTemplate::New(&AlertCallback, v8_this_); |
| 117 global_template->Set(v8::String::New("alert"), alert_template); | 117 global_template->Set(v8::String::New("alert"), alert_template); |
| 118 | 118 |
| 119 v8::Local<v8::FunctionTemplate> my_ip_address_template = | 119 v8::Local<v8::FunctionTemplate> my_ip_address_template = |
| 120 v8::FunctionTemplate::New(&MyIpAddressCallback, v8_this_); | 120 v8::FunctionTemplate::New(&MyIpAddressCallback, v8_this_); |
| 121 global_template->Set(v8::String::New("myIpAddress"), | 121 global_template->Set(v8::String::New("myIpAddress"), |
| 122 my_ip_address_template); | 122 my_ip_address_template); |
| 123 | 123 |
| 124 v8::Local<v8::FunctionTemplate> dns_resolve_template = | 124 v8::Local<v8::FunctionTemplate> dns_resolve_template = |
| 125 v8::FunctionTemplate::New(&DnsResolveCallback, v8_this_); | 125 v8::FunctionTemplate::New(&DnsResolveCallback, v8_this_); |
| 126 global_template->Set(v8::String::New("dnsResolve"), | 126 global_template->Set(v8::String::New("dnsResolve"), |
| 127 dns_resolve_template); | 127 dns_resolve_template); |
| 128 | 128 |
| 129 v8_context_ = v8::Context::New(NULL, global_template); | 129 v8_context_ = v8::Context::New(NULL, global_template); |
| 130 | 130 |
| 131 v8::Context::Scope ctx(v8_context_); | 131 v8::Context::Scope ctx(v8_context_); |
| 132 | 132 |
| 133 v8::TryCatch try_catch; | 133 v8::TryCatch try_catch; |
| 134 | 134 |
| 135 // Compile the script, including the PAC library functions. | 135 // Compile the script, including the PAC library functions. |
| 136 std::string text_raw = pac_data + PROXY_RESOLVER_SCRIPT; | 136 std::string text_raw_utf8 = pac_data_utf8 + PROXY_RESOLVER_SCRIPT; |
| 137 v8::Local<v8::String> text = StdStringToV8String(text_raw); | 137 v8::Local<v8::String> text = StdStringToV8String(text_raw_utf8); |
| 138 v8::ScriptOrigin origin = v8::ScriptOrigin( | 138 v8::ScriptOrigin origin = v8::ScriptOrigin( |
| 139 v8::String::New(kPacResourceName)); | 139 v8::String::New(kPacResourceName)); |
| 140 v8::Local<v8::Script> code = v8::Script::Compile(text, &origin); | 140 v8::Local<v8::Script> code = v8::Script::Compile(text, &origin); |
| 141 | 141 |
| 142 // Execute. | 142 // Execute. |
| 143 if (!code.IsEmpty()) | 143 if (!code.IsEmpty()) |
| 144 code->Run(); | 144 code->Run(); |
| 145 | 145 |
| 146 if (try_catch.HasCaught()) { | 146 if (try_catch.HasCaught()) { |
| 147 HandleError(try_catch.Message()); | 147 HandleError(try_catch.Message()); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 | 277 |
| 278 return rv; | 278 return rv; |
| 279 } | 279 } |
| 280 | 280 |
| 281 void ProxyResolverV8::CancelRequest(RequestHandle request) { | 281 void ProxyResolverV8::CancelRequest(RequestHandle request) { |
| 282 // This is a synchronous ProxyResolver; no possibility for async requests. | 282 // This is a synchronous ProxyResolver; no possibility for async requests. |
| 283 NOTREACHED(); | 283 NOTREACHED(); |
| 284 } | 284 } |
| 285 | 285 |
| 286 int ProxyResolverV8::SetPacScript(const GURL& /*url*/, | 286 int ProxyResolverV8::SetPacScript(const GURL& /*url*/, |
| 287 const std::string& bytes, | 287 const std::string& bytes_utf8, |
| 288 CompletionCallback* /*callback*/) { | 288 CompletionCallback* /*callback*/) { |
| 289 context_.reset(); | 289 context_.reset(); |
| 290 if (bytes.empty()) | 290 if (bytes_utf8.empty()) |
| 291 return ERR_PAC_SCRIPT_FAILED; | 291 return ERR_PAC_SCRIPT_FAILED; |
| 292 | 292 |
| 293 // Try parsing the PAC script. | 293 // Try parsing the PAC script. |
| 294 scoped_ptr<Context> context(new Context(js_bindings_.get())); | 294 scoped_ptr<Context> context(new Context(js_bindings_.get())); |
| 295 int rv = context->InitV8(bytes); | 295 int rv = context->InitV8(bytes_utf8); |
| 296 if (rv == OK) | 296 if (rv == OK) |
| 297 context_.reset(context.release()); | 297 context_.reset(context.release()); |
| 298 return rv; | 298 return rv; |
| 299 } | 299 } |
| 300 | 300 |
| 301 } // namespace net | 301 } // namespace net |
| OLD | NEW |