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

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

Issue 223016: Fix a bug where if a PAC script ended with a comment and no newline, it would... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: remove unused TryCatch Created 11 years, 2 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/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"
11 #include "net/base/net_errors.h" 11 #include "net/base/net_errors.h"
12 #include "net/proxy/proxy_info.h" 12 #include "net/proxy/proxy_info.h"
13 #include "net/proxy/proxy_resolver_js_bindings.h" 13 #include "net/proxy/proxy_resolver_js_bindings.h"
14 #include "net/proxy/proxy_resolver_script.h" 14 #include "net/proxy/proxy_resolver_script.h"
15 #include "v8/include/v8.h" 15 #include "v8/include/v8.h"
16 16
17 namespace net { 17 namespace net {
18 18
19 namespace { 19 namespace {
20 20
21 // Pseudo-name for the PAC script. 21 // Pseudo-name for the PAC script.
22 const char kPacResourceName[] = "proxy-pac-script.js"; 22 const char kPacResourceName[] = "proxy-pac-script.js";
23 // Pseudo-name for the PAC utility script.
24 const char kPacUtilityResourceName[] = "proxy-pac-utility-script.js";
23 25
24 // Convert a V8 String to a std::string. 26 // Convert a V8 String to a std::string.
25 std::string V8StringToStdString(v8::Handle<v8::String> s) { 27 std::string V8StringToStdString(v8::Handle<v8::String> s) {
26 int len = s->Utf8Length(); 28 int len = s->Utf8Length();
27 std::string result; 29 std::string result;
28 s->WriteUtf8(WriteInto(&result, len + 1), len); 30 s->WriteUtf8(WriteInto(&result, len + 1), len);
29 return result; 31 return result;
30 } 32 }
31 33
32 // Convert a std::string (UTF8) to a V8 string. 34 // Convert a std::string (UTF8) to a V8 string.
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 125
124 v8::Local<v8::FunctionTemplate> dns_resolve_template = 126 v8::Local<v8::FunctionTemplate> dns_resolve_template =
125 v8::FunctionTemplate::New(&DnsResolveCallback, v8_this_); 127 v8::FunctionTemplate::New(&DnsResolveCallback, v8_this_);
126 global_template->Set(v8::String::New("dnsResolve"), 128 global_template->Set(v8::String::New("dnsResolve"),
127 dns_resolve_template); 129 dns_resolve_template);
128 130
129 v8_context_ = v8::Context::New(NULL, global_template); 131 v8_context_ = v8::Context::New(NULL, global_template);
130 132
131 v8::Context::Scope ctx(v8_context_); 133 v8::Context::Scope ctx(v8_context_);
132 134
133 v8::TryCatch try_catch; 135 // Add the PAC utility functions to the environment.
136 // (This script should never fail, as it is a string literal!)
137 int rv = RunScript(PROXY_RESOLVER_SCRIPT, kPacUtilityResourceName);
138 if (rv != OK) {
139 NOTREACHED();
140 return rv;
141 }
134 142
135 // Compile the script, including the PAC library functions. 143 // Add the user's PAC code to the environment.
136 std::string text_raw_utf8 = pac_data_utf8 + PROXY_RESOLVER_SCRIPT; 144 rv = RunScript(pac_data_utf8, kPacResourceName);
137 v8::Local<v8::String> text = StdStringToV8String(text_raw_utf8); 145 if (rv != OK)
138 v8::ScriptOrigin origin = v8::ScriptOrigin( 146 return rv;
139 v8::String::New(kPacResourceName));
140 v8::Local<v8::Script> code = v8::Script::Compile(text, &origin);
141
142 // Execute.
143 if (!code.IsEmpty())
144 code->Run();
145
146 if (try_catch.HasCaught()) {
147 HandleError(try_catch.Message());
148 return ERR_PAC_SCRIPT_FAILED;
149 }
150 147
151 // At a minimum, the FindProxyForURL() function must be defined for this 148 // At a minimum, the FindProxyForURL() function must be defined for this
152 // to be a legitimiate PAC script. 149 // to be a legitimiate PAC script.
153 v8::Local<v8::Value> function; 150 v8::Local<v8::Value> function;
154 if (!GetFindProxyForURL(&function)) 151 if (!GetFindProxyForURL(&function))
155 return ERR_PAC_SCRIPT_FAILED; 152 return ERR_PAC_SCRIPT_FAILED;
156 153
157 return OK; 154 return OK;
158 } 155 }
159 156
(...skipping 12 matching lines...) Expand all
172 if (message.IsEmpty()) 169 if (message.IsEmpty())
173 return; 170 return;
174 171
175 // Otherwise dispatch to the bindings. 172 // Otherwise dispatch to the bindings.
176 int line_number = message->GetLineNumber(); 173 int line_number = message->GetLineNumber();
177 std::string error_message; 174 std::string error_message;
178 V8ObjectToString(message->Get(), &error_message); 175 V8ObjectToString(message->Get(), &error_message);
179 js_bindings_->OnError(line_number, error_message); 176 js_bindings_->OnError(line_number, error_message);
180 } 177 }
181 178
179 // Compiles and runs |script_utf8| in the current V8 context.
180 // Returns OK on success, otherwise an error code.
181 int RunScript(const std::string& script_utf8, const char* script_name) {
182 v8::TryCatch try_catch;
183
184 // Compile the script.
185 v8::Local<v8::String> text = StdStringToV8String(script_utf8);
186 v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New(script_name));
187 v8::Local<v8::Script> code = v8::Script::Compile(text, &origin);
188
189 // Execute.
190 if (!code.IsEmpty())
191 code->Run();
192
193 // Check for errors.
194 if (try_catch.HasCaught()) {
195 HandleError(try_catch.Message());
196 return ERR_PAC_SCRIPT_FAILED;
197 }
198
199 return OK;
200 }
201
182 // V8 callback for when "alert()" is invoked by the PAC script. 202 // V8 callback for when "alert()" is invoked by the PAC script.
183 static v8::Handle<v8::Value> AlertCallback(const v8::Arguments& args) { 203 static v8::Handle<v8::Value> AlertCallback(const v8::Arguments& args) {
184 Context* context = 204 Context* context =
185 static_cast<Context*>(v8::External::Cast(*args.Data())->Value()); 205 static_cast<Context*>(v8::External::Cast(*args.Data())->Value());
186 206
187 // Like firefox we assume "undefined" if no argument was specified, and 207 // Like firefox we assume "undefined" if no argument was specified, and
188 // disregard any arguments beyond the first. 208 // disregard any arguments beyond the first.
189 std::string message; 209 std::string message;
190 if (args.Length() == 0) { 210 if (args.Length() == 0) {
191 message = "undefined"; 211 message = "undefined";
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 312
293 // Try parsing the PAC script. 313 // Try parsing the PAC script.
294 scoped_ptr<Context> context(new Context(js_bindings_.get())); 314 scoped_ptr<Context> context(new Context(js_bindings_.get()));
295 int rv = context->InitV8(bytes_utf8); 315 int rv = context->InitV8(bytes_utf8);
296 if (rv == OK) 316 if (rv == OK)
297 context_.reset(context.release()); 317 context_.reset(context.release());
298 return rv; 318 return rv;
299 } 319 }
300 320
301 } // namespace net 321 } // 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