| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 EXTENSIONS_RENDERER_MODULE_SYSTEM_H_ | 5 #ifndef EXTENSIONS_RENDERER_MODULE_SYSTEM_H_ |
| 6 #define EXTENSIONS_RENDERER_MODULE_SYSTEM_H_ | 6 #define EXTENSIONS_RENDERER_MODULE_SYSTEM_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "extensions/renderer/native_handler.h" | 16 #include "extensions/renderer/native_handler.h" |
| 17 #include "extensions/renderer/object_backed_native_handler.h" | 17 #include "extensions/renderer/object_backed_native_handler.h" |
| 18 #include "gin/modules/module_registry_observer.h" |
| 18 #include "v8/include/v8.h" | 19 #include "v8/include/v8.h" |
| 19 | 20 |
| 20 namespace extensions { | 21 namespace extensions { |
| 21 | 22 |
| 22 class ScriptContext; | 23 class ScriptContext; |
| 23 | 24 |
| 24 // A module system for JS similar to node.js' require() function. | 25 // A module system for JS similar to node.js' require() function. |
| 25 // Each module has three variables in the global scope: | 26 // Each module has three variables in the global scope: |
| 26 // - exports, an object returned to dependencies who require() this | 27 // - exports, an object returned to dependencies who require() this |
| 27 // module. | 28 // module. |
| 28 // - require, a function that takes a module name as an argument and returns | 29 // - require, a function that takes a module name as an argument and returns |
| 29 // that module's exports object. | 30 // that module's exports object. |
| 30 // - requireNative, a function that takes the name of a registered | 31 // - requireNative, a function that takes the name of a registered |
| 31 // NativeHandler and returns an object that contains the functions the | 32 // NativeHandler and returns an object that contains the functions the |
| 32 // NativeHandler defines. | 33 // NativeHandler defines. |
| 33 // | 34 // |
| 34 // Each module in a ModuleSystem is executed at most once and its exports | 35 // Each module in a ModuleSystem is executed at most once and its exports |
| 35 // object cached. | 36 // object cached. |
| 36 // | 37 // |
| 37 // Note that a ModuleSystem must be used only in conjunction with a single | 38 // Note that a ModuleSystem must be used only in conjunction with a single |
| 38 // v8::Context. | 39 // v8::Context. |
| 39 // TODO(koz): Rename this to JavaScriptModuleSystem. | 40 // TODO(koz): Rename this to JavaScriptModuleSystem. |
| 40 class ModuleSystem : public ObjectBackedNativeHandler { | 41 class ModuleSystem : public ObjectBackedNativeHandler, |
| 42 public gin::ModuleRegistryObserver { |
| 41 public: | 43 public: |
| 42 class SourceMap { | 44 class SourceMap { |
| 43 public: | 45 public: |
| 44 virtual ~SourceMap() {} | 46 virtual ~SourceMap() {} |
| 45 virtual v8::Handle<v8::Value> GetSource(v8::Isolate* isolate, | 47 virtual v8::Handle<v8::Value> GetSource(v8::Isolate* isolate, |
| 46 const std::string& name) = 0; | 48 const std::string& name) = 0; |
| 47 virtual bool Contains(const std::string& name) = 0; | 49 virtual bool Contains(const std::string& name) = 0; |
| 48 }; | 50 }; |
| 49 | 51 |
| 50 class ExceptionHandler { | 52 class ExceptionHandler { |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 const v8::PropertyCallbackInfo<v8::Value>& info); | 153 const v8::PropertyCallbackInfo<v8::Value>& info); |
| 152 // Retrieves the lazily defined field specified by |property| on a native | 154 // Retrieves the lazily defined field specified by |property| on a native |
| 153 // object. | 155 // object. |
| 154 static void NativeLazyFieldGetter( | 156 static void NativeLazyFieldGetter( |
| 155 v8::Local<v8::String> property, | 157 v8::Local<v8::String> property, |
| 156 const v8::PropertyCallbackInfo<v8::Value>& info); | 158 const v8::PropertyCallbackInfo<v8::Value>& info); |
| 157 | 159 |
| 158 // Called when an exception is thrown but not caught. | 160 // Called when an exception is thrown but not caught. |
| 159 void HandleException(const v8::TryCatch& try_catch); | 161 void HandleException(const v8::TryCatch& try_catch); |
| 160 | 162 |
| 161 // Ensure that require_ has been evaluated from require.js. | |
| 162 void EnsureRequireLoaded(); | |
| 163 | |
| 164 void RequireForJs(const v8::FunctionCallbackInfo<v8::Value>& args); | 163 void RequireForJs(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 165 v8::Local<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); | 164 v8::Local<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); |
| 166 | 165 |
| 166 bool LoadModule(const std::string& module_name); |
| 167 bool LoadNative(const std::string& native_name); |
| 168 |
| 169 // gin::ModuleRegistryObserver overrides. |
| 170 virtual void OnDidAddPendingModule( |
| 171 const std::string& id, |
| 172 const std::vector<std::string>& dependencies) OVERRIDE; |
| 173 |
| 167 typedef v8::Handle<v8::Value>(ModuleSystem::*RequireFunction)( | 174 typedef v8::Handle<v8::Value>(ModuleSystem::*RequireFunction)( |
| 168 const std::string&); | 175 const std::string&); |
| 169 // Base implementation of a LazyFieldGetter which uses |require_fn| to require | 176 // Base implementation of a LazyFieldGetter which uses |require_fn| to require |
| 170 // modules. | 177 // modules. |
| 171 static void LazyFieldGetterInner( | 178 static void LazyFieldGetterInner( |
| 172 v8::Local<v8::String> property, | 179 v8::Local<v8::String> property, |
| 173 const v8::PropertyCallbackInfo<v8::Value>& info, | 180 const v8::PropertyCallbackInfo<v8::Value>& info, |
| 174 RequireFunction require_function); | 181 RequireFunction require_function); |
| 175 | 182 |
| 176 // Return the named source file stored in the source map. | 183 // Return the named source file stored in the source map. |
| 177 // |args[0]| - the name of a source file in source_map_. | 184 // |args[0]| - the name of a source file in source_map_. |
| 178 v8::Handle<v8::Value> GetSource(const std::string& module_name); | 185 v8::Handle<v8::Value> GetSource(const std::string& module_name); |
| 179 | 186 |
| 180 // Return an object that contains the native methods defined by the named | 187 // Return an object that contains the native methods defined by the named |
| 181 // NativeHandler. | 188 // NativeHandler. |
| 182 // |args[0]| - the name of a native handler object. | 189 // |args[0]| - the name of a native handler object. |
| 183 v8::Handle<v8::Value> RequireNativeFromString(const std::string& native_name); | 190 v8::Handle<v8::Value> RequireNativeFromString(const std::string& native_name); |
| 184 void RequireNative(const v8::FunctionCallbackInfo<v8::Value>& args); | 191 void RequireNative(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 185 | 192 |
| 186 // Wraps |source| in a (function(require, requireNative, exports) {...}). | 193 // Wraps |source| in a (function(define) {...}). |
| 187 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); | 194 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source, |
| 195 const std::string& module_name); |
| 196 v8::Handle<v8::String> WrapAmdSource(v8::Handle<v8::String> source); |
| 188 | 197 |
| 189 // NativeHandler implementation which returns the private area of an Object. | 198 // NativeHandler implementation which returns the private area of an Object. |
| 190 void Private(const v8::FunctionCallbackInfo<v8::Value>& args); | 199 void Private(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 191 | 200 |
| 192 // NativeHandler implementation which returns a function wrapper for a | |
| 193 // provided function. | |
| 194 void CreateFunctionWrapper(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 195 | |
| 196 ScriptContext* context_; | 201 ScriptContext* context_; |
| 197 | 202 |
| 198 // A map from module names to the JS source for that module. GetSource() | 203 // A map from module names to the JS source for that module. GetSource() |
| 199 // performs a lookup on this map. | 204 // performs a lookup on this map. |
| 200 SourceMap* source_map_; | 205 SourceMap* source_map_; |
| 201 | 206 |
| 202 // A map from native handler names to native handlers. | 207 // A map from native handler names to native handlers. |
| 203 NativeHandlerMap native_handler_map_; | 208 NativeHandlerMap native_handler_map_; |
| 204 | 209 |
| 205 // When 0, natives are disabled, otherwise indicates how many callers have | 210 // When 0, natives are disabled, otherwise indicates how many callers have |
| 206 // pinned natives as enabled. | 211 // pinned natives as enabled. |
| 207 int natives_enabled_; | 212 int natives_enabled_; |
| 208 | 213 |
| 209 // Called when an exception is thrown but not caught in JS. Overridable by | 214 // Called when an exception is thrown but not caught in JS. Overridable by |
| 210 // tests. | 215 // tests. |
| 211 scoped_ptr<ExceptionHandler> exception_handler_; | 216 scoped_ptr<ExceptionHandler> exception_handler_; |
| 212 | 217 |
| 213 std::set<std::string> overridden_native_handlers_; | 218 std::set<std::string> overridden_native_handlers_; |
| 214 | 219 |
| 220 std::set<std::string> modules_supporting_amd_; |
| 221 |
| 222 std::set<std::string> failed_module_loads_; |
| 223 |
| 215 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); | 224 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); |
| 216 }; | 225 }; |
| 217 | 226 |
| 218 } // namespace extensions | 227 } // namespace extensions |
| 219 | 228 |
| 220 #endif // EXTENSIONS_RENDERER_MODULE_SYSTEM_H_ | 229 #endif // EXTENSIONS_RENDERER_MODULE_SYSTEM_H_ |
| OLD | NEW |