Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(401)

Side by Side Diff: net/proxy/proxy_resolver_v8.cc

Issue 235001: Backport r27035.... (Closed) Base URL: svn://chrome-svn/chrome/branches/195/src/
Patch Set: Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/waitable_event.h" 10 #include "base/waitable_event.h"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "googleurl/src/gurl.h" 12 #include "googleurl/src/gurl.h"
13 #include "net/base/address_list.h" 13 #include "net/base/address_list.h"
14 #include "net/base/host_resolver.h" 14 #include "net/base/host_resolver.h"
15 #include "net/base/net_errors.h" 15 #include "net/base/net_errors.h"
16 #include "net/base/net_util.h" 16 #include "net/base/net_util.h"
17 #include "net/proxy/proxy_info.h" 17 #include "net/proxy/proxy_info.h"
18 #include "net/proxy/proxy_resolver_script.h" 18 #include "net/proxy/proxy_resolver_script.h"
19 #include "v8/include/v8.h" 19 #include "v8/include/v8.h"
20 20
21 21
22 namespace net { 22 namespace net {
23 23
24 namespace { 24 namespace {
25 25
26 // Pseudo-name for the PAC script. 26 // Pseudo-name for the PAC script.
27 const char kPacResourceName[] = "proxy-pac-script.js"; 27 const char kPacResourceName[] = "proxy-pac-script.js";
28 // Pseudo-name for the PAC utility script.
29 const char kPacUtilityResourceName[] = "proxy-pac-utility-script.js";
28 30
29 // Convert a V8 String to a std::string. 31 // Convert a V8 String to a std::string.
30 std::string V8StringToStdString(v8::Handle<v8::String> s) { 32 std::string V8StringToStdString(v8::Handle<v8::String> s) {
31 int len = s->Utf8Length(); 33 int len = s->Utf8Length();
32 std::string result; 34 std::string result;
33 s->WriteUtf8(WriteInto(&result, len + 1), len); 35 s->WriteUtf8(WriteInto(&result, len + 1), len);
34 return result; 36 return result;
35 } 37 }
36 38
37 // Convert a std::string to a V8 string. 39 // Convert a std::string to a V8 string.
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 249
248 v8::Local<v8::FunctionTemplate> dns_resolve_template = 250 v8::Local<v8::FunctionTemplate> dns_resolve_template =
249 v8::FunctionTemplate::New(&DnsResolveCallback, v8_this_); 251 v8::FunctionTemplate::New(&DnsResolveCallback, v8_this_);
250 global_template->Set(v8::String::New("dnsResolve"), 252 global_template->Set(v8::String::New("dnsResolve"),
251 dns_resolve_template); 253 dns_resolve_template);
252 254
253 v8_context_ = v8::Context::New(NULL, global_template); 255 v8_context_ = v8::Context::New(NULL, global_template);
254 256
255 v8::Context::Scope ctx(v8_context_); 257 v8::Context::Scope ctx(v8_context_);
256 258
257 v8::TryCatch try_catch; 259 // Add the PAC utility functions to the environment.
260 // (This script should never fail, as it is a string literal!)
261 int rv = RunScript(PROXY_RESOLVER_SCRIPT, kPacUtilityResourceName);
262 if (rv != OK) {
263 NOTREACHED();
264 return;
265 }
258 266
259 // Compile the script, including the PAC library functions. 267 // Add the user's PAC code to the environment.
260 std::string text_raw = pac_data + PROXY_RESOLVER_SCRIPT; 268 rv = RunScript(pac_data, kPacResourceName);
261 v8::Local<v8::String> text = StdStringToV8String(text_raw); 269 if (rv != OK)
262 v8::ScriptOrigin origin = v8::ScriptOrigin( 270 return;
263 v8::String::New(kPacResourceName));
264 v8::Local<v8::Script> code = v8::Script::Compile(text, &origin);
265
266 // Execute.
267 if (!code.IsEmpty())
268 code->Run();
269
270 if (try_catch.HasCaught())
271 HandleError(try_catch.Message());
272 } 271 }
273 272
274 // Handle an exception thrown by V8. 273 // Handle an exception thrown by V8.
275 void HandleError(v8::Handle<v8::Message> message) { 274 void HandleError(v8::Handle<v8::Message> message) {
276 if (message.IsEmpty()) 275 if (message.IsEmpty())
277 return; 276 return;
278 277
279 // Otherwise dispatch to the bindings. 278 // Otherwise dispatch to the bindings.
280 int line_number = message->GetLineNumber(); 279 int line_number = message->GetLineNumber();
281 std::string error_message; 280 std::string error_message;
282 V8ObjectToString(message->Get(), &error_message); 281 V8ObjectToString(message->Get(), &error_message);
283 js_bindings_->OnError(line_number, error_message); 282 js_bindings_->OnError(line_number, error_message);
284 } 283 }
285 284
285 // Compiles and runs |script_utf8| in the current V8 context.
286 // Returns OK on success, otherwise an error code.
287 int RunScript(const std::string& script_utf8, const char* script_name) {
288 v8::TryCatch try_catch;
289
290 // Compile the script.
291 v8::Local<v8::String> text = StdStringToV8String(script_utf8);
292 v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New(script_name));
293 v8::Local<v8::Script> code = v8::Script::Compile(text, &origin);
294
295 // Execute.
296 if (!code.IsEmpty())
297 code->Run();
298
299 // Check for errors.
300 if (try_catch.HasCaught()) {
301 HandleError(try_catch.Message());
302 return ERR_PAC_SCRIPT_FAILED;
303 }
304
305 return OK;
306 }
307
286 // V8 callback for when "alert()" is invoked by the PAC script. 308 // V8 callback for when "alert()" is invoked by the PAC script.
287 static v8::Handle<v8::Value> AlertCallback(const v8::Arguments& args) { 309 static v8::Handle<v8::Value> AlertCallback(const v8::Arguments& args) {
288 Context* context = 310 Context* context =
289 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); 311 static_cast<Context*>(v8::External::Cast(*args.Data())->Value());
290 312
291 // Like firefox we assume "undefined" if no argument was specified, and 313 // Like firefox we assume "undefined" if no argument was specified, and
292 // disregard any arguments beyond the first. 314 // disregard any arguments beyond the first.
293 std::string message; 315 std::string message;
294 if (args.Length() == 0) { 316 if (args.Length() == 0) {
295 message = "undefined"; 317 message = "undefined";
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 context_.reset(new Context(js_bindings_.get(), data)); 389 context_.reset(new Context(js_bindings_.get(), data));
368 } 390 }
369 391
370 // static 392 // static
371 ProxyResolverV8::JSBindings* ProxyResolverV8::CreateDefaultBindings( 393 ProxyResolverV8::JSBindings* ProxyResolverV8::CreateDefaultBindings(
372 HostResolver* host_resolver, MessageLoop* host_resolver_loop) { 394 HostResolver* host_resolver, MessageLoop* host_resolver_loop) {
373 return new DefaultJSBindings(host_resolver, host_resolver_loop); 395 return new DefaultJSBindings(host_resolver, host_resolver_loop);
374 } 396 }
375 397
376 } // namespace net 398 } // namespace net
OLDNEW
« no previous file with comments | « net/data/proxy_resolver_v8_unittest/ends_with_comment.js ('k') | net/proxy/proxy_resolver_v8_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698