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/stl_util.h" |
8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup
pression.h" | 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup
pression.h" |
9 | 10 |
10 namespace { | 11 namespace { |
11 | 12 |
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 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 HandleException(try_catch); | 153 HandleException(try_catch); |
153 return v8::Undefined(); | 154 return v8::Undefined(); |
154 } | 155 } |
155 } | 156 } |
156 modules->Set(module_name, exports); | 157 modules->Set(module_name, exports); |
157 return handle_scope.Close(exports); | 158 return handle_scope.Close(exports); |
158 } | 159 } |
159 | 160 |
160 void ModuleSystem::CallModuleMethod(const std::string& module_name, | 161 void ModuleSystem::CallModuleMethod(const std::string& module_name, |
161 const std::string& method_name) { | 162 const std::string& method_name) { |
| 163 std::vector<v8::Handle<v8::Value> > args; |
| 164 CallModuleMethod(module_name, method_name, &args); |
| 165 } |
| 166 |
| 167 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
| 168 const std::string& module_name, |
| 169 const std::string& method_name, |
| 170 std::vector<v8::Handle<v8::Value> >* args) { |
162 v8::HandleScope handle_scope; | 171 v8::HandleScope handle_scope; |
163 v8::Local<v8::Value> module = | 172 v8::Local<v8::Value> module = |
164 v8::Local<v8::Value>::New( | 173 v8::Local<v8::Value>::New( |
165 RequireForJsInner(v8::String::New(module_name.c_str()))); | 174 RequireForJsInner(v8::String::New(module_name.c_str()))); |
166 if (module.IsEmpty() || !module->IsObject()) | 175 if (module.IsEmpty() || !module->IsObject()) |
167 return; | 176 return v8::Local<v8::Value>(); |
168 v8::Local<v8::Value> value = | 177 v8::Local<v8::Value> value = |
169 v8::Handle<v8::Object>::Cast(module)->Get( | 178 v8::Handle<v8::Object>::Cast(module)->Get( |
170 v8::String::New(method_name.c_str())); | 179 v8::String::New(method_name.c_str())); |
171 if (value.IsEmpty() || !value->IsFunction()) | 180 if (value.IsEmpty() || !value->IsFunction()) |
172 return; | 181 return v8::Local<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::Local<v8::Value> result; |
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 result = func->Call(global, args->size(), vector_as_array(args)); |
182 if (try_catch.HasCaught()) | 192 if (try_catch.HasCaught()) |
183 HandleException(try_catch); | 193 HandleException(try_catch); |
184 } | 194 } |
| 195 return handle_scope.Close(result); |
185 } | 196 } |
186 | 197 |
187 void ModuleSystem::RegisterNativeHandler(const std::string& name, | 198 void ModuleSystem::RegisterNativeHandler(const std::string& name, |
188 scoped_ptr<NativeHandler> native_handler) { | 199 scoped_ptr<NativeHandler> native_handler) { |
189 native_handler_map_[name] = | 200 native_handler_map_[name] = |
190 linked_ptr<NativeHandler>(native_handler.release()); | 201 linked_ptr<NativeHandler>(native_handler.release()); |
191 } | 202 } |
192 | 203 |
193 void ModuleSystem::OverrideNativeHandler(const std::string& name) { | 204 void ModuleSystem::OverrideNativeHandler(const std::string& name) { |
194 overridden_native_handlers_.insert(name); | 205 overridden_native_handlers_.insert(name); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 v8::Handle<v8::String> right = v8::String::New("\n})"); | 308 v8::Handle<v8::String> right = v8::String::New("\n})"); |
298 return handle_scope.Close( | 309 return handle_scope.Close( |
299 v8::String::Concat(left, v8::String::Concat(source, right))); | 310 v8::String::Concat(left, v8::String::Concat(source, right))); |
300 } | 311 } |
301 | 312 |
302 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { | 313 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { |
303 return v8::ThrowException(v8::String::New(message.c_str())); | 314 return v8::ThrowException(v8::String::New(message.c_str())); |
304 } | 315 } |
305 | 316 |
306 } // extensions | 317 } // extensions |
OLD | NEW |