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

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

Issue 234413005: Move most of ChromeV8Context to a base ScriptContext (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 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 (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 EXTENSIONS_RENDERER_MODULE_SYSTEM_H_
6 #define CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ 6 #define EXTENSIONS_RENDERER_MODULE_SYSTEM_H_
7
8 #include "base/compiler_specific.h"
9 #include "base/memory/linked_ptr.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/renderer/extensions/object_backed_native_handler.h"
12 #include "extensions/renderer/native_handler.h"
13 #include "v8/include/v8.h"
14 7
15 #include <map> 8 #include <map>
16 #include <set> 9 #include <set>
17 #include <string> 10 #include <string>
18 #include <vector> 11 #include <vector>
19 12
13 #include "base/compiler_specific.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "extensions/renderer/native_handler.h"
17 #include "extensions/renderer/object_backed_native_handler.h"
18 #include "v8/include/v8.h"
19
20 namespace extensions { 20 namespace extensions {
21 21
22 class ChromeV8Context; 22 class ScriptContext;
23 23
24 // A module system for JS similar to node.js' require() function. 24 // A module system for JS similar to node.js' require() function.
25 // Each module has three variables in the global scope: 25 // Each module has three variables in the global scope:
26 // - exports, an object returned to dependencies who require() this 26 // - exports, an object returned to dependencies who require() this
27 // module. 27 // module.
28 // - require, a function that takes a module name as an argument and returns 28 // - require, a function that takes a module name as an argument and returns
29 // that module's exports object. 29 // that module's exports object.
30 // - requireNative, a function that takes the name of a registered 30 // - requireNative, a function that takes the name of a registered
31 // NativeHandler and returns an object that contains the functions the 31 // NativeHandler and returns an object that contains the functions the
32 // NativeHandler defines. 32 // NativeHandler defines.
(...skipping 29 matching lines...) Expand all
62 public: 62 public:
63 explicit NativesEnabledScope(ModuleSystem* module_system); 63 explicit NativesEnabledScope(ModuleSystem* module_system);
64 ~NativesEnabledScope(); 64 ~NativesEnabledScope();
65 65
66 private: 66 private:
67 ModuleSystem* module_system_; 67 ModuleSystem* module_system_;
68 DISALLOW_COPY_AND_ASSIGN(NativesEnabledScope); 68 DISALLOW_COPY_AND_ASSIGN(NativesEnabledScope);
69 }; 69 };
70 70
71 // |source_map| is a weak pointer. 71 // |source_map| is a weak pointer.
72 ModuleSystem(ChromeV8Context* context, SourceMap* source_map); 72 ModuleSystem(ScriptContext* context, SourceMap* source_map);
73 virtual ~ModuleSystem(); 73 virtual ~ModuleSystem();
74 74
75 // Require the specified module. This is the equivalent of calling 75 // Require the specified module. This is the equivalent of calling
76 // require('module_name') from the loaded JS files. 76 // require('module_name') from the loaded JS files.
77 v8::Handle<v8::Value> Require(const std::string& module_name); 77 v8::Handle<v8::Value> Require(const std::string& module_name);
78 void Require(const v8::FunctionCallbackInfo<v8::Value>& args); 78 void Require(const v8::FunctionCallbackInfo<v8::Value>& args);
79 79
80 // Run |code| in the current context with the name |name| used for stack 80 // Run |code| in the current context with the name |name| used for stack
81 // traces. 81 // traces.
82 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, 82 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code,
83 v8::Handle<v8::String> name); 83 v8::Handle<v8::String> name);
84 84
85 // Calls the specified method exported by the specified module. This is 85 // Calls the specified method exported by the specified module. This is
86 // equivalent to calling require('module_name').method_name() from JS. 86 // equivalent to calling require('module_name').method_name() from JS.
87 v8::Local<v8::Value> CallModuleMethod(const std::string& module_name, 87 v8::Local<v8::Value> CallModuleMethod(const std::string& module_name,
88 const std::string& method_name); 88 const std::string& method_name);
89 v8::Local<v8::Value> CallModuleMethod( 89 v8::Local<v8::Value> CallModuleMethod(
90 const std::string& module_name, 90 const std::string& module_name,
91 const std::string& method_name, 91 const std::string& method_name,
92 std::vector<v8::Handle<v8::Value> >* args); 92 std::vector<v8::Handle<v8::Value> >* args);
93 v8::Local<v8::Value> CallModuleMethod( 93 v8::Local<v8::Value> CallModuleMethod(const std::string& module_name,
94 const std::string& module_name, 94 const std::string& method_name,
95 const std::string& method_name, 95 int argc,
96 int argc, 96 v8::Handle<v8::Value> argv[]);
97 v8::Handle<v8::Value> argv[]);
98 97
99 // Register |native_handler| as a potential target for requireNative(), so 98 // Register |native_handler| as a potential target for requireNative(), so
100 // calls to requireNative(|name|) from JS will return a new object created by 99 // calls to requireNative(|name|) from JS will return a new object created by
101 // |native_handler|. 100 // |native_handler|.
102 void RegisterNativeHandler(const std::string& name, 101 void RegisterNativeHandler(const std::string& name,
103 scoped_ptr<NativeHandler> native_handler); 102 scoped_ptr<NativeHandler> native_handler);
104 103
105 // Causes requireNative(|name|) to look for its module in |source_map_| 104 // Causes requireNative(|name|) to look for its module in |source_map_|
106 // instead of using a registered native handler. This can be used in unit 105 // instead of using a registered native handler. This can be used in unit
107 // tests to mock out native modules. 106 // tests to mock out native modules.
(...skipping 26 matching lines...) Expand all
134 const std::string& field, 133 const std::string& field,
135 const std::string& module_name, 134 const std::string& module_name,
136 const std::string& module_field); 135 const std::string& module_field);
137 136
138 // Passes exceptions to |handler| rather than console::Fatal. 137 // Passes exceptions to |handler| rather than console::Fatal.
139 void SetExceptionHandlerForTest(scoped_ptr<ExceptionHandler> handler) { 138 void SetExceptionHandlerForTest(scoped_ptr<ExceptionHandler> handler) {
140 exception_handler_ = handler.Pass(); 139 exception_handler_ = handler.Pass();
141 } 140 }
142 141
143 protected: 142 protected:
144 friend class ChromeV8Context; 143 friend class ScriptContext;
145 virtual void Invalidate() OVERRIDE; 144 virtual void Invalidate() OVERRIDE;
146 145
147 private: 146 private:
148 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; 147 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap;
149 148
150 // Retrieves the lazily defined field specified by |property|. 149 // Retrieves the lazily defined field specified by |property|.
151 static void LazyFieldGetter(v8::Local<v8::String> property, 150 static void LazyFieldGetter(v8::Local<v8::String> property,
152 const v8::PropertyCallbackInfo<v8::Value>& info); 151 const v8::PropertyCallbackInfo<v8::Value>& info);
153 // Retrieves the lazily defined field specified by |property| on a native 152 // Retrieves the lazily defined field specified by |property| on a native
154 // object. 153 // object.
155 static void NativeLazyFieldGetter( 154 static void NativeLazyFieldGetter(
156 v8::Local<v8::String> property, 155 v8::Local<v8::String> property,
157 const v8::PropertyCallbackInfo<v8::Value>& info); 156 const v8::PropertyCallbackInfo<v8::Value>& info);
158 157
159 // Called when an exception is thrown but not caught. 158 // Called when an exception is thrown but not caught.
160 void HandleException(const v8::TryCatch& try_catch); 159 void HandleException(const v8::TryCatch& try_catch);
161 160
162 // Ensure that require_ has been evaluated from require.js. 161 // Ensure that require_ has been evaluated from require.js.
163 void EnsureRequireLoaded(); 162 void EnsureRequireLoaded();
164 163
165 void RequireForJs(const v8::FunctionCallbackInfo<v8::Value>& args); 164 void RequireForJs(const v8::FunctionCallbackInfo<v8::Value>& args);
166 v8::Local<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); 165 v8::Local<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name);
167 166
168 typedef v8::Handle<v8::Value> (ModuleSystem::*RequireFunction)( 167 typedef v8::Handle<v8::Value>(ModuleSystem::*RequireFunction)(
169 const std::string&); 168 const std::string&);
170 // Base implementation of a LazyFieldGetter which uses |require_fn| to require 169 // Base implementation of a LazyFieldGetter which uses |require_fn| to require
171 // modules. 170 // modules.
172 static void LazyFieldGetterInner( 171 static void LazyFieldGetterInner(
173 v8::Local<v8::String> property, 172 v8::Local<v8::String> property,
174 const v8::PropertyCallbackInfo<v8::Value>& info, 173 const v8::PropertyCallbackInfo<v8::Value>& info,
175 RequireFunction require_function); 174 RequireFunction require_function);
176 175
177 // Return the named source file stored in the source map. 176 // Return the named source file stored in the source map.
178 // |args[0]| - the name of a source file in source_map_. 177 // |args[0]| - the name of a source file in source_map_.
179 v8::Handle<v8::Value> GetSource(const std::string& module_name); 178 v8::Handle<v8::Value> GetSource(const std::string& module_name);
180 179
181 // 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
182 // NativeHandler. 181 // NativeHandler.
183 // |args[0]| - the name of a native handler object. 182 // |args[0]| - the name of a native handler object.
184 v8::Handle<v8::Value> RequireNativeFromString(const std::string& native_name); 183 v8::Handle<v8::Value> RequireNativeFromString(const std::string& native_name);
185 void RequireNative(const v8::FunctionCallbackInfo<v8::Value>& args); 184 void RequireNative(const v8::FunctionCallbackInfo<v8::Value>& args);
186 185
187 // Wraps |source| in a (function(require, requireNative, exports) {...}). 186 // Wraps |source| in a (function(require, requireNative, exports) {...}).
188 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); 187 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source);
189 188
190 // NativeHandler implementation which returns the private area of an Object. 189 // NativeHandler implementation which returns the private area of an Object.
191 void Private(const v8::FunctionCallbackInfo<v8::Value>& args); 190 void Private(const v8::FunctionCallbackInfo<v8::Value>& args);
192 191
193 // NativeHandler implementation which returns a function wrapper for a 192 // NativeHandler implementation which returns a function wrapper for a
194 // provided function. 193 // provided function.
195 void CreateFunctionWrapper(const v8::FunctionCallbackInfo<v8::Value>& args); 194 void CreateFunctionWrapper(const v8::FunctionCallbackInfo<v8::Value>& args);
196 195
197 ChromeV8Context* context_; 196 ScriptContext* context_;
198 197
199 // A map from module names to the JS source for that module. GetSource() 198 // A map from module names to the JS source for that module. GetSource()
200 // performs a lookup on this map. 199 // performs a lookup on this map.
201 SourceMap* source_map_; 200 SourceMap* source_map_;
202 201
203 // A map from native handler names to native handlers. 202 // A map from native handler names to native handlers.
204 NativeHandlerMap native_handler_map_; 203 NativeHandlerMap native_handler_map_;
205 204
206 // When 0, natives are disabled, otherwise indicates how many callers have 205 // When 0, natives are disabled, otherwise indicates how many callers have
207 // pinned natives as enabled. 206 // pinned natives as enabled.
208 int natives_enabled_; 207 int natives_enabled_;
209 208
210 // Called when an exception is thrown but not caught in JS. Overridable by 209 // Called when an exception is thrown but not caught in JS. Overridable by
211 // tests. 210 // tests.
212 scoped_ptr<ExceptionHandler> exception_handler_; 211 scoped_ptr<ExceptionHandler> exception_handler_;
213 212
214 std::set<std::string> overridden_native_handlers_; 213 std::set<std::string> overridden_native_handlers_;
215 214
216 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); 215 DISALLOW_COPY_AND_ASSIGN(ModuleSystem);
217 }; 216 };
218 217
219 } // namespace extensions 218 } // namespace extensions
220 219
221 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ 220 #endif // EXTENSIONS_RENDERER_MODULE_SYSTEM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698