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

Unified Diff: extensions/renderer/module_system.cc

Issue 862793003: Expose requireAsync method and make it more robust (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Unit tests. Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « extensions/renderer/module_system.h ('k') | extensions/renderer/resources/mojo_private_custom_bindings.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/renderer/module_system.cc
diff --git a/extensions/renderer/module_system.cc b/extensions/renderer/module_system.cc
index 78d485c3ccaf95b440563bd55fe1f0cbf60de0a5..c66ab94ca0c672ab8935f896bd2e70ac22e41659 100644
--- a/extensions/renderer/module_system.cc
+++ b/extensions/renderer/module_system.cc
@@ -227,7 +227,15 @@ v8::Local<v8::Value> ModuleSystem::RequireForJsInner(
if (!exports->IsUndefined())
return handle_scope.Escape(exports);
- exports = LoadModule(*v8::String::Utf8Value(module_name));
+ std::string module_name_str = *v8::String::Utf8Value(module_name);
+
+ v8::Handle<v8::Value> source(GetSource(module_name_str));
+ if (source.IsEmpty() || source->IsUndefined()) {
+ Fatal(context_, "No source for require(" + module_name_str + ")");
+ return v8::Undefined(GetIsolate());
+ }
+
+ exports = LoadModule(module_name_str, source);
modules->Set(module_name, exports);
return handle_scope.Escape(exports);
}
@@ -538,13 +546,24 @@ void ModuleSystem::RequireAsync(
GetIsolate(), "Extension view no longer exists")));
return;
}
+
+ v8::Handle<v8::Value> source(GetSource(module_name));
+ if ((source.IsEmpty() || source->IsUndefined()) &&
Sam McNally 2015/01/22 07:42:11 I'm not a fan of this. It introduces ordering depe
+ !ContainsKey(module_registry->available_modules(), module_name)) {
+ std::string error_text = "No source for require(" + module_name + ")";
+ Fatal(context_, error_text);
+ resolver->Reject(v8::Exception::Error(
+ v8::String::NewFromUtf8(GetIsolate(), error_text.c_str())));
+ return;
+ }
+
module_registry->LoadModule(GetIsolate(),
module_name,
base::Bind(&ModuleSystem::OnModuleLoaded,
weak_factory_.GetWeakPtr(),
base::Passed(&persistent_resolver)));
if (module_registry->available_modules().count(module_name) == 0)
- LoadModule(module_name);
+ LoadModule(module_name, source);
}
v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) {
@@ -588,17 +607,14 @@ void ModuleSystem::Private(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().Set(privates);
}
-v8::Handle<v8::Value> ModuleSystem::LoadModule(const std::string& module_name) {
+v8::Handle<v8::Value> ModuleSystem::LoadModule(
+ const std::string& module_name,
+ v8::Handle<v8::Value> module_source) {
v8::EscapableHandleScope handle_scope(GetIsolate());
v8::Context::Scope context_scope(context()->v8_context());
- v8::Handle<v8::Value> source(GetSource(module_name));
- if (source.IsEmpty() || source->IsUndefined()) {
- Fatal(context_, "No source for require(" + module_name + ")");
- return v8::Undefined(GetIsolate());
- }
v8::Handle<v8::String> wrapped_source(
- WrapSource(v8::Handle<v8::String>::Cast(source)));
+ WrapSource(v8::Handle<v8::String>::Cast(module_source)));
// Modules are wrapped in (function(){...}) so they always return functions.
v8::Handle<v8::Value> func_as_value =
RunString(wrapped_source,
@@ -669,7 +685,7 @@ void ModuleSystem::OnDidAddPendingModule(
// responsible for loading their module dependencies when required.
if (registry->available_modules().count(dependency) == 0 &&
(module_system_managed || source_map_->Contains(dependency))) {
- LoadModule(dependency);
+ LoadModule(dependency, GetSource(dependency));
}
}
registry->AttemptToLoadMoreModules(GetIsolate());
@@ -678,8 +694,10 @@ void ModuleSystem::OnDidAddPendingModule(
void ModuleSystem::OnModuleLoaded(
scoped_ptr<v8::UniquePersistent<v8::Promise::Resolver> > resolver,
v8::Handle<v8::Value> value) {
- if (!is_valid())
+ if (!is_valid()) {
+ LOG(ERROR) << "!is_valid";
return;
+ }
v8::HandleScope handle_scope(GetIsolate());
v8::Handle<v8::Promise::Resolver> resolver_local(
v8::Local<v8::Promise::Resolver>::New(GetIsolate(), *resolver));
« no previous file with comments | « extensions/renderer/module_system.h ('k') | extensions/renderer/resources/mojo_private_custom_bindings.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698