OLD | NEW |
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/debug/alias.h" | 8 #include "base/debug/alias.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "chrome/common/extensions/extension_messages.h" | 12 #include "chrome/common/extensions/extension_messages.h" |
13 #include "chrome/renderer/extensions/chrome_v8_context.h" | 13 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 14 #include "chrome/renderer/extensions/console.h" |
14 #include "content/public/renderer/render_view.h" | 15 #include "content/public/renderer/render_view.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup
pression.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup
pression.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 const char* kModuleSystem = "module_system"; | 20 const char* kModuleSystem = "module_system"; |
20 const char* kModuleName = "module_name"; | 21 const char* kModuleName = "module_name"; |
21 const char* kModuleField = "module_field"; | 22 const char* kModuleField = "module_field"; |
22 const char* kModulesField = "modules"; | 23 const char* kModulesField = "modules"; |
23 | 24 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( | 139 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( |
139 v8::Handle<v8::String> module_name) { | 140 v8::Handle<v8::String> module_name) { |
140 v8::HandleScope handle_scope; | 141 v8::HandleScope handle_scope; |
141 v8::Handle<v8::Object> global(v8_context()->Global()); | 142 v8::Handle<v8::Object> global(v8_context()->Global()); |
142 | 143 |
143 // The module system might have been deleted. This can happen if a different | 144 // The module system might have been deleted. This can happen if a different |
144 // context keeps a reference to us, but our frame is destroyed (e.g. | 145 // context keeps a reference to us, but our frame is destroyed (e.g. |
145 // background page keeps reference to chrome object in a closed popup). | 146 // background page keeps reference to chrome object in a closed popup). |
146 v8::Handle<v8::Value> modules_value = | 147 v8::Handle<v8::Value> modules_value = |
147 global->GetHiddenValue(v8::String::New(kModulesField)); | 148 global->GetHiddenValue(v8::String::New(kModulesField)); |
148 if (modules_value.IsEmpty() || modules_value->IsUndefined()) | 149 if (modules_value.IsEmpty() || modules_value->IsUndefined()) { |
149 return ThrowException("Extension view no longer exists"); | 150 console::Error(v8::Context::GetCalling(), |
| 151 "Extension view no longer exists"); |
| 152 } |
150 | 153 |
151 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value)); | 154 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value)); |
152 v8::Handle<v8::Value> exports(modules->Get(module_name)); | 155 v8::Handle<v8::Value> exports(modules->Get(module_name)); |
153 if (!exports->IsUndefined()) | 156 if (!exports->IsUndefined()) |
154 return handle_scope.Close(exports); | 157 return handle_scope.Close(exports); |
155 | 158 |
156 v8::Handle<v8::Value> source(GetSource(module_name)); | 159 std::string module_name_str = *v8::String::AsciiValue(module_name); |
| 160 v8::Handle<v8::Value> source(GetSource(module_name_str)); |
157 if (source->IsUndefined()) | 161 if (source->IsUndefined()) |
158 return handle_scope.Close(v8::Undefined()); | 162 return handle_scope.Close(v8::Undefined()); |
159 v8::Handle<v8::String> wrapped_source(WrapSource( | 163 v8::Handle<v8::String> wrapped_source(WrapSource( |
160 v8::Handle<v8::String>::Cast(source))); | 164 v8::Handle<v8::String>::Cast(source))); |
161 v8::Handle<v8::Function> func = | 165 v8::Handle<v8::Function> func = |
162 v8::Handle<v8::Function>::Cast(RunString(wrapped_source, module_name)); | 166 v8::Handle<v8::Function>::Cast(RunString(wrapped_source, module_name)); |
163 if (func.IsEmpty()) { | 167 CHECK(!func.IsEmpty()) << "Bad source code for " << module_name_str; |
164 return ThrowException(std::string(*v8::String::AsciiValue(module_name)) + | |
165 ": Bad source"); | |
166 } | |
167 | 168 |
168 exports = v8::Object::New(); | 169 exports = v8::Object::New(); |
169 v8::Handle<v8::Object> natives(NewInstance()); | 170 v8::Handle<v8::Object> natives(NewInstance()); |
170 v8::Handle<v8::Value> args[] = { | 171 v8::Handle<v8::Value> args[] = { |
171 natives->Get(v8::String::NewSymbol("require")), | 172 natives->Get(v8::String::NewSymbol("require")), |
172 natives->Get(v8::String::NewSymbol("requireNative")), | 173 natives->Get(v8::String::NewSymbol("requireNative")), |
173 exports, | 174 exports, |
174 }; | 175 }; |
175 { | 176 { |
176 WebKit::WebScopedMicrotaskSuppression suppression; | 177 WebKit::WebScopedMicrotaskSuppression suppression; |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 return handle_scope.Close(result); | 366 return handle_scope.Close(result); |
366 } | 367 } |
367 | 368 |
368 result = script->Run(); | 369 result = script->Run(); |
369 if (try_catch.HasCaught()) | 370 if (try_catch.HasCaught()) |
370 HandleException(try_catch); | 371 HandleException(try_catch); |
371 | 372 |
372 return handle_scope.Close(result); | 373 return handle_scope.Close(result); |
373 } | 374 } |
374 | 375 |
375 v8::Handle<v8::Value> ModuleSystem::GetSource( | 376 v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) { |
376 v8::Handle<v8::String> source_name) { | |
377 v8::HandleScope handle_scope; | 377 v8::HandleScope handle_scope; |
378 std::string module_name = *v8::String::AsciiValue(source_name); | |
379 if (!source_map_->Contains(module_name)) | 378 if (!source_map_->Contains(module_name)) |
380 return v8::Undefined(); | 379 return v8::Undefined(); |
381 return handle_scope.Close(source_map_->GetSource(module_name)); | 380 return handle_scope.Close(source_map_->GetSource(module_name)); |
382 } | 381 } |
383 | 382 |
384 v8::Handle<v8::Value> ModuleSystem::RequireNative(const v8::Arguments& args) { | 383 v8::Handle<v8::Value> ModuleSystem::RequireNative(const v8::Arguments& args) { |
385 CHECK_EQ(1, args.Length()); | 384 CHECK_EQ(1, args.Length()); |
386 std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); | 385 std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); |
387 return RequireNativeFromString(native_name); | 386 return RequireNativeFromString(native_name); |
388 } | 387 } |
389 | 388 |
390 v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString( | 389 v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString( |
391 const std::string& native_name) { | 390 const std::string& native_name) { |
392 if (natives_enabled_ == 0) | 391 if (natives_enabled_ == 0) |
393 return ThrowException("Natives disabled"); | 392 return v8::ThrowException(v8::String::New("Natives disabled")); |
394 if (overridden_native_handlers_.count(native_name) > 0u) | 393 if (overridden_native_handlers_.count(native_name) > 0u) |
395 return RequireForJsInner(v8::String::New(native_name.c_str())); | 394 return RequireForJsInner(v8::String::New(native_name.c_str())); |
396 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); | 395 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); |
397 if (i == native_handler_map_.end()) | 396 if (i == native_handler_map_.end()) |
398 return v8::Undefined(); | 397 return v8::Undefined(); |
399 return i->second->NewInstance(); | 398 return i->second->NewInstance(); |
400 } | 399 } |
401 | 400 |
402 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { | 401 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { |
403 v8::HandleScope handle_scope; | 402 v8::HandleScope handle_scope; |
404 v8::Handle<v8::String> left = v8::String::New( | 403 v8::Handle<v8::String> left = v8::String::New( |
405 "(function(require, requireNative, exports) {'use strict';"); | 404 "(function(require, requireNative, exports) {'use strict';"); |
406 v8::Handle<v8::String> right = v8::String::New("\n})"); | 405 v8::Handle<v8::String> right = v8::String::New("\n})"); |
407 return handle_scope.Close( | 406 return handle_scope.Close( |
408 v8::String::Concat(left, v8::String::Concat(source, right))); | 407 v8::String::Concat(left, v8::String::Concat(source, right))); |
409 } | 408 } |
410 | 409 |
411 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { | |
412 return v8::ThrowException(v8::String::New(message.c_str())); | |
413 } | |
414 | |
415 } // extensions | 410 } // extensions |
OLD | NEW |