| 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 #ifndef CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ | 5 #ifndef CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ |
| 6 #define CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ | 6 #define CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ |
| 7 | 7 |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/memory/linked_ptr.h" | 9 #include "base/memory/linked_ptr.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "chrome/renderer/extensions/native_handler.h" | 11 #include "chrome/renderer/extensions/native_handler.h" |
| 12 #include "v8/include/v8.h" | 12 #include "v8/include/v8.h" |
| 13 | 13 |
| 14 #include <map> | 14 #include <map> |
| 15 #include <set> | 15 #include <set> |
| 16 #include <string> | 16 #include <string> |
| 17 #include <vector> |
| 17 | 18 |
| 18 namespace extensions { | 19 namespace extensions { |
| 19 | 20 |
| 20 // A module system for JS similar to node.js' require() function. | 21 // A module system for JS similar to node.js' require() function. |
| 21 // Each module has three variables in the global scope: | 22 // Each module has three variables in the global scope: |
| 22 // - exports, an object returned to dependencies who require() this | 23 // - exports, an object returned to dependencies who require() this |
| 23 // module. | 24 // module. |
| 24 // - require, a function that takes a module name as an argument and returns | 25 // - require, a function that takes a module name as an argument and returns |
| 25 // that module's exports object. | 26 // that module's exports object. |
| 26 // - requireNative, a function that takes the name of a registered | 27 // - requireNative, a function that takes the name of a registered |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 void Require(const std::string& module_name); | 75 void Require(const std::string& module_name); |
| 75 v8::Handle<v8::Value> Require(const v8::Arguments& args); | 76 v8::Handle<v8::Value> Require(const v8::Arguments& args); |
| 76 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); | 77 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); |
| 77 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); | 78 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); |
| 78 | 79 |
| 79 // Calls the specified method exported by the specified module. This is | 80 // Calls the specified method exported by the specified module. This is |
| 80 // equivalent to calling require('module_name').method_name() from JS. | 81 // equivalent to calling require('module_name').method_name() from JS. |
| 81 void CallModuleMethod(const std::string& module_name, | 82 void CallModuleMethod(const std::string& module_name, |
| 82 const std::string& method_name); | 83 const std::string& method_name); |
| 83 | 84 |
| 85 // Calls the specified method exported by the specified module. This is |
| 86 // equivalent to calling require('module_name').method_name(args) from JS. |
| 87 v8::Local<v8::Value> CallModuleMethod( |
| 88 const std::string& module_name, |
| 89 const std::string& method_name, |
| 90 std::vector<v8::Handle<v8::Value> >* args); |
| 91 |
| 84 // Register |native_handler| as a potential target for requireNative(), so | 92 // Register |native_handler| as a potential target for requireNative(), so |
| 85 // calls to requireNative(|name|) from JS will return a new object created by | 93 // calls to requireNative(|name|) from JS will return a new object created by |
| 86 // |native_handler|. | 94 // |native_handler|. |
| 87 void RegisterNativeHandler(const std::string& name, | 95 void RegisterNativeHandler(const std::string& name, |
| 88 scoped_ptr<NativeHandler> native_handler); | 96 scoped_ptr<NativeHandler> native_handler); |
| 89 | 97 |
| 90 // Causes requireNative(|name|) to look for its module in |source_map_| | 98 // Causes requireNative(|name|) to look for its module in |source_map_| |
| 91 // instead of using a registered native handler. This can be used in unit | 99 // instead of using a registered native handler. This can be used in unit |
| 92 // tests to mock out native modules. | 100 // tests to mock out native modules. |
| 93 void OverrideNativeHandler(const std::string& name); | 101 void OverrideNativeHandler(const std::string& name); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 scoped_ptr<ExceptionHandler> exception_handler_; | 165 scoped_ptr<ExceptionHandler> exception_handler_; |
| 158 | 166 |
| 159 std::set<std::string> overridden_native_handlers_; | 167 std::set<std::string> overridden_native_handlers_; |
| 160 | 168 |
| 161 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); | 169 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); |
| 162 }; | 170 }; |
| 163 | 171 |
| 164 } // extensions | 172 } // extensions |
| 165 | 173 |
| 166 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ | 174 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ |
| OLD | NEW |