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/native_handler.h" | 11 #include "chrome/renderer/extensions/object_backed_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 | 17 |
18 namespace extensions { | 18 namespace extensions { |
19 | 19 |
20 // A module system for JS similar to node.js' require() function. | 20 // A module system for JS similar to node.js' require() function. |
21 // Each module has three variables in the global scope: | 21 // Each module has three variables in the global scope: |
22 // - exports, an object returned to dependencies who require() this | 22 // - exports, an object returned to dependencies who require() this |
23 // module. | 23 // module. |
24 // - require, a function that takes a module name as an argument and returns | 24 // - require, a function that takes a module name as an argument and returns |
25 // that module's exports object. | 25 // that module's exports object. |
26 // - requireNative, a function that takes the name of a registered | 26 // - requireNative, a function that takes the name of a registered |
27 // NativeHandler and returns an object that contains the functions the | 27 // NativeHandler and returns an object that contains the functions the |
28 // NativeHandler defines. | 28 // NativeHandler defines. |
29 // | 29 // |
30 // Each module in a ModuleSystem is executed at most once and its exports | 30 // Each module in a ModuleSystem is executed at most once and its exports |
31 // object cached. | 31 // object cached. |
32 // | 32 // |
33 // Note that a ModuleSystem must be used only in conjunction with a single | 33 // Note that a ModuleSystem must be used only in conjunction with a single |
34 // v8::Context. | 34 // v8::Context. |
35 // TODO(koz): Rename this to JavaScriptModuleSystem. | 35 // TODO(koz): Rename this to JavaScriptModuleSystem. |
36 class ModuleSystem : public NativeHandler { | 36 class ModuleSystem : public ObjectBackedNativeHandler { |
37 public: | 37 public: |
38 class SourceMap { | 38 class SourceMap { |
39 public: | 39 public: |
40 virtual ~SourceMap() {} | 40 virtual ~SourceMap() {} |
41 virtual v8::Handle<v8::Value> GetSource(const std::string& name) = 0; | 41 virtual v8::Handle<v8::Value> GetSource(const std::string& name) = 0; |
42 virtual bool Contains(const std::string& name) = 0; | 42 virtual bool Contains(const std::string& name) = 0; |
43 }; | 43 }; |
44 | 44 |
45 class ExceptionHandler { | 45 class ExceptionHandler { |
46 public: | 46 public: |
(...skipping 17 matching lines...) Expand all Loading... | |
64 virtual ~ModuleSystem(); | 64 virtual ~ModuleSystem(); |
65 | 65 |
66 // Returns true if the current context has a ModuleSystem installed in it. | 66 // Returns true if the current context has a ModuleSystem installed in it. |
67 static bool IsPresentInCurrentContext(); | 67 static bool IsPresentInCurrentContext(); |
68 | 68 |
69 // Dumps the debug info from |try_catch| to LOG(ERROR). | 69 // Dumps the debug info from |try_catch| to LOG(ERROR). |
70 static void DumpException(const v8::TryCatch& try_catch); | 70 static void DumpException(const v8::TryCatch& try_catch); |
71 | 71 |
72 // Require the specified module. This is the equivalent of calling | 72 // Require the specified module. This is the equivalent of calling |
73 // require('module_name') from the loaded JS files. | 73 // require('module_name') from the loaded JS files. |
74 void Require(const std::string& module_name); | 74 v8::Handle<v8::Value> Require(const std::string& module_name); |
75 v8::Handle<v8::Value> Require(const v8::Arguments& args); | 75 v8::Handle<v8::Value> Require(const v8::Arguments& args); |
76 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); | 76 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); |
77 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); | 77 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); |
78 | 78 |
79 // Calls the specified method exported by the specified module. This is | 79 // Calls the specified method exported by the specified module. This is |
80 // equivalent to calling require('module_name').method_name() from JS. | 80 // equivalent to calling require('module_name').method_name() from JS. |
81 void CallModuleMethod(const std::string& module_name, | 81 void CallModuleMethod(const std::string& module_name, |
82 const std::string& method_name); | 82 const std::string& method_name); |
83 | 83 |
84 // Call |value| with arguments |argv| and return the result. | |
85 v8::Handle<v8::Value> CallModuleMethod(v8::Local<v8::Value> value, | |
not at google - send to devlin
2013/01/24 21:10:12
would prefer this to be a v8::Function but if it a
cduvall
2013/01/24 22:15:15
Added TODO.
| |
86 int argc, | |
87 v8::Handle<v8::Value> argv[]); | |
88 | |
84 // Register |native_handler| as a potential target for requireNative(), so | 89 // Register |native_handler| as a potential target for requireNative(), so |
85 // calls to requireNative(|name|) from JS will return a new object created by | 90 // calls to requireNative(|name|) from JS will return a new object created by |
86 // |native_handler|. | 91 // |native_handler|. |
87 void RegisterNativeHandler(const std::string& name, | 92 void RegisterNativeHandler(const std::string& name, |
88 scoped_ptr<NativeHandler> native_handler); | 93 scoped_ptr<NativeHandler> native_handler); |
94 bool HasNativeHandler(const std::string& name); | |
not at google - send to devlin
2013/01/24 21:10:12
docs on all these methods
cduvall
2013/01/24 22:15:15
Done.
| |
89 | 95 |
90 // Causes requireNative(|name|) to look for its module in |source_map_| | 96 // Causes requireNative(|name|) to look for its module in |source_map_| |
91 // instead of using a registered native handler. This can be used in unit | 97 // instead of using a registered native handler. This can be used in unit |
92 // tests to mock out native modules. | 98 // tests to mock out native modules. |
93 void OverrideNativeHandler(const std::string& name); | 99 void OverrideNativeHandler(const std::string& name); |
94 | 100 |
95 // Executes |code| in the current context with |name| as the filename. | 101 // Executes |code| in the current context with |name| as the filename. |
96 void RunString(const std::string& code, const std::string& name); | 102 void RunString(const std::string& code, const std::string& name); |
97 | 103 |
98 // Retrieves the lazily defined field specified by |property|. | 104 // Retrieves the lazily defined field specified by |property|. |
99 static v8::Handle<v8::Value> LazyFieldGetter(v8::Local<v8::String> property, | 105 static v8::Handle<v8::Value> LazyFieldGetter(v8::Local<v8::String> property, |
100 const v8::AccessorInfo& info); | 106 const v8::AccessorInfo& info); |
107 static v8::Handle<v8::Value> NativeLazyFieldGetter( | |
108 v8::Local<v8::String> property, | |
109 const v8::AccessorInfo& info); | |
110 | |
111 typedef v8::Handle<v8::Object> (*GetModuleFunc)(ModuleSystem*, | |
112 v8::Handle<v8::Object>); | |
113 static v8::Handle<v8::Value> BaseLazyFieldGetter( | |
114 v8::Local<v8::String> property, | |
115 const v8::AccessorInfo& info, | |
116 GetModuleFunc get_module); | |
117 static v8::Handle<v8::Object> GetModule(ModuleSystem* module_system, | |
118 v8::Handle<v8::Object> parameters); | |
119 static v8::Handle<v8::Object> GetNativeModule( | |
120 ModuleSystem* module_system, | |
121 v8::Handle<v8::Object> parameters); | |
not at google - send to devlin
2013/01/24 21:10:12
why are these static?
cduvall
2013/01/24 22:15:15
Because... who knows. They aren't anymore.
| |
101 | 122 |
102 // Make |object|.|field| lazily evaluate to the result of | 123 // Make |object|.|field| lazily evaluate to the result of |
103 // require(|module_name|)[|module_field|]. | 124 // require(|module_name|)[|module_field|]. |
104 void SetLazyField(v8::Handle<v8::Object> object, | 125 void SetLazyField(v8::Handle<v8::Object> object, |
105 const std::string& field, | 126 const std::string& field, |
106 const std::string& module_name, | 127 const std::string& module_name, |
107 const std::string& module_field); | 128 const std::string& module_field); |
108 | 129 |
130 // Make |object|.|field| lazily evaluate to the result of | |
131 // requireNative(|module_name|)[|module_field|]. | |
132 void SetNativeLazyField(v8::Handle<v8::Object> object, | |
133 const std::string& field, | |
134 const std::string& module_name, | |
135 const std::string& module_field); | |
136 | |
109 void set_exception_handler(scoped_ptr<ExceptionHandler> handler) { | 137 void set_exception_handler(scoped_ptr<ExceptionHandler> handler) { |
110 exception_handler_ = handler.Pass(); | 138 exception_handler_ = handler.Pass(); |
111 } | 139 } |
112 | 140 |
113 private: | 141 private: |
114 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; | 142 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; |
115 | 143 |
116 // Called when an exception is thrown but not caught. | 144 // Called when an exception is thrown but not caught. |
117 void HandleException(const v8::TryCatch& try_catch); | 145 void HandleException(const v8::TryCatch& try_catch); |
118 | 146 |
119 // Ensure that require_ has been evaluated from require.js. | 147 // Ensure that require_ has been evaluated from require.js. |
120 void EnsureRequireLoaded(); | 148 void EnsureRequireLoaded(); |
121 | 149 |
122 // Run |code| in the current context with the name |name| used for stack | 150 // Run |code| in the current context with the name |name| used for stack |
123 // traces. | 151 // traces. |
124 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, | 152 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, |
125 v8::Handle<v8::String> name); | 153 v8::Handle<v8::String> name); |
126 | 154 |
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 | |
127 // Return the named source file stored in the source map. | 161 // Return the named source file stored in the source map. |
128 // |args[0]| - the name of a source file in source_map_. | 162 // |args[0]| - the name of a source file in source_map_. |
129 v8::Handle<v8::Value> GetSource(v8::Handle<v8::String> source_name); | 163 v8::Handle<v8::Value> GetSource(v8::Handle<v8::String> source_name); |
130 | 164 |
131 // Return an object that contains the native methods defined by the named | 165 // Return an object that contains the native methods defined by the named |
132 // NativeHandler. | 166 // NativeHandler. |
133 // |args[0]| - the name of a native handler object. | 167 // |args[0]| - the name of a native handler object. |
168 v8::Handle<v8::Value> GetNativeFromString(const std::string& native_name); | |
134 v8::Handle<v8::Value> GetNative(const v8::Arguments& args); | 169 v8::Handle<v8::Value> GetNative(const v8::Arguments& args); |
135 | 170 |
136 // Wraps |source| in a (function(require, requireNative, exports) {...}). | 171 // Wraps |source| in a (function(require, requireNative, exports) {...}). |
137 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); | 172 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); |
138 | 173 |
139 // Throws an exception in the calling JS context. | 174 // Throws an exception in the calling JS context. |
140 v8::Handle<v8::Value> ThrowException(const std::string& message); | 175 v8::Handle<v8::Value> ThrowException(const std::string& message); |
141 | 176 |
142 // The context that this ModuleSystem is for. | 177 // The context that this ModuleSystem is for. |
143 v8::Persistent<v8::Context> context_; | 178 v8::Persistent<v8::Context> context_; |
(...skipping 13 matching lines...) Expand all Loading... | |
157 scoped_ptr<ExceptionHandler> exception_handler_; | 192 scoped_ptr<ExceptionHandler> exception_handler_; |
158 | 193 |
159 std::set<std::string> overridden_native_handlers_; | 194 std::set<std::string> overridden_native_handlers_; |
160 | 195 |
161 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); | 196 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); |
162 }; | 197 }; |
163 | 198 |
164 } // extensions | 199 } // extensions |
165 | 200 |
166 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ | 201 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ |
OLD | NEW |