Chromium Code Reviews| 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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 const std::string& method_name) { | 162 const std::string& method_name) { |
| 162 v8::HandleScope handle_scope; | 163 v8::HandleScope handle_scope; |
| 163 v8::Local<v8::Value> module = | 164 v8::Local<v8::Value> module = |
| 164 v8::Local<v8::Value>::New( | 165 v8::Local<v8::Value>::New( |
| 165 RequireForJsInner(v8::String::New(module_name.c_str()))); | 166 RequireForJsInner(v8::String::New(module_name.c_str()))); |
| 166 if (module.IsEmpty() || !module->IsObject()) | 167 if (module.IsEmpty() || !module->IsObject()) |
| 167 return; | 168 return; |
| 168 v8::Local<v8::Value> value = | 169 v8::Local<v8::Value> value = |
| 169 v8::Handle<v8::Object>::Cast(module)->Get( | 170 v8::Handle<v8::Object>::Cast(module)->Get( |
| 170 v8::String::New(method_name.c_str())); | 171 v8::String::New(method_name.c_str())); |
| 172 CallModuleMethod(value, 0, NULL); | |
| 173 } | |
| 174 | |
| 175 v8::Handle<v8::Value> ModuleSystem::CallModuleMethod( | |
| 176 v8::Local<v8::Value> value, | |
| 177 int argc, | |
| 178 v8::Handle<v8::Value> argv[]) { | |
| 171 if (value.IsEmpty() || !value->IsFunction()) | 179 if (value.IsEmpty() || !value->IsFunction()) |
| 172 return; | 180 return v8::Handle<v8::Value>(); |
| 173 v8::Handle<v8::Function> func = | 181 v8::Handle<v8::Function> func = |
| 174 v8::Handle<v8::Function>::Cast(value); | 182 v8::Handle<v8::Function>::Cast(value); |
| 175 // TODO(jeremya/koz): refer to context_ here, not the current context. | 183 // TODO(jeremya/koz): refer to context_ here, not the current context. |
| 176 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); | 184 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); |
| 185 v8::Handle<v8::Value> ret; | |
| 177 { | 186 { |
| 178 WebKit::WebScopedMicrotaskSuppression suppression; | 187 WebKit::WebScopedMicrotaskSuppression suppression; |
| 179 v8::TryCatch try_catch; | 188 v8::TryCatch try_catch; |
| 180 try_catch.SetCaptureMessage(true); | 189 try_catch.SetCaptureMessage(true); |
| 181 func->Call(global, 0, NULL); | 190 ret = func->Call(global, argc, argv); |
| 182 if (try_catch.HasCaught()) | 191 if (try_catch.HasCaught()) |
| 183 HandleException(try_catch); | 192 HandleException(try_catch); |
| 184 } | 193 } |
| 194 return ret; | |
| 195 } | |
| 196 | |
| 197 bool ModuleSystem::HasNativeHandler(const std::string& name) { | |
| 198 return native_handler_map_.find(name) != native_handler_map_.end(); | |
| 185 } | 199 } |
| 186 | 200 |
| 187 void ModuleSystem::RegisterNativeHandler(const std::string& name, | 201 void ModuleSystem::RegisterNativeHandler(const std::string& name, |
| 188 scoped_ptr<NativeHandler> native_handler) { | 202 scoped_ptr<NativeHandler> native_handler) { |
| 189 native_handler_map_[name] = | 203 native_handler_map_[name] = |
| 190 linked_ptr<NativeHandler>(native_handler.release()); | 204 linked_ptr<NativeHandler>(native_handler.release()); |
| 191 } | 205 } |
| 192 | 206 |
| 193 void ModuleSystem::OverrideNativeHandler(const std::string& name) { | 207 void ModuleSystem::OverrideNativeHandler(const std::string& name) { |
| 194 overridden_native_handlers_.insert(name); | 208 overridden_native_handlers_.insert(name); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 221 NativesEnabledScope scope(module_system); | 235 NativesEnabledScope scope(module_system); |
| 222 module = v8::Handle<v8::Object>::Cast(module_system->RequireForJsInner( | 236 module = v8::Handle<v8::Object>::Cast(module_system->RequireForJsInner( |
| 223 parameters->Get(v8::String::New(kModuleName))->ToString())); | 237 parameters->Get(v8::String::New(kModuleName))->ToString())); |
| 224 } | 238 } |
| 225 if (module.IsEmpty()) | 239 if (module.IsEmpty()) |
| 226 return handle_scope.Close(v8::Handle<v8::Value>()); | 240 return handle_scope.Close(v8::Handle<v8::Value>()); |
| 227 | 241 |
| 228 v8::Handle<v8::String> field = | 242 v8::Handle<v8::String> field = |
| 229 parameters->Get(v8::String::New(kModuleField))->ToString(); | 243 parameters->Get(v8::String::New(kModuleField))->ToString(); |
| 230 | 244 |
| 231 return handle_scope.Close(module->Get(field)); | 245 v8::Local<v8::Value> value = module->Get(field); |
|
not at google - send to devlin
2012/12/13 22:26:40
Something to add a TODO about - it seems like we c
| |
| 246 if (!parameters->Has(v8::String::New(kArgument))) | |
| 247 return handle_scope.Close(module->Get(field)); | |
| 248 v8::Handle<v8::Value> argv[] = {parameters->Get(v8::String::New(kArgument))}; | |
| 249 | |
| 250 NativesEnabledScope scope(module_system); | |
| 251 return handle_scope.Close(module_system->CallModuleMethod(value, 1, argv)); | |
|
not at google - send to devlin
2012/12/13 22:26:40
Yeah, all this stuff - can it go in schema_binding
| |
| 232 } | 252 } |
| 233 | 253 |
| 234 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, | 254 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
| 235 const std::string& field, | 255 const std::string& field, |
| 236 const std::string& module_name, | 256 const std::string& module_name, |
| 237 const std::string& module_field) { | 257 const std::string& module_field) { |
| 258 SetLazyField(object, field, module_name, module_field, ""); | |
| 259 } | |
| 260 | |
| 261 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, | |
| 262 const std::string& field, | |
| 263 const std::string& module_name, | |
| 264 const std::string& module_field, | |
| 265 const std::string& arg) { | |
| 238 v8::HandleScope handle_scope; | 266 v8::HandleScope handle_scope; |
| 239 v8::Handle<v8::Object> parameters = v8::Object::New(); | 267 v8::Handle<v8::Object> parameters = v8::Object::New(); |
| 240 parameters->Set(v8::String::New(kModuleName), | 268 parameters->Set(v8::String::New(kModuleName), |
| 241 v8::String::New(module_name.c_str())); | 269 v8::String::New(module_name.c_str())); |
| 242 parameters->Set(v8::String::New(kModuleField), | 270 parameters->Set(v8::String::New(kModuleField), |
| 243 v8::String::New(module_field.c_str())); | 271 v8::String::New(module_field.c_str())); |
| 272 if (arg.size()) { | |
| 273 parameters->Set(v8::String::New(kArgument), | |
| 274 v8::String::New(arg.c_str())); | |
| 275 } | |
| 244 | 276 |
| 245 object->SetAccessor(v8::String::New(field.c_str()), | 277 object->SetAccessor(v8::String::New(field.c_str()), |
| 246 &ModuleSystem::LazyFieldGetter, | 278 &ModuleSystem::LazyFieldGetter, |
| 247 NULL, | 279 NULL, |
| 248 parameters); | 280 parameters); |
| 249 } | 281 } |
| 250 | 282 |
| 251 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, | 283 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, |
| 252 v8::Handle<v8::String> name) { | 284 v8::Handle<v8::String> name) { |
| 253 v8::HandleScope handle_scope; | 285 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})"); | 329 v8::Handle<v8::String> right = v8::String::New("\n})"); |
| 298 return handle_scope.Close( | 330 return handle_scope.Close( |
| 299 v8::String::Concat(left, v8::String::Concat(source, right))); | 331 v8::String::Concat(left, v8::String::Concat(source, right))); |
| 300 } | 332 } |
| 301 | 333 |
| 302 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { | 334 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { |
| 303 return v8::ThrowException(v8::String::New(message.c_str())); | 335 return v8::ThrowException(v8::String::New(message.c_str())); |
| 304 } | 336 } |
| 305 | 337 |
| 306 } // extensions | 338 } // extensions |
| OLD | NEW |