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 #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/object_backed_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 | 17 |
| 18 namespace extensions { | 18 namespace extensions { |
| 19 | 19 |
| 20 // A module system for JS similar to node.js' require() function. | 20 // A module system for JS similar to node.js' require() function. |
| 21 // Each module has three variables in the global scope: | 21 // Each module has three variables in the global scope: |
| 22 // - exports, an object returned to dependencies who require() this | 22 // - exports, an object returned to dependencies who require() this |
| 23 // module. | 23 // module. |
| 24 // - require, a function that takes a module name as an argument and returns | 24 // - require, a function that takes a module name as an argument and returns |
| 25 // that module's exports object. | 25 // that module's exports object. |
| 26 // - requireNative, a function that takes the name of a registered | 26 // - requireNative, a function that takes the name of a registered |
| 27 // NativeHandler and returns an object that contains the functions the | 27 // NativeHandler and returns an object that contains the functions the |
| 28 // NativeHandler defines. | 28 // NativeHandler defines. |
| 29 // | 29 // |
| 30 // Each module in a ModuleSystem is executed at most once and its exports | 30 // Each module in a ModuleSystem is executed at most once and its exports |
| 31 // object cached. | 31 // object cached. |
| 32 // | 32 // |
| 33 // Note that a ModuleSystem must be used only in conjunction with a single | 33 // Note that a ModuleSystem must be used only in conjunction with a single |
| 34 // v8::Context. | 34 // v8::Context. |
| 35 // TODO(koz): Rename this to JavaScriptModuleSystem. | 35 // TODO(koz): Rename this to JavaScriptModuleSystem. |
| 36 class ModuleSystem : public NativeHandler { | 36 class ModuleSystem : public ObjectBackedNativeHandler { |
| 37 public: | 37 public: |
| 38 class SourceMap { | 38 class SourceMap { |
| 39 public: | 39 public: |
| 40 virtual ~SourceMap() {} | 40 virtual ~SourceMap() {} |
| 41 virtual v8::Handle<v8::Value> GetSource(const std::string& name) = 0; | 41 virtual v8::Handle<v8::Value> GetSource(const std::string& name) = 0; |
| 42 virtual bool Contains(const std::string& name) = 0; | 42 virtual bool Contains(const std::string& name) = 0; |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 class ExceptionHandler { | 45 class ExceptionHandler { |
| 46 public: | 46 public: |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 64 virtual ~ModuleSystem(); | 64 virtual ~ModuleSystem(); |
| 65 | 65 |
| 66 // Returns true if the current context has a ModuleSystem installed in it. | 66 // Returns true if the current context has a ModuleSystem installed in it. |
| 67 static bool IsPresentInCurrentContext(); | 67 static bool IsPresentInCurrentContext(); |
| 68 | 68 |
| 69 // Dumps the debug info from |try_catch| to LOG(ERROR). | 69 // Dumps the debug info from |try_catch| to LOG(ERROR). |
| 70 static void DumpException(const v8::TryCatch& try_catch); | 70 static void DumpException(const v8::TryCatch& try_catch); |
| 71 | 71 |
| 72 // Require the specified module. This is the equivalent of calling | 72 // Require the specified module. This is the equivalent of calling |
| 73 // require('module_name') from the loaded JS files. | 73 // require('module_name') from the loaded JS files. |
| 74 void Require(const std::string& module_name); | 74 v8::Handle<v8::Value> Require(const std::string& module_name); |
| 75 v8::Handle<v8::Value> Require(const v8::Arguments& args); | 75 v8::Handle<v8::Value> Require(const v8::Arguments& args); |
| 76 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); | 76 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); |
| 77 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); | 77 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); |
|
not at google - send to devlin
2013/01/25 00:44:31
hm these two methods (RequireForJs/RequireForJsInn
cduvall
2013/02/12 02:13:47
Done.
| |
| 78 | 78 |
| 79 // Calls the specified method exported by the specified module. This is | 79 // Calls the specified method exported by the specified module. This is |
| 80 // equivalent to calling require('module_name').method_name() from JS. | 80 // equivalent to calling require('module_name').method_name() from JS. |
| 81 void CallModuleMethod(const std::string& module_name, | 81 void CallModuleMethod(const std::string& module_name, |
| 82 const std::string& method_name); | 82 const std::string& method_name); |
| 83 | 83 |
| 84 // Call |value| with arguments |argv| and return the result. | |
| 85 // TODO(cduvall): Make a v8::Function | |
| 86 v8::Handle<v8::Value> CallModuleMethod(v8::Local<v8::Value> value, | |
| 87 int argc, | |
| 88 v8::Handle<v8::Value> argv[]); | |
| 89 | |
| 84 // Register |native_handler| as a potential target for requireNative(), so | 90 // Register |native_handler| as a potential target for requireNative(), so |
| 85 // calls to requireNative(|name|) from JS will return a new object created by | 91 // calls to requireNative(|name|) from JS will return a new object created by |
| 86 // |native_handler|. | 92 // |native_handler|. |
| 87 void RegisterNativeHandler(const std::string& name, | 93 void RegisterNativeHandler(const std::string& name, |
| 88 scoped_ptr<NativeHandler> native_handler); | 94 scoped_ptr<NativeHandler> native_handler); |
| 95 // Check if a native handler has been registered for this |name|. | |
| 96 bool HasNativeHandler(const std::string& name); | |
| 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); |
| 94 | 102 |
| 95 // Executes |code| in the current context with |name| as the filename. | 103 // Executes |code| in the current context with |name| as the filename. |
| 96 void RunString(const std::string& code, const std::string& name); | 104 void RunString(const std::string& code, const std::string& name); |
| 97 | 105 |
| 98 // Retrieves the lazily defined field specified by |property|. | 106 // Retrieves the lazily defined field specified by |property|. |
| 99 static v8::Handle<v8::Value> LazyFieldGetter(v8::Local<v8::String> property, | 107 static v8::Handle<v8::Value> LazyFieldGetter(v8::Local<v8::String> property, |
| 100 const v8::AccessorInfo& info); | 108 const v8::AccessorInfo& info); |
| 109 // Retrieves the lazily defined field specified by |property| on a native | |
| 110 // object. | |
| 111 static v8::Handle<v8::Value> NativeLazyFieldGetter( | |
| 112 v8::Local<v8::String> property, | |
| 113 const v8::AccessorInfo& info); | |
| 114 | |
| 115 typedef v8::Handle<v8::Object> (ModuleSystem::*GetModuleFunc)( | |
| 116 v8::Handle<v8::Object>); | |
| 117 // Base implementation of a LazyFieldGetter that can be customized by passing | |
| 118 // in a |get_module| function. | |
| 119 static v8::Handle<v8::Value> BaseLazyFieldGetter( | |
|
not at google - send to devlin
2013/01/25 00:44:31
Should be private.
Also the "base" confused me, i
cduvall
2013/02/12 02:13:47
Done.
| |
| 120 v8::Local<v8::String> property, | |
| 121 const v8::AccessorInfo& info, | |
| 122 GetModuleFunc get_module); | |
| 123 // Gets a module. Same as calling |require|. | |
| 124 v8::Handle<v8::Object> GetModule(v8::Handle<v8::Object> parameters); | |
| 125 // Gets a native module. Same as calling |requireNative|. | |
| 126 v8::Handle<v8::Object> GetNativeModule(v8::Handle<v8::Object> parameters); | |
|
not at google - send to devlin
2013/01/25 00:44:31
should all be private
I also don't think these ar
cduvall
2013/02/12 02:13:47
Done.
| |
| 101 | 127 |
| 102 // Make |object|.|field| lazily evaluate to the result of | 128 // Make |object|.|field| lazily evaluate to the result of |
| 103 // require(|module_name|)[|module_field|]. | 129 // require(|module_name|)[|module_field|]. |
| 104 void SetLazyField(v8::Handle<v8::Object> object, | 130 void SetLazyField(v8::Handle<v8::Object> object, |
| 105 const std::string& field, | 131 const std::string& field, |
| 106 const std::string& module_name, | 132 const std::string& module_name, |
| 107 const std::string& module_field); | 133 const std::string& module_field); |
| 108 | 134 |
| 135 // Make |object|.|field| lazily evaluate to the result of | |
| 136 // requireNative(|module_name|)[|module_field|]. | |
| 137 void SetNativeLazyField(v8::Handle<v8::Object> object, | |
| 138 const std::string& field, | |
| 139 const std::string& module_name, | |
| 140 const std::string& module_field); | |
| 141 | |
| 109 void set_exception_handler(scoped_ptr<ExceptionHandler> handler) { | 142 void set_exception_handler(scoped_ptr<ExceptionHandler> handler) { |
| 110 exception_handler_ = handler.Pass(); | 143 exception_handler_ = handler.Pass(); |
| 111 } | 144 } |
| 112 | 145 |
| 113 private: | 146 private: |
| 114 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; | 147 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; |
| 115 | 148 |
| 116 // Called when an exception is thrown but not caught. | 149 // Called when an exception is thrown but not caught. |
| 117 void HandleException(const v8::TryCatch& try_catch); | 150 void HandleException(const v8::TryCatch& try_catch); |
| 118 | 151 |
| 119 // Ensure that require_ has been evaluated from require.js. | 152 // Ensure that require_ has been evaluated from require.js. |
| 120 void EnsureRequireLoaded(); | 153 void EnsureRequireLoaded(); |
| 121 | 154 |
| 122 // Run |code| in the current context with the name |name| used for stack | 155 // Run |code| in the current context with the name |name| used for stack |
| 123 // traces. | 156 // traces. |
| 124 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, | 157 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, |
| 125 v8::Handle<v8::String> name); | 158 v8::Handle<v8::String> name); |
| 126 | 159 |
| 160 // Sets a lazy field using the specified |getter|. | |
| 161 void SetLazyField(v8::Handle<v8::Object> object, | |
| 162 const std::string& field, | |
| 163 const std::string& module_name, | |
| 164 const std::string& module_field, | |
| 165 v8::AccessorGetter getter); | |
| 166 | |
| 127 // Return the named source file stored in the source map. | 167 // Return the named source file stored in the source map. |
| 128 // |args[0]| - the name of a source file in source_map_. | 168 // |args[0]| - the name of a source file in source_map_. |
| 129 v8::Handle<v8::Value> GetSource(v8::Handle<v8::String> source_name); | 169 v8::Handle<v8::Value> GetSource(v8::Handle<v8::String> source_name); |
| 130 | 170 |
| 131 // Return an object that contains the native methods defined by the named | 171 // Return an object that contains the native methods defined by the named |
| 132 // NativeHandler. | 172 // NativeHandler. |
| 133 // |args[0]| - the name of a native handler object. | 173 // |args[0]| - the name of a native handler object. |
| 174 v8::Handle<v8::Value> GetNativeFromString(const std::string& native_name); | |
| 134 v8::Handle<v8::Value> GetNative(const v8::Arguments& args); | 175 v8::Handle<v8::Value> GetNative(const v8::Arguments& args); |
| 135 | 176 |
| 136 // Wraps |source| in a (function(require, requireNative, exports) {...}). | 177 // Wraps |source| in a (function(require, requireNative, exports) {...}). |
| 137 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); | 178 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); |
| 138 | 179 |
| 139 // Throws an exception in the calling JS context. | 180 // Throws an exception in the calling JS context. |
| 140 v8::Handle<v8::Value> ThrowException(const std::string& message); | 181 v8::Handle<v8::Value> ThrowException(const std::string& message); |
| 141 | 182 |
| 142 // The context that this ModuleSystem is for. | 183 // The context that this ModuleSystem is for. |
| 143 v8::Persistent<v8::Context> context_; | 184 v8::Persistent<v8::Context> context_; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 157 scoped_ptr<ExceptionHandler> exception_handler_; | 198 scoped_ptr<ExceptionHandler> exception_handler_; |
| 158 | 199 |
| 159 std::set<std::string> overridden_native_handlers_; | 200 std::set<std::string> overridden_native_handlers_; |
| 160 | 201 |
| 161 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); | 202 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); |
| 162 }; | 203 }; |
| 163 | 204 |
| 164 } // extensions | 205 } // extensions |
| 165 | 206 |
| 166 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ | 207 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ |
| OLD | NEW |