| 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/object_backed_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 #include <vector> |
| 18 | 18 |
| 19 namespace extensions { | 19 namespace extensions { |
| 20 | 20 |
| 21 // A module system for JS similar to node.js' require() function. | 21 // A module system for JS similar to node.js' require() function. |
| 22 // Each module has three variables in the global scope: | 22 // Each module has three variables in the global scope: |
| 23 // - exports, an object returned to dependencies who require() this | 23 // - exports, an object returned to dependencies who require() this |
| 24 // module. | 24 // module. |
| 25 // - 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 |
| 26 // that module's exports object. | 26 // that module's exports object. |
| 27 // - requireNative, a function that takes the name of a registered | 27 // - requireNative, a function that takes the name of a registered |
| 28 // NativeHandler and returns an object that contains the functions the | 28 // NativeHandler and returns an object that contains the functions the |
| 29 // NativeHandler defines. | 29 // NativeHandler defines. |
| 30 // | 30 // |
| 31 // Each module in a ModuleSystem is executed at most once and its exports | 31 // Each module in a ModuleSystem is executed at most once and its exports |
| 32 // object cached. | 32 // object cached. |
| 33 // | 33 // |
| 34 // Note that a ModuleSystem must be used only in conjunction with a single | 34 // Note that a ModuleSystem must be used only in conjunction with a single |
| 35 // v8::Context. | 35 // v8::Context. |
| 36 // TODO(koz): Rename this to JavaScriptModuleSystem. | 36 // TODO(koz): Rename this to JavaScriptModuleSystem. |
| 37 class ModuleSystem : public ObjectBackedNativeHandler { | 37 class ModuleSystem : public NativeHandler { |
| 38 public: | 38 public: |
| 39 class SourceMap { | 39 class SourceMap { |
| 40 public: | 40 public: |
| 41 virtual ~SourceMap() {} | 41 virtual ~SourceMap() {} |
| 42 virtual v8::Handle<v8::Value> GetSource(const std::string& name) = 0; | 42 virtual v8::Handle<v8::Value> GetSource(const std::string& name) = 0; |
| 43 virtual bool Contains(const std::string& name) = 0; | 43 virtual bool Contains(const std::string& name) = 0; |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 class ExceptionHandler { | 46 class ExceptionHandler { |
| 47 public: | 47 public: |
| 48 virtual ~ExceptionHandler() {} | 48 virtual ~ExceptionHandler() {} |
| 49 virtual void HandleUncaughtException() = 0; | 49 virtual void HandleUncaughtException() = 0; |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 // Enables native bindings for the duration of its lifetime. | 52 // Enables native bindings for the duration of its lifetime. |
| 53 class NativesEnabledScope { | 53 class NativesEnabledScope { |
| 54 public: | 54 public: |
| 55 explicit NativesEnabledScope(ModuleSystem* module_system); | 55 explicit NativesEnabledScope(ModuleSystem* module_system); |
| 56 ~NativesEnabledScope(); | 56 ~NativesEnabledScope(); |
| 57 | 57 |
| 58 private: | 58 private: |
| 59 ModuleSystem* module_system_; | 59 ModuleSystem* module_system_; |
| 60 DISALLOW_COPY_AND_ASSIGN(NativesEnabledScope); | 60 DISALLOW_COPY_AND_ASSIGN(NativesEnabledScope); |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 // |source_map| is a weak pointer. | 63 // |source_map| is a weak pointer. |
| 64 ModuleSystem(v8::Handle<v8::Context> context, SourceMap* source_map); | 64 explicit ModuleSystem(v8::Handle<v8::Context> context, SourceMap* source_map); |
| 65 virtual ~ModuleSystem(); | 65 virtual ~ModuleSystem(); |
| 66 | 66 |
| 67 // Returns true if the current context has a ModuleSystem installed in it. | 67 // Returns true if the current context has a ModuleSystem installed in it. |
| 68 static bool IsPresentInCurrentContext(); | 68 static bool IsPresentInCurrentContext(); |
| 69 | 69 |
| 70 // Dumps the debug info from |try_catch| to LOG(ERROR). | 70 // Dumps the debug info from |try_catch| to LOG(ERROR). |
| 71 static void DumpException(const v8::TryCatch& try_catch); | 71 static void DumpException(const v8::TryCatch& try_catch); |
| 72 | 72 |
| 73 // Require the specified module. This is the equivalent of calling | 73 // Require the specified module. This is the equivalent of calling |
| 74 // require('module_name') from the loaded JS files. | 74 // require('module_name') from the loaded JS files. |
| 75 v8::Handle<v8::Value> Require(const std::string& module_name); | 75 void Require(const std::string& module_name); |
| 76 v8::Handle<v8::Value> Require(const v8::Arguments& args); | 76 v8::Handle<v8::Value> Require(const v8::Arguments& args); |
| 77 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); |
| 78 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); |
| 77 | 79 |
| 78 // Calls the specified method exported by the specified module. This is | 80 // Calls the specified method exported by the specified module. This is |
| 79 // equivalent to calling require('module_name').method_name() from JS. | 81 // equivalent to calling require('module_name').method_name() from JS. |
| 80 v8::Local<v8::Value> CallModuleMethod(const std::string& module_name, | 82 void CallModuleMethod(const std::string& module_name, |
| 81 const std::string& method_name); | 83 const std::string& method_name); |
| 82 | 84 |
| 83 // Calls the specified method exported by the specified module. This is | 85 // Calls the specified method exported by the specified module. This is |
| 84 // equivalent to calling require('module_name').method_name(args) from JS. | 86 // equivalent to calling require('module_name').method_name(args) from JS. |
| 85 v8::Local<v8::Value> CallModuleMethod( | 87 v8::Local<v8::Value> CallModuleMethod( |
| 86 const std::string& module_name, | 88 const std::string& module_name, |
| 87 const std::string& method_name, | 89 const std::string& method_name, |
| 88 std::vector<v8::Handle<v8::Value> >* args); | 90 std::vector<v8::Handle<v8::Value> >* args); |
| 89 | 91 |
| 90 // Register |native_handler| as a potential target for requireNative(), so | 92 // Register |native_handler| as a potential target for requireNative(), so |
| 91 // 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 |
| 92 // |native_handler|. | 94 // |native_handler|. |
| 93 void RegisterNativeHandler(const std::string& name, | 95 void RegisterNativeHandler(const std::string& name, |
| 94 scoped_ptr<NativeHandler> native_handler); | 96 scoped_ptr<NativeHandler> native_handler); |
| 95 // Check if a native handler has been registered for this |name|. | |
| 96 bool HasNativeHandler(const std::string& name); | |
| 97 | 97 |
| 98 // Causes requireNative(|name|) to look for its module in |source_map_| | 98 // Causes requireNative(|name|) to look for its module in |source_map_| |
| 99 // 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 |
| 100 // tests to mock out native modules. | 100 // tests to mock out native modules. |
| 101 void OverrideNativeHandler(const std::string& name); | 101 void OverrideNativeHandler(const std::string& name); |
| 102 | 102 |
| 103 // Executes |code| in the current context with |name| as the filename. | 103 // Executes |code| in the current context with |name| as the filename. |
| 104 void RunString(const std::string& code, const std::string& name); | 104 void RunString(const std::string& code, const std::string& name); |
| 105 | 105 |
| 106 // Retrieves the lazily defined field specified by |property|. | 106 // Retrieves the lazily defined field specified by |property|. |
| 107 static v8::Handle<v8::Value> LazyFieldGetter(v8::Local<v8::String> property, | 107 static v8::Handle<v8::Value> LazyFieldGetter(v8::Local<v8::String> property, |
| 108 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 | 109 |
| 115 // Make |object|.|field| lazily evaluate to the result of | 110 // Make |object|.|field| lazily evaluate to the result of |
| 116 // require(|module_name|)[|module_field|]. | 111 // require(|module_name|)[|module_field|]. |
| 117 void SetLazyField(v8::Handle<v8::Object> object, | 112 void SetLazyField(v8::Handle<v8::Object> object, |
| 118 const std::string& field, | 113 const std::string& field, |
| 119 const std::string& module_name, | 114 const std::string& module_name, |
| 120 const std::string& module_field); | 115 const std::string& module_field); |
| 121 | 116 |
| 122 // Make |object|.|field| lazily evaluate to the result of | |
| 123 // requireNative(|module_name|)[|module_field|]. | |
| 124 void SetNativeLazyField(v8::Handle<v8::Object> object, | |
| 125 const std::string& field, | |
| 126 const std::string& module_name, | |
| 127 const std::string& module_field); | |
| 128 | |
| 129 void set_exception_handler(scoped_ptr<ExceptionHandler> handler) { | 117 void set_exception_handler(scoped_ptr<ExceptionHandler> handler) { |
| 130 exception_handler_ = handler.Pass(); | 118 exception_handler_ = handler.Pass(); |
| 131 } | 119 } |
| 132 | 120 |
| 133 virtual bool Invalidate() OVERRIDE; | |
| 134 | |
| 135 private: | 121 private: |
| 136 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; | 122 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; |
| 137 | 123 |
| 138 // Called when an exception is thrown but not caught. | 124 // Called when an exception is thrown but not caught. |
| 139 void HandleException(const v8::TryCatch& try_catch); | 125 void HandleException(const v8::TryCatch& try_catch); |
| 140 | 126 |
| 141 static std::string CreateExceptionString(const v8::TryCatch& try_catch); | |
| 142 | |
| 143 // Ensure that require_ has been evaluated from require.js. | 127 // Ensure that require_ has been evaluated from require.js. |
| 144 void EnsureRequireLoaded(); | 128 void EnsureRequireLoaded(); |
| 145 | 129 |
| 146 // Run |code| in the current context with the name |name| used for stack | 130 // Run |code| in the current context with the name |name| used for stack |
| 147 // traces. | 131 // traces. |
| 148 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, | 132 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, |
| 149 v8::Handle<v8::String> name); | 133 v8::Handle<v8::String> name); |
| 150 | 134 |
| 151 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); | |
| 152 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); | |
| 153 | |
| 154 // Sets a lazy field using the specified |getter|. | |
| 155 void SetLazyField(v8::Handle<v8::Object> object, | |
| 156 const std::string& field, | |
| 157 const std::string& module_name, | |
| 158 const std::string& module_field, | |
| 159 v8::AccessorGetter getter); | |
| 160 | |
| 161 typedef v8::Handle<v8::Value> (ModuleSystem::*GetModuleFunc)( | |
| 162 const std::string&); | |
| 163 // Base implementation of a LazyFieldGetter that can be customized by passing | |
| 164 // in a |get_module| function. | |
| 165 static v8::Handle<v8::Value> LazyFieldGetterInner( | |
| 166 v8::Local<v8::String> property, | |
| 167 const v8::AccessorInfo& info, | |
| 168 GetModuleFunc get_module); | |
| 169 | |
| 170 // Return the named source file stored in the source map. | 135 // Return the named source file stored in the source map. |
| 171 // |args[0]| - the name of a source file in source_map_. | 136 // |args[0]| - the name of a source file in source_map_. |
| 172 v8::Handle<v8::Value> GetSource(v8::Handle<v8::String> source_name); | 137 v8::Handle<v8::Value> GetSource(v8::Handle<v8::String> source_name); |
| 173 | 138 |
| 174 // Return an object that contains the native methods defined by the named | 139 // Return an object that contains the native methods defined by the named |
| 175 // NativeHandler. | 140 // NativeHandler. |
| 176 // |args[0]| - the name of a native handler object. | 141 // |args[0]| - the name of a native handler object. |
| 177 v8::Handle<v8::Value> RequireNativeFromString(const std::string& native_name); | 142 v8::Handle<v8::Value> GetNative(const v8::Arguments& args); |
| 178 v8::Handle<v8::Value> RequireNative(const v8::Arguments& args); | |
| 179 | 143 |
| 180 // Wraps |source| in a (function(require, requireNative, exports) {...}). | 144 // Wraps |source| in a (function(require, requireNative, exports) {...}). |
| 181 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); | 145 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); |
| 182 | 146 |
| 183 // Throws an exception in the calling JS context. | 147 // Throws an exception in the calling JS context. |
| 184 v8::Handle<v8::Value> ThrowException(const std::string& message); | 148 v8::Handle<v8::Value> ThrowException(const std::string& message); |
| 185 | 149 |
| 150 // The context that this ModuleSystem is for. |
| 151 v8::Persistent<v8::Context> context_; |
| 152 |
| 186 // A map from module names to the JS source for that module. GetSource() | 153 // A map from module names to the JS source for that module. GetSource() |
| 187 // performs a lookup on this map. | 154 // performs a lookup on this map. |
| 188 SourceMap* source_map_; | 155 SourceMap* source_map_; |
| 189 | 156 |
| 190 // A map from native handler names to native handlers. | 157 // A map from native handler names to native handlers. |
| 191 NativeHandlerMap native_handler_map_; | 158 NativeHandlerMap native_handler_map_; |
| 192 | 159 |
| 193 // When 0, natives are disabled, otherwise indicates how many callers have | 160 // When 0, natives are disabled, otherwise indicates how many callers have |
| 194 // pinned natives as enabled. | 161 // pinned natives as enabled. |
| 195 int natives_enabled_; | 162 int natives_enabled_; |
| 196 | 163 |
| 197 // Set to false if |context_| has been deleted and this should not be used. | |
| 198 bool is_valid_; | |
| 199 | |
| 200 // Called when an exception is thrown but not caught in JS. | 164 // Called when an exception is thrown but not caught in JS. |
| 201 scoped_ptr<ExceptionHandler> exception_handler_; | 165 scoped_ptr<ExceptionHandler> exception_handler_; |
| 202 | 166 |
| 203 std::set<std::string> overridden_native_handlers_; | 167 std::set<std::string> overridden_native_handlers_; |
| 204 | 168 |
| 205 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); | 169 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); |
| 206 }; | 170 }; |
| 207 | 171 |
| 208 } // extensions | 172 } // extensions |
| 209 | 173 |
| 210 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ | 174 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ |
| OLD | NEW |