Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: extensions/renderer/module_system.h

Issue 359413004: Add support for using AMD modules from extensions modules. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cross-context tests Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
186 // Wraps |source| in a (function(require, requireNative, exports) {...}). 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
189 // Wraps |source| in a (function(define, require, requireNative, ...) {...}).
187 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); 190 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source);
188 191
189 // NativeHandler implementation which returns the private area of an Object. 192 // NativeHandler implementation which returns the private area of an Object.
190 void Private(const v8::FunctionCallbackInfo<v8::Value>& args); 193 void Private(const v8::FunctionCallbackInfo<v8::Value>& args);
191 194
192 // NativeHandler implementation which returns a function wrapper for a 195 // Loads and runs a Javascript module.
193 // provided function. 196 v8::Handle<v8::Value> LoadModule(const std::string& module_name);
194 void CreateFunctionWrapper(const v8::FunctionCallbackInfo<v8::Value>& args); 197
198 // Invoked when a module is loaded in response to a requireAsync call.
199 // Resolves |resolver| with |value|.
200 void OnModuleLoaded(
201 scoped_ptr<v8::UniquePersistent<v8::Promise::Resolver> > resolver,
202 v8::Handle<v8::Value> value);
203
204 // gin::ModuleRegistryObserver overrides.
205 virtual void OnDidAddPendingModule(
206 const std::string& id,
207 const std::vector<std::string>& dependencies) OVERRIDE;
195 208
196 ScriptContext* context_; 209 ScriptContext* context_;
197 210
198 // A map from module names to the JS source for that module. GetSource() 211 // A map from module names to the JS source for that module. GetSource()
199 // performs a lookup on this map. 212 // performs a lookup on this map.
200 SourceMap* source_map_; 213 SourceMap* source_map_;
201 214
202 // A map from native handler names to native handlers. 215 // A map from native handler names to native handlers.
203 NativeHandlerMap native_handler_map_; 216 NativeHandlerMap native_handler_map_;
204 217
205 // When 0, natives are disabled, otherwise indicates how many callers have 218 // When 0, natives are disabled, otherwise indicates how many callers have
206 // pinned natives as enabled. 219 // pinned natives as enabled.
207 int natives_enabled_; 220 int natives_enabled_;
208 221
209 // Called when an exception is thrown but not caught in JS. Overridable by 222 // Called when an exception is thrown but not caught in JS. Overridable by
210 // tests. 223 // tests.
211 scoped_ptr<ExceptionHandler> exception_handler_; 224 scoped_ptr<ExceptionHandler> exception_handler_;
212 225
213 std::set<std::string> overridden_native_handlers_; 226 std::set<std::string> overridden_native_handlers_;
214 227
228 base::WeakPtrFactory<ModuleSystem> weak_factory_;
229
215 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); 230 DISALLOW_COPY_AND_ASSIGN(ModuleSystem);
216 }; 231 };
217 232
218 } // namespace extensions 233 } // namespace extensions
219 234
220 #endif // EXTENSIONS_RENDERER_MODULE_SYSTEM_H_ 235 #endif // EXTENSIONS_RENDERER_MODULE_SYSTEM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698