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 "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup
pression.h" | 8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup
pression.h" |
9 | 9 |
10 namespace { | 10 namespace { |
11 | 11 |
| 12 const char* kArgument = "argument"; |
12 const char* kModuleSystem = "module_system"; | 13 const char* kModuleSystem = "module_system"; |
13 const char* kModuleName = "module_name"; | 14 const char* kModuleName = "module_name"; |
14 const char* kModuleField = "module_field"; | 15 const char* kModuleField = "module_field"; |
15 const char* kModulesField = "modules"; | 16 const char* kModulesField = "modules"; |
16 | 17 |
17 } // namespace | 18 } // namespace |
18 | 19 |
19 namespace extensions { | 20 namespace extensions { |
20 | 21 |
21 ModuleSystem::ModuleSystem(v8::Handle<v8::Context> context, | 22 ModuleSystem::ModuleSystem(v8::Handle<v8::Context> context, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 stack_trace.assign(*stack_value, stack_value.length()); | 97 stack_trace.assign(*stack_value, stack_value.length()); |
97 else | 98 else |
98 stack_trace = "<could not convert stack trace to string>"; | 99 stack_trace = "<could not convert stack trace to string>"; |
99 } | 100 } |
100 | 101 |
101 LOG(ERROR) << "[" << resource_name << "(" << message->GetLineNumber() << ")] " | 102 LOG(ERROR) << "[" << resource_name << "(" << message->GetLineNumber() << ")] " |
102 << error_message | 103 << error_message |
103 << "{" << stack_trace << "}"; | 104 << "{" << stack_trace << "}"; |
104 } | 105 } |
105 | 106 |
106 void ModuleSystem::Require(const std::string& module_name) { | 107 v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) { |
107 v8::HandleScope handle_scope; | 108 v8::HandleScope handle_scope; |
108 RequireForJsInner(v8::String::New(module_name.c_str())); | 109 return handle_scope.Close( |
| 110 RequireForJsInner(v8::String::New(module_name.c_str()))); |
109 } | 111 } |
110 | 112 |
111 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { | 113 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { |
112 v8::HandleScope handle_scope; | 114 v8::HandleScope handle_scope; |
113 v8::Handle<v8::String> module_name = args[0]->ToString(); | 115 v8::Handle<v8::String> module_name = args[0]->ToString(); |
114 return handle_scope.Close(RequireForJsInner(module_name)); | 116 return handle_scope.Close(RequireForJsInner(module_name)); |
115 } | 117 } |
116 | 118 |
117 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( | 119 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( |
118 v8::Handle<v8::String> module_name) { | 120 v8::Handle<v8::String> module_name) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 const std::string& method_name) { | 163 const std::string& method_name) { |
162 v8::HandleScope handle_scope; | 164 v8::HandleScope handle_scope; |
163 v8::Local<v8::Value> module = | 165 v8::Local<v8::Value> module = |
164 v8::Local<v8::Value>::New( | 166 v8::Local<v8::Value>::New( |
165 RequireForJsInner(v8::String::New(module_name.c_str()))); | 167 RequireForJsInner(v8::String::New(module_name.c_str()))); |
166 if (module.IsEmpty() || !module->IsObject()) | 168 if (module.IsEmpty() || !module->IsObject()) |
167 return; | 169 return; |
168 v8::Local<v8::Value> value = | 170 v8::Local<v8::Value> value = |
169 v8::Handle<v8::Object>::Cast(module)->Get( | 171 v8::Handle<v8::Object>::Cast(module)->Get( |
170 v8::String::New(method_name.c_str())); | 172 v8::String::New(method_name.c_str())); |
| 173 CallModuleMethod(value, 0, NULL); |
| 174 } |
| 175 |
| 176 v8::Handle<v8::Value> ModuleSystem::CallModuleMethod( |
| 177 v8::Local<v8::Value> value, |
| 178 int argc, |
| 179 v8::Handle<v8::Value> argv[]) { |
171 if (value.IsEmpty() || !value->IsFunction()) | 180 if (value.IsEmpty() || !value->IsFunction()) |
172 return; | 181 return v8::Handle<v8::Value>(); |
173 v8::Handle<v8::Function> func = | 182 v8::Handle<v8::Function> func = |
174 v8::Handle<v8::Function>::Cast(value); | 183 v8::Handle<v8::Function>::Cast(value); |
175 // TODO(jeremya/koz): refer to context_ here, not the current context. | 184 // TODO(jeremya/koz): refer to context_ here, not the current context. |
176 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); | 185 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); |
| 186 v8::Handle<v8::Value> ret; |
177 { | 187 { |
178 WebKit::WebScopedMicrotaskSuppression suppression; | 188 WebKit::WebScopedMicrotaskSuppression suppression; |
179 v8::TryCatch try_catch; | 189 v8::TryCatch try_catch; |
180 try_catch.SetCaptureMessage(true); | 190 try_catch.SetCaptureMessage(true); |
181 func->Call(global, 0, NULL); | 191 ret = func->Call(global, argc, argv); |
182 if (try_catch.HasCaught()) | 192 if (try_catch.HasCaught()) |
183 HandleException(try_catch); | 193 HandleException(try_catch); |
184 } | 194 } |
| 195 return ret; |
| 196 } |
| 197 |
| 198 bool ModuleSystem::HasNativeHandler(const std::string& name) { |
| 199 return native_handler_map_.find(name) != native_handler_map_.end(); |
185 } | 200 } |
186 | 201 |
187 void ModuleSystem::RegisterNativeHandler(const std::string& name, | 202 void ModuleSystem::RegisterNativeHandler(const std::string& name, |
188 scoped_ptr<NativeHandler> native_handler) { | 203 scoped_ptr<NativeHandler> native_handler) { |
189 native_handler_map_[name] = | 204 native_handler_map_[name] = |
190 linked_ptr<NativeHandler>(native_handler.release()); | 205 linked_ptr<NativeHandler>(native_handler.release()); |
191 } | 206 } |
192 | 207 |
193 void ModuleSystem::OverrideNativeHandler(const std::string& name) { | 208 void ModuleSystem::OverrideNativeHandler(const std::string& name) { |
194 overridden_native_handlers_.insert(name); | 209 overridden_native_handlers_.insert(name); |
(...skipping 26 matching lines...) Expand all Loading... |
221 NativesEnabledScope scope(module_system); | 236 NativesEnabledScope scope(module_system); |
222 module = v8::Handle<v8::Object>::Cast(module_system->RequireForJsInner( | 237 module = v8::Handle<v8::Object>::Cast(module_system->RequireForJsInner( |
223 parameters->Get(v8::String::New(kModuleName))->ToString())); | 238 parameters->Get(v8::String::New(kModuleName))->ToString())); |
224 } | 239 } |
225 if (module.IsEmpty()) | 240 if (module.IsEmpty()) |
226 return handle_scope.Close(v8::Handle<v8::Value>()); | 241 return handle_scope.Close(v8::Handle<v8::Value>()); |
227 | 242 |
228 v8::Handle<v8::String> field = | 243 v8::Handle<v8::String> field = |
229 parameters->Get(v8::String::New(kModuleField))->ToString(); | 244 parameters->Get(v8::String::New(kModuleField))->ToString(); |
230 | 245 |
231 return handle_scope.Close(module->Get(field)); | 246 v8::Local<v8::Value> value = module->Get(field); |
| 247 if (!parameters->Has(v8::String::New(kArgument))) |
| 248 return handle_scope.Close(module->Get(field)); |
| 249 v8::Handle<v8::Value> argv[] = {parameters->Get(v8::String::New(kArgument))}; |
| 250 |
| 251 NativesEnabledScope scope(module_system); |
| 252 return handle_scope.Close(module_system->CallModuleMethod(value, 1, argv)); |
232 } | 253 } |
233 | 254 |
234 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, | 255 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
235 const std::string& field, | 256 const std::string& field, |
236 const std::string& module_name, | 257 const std::string& module_name, |
237 const std::string& module_field) { | 258 const std::string& module_field) { |
| 259 SetLazyField(object, field, module_name, module_field, ""); |
| 260 } |
| 261 |
| 262 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
| 263 const std::string& field, |
| 264 const std::string& module_name, |
| 265 const std::string& module_field, |
| 266 const std::string& arg) { |
238 v8::HandleScope handle_scope; | 267 v8::HandleScope handle_scope; |
239 v8::Handle<v8::Object> parameters = v8::Object::New(); | 268 v8::Handle<v8::Object> parameters = v8::Object::New(); |
240 parameters->Set(v8::String::New(kModuleName), | 269 parameters->Set(v8::String::New(kModuleName), |
241 v8::String::New(module_name.c_str())); | 270 v8::String::New(module_name.c_str())); |
242 parameters->Set(v8::String::New(kModuleField), | 271 parameters->Set(v8::String::New(kModuleField), |
243 v8::String::New(module_field.c_str())); | 272 v8::String::New(module_field.c_str())); |
| 273 if (arg.size()) { |
| 274 parameters->Set(v8::String::New(kArgument), |
| 275 v8::String::New(arg.c_str())); |
| 276 } |
244 | 277 |
245 object->SetAccessor(v8::String::New(field.c_str()), | 278 object->SetAccessor(v8::String::New(field.c_str()), |
246 &ModuleSystem::LazyFieldGetter, | 279 &ModuleSystem::LazyFieldGetter, |
247 NULL, | 280 NULL, |
248 parameters); | 281 parameters); |
249 } | 282 } |
250 | 283 |
251 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, | 284 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, |
252 v8::Handle<v8::String> name) { | 285 v8::Handle<v8::String> name) { |
253 v8::HandleScope handle_scope; | 286 v8::HandleScope handle_scope; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 v8::Handle<v8::String> right = v8::String::New("\n})"); | 330 v8::Handle<v8::String> right = v8::String::New("\n})"); |
298 return handle_scope.Close( | 331 return handle_scope.Close( |
299 v8::String::Concat(left, v8::String::Concat(source, right))); | 332 v8::String::Concat(left, v8::String::Concat(source, right))); |
300 } | 333 } |
301 | 334 |
302 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { | 335 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { |
303 return v8::ThrowException(v8::String::New(message.c_str())); | 336 return v8::ThrowException(v8::String::New(message.c_str())); |
304 } | 337 } |
305 | 338 |
306 } // extensions | 339 } // extensions |
OLD | NEW |