| 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> |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 // | 37 // |
| 38 // 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 |
| 39 // v8::Context. | 39 // v8::Context. |
| 40 // TODO(koz): Rename this to JavaScriptModuleSystem. | 40 // TODO(koz): Rename this to JavaScriptModuleSystem. |
| 41 class ModuleSystem : public ObjectBackedNativeHandler, | 41 class ModuleSystem : public ObjectBackedNativeHandler, |
| 42 public gin::ModuleRegistryObserver { | 42 public gin::ModuleRegistryObserver { |
| 43 public: | 43 public: |
| 44 class SourceMap { | 44 class SourceMap { |
| 45 public: | 45 public: |
| 46 virtual ~SourceMap() {} | 46 virtual ~SourceMap() {} |
| 47 virtual v8::Handle<v8::Value> GetSource(v8::Isolate* isolate, | 47 virtual v8::Local<v8::Value> GetSource(v8::Isolate* isolate, |
| 48 const std::string& name) = 0; | 48 const std::string& name) = 0; |
| 49 virtual bool Contains(const std::string& name) = 0; | 49 virtual bool Contains(const std::string& name) = 0; |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 class ExceptionHandler { | 52 class ExceptionHandler { |
| 53 public: | 53 public: |
| 54 virtual ~ExceptionHandler() {} | 54 virtual ~ExceptionHandler() {} |
| 55 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) = 0; | 55 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) = 0; |
| 56 | 56 |
| 57 protected: | 57 protected: |
| 58 // Formats |try_catch| as a nice string. | 58 // Formats |try_catch| as a nice string. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 69 ModuleSystem* module_system_; | 69 ModuleSystem* module_system_; |
| 70 DISALLOW_COPY_AND_ASSIGN(NativesEnabledScope); | 70 DISALLOW_COPY_AND_ASSIGN(NativesEnabledScope); |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 // |source_map| is a weak pointer. | 73 // |source_map| is a weak pointer. |
| 74 ModuleSystem(ScriptContext* context, SourceMap* source_map); | 74 ModuleSystem(ScriptContext* context, SourceMap* source_map); |
| 75 ~ModuleSystem() override; | 75 ~ModuleSystem() override; |
| 76 | 76 |
| 77 // Require the specified module. This is the equivalent of calling | 77 // Require the specified module. This is the equivalent of calling |
| 78 // require('module_name') from the loaded JS files. | 78 // require('module_name') from the loaded JS files. |
| 79 v8::Handle<v8::Value> Require(const std::string& module_name); | 79 v8::Local<v8::Value> Require(const std::string& module_name); |
| 80 void Require(const v8::FunctionCallbackInfo<v8::Value>& args); | 80 void Require(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 81 | 81 |
| 82 // Run |code| in the current context with the name |name| used for stack | 82 // Run |code| in the current context with the name |name| used for stack |
| 83 // traces. | 83 // traces. |
| 84 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, | 84 v8::Local<v8::Value> RunString(v8::Local<v8::String> code, |
| 85 v8::Handle<v8::String> name); | 85 v8::Local<v8::String> name); |
| 86 | 86 |
| 87 // Calls the specified method exported by the specified module. This is | 87 // Calls the specified method exported by the specified module. This is |
| 88 // equivalent to calling require('module_name').method_name() from JS. | 88 // equivalent to calling require('module_name').method_name() from JS. |
| 89 v8::Local<v8::Value> CallModuleMethod(const std::string& module_name, | 89 v8::Local<v8::Value> CallModuleMethod(const std::string& module_name, |
| 90 const std::string& method_name); | 90 const std::string& method_name); |
| 91 v8::Local<v8::Value> CallModuleMethod( | 91 v8::Local<v8::Value> CallModuleMethod( |
| 92 const std::string& module_name, | 92 const std::string& module_name, |
| 93 const std::string& method_name, | 93 const std::string& method_name, |
| 94 std::vector<v8::Handle<v8::Value> >* args); | 94 std::vector<v8::Local<v8::Value>>* args); |
| 95 v8::Local<v8::Value> CallModuleMethod(const std::string& module_name, | 95 v8::Local<v8::Value> CallModuleMethod(const std::string& module_name, |
| 96 const std::string& method_name, | 96 const std::string& method_name, |
| 97 int argc, | 97 int argc, |
| 98 v8::Handle<v8::Value> argv[]); | 98 v8::Local<v8::Value> argv[]); |
| 99 | 99 |
| 100 // Register |native_handler| as a potential target for requireNative(), so | 100 // Register |native_handler| as a potential target for requireNative(), so |
| 101 // calls to requireNative(|name|) from JS will return a new object created by | 101 // calls to requireNative(|name|) from JS will return a new object created by |
| 102 // |native_handler|. | 102 // |native_handler|. |
| 103 void RegisterNativeHandler(const std::string& name, | 103 void RegisterNativeHandler(const std::string& name, |
| 104 scoped_ptr<NativeHandler> native_handler); | 104 scoped_ptr<NativeHandler> native_handler); |
| 105 | 105 |
| 106 // Causes requireNative(|name|) to look for its module in |source_map_| | 106 // Causes requireNative(|name|) to look for its module in |source_map_| |
| 107 // instead of using a registered native handler. This can be used in unit | 107 // instead of using a registered native handler. This can be used in unit |
| 108 // tests to mock out native modules. | 108 // tests to mock out native modules. |
| 109 void OverrideNativeHandlerForTest(const std::string& name); | 109 void OverrideNativeHandlerForTest(const std::string& name); |
| 110 | 110 |
| 111 // Executes |code| in the current context with |name| as the filename. | 111 // Executes |code| in the current context with |name| as the filename. |
| 112 void RunString(const std::string& code, const std::string& name); | 112 void RunString(const std::string& code, const std::string& name); |
| 113 | 113 |
| 114 // Make |object|.|field| lazily evaluate to the result of | 114 // Make |object|.|field| lazily evaluate to the result of |
| 115 // require(|module_name|)[|module_field|]. | 115 // require(|module_name|)[|module_field|]. |
| 116 // | 116 // |
| 117 // TODO(kalman): All targets for this method are ObjectBackedNativeHandlers, | 117 // TODO(kalman): All targets for this method are ObjectBackedNativeHandlers, |
| 118 // move this logic into those classes (in fact, the chrome | 118 // move this logic into those classes (in fact, the chrome |
| 119 // object is the only client, only that needs to implement it). | 119 // object is the only client, only that needs to implement it). |
| 120 void SetLazyField(v8::Handle<v8::Object> object, | 120 void SetLazyField(v8::Local<v8::Object> object, |
| 121 const std::string& field, | 121 const std::string& field, |
| 122 const std::string& module_name, | 122 const std::string& module_name, |
| 123 const std::string& module_field); | 123 const std::string& module_field); |
| 124 | 124 |
| 125 void SetLazyField(v8::Handle<v8::Object> object, | 125 void SetLazyField(v8::Local<v8::Object> object, |
| 126 const std::string& field, | 126 const std::string& field, |
| 127 const std::string& module_name, | 127 const std::string& module_name, |
| 128 const std::string& module_field, | 128 const std::string& module_field, |
| 129 v8::AccessorGetterCallback getter); | 129 v8::AccessorGetterCallback getter); |
| 130 | 130 |
| 131 // Make |object|.|field| lazily evaluate to the result of | 131 // Make |object|.|field| lazily evaluate to the result of |
| 132 // requireNative(|module_name|)[|module_field|]. | 132 // requireNative(|module_name|)[|module_field|]. |
| 133 // TODO(kalman): Same as above. | 133 // TODO(kalman): Same as above. |
| 134 void SetNativeLazyField(v8::Handle<v8::Object> object, | 134 void SetNativeLazyField(v8::Local<v8::Object> object, |
| 135 const std::string& field, | 135 const std::string& field, |
| 136 const std::string& module_name, | 136 const std::string& module_name, |
| 137 const std::string& module_field); | 137 const std::string& module_field); |
| 138 | 138 |
| 139 // Passes exceptions to |handler| rather than console::Fatal. | 139 // Passes exceptions to |handler| rather than console::Fatal. |
| 140 void SetExceptionHandlerForTest(scoped_ptr<ExceptionHandler> handler) { | 140 void SetExceptionHandlerForTest(scoped_ptr<ExceptionHandler> handler) { |
| 141 exception_handler_ = handler.Pass(); | 141 exception_handler_ = handler.Pass(); |
| 142 } | 142 } |
| 143 | 143 |
| 144 protected: | 144 protected: |
| (...skipping 10 matching lines...) Expand all Loading... |
| 155 // Retrieves the lazily defined field specified by |property| on a native | 155 // Retrieves the lazily defined field specified by |property| on a native |
| 156 // object. | 156 // object. |
| 157 static void NativeLazyFieldGetter( | 157 static void NativeLazyFieldGetter( |
| 158 v8::Local<v8::String> property, | 158 v8::Local<v8::String> property, |
| 159 const v8::PropertyCallbackInfo<v8::Value>& info); | 159 const v8::PropertyCallbackInfo<v8::Value>& info); |
| 160 | 160 |
| 161 // Called when an exception is thrown but not caught. | 161 // Called when an exception is thrown but not caught. |
| 162 void HandleException(const v8::TryCatch& try_catch); | 162 void HandleException(const v8::TryCatch& try_catch); |
| 163 | 163 |
| 164 void RequireForJs(const v8::FunctionCallbackInfo<v8::Value>& args); | 164 void RequireForJs(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 165 v8::Local<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); | 165 v8::Local<v8::Value> RequireForJsInner(v8::Local<v8::String> module_name); |
| 166 | 166 |
| 167 typedef v8::Handle<v8::Value>(ModuleSystem::*RequireFunction)( | 167 typedef v8::Local<v8::Value>(ModuleSystem::*RequireFunction)( |
| 168 const std::string&); | 168 const std::string&); |
| 169 // Base implementation of a LazyFieldGetter which uses |require_fn| to require | 169 // Base implementation of a LazyFieldGetter which uses |require_fn| to require |
| 170 // modules. | 170 // modules. |
| 171 static void LazyFieldGetterInner( | 171 static void LazyFieldGetterInner( |
| 172 v8::Local<v8::String> property, | 172 v8::Local<v8::String> property, |
| 173 const v8::PropertyCallbackInfo<v8::Value>& info, | 173 const v8::PropertyCallbackInfo<v8::Value>& info, |
| 174 RequireFunction require_function); | 174 RequireFunction require_function); |
| 175 | 175 |
| 176 // Return the named source file stored in the source map. | 176 // Return the named source file stored in the source map. |
| 177 // |args[0]| - the name of a source file in source_map_. | 177 // |args[0]| - the name of a source file in source_map_. |
| 178 v8::Handle<v8::Value> GetSource(const std::string& module_name); | 178 v8::Local<v8::Value> GetSource(const std::string& module_name); |
| 179 | 179 |
| 180 // Return an object that contains the native methods defined by the named | 180 // Return an object that contains the native methods defined by the named |
| 181 // NativeHandler. | 181 // NativeHandler. |
| 182 // |args[0]| - the name of a native handler object. | 182 // |args[0]| - the name of a native handler object. |
| 183 v8::Handle<v8::Value> RequireNativeFromString(const std::string& native_name); | 183 v8::Local<v8::Value> RequireNativeFromString(const std::string& native_name); |
| 184 void RequireNative(const v8::FunctionCallbackInfo<v8::Value>& args); | 184 void RequireNative(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 185 | 185 |
| 186 // Return a promise for a requested module. | 186 // Return a promise for a requested module. |
| 187 // |args[0]| - the name of a module. | 187 // |args[0]| - the name of a module. |
| 188 void RequireAsync(const v8::FunctionCallbackInfo<v8::Value>& args); | 188 void RequireAsync(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 189 | 189 |
| 190 // Wraps |source| in a (function(define, require, requireNative, ...) {...}). | 190 // Wraps |source| in a (function(define, require, requireNative, ...) {...}). |
| 191 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); | 191 v8::Local<v8::String> WrapSource(v8::Local<v8::String> source); |
| 192 | 192 |
| 193 // NativeHandler implementation which returns the private area of an Object. | 193 // NativeHandler implementation which returns the private area of an Object. |
| 194 void Private(const v8::FunctionCallbackInfo<v8::Value>& args); | 194 void Private(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 195 | 195 |
| 196 // Loads and runs a Javascript module. | 196 // Loads and runs a Javascript module. |
| 197 v8::Handle<v8::Value> LoadModule(const std::string& module_name); | 197 v8::Local<v8::Value> LoadModule(const std::string& module_name); |
| 198 | 198 |
| 199 // Invoked when a module is loaded in response to a requireAsync call. | 199 // Invoked when a module is loaded in response to a requireAsync call. |
| 200 // Resolves |resolver| with |value|. | 200 // Resolves |resolver| with |value|. |
| 201 void OnModuleLoaded(scoped_ptr<v8::Global<v8::Promise::Resolver>> resolver, | 201 void OnModuleLoaded(scoped_ptr<v8::Global<v8::Promise::Resolver>> resolver, |
| 202 v8::Handle<v8::Value> value); | 202 v8::Local<v8::Value> value); |
| 203 | 203 |
| 204 // gin::ModuleRegistryObserver overrides. | 204 // gin::ModuleRegistryObserver overrides. |
| 205 void OnDidAddPendingModule( | 205 void OnDidAddPendingModule( |
| 206 const std::string& id, | 206 const std::string& id, |
| 207 const std::vector<std::string>& dependencies) override; | 207 const std::vector<std::string>& dependencies) override; |
| 208 | 208 |
| 209 // Marks any existing NativeHandler named |name| as clobbered. | 209 // Marks any existing NativeHandler named |name| as clobbered. |
| 210 // See |clobbered_native_handlers_|. | 210 // See |clobbered_native_handlers_|. |
| 211 void ClobberExistingNativeHandler(const std::string& name); | 211 void ClobberExistingNativeHandler(const std::string& name); |
| 212 | 212 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 238 std::vector<linked_ptr<NativeHandler>> clobbered_native_handlers_; | 238 std::vector<linked_ptr<NativeHandler>> clobbered_native_handlers_; |
| 239 | 239 |
| 240 base::WeakPtrFactory<ModuleSystem> weak_factory_; | 240 base::WeakPtrFactory<ModuleSystem> weak_factory_; |
| 241 | 241 |
| 242 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); | 242 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); |
| 243 }; | 243 }; |
| 244 | 244 |
| 245 } // namespace extensions | 245 } // namespace extensions |
| 246 | 246 |
| 247 #endif // EXTENSIONS_RENDERER_MODULE_SYSTEM_H_ | 247 #endif // EXTENSIONS_RENDERER_MODULE_SYSTEM_H_ |
| OLD | NEW |