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 |
167 typedef v8::Handle<v8::Value>(ModuleSystem::*RequireFunction)( | 166 typedef v8::Handle<v8::Value>(ModuleSystem::*RequireFunction)( |
168 const std::string&); | 167 const std::string&); |
169 // Base implementation of a LazyFieldGetter which uses |require_fn| to require | 168 // Base implementation of a LazyFieldGetter which uses |require_fn| to require |
170 // modules. | 169 // modules. |
171 static void LazyFieldGetterInner( | 170 static void LazyFieldGetterInner( |
172 v8::Local<v8::String> property, | 171 v8::Local<v8::String> property, |
173 const v8::PropertyCallbackInfo<v8::Value>& info, | 172 const v8::PropertyCallbackInfo<v8::Value>& info, |
174 RequireFunction require_function); | 173 RequireFunction require_function); |
175 | 174 |
176 // Return the named source file stored in the source map. | 175 // Return the named source file stored in the source map. |
177 // |args[0]| - the name of a source file in source_map_. | 176 // |args[0]| - the name of a source file in source_map_. |
178 v8::Handle<v8::Value> GetSource(const std::string& module_name); | 177 v8::Handle<v8::Value> GetSource(const std::string& module_name); |
179 | 178 |
180 // Return an object that contains the native methods defined by the named | 179 // Return an object that contains the native methods defined by the named |
181 // NativeHandler. | 180 // NativeHandler. |
182 // |args[0]| - the name of a native handler object. | 181 // |args[0]| - the name of a native handler object. |
183 v8::Handle<v8::Value> RequireNativeFromString(const std::string& native_name); | 182 v8::Handle<v8::Value> RequireNativeFromString(const std::string& native_name); |
184 void RequireNative(const v8::FunctionCallbackInfo<v8::Value>& args); | 183 void RequireNative(const v8::FunctionCallbackInfo<v8::Value>& args); |
185 | 184 |
185 // Return a promise for a requested module. | |
186 // |args[0]| - the name of a module. | |
187 void RequireAsync(const v8::FunctionCallbackInfo<v8::Value>& args); | |
188 | |
186 // Wraps |source| in a (function(require, requireNative, exports) {...}). | 189 // Wraps |source| in a (function(require, requireNative, exports) {...}). |
187 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); | 190 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); |
188 | 191 |
192 // Wraps |source| in a (function(define) {...}). | |
193 v8::Handle<v8::String> WrapAmdSource(v8::Handle<v8::String> source); | |
not at google - send to devlin
2014/07/01 18:13:05
nit: Amd looks like a typo for And. could you use
Sam McNally
2014/07/02 03:26:35
Merged it into WrapSource.
| |
194 | |
189 // NativeHandler implementation which returns the private area of an Object. | 195 // NativeHandler implementation which returns the private area of an Object. |
190 void Private(const v8::FunctionCallbackInfo<v8::Value>& args); | 196 void Private(const v8::FunctionCallbackInfo<v8::Value>& args); |
191 | 197 |
192 // NativeHandler implementation which returns a function wrapper for a | 198 // Loads and runs an AMD module. |
193 // provided function. | 199 void LoadAmdModule(const std::string& module_name); |
194 void CreateFunctionWrapper(const v8::FunctionCallbackInfo<v8::Value>& args); | 200 |
201 // Invoked when a module is loaded in response to a requireAsync call. | |
202 // Resolves |resolver| with |value|. | |
203 void OnModuleLoaded( | |
204 scoped_ptr<v8::UniquePersistent<v8::Promise::Resolver> > resolver, | |
205 v8::Handle<v8::Value> value); | |
206 | |
207 // gin::ModuleRegistryObserver overrides. | |
208 virtual void OnDidAddPendingModule( | |
209 const std::string& id, | |
210 const std::vector<std::string>& dependencies) OVERRIDE; | |
195 | 211 |
196 ScriptContext* context_; | 212 ScriptContext* context_; |
197 | 213 |
198 // A map from module names to the JS source for that module. GetSource() | 214 // A map from module names to the JS source for that module. GetSource() |
199 // performs a lookup on this map. | 215 // performs a lookup on this map. |
200 SourceMap* source_map_; | 216 SourceMap* source_map_; |
201 | 217 |
202 // A map from native handler names to native handlers. | 218 // A map from native handler names to native handlers. |
203 NativeHandlerMap native_handler_map_; | 219 NativeHandlerMap native_handler_map_; |
204 | 220 |
205 // When 0, natives are disabled, otherwise indicates how many callers have | 221 // When 0, natives are disabled, otherwise indicates how many callers have |
206 // pinned natives as enabled. | 222 // pinned natives as enabled. |
207 int natives_enabled_; | 223 int natives_enabled_; |
208 | 224 |
209 // Called when an exception is thrown but not caught in JS. Overridable by | 225 // Called when an exception is thrown but not caught in JS. Overridable by |
210 // tests. | 226 // tests. |
211 scoped_ptr<ExceptionHandler> exception_handler_; | 227 scoped_ptr<ExceptionHandler> exception_handler_; |
212 | 228 |
213 std::set<std::string> overridden_native_handlers_; | 229 std::set<std::string> overridden_native_handlers_; |
214 | 230 |
231 base::WeakPtrFactory<ModuleSystem> weak_factory_; | |
232 | |
215 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); | 233 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); |
216 }; | 234 }; |
217 | 235 |
218 } // namespace extensions | 236 } // namespace extensions |
219 | 237 |
220 #endif // EXTENSIONS_RENDERER_MODULE_SYSTEM_H_ | 238 #endif // EXTENSIONS_RENDERER_MODULE_SYSTEM_H_ |
OLD | NEW |