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

Side by Side Diff: chrome/renderer/extensions/module_system.cc

Issue 23604050: Namespace internal extension JavaScript modules with extensions:: so that they (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Error Console Tests Fixed (uploaded by rdevlin.cronin) Created 7 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) 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 "chrome/renderer/extensions/module_system.h" 5 #include "chrome/renderer/extensions/module_system.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/debug/trace_event.h" 9 #include "base/debug/trace_event.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 if (modules_value.IsEmpty() || modules_value->IsUndefined()) { 203 if (modules_value.IsEmpty() || modules_value->IsUndefined()) {
204 Warn("Extension view no longer exists"); 204 Warn("Extension view no longer exists");
205 return v8::Undefined(); 205 return v8::Undefined();
206 } 206 }
207 207
208 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value)); 208 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value));
209 v8::Handle<v8::Value> exports(modules->Get(module_name)); 209 v8::Handle<v8::Value> exports(modules->Get(module_name));
210 if (!exports->IsUndefined()) 210 if (!exports->IsUndefined())
211 return handle_scope.Close(exports); 211 return handle_scope.Close(exports);
212 212
213 std::string module_name_str = *v8::String::AsciiValue(module_name); 213 std::string module_name_str = *v8::String::Utf8Value(module_name);
214 v8::Handle<v8::Value> source(GetSource(module_name_str)); 214 v8::Handle<v8::Value> source(GetSource(module_name_str));
215 if (source.IsEmpty() || source->IsUndefined()) { 215 if (source.IsEmpty() || source->IsUndefined()) {
216 Fatal(context_, "No source for require(" + module_name_str + ")"); 216 Fatal(context_, "No source for require(" + module_name_str + ")");
217 return v8::Undefined(); 217 return v8::Undefined();
218 } 218 }
219 v8::Handle<v8::String> wrapped_source(WrapSource( 219 v8::Handle<v8::String> wrapped_source(WrapSource(
220 v8::Handle<v8::String>::Cast(source))); 220 v8::Handle<v8::String>::Cast(source)));
221 // Modules are wrapped in (function(){...}) so they always return functions. 221 // Modules are wrapped in (function(){...}) so they always return functions.
222 v8::Handle<v8::Value> func_as_value = RunString(wrapped_source, module_name); 222 v8::Handle<v8::Value> func_as_value = RunString(wrapped_source, module_name);
223 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { 223 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) {
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) { 369 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) {
370 // ModuleSystem has been deleted. 370 // ModuleSystem has been deleted.
371 // TODO(kalman): See comment in header file. 371 // TODO(kalman): See comment in header file.
372 Warn("Module system has been deleted, does extension view exist?"); 372 Warn("Module system has been deleted, does extension view exist?");
373 return; 373 return;
374 } 374 }
375 375
376 ModuleSystem* module_system = static_cast<ModuleSystem*>( 376 ModuleSystem* module_system = static_cast<ModuleSystem*>(
377 v8::Handle<v8::External>::Cast(module_system_value)->Value()); 377 v8::Handle<v8::External>::Cast(module_system_value)->Value());
378 378
379 std::string name = *v8::String::AsciiValue( 379 std::string name = *v8::String::Utf8Value(
380 parameters->Get(v8::String::New(kModuleName))->ToString()); 380 parameters->Get(v8::String::New(kModuleName))->ToString());
381 381
382 // Switch to our v8 context because we need functions created while running 382 // Switch to our v8 context because we need functions created while running
383 // the require()d module to belong to our context, not the current one. 383 // the require()d module to belong to our context, not the current one.
384 v8::Context::Scope context_scope(context); 384 v8::Context::Scope context_scope(context);
385 NativesEnabledScope natives_enabled_scope(module_system); 385 NativesEnabledScope natives_enabled_scope(module_system);
386 386
387 v8::TryCatch try_catch; 387 v8::TryCatch try_catch;
388 v8::Handle<v8::Value> module_value = (module_system->*require_function)(name); 388 v8::Handle<v8::Value> module_value = (module_system->*require_function)(name);
389 if (try_catch.HasCaught()) { 389 if (try_catch.HasCaught()) {
390 module_system->HandleException(try_catch); 390 module_system->HandleException(try_catch);
391 return; 391 return;
392 } 392 }
393 if (module_value.IsEmpty() || !module_value->IsObject()) { 393 if (module_value.IsEmpty() || !module_value->IsObject()) {
394 // require_function will have already logged this, we don't need to. 394 // require_function will have already logged this, we don't need to.
395 return; 395 return;
396 } 396 }
397 397
398 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(module_value); 398 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(module_value);
399 v8::Handle<v8::String> field = 399 v8::Handle<v8::String> field =
400 parameters->Get(v8::String::New(kModuleField))->ToString(); 400 parameters->Get(v8::String::New(kModuleField))->ToString();
401 401
402 if (!module->Has(field)) { 402 if (!module->Has(field)) {
403 std::string field_str = *v8::String::AsciiValue(field); 403 std::string field_str = *v8::String::Utf8Value(field);
404 Fatal(module_system->context_, 404 Fatal(module_system->context_,
405 "Lazy require of " + name + "." + field_str + " did not set the " + 405 "Lazy require of " + name + "." + field_str + " did not set the " +
406 field_str + " field"); 406 field_str + " field");
407 return; 407 return;
408 } 408 }
409 409
410 v8::Local<v8::Value> new_field = module->Get(field); 410 v8::Local<v8::Value> new_field = module->Get(field);
411 if (try_catch.HasCaught()) { 411 if (try_catch.HasCaught()) {
412 module_system->HandleException(try_catch); 412 module_system->HandleException(try_catch);
413 return; 413 return;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 461
462 v8::Isolate* ModuleSystem::GetIsolate() const { 462 v8::Isolate* ModuleSystem::GetIsolate() const {
463 return context_->isolate(); 463 return context_->isolate();
464 } 464 }
465 465
466 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, 466 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
467 v8::Handle<v8::String> name) { 467 v8::Handle<v8::String> name) {
468 v8::HandleScope handle_scope(GetIsolate()); 468 v8::HandleScope handle_scope(GetIsolate());
469 v8::Context::Scope context_scope(context()->v8_context()); 469 v8::Context::Scope context_scope(context()->v8_context());
470 470
471 // Prepend extensions:: to |name| so that internal code can be differentiated
472 // from external code in stack traces. This has no effect on behaviour.
473 std::string internal_name = base::StringPrintf("extensions::%s",
474 *v8::String::Utf8Value(name));
475
471 WebKit::WebScopedMicrotaskSuppression suppression; 476 WebKit::WebScopedMicrotaskSuppression suppression;
472 v8::TryCatch try_catch; 477 v8::TryCatch try_catch;
473 try_catch.SetCaptureMessage(true); 478 try_catch.SetCaptureMessage(true);
474 v8::Handle<v8::Script> script(v8::Script::New(code, name)); 479 v8::Handle<v8::Script> script(v8::Script::New(
480 code, v8::String::New(internal_name.c_str(), internal_name.size())));
475 if (try_catch.HasCaught()) { 481 if (try_catch.HasCaught()) {
476 HandleException(try_catch); 482 HandleException(try_catch);
477 return v8::Undefined(); 483 return v8::Undefined();
478 } 484 }
479 485
480 v8::Handle<v8::Value> result = script->Run(); 486 v8::Handle<v8::Value> result = script->Run();
481 if (try_catch.HasCaught()) { 487 if (try_catch.HasCaught()) {
482 HandleException(try_catch); 488 HandleException(try_catch);
483 return v8::Undefined(); 489 return v8::Undefined();
484 } 490 }
485 491
486 return handle_scope.Close(result); 492 return handle_scope.Close(result);
487 } 493 }
488 494
489 v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) { 495 v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) {
490 v8::HandleScope handle_scope(GetIsolate()); 496 v8::HandleScope handle_scope(GetIsolate());
491 if (!source_map_->Contains(module_name)) 497 if (!source_map_->Contains(module_name))
492 return v8::Undefined(); 498 return v8::Undefined();
493 return handle_scope.Close(source_map_->GetSource(module_name)); 499 return handle_scope.Close(source_map_->GetSource(module_name));
494 } 500 }
495 501
496 void ModuleSystem::RequireNative( 502 void ModuleSystem::RequireNative(
497 const v8::FunctionCallbackInfo<v8::Value>& args) { 503 const v8::FunctionCallbackInfo<v8::Value>& args) {
498 CHECK_EQ(1, args.Length()); 504 CHECK_EQ(1, args.Length());
499 std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); 505 std::string native_name = *v8::String::Utf8Value(args[0]->ToString());
500 args.GetReturnValue().Set(RequireNativeFromString(native_name)); 506 args.GetReturnValue().Set(RequireNativeFromString(native_name));
501 } 507 }
502 508
503 v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString( 509 v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString(
504 const std::string& native_name) { 510 const std::string& native_name) {
505 if (natives_enabled_ == 0) { 511 if (natives_enabled_ == 0) {
506 // HACK: if in test throw exception so that we can test the natives-disabled 512 // HACK: if in test throw exception so that we can test the natives-disabled
507 // logic; however, under normal circumstances, this is programmer error so 513 // logic; however, under normal circumstances, this is programmer error so
508 // we could crash. 514 // we could crash.
509 if (exception_handler_) 515 if (exception_handler_)
(...skipping 21 matching lines...) Expand all
531 "(function(require, requireNative, exports, " 537 "(function(require, requireNative, exports, "
532 "console, " 538 "console, "
533 "$Array, $Function, $JSON, $Object, $RegExp, $String) {" 539 "$Array, $Function, $JSON, $Object, $RegExp, $String) {"
534 "'use strict';"); 540 "'use strict';");
535 v8::Handle<v8::String> right = v8::String::New("\n})"); 541 v8::Handle<v8::String> right = v8::String::New("\n})");
536 return handle_scope.Close( 542 return handle_scope.Close(
537 v8::String::Concat(left, v8::String::Concat(source, right))); 543 v8::String::Concat(left, v8::String::Concat(source, right)));
538 } 544 }
539 545
540 } // namespace extensions 546 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/error_console/error_console_browsertest.cc ('k') | extensions/common/extension_urls.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698