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 "chrome/renderer/extensions/scoped_persistent.h" | |
13 #include "v8/include/v8.h" | 12 #include "v8/include/v8.h" |
14 | 13 |
15 #include <map> | 14 #include <map> |
16 #include <set> | 15 #include <set> |
17 #include <string> | 16 #include <string> |
18 #include <vector> | 17 #include <vector> |
19 | 18 |
20 namespace extensions { | 19 namespace extensions { |
21 | 20 |
22 // A module system for JS similar to node.js' require() function. | 21 // A module system for JS similar to node.js' require() function. |
23 // Each module has three variables in the global scope: | 22 // Each module has three variables in the global scope: |
24 // - exports, an object returned to dependencies who require() this | 23 // - exports, an object returned to dependencies who require() this |
25 // module. | 24 // module. |
26 // - require, a function that takes a module name as an argument and returns | 25 // - require, a function that takes a module name as an argument and returns |
27 // that module's exports object. | 26 // that module's exports object. |
28 // - requireNative, a function that takes the name of a registered | 27 // - requireNative, a function that takes the name of a registered |
29 // NativeHandler and returns an object that contains the functions the | 28 // NativeHandler and returns an object that contains the functions the |
30 // NativeHandler defines. | 29 // NativeHandler defines. |
31 // | 30 // |
32 // Each module in a ModuleSystem is executed at most once and its exports | 31 // Each module in a ModuleSystem is executed at most once and its exports |
33 // object cached. | 32 // object cached. |
34 // | 33 // |
35 // Note that a ModuleSystem must be used only in conjunction with a single | 34 // Note that a ModuleSystem must be used only in conjunction with a single |
36 // v8::Context. | 35 // v8::Context. |
37 // TODO(koz): Rename this to JavaScriptModuleSystem. | 36 // TODO(koz): Rename this to JavaScriptModuleSystem. |
38 class ModuleSystem : public NativeHandler { | 37 class ModuleSystem : public ObjectBackedNativeHandler { |
39 public: | 38 public: |
40 class SourceMap { | 39 class SourceMap { |
41 public: | 40 public: |
42 virtual ~SourceMap() {} | 41 virtual ~SourceMap() {} |
43 virtual v8::Handle<v8::Value> GetSource(const std::string& name) = 0; | 42 virtual v8::Handle<v8::Value> GetSource(const std::string& name) = 0; |
44 virtual bool Contains(const std::string& name) = 0; | 43 virtual bool Contains(const std::string& name) = 0; |
45 }; | 44 }; |
46 | 45 |
47 class ExceptionHandler { | 46 class ExceptionHandler { |
48 public: | 47 public: |
49 virtual ~ExceptionHandler() {} | 48 virtual ~ExceptionHandler() {} |
50 virtual void HandleUncaughtException() = 0; | 49 virtual void HandleUncaughtException() = 0; |
51 }; | 50 }; |
52 | 51 |
53 // Enables native bindings for the duration of its lifetime. | 52 // Enables native bindings for the duration of its lifetime. |
54 class NativesEnabledScope { | 53 class NativesEnabledScope { |
55 public: | 54 public: |
56 explicit NativesEnabledScope(ModuleSystem* module_system); | 55 explicit NativesEnabledScope(ModuleSystem* module_system); |
57 ~NativesEnabledScope(); | 56 ~NativesEnabledScope(); |
58 | 57 |
59 private: | 58 private: |
60 ModuleSystem* module_system_; | 59 ModuleSystem* module_system_; |
61 DISALLOW_COPY_AND_ASSIGN(NativesEnabledScope); | 60 DISALLOW_COPY_AND_ASSIGN(NativesEnabledScope); |
62 }; | 61 }; |
63 | 62 |
64 // |source_map| is a weak pointer. | 63 // |source_map| is a weak pointer. |
65 explicit ModuleSystem(v8::Handle<v8::Context> context, SourceMap* source_map); | 64 ModuleSystem(v8::Handle<v8::Context> context, SourceMap* source_map); |
66 virtual ~ModuleSystem(); | 65 virtual ~ModuleSystem(); |
67 | 66 |
68 // Returns true if the current context has a ModuleSystem installed in it. | |
69 static bool IsPresentInCurrentContext(); | |
70 | |
71 // Dumps the debug info from |try_catch| to LOG(ERROR). | 67 // Dumps the debug info from |try_catch| to LOG(ERROR). |
72 static void DumpException(const v8::TryCatch& try_catch); | 68 static void DumpException(const v8::TryCatch& try_catch); |
73 | 69 |
74 // Require the specified module. This is the equivalent of calling | 70 // Require the specified module. This is the equivalent of calling |
75 // require('module_name') from the loaded JS files. | 71 // require('module_name') from the loaded JS files. |
76 void Require(const std::string& module_name); | 72 v8::Handle<v8::Value> Require(const std::string& module_name); |
77 v8::Handle<v8::Value> Require(const v8::Arguments& args); | 73 v8::Handle<v8::Value> Require(const v8::Arguments& args); |
78 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); | |
79 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); | |
80 | 74 |
81 // Calls the specified method exported by the specified module. This is | 75 // Calls the specified method exported by the specified module. This is |
82 // equivalent to calling require('module_name').method_name() from JS. | 76 // equivalent to calling require('module_name').method_name() from JS. |
83 void CallModuleMethod(const std::string& module_name, | 77 v8::Local<v8::Value> CallModuleMethod(const std::string& module_name, |
84 const std::string& method_name); | 78 const std::string& method_name); |
85 | 79 |
86 // Calls the specified method exported by the specified module. This is | 80 // Calls the specified method exported by the specified module. This is |
87 // equivalent to calling require('module_name').method_name(args) from JS. | 81 // equivalent to calling require('module_name').method_name(args) from JS. |
88 v8::Local<v8::Value> CallModuleMethod( | 82 v8::Local<v8::Value> CallModuleMethod( |
89 const std::string& module_name, | 83 const std::string& module_name, |
90 const std::string& method_name, | 84 const std::string& method_name, |
91 std::vector<v8::Handle<v8::Value> >* args); | 85 std::vector<v8::Handle<v8::Value> >* args); |
92 | 86 |
93 // Register |native_handler| as a potential target for requireNative(), so | 87 // Register |native_handler| as a potential target for requireNative(), so |
94 // calls to requireNative(|name|) from JS will return a new object created by | 88 // calls to requireNative(|name|) from JS will return a new object created by |
95 // |native_handler|. | 89 // |native_handler|. |
96 void RegisterNativeHandler(const std::string& name, | 90 void RegisterNativeHandler(const std::string& name, |
97 scoped_ptr<NativeHandler> native_handler); | 91 scoped_ptr<NativeHandler> native_handler); |
| 92 // Check if a native handler has been registered for this |name|. |
| 93 bool HasNativeHandler(const std::string& name); |
98 | 94 |
99 // Causes requireNative(|name|) to look for its module in |source_map_| | 95 // Causes requireNative(|name|) to look for its module in |source_map_| |
100 // instead of using a registered native handler. This can be used in unit | 96 // instead of using a registered native handler. This can be used in unit |
101 // tests to mock out native modules. | 97 // tests to mock out native modules. |
102 void OverrideNativeHandler(const std::string& name); | 98 void OverrideNativeHandler(const std::string& name); |
103 | 99 |
104 // Executes |code| in the current context with |name| as the filename. | 100 // Executes |code| in the current context with |name| as the filename. |
105 void RunString(const std::string& code, const std::string& name); | 101 void RunString(const std::string& code, const std::string& name); |
106 | 102 |
107 // Retrieves the lazily defined field specified by |property|. | 103 // Retrieves the lazily defined field specified by |property|. |
108 static v8::Handle<v8::Value> LazyFieldGetter(v8::Local<v8::String> property, | 104 static v8::Handle<v8::Value> LazyFieldGetter(v8::Local<v8::String> property, |
109 const v8::AccessorInfo& info); | 105 const v8::AccessorInfo& info); |
| 106 // Retrieves the lazily defined field specified by |property| on a native |
| 107 // object. |
| 108 static v8::Handle<v8::Value> NativeLazyFieldGetter( |
| 109 v8::Local<v8::String> property, |
| 110 const v8::AccessorInfo& info); |
110 | 111 |
111 // Make |object|.|field| lazily evaluate to the result of | 112 // Make |object|.|field| lazily evaluate to the result of |
112 // require(|module_name|)[|module_field|]. | 113 // require(|module_name|)[|module_field|]. |
113 void SetLazyField(v8::Handle<v8::Object> object, | 114 void SetLazyField(v8::Handle<v8::Object> object, |
114 const std::string& field, | 115 const std::string& field, |
115 const std::string& module_name, | 116 const std::string& module_name, |
116 const std::string& module_field); | 117 const std::string& module_field); |
117 | 118 |
| 119 // Make |object|.|field| lazily evaluate to the result of |
| 120 // requireNative(|module_name|)[|module_field|]. |
| 121 void SetNativeLazyField(v8::Handle<v8::Object> object, |
| 122 const std::string& field, |
| 123 const std::string& module_name, |
| 124 const std::string& module_field); |
| 125 |
118 void set_exception_handler(scoped_ptr<ExceptionHandler> handler) { | 126 void set_exception_handler(scoped_ptr<ExceptionHandler> handler) { |
119 exception_handler_ = handler.Pass(); | 127 exception_handler_ = handler.Pass(); |
120 } | 128 } |
121 | 129 |
| 130 protected: |
| 131 friend class ChromeV8Context; |
| 132 virtual void Invalidate() OVERRIDE; |
| 133 |
122 private: | 134 private: |
123 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; | 135 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; |
124 | 136 |
125 // Called when an exception is thrown but not caught. | 137 // Called when an exception is thrown but not caught. |
126 void HandleException(const v8::TryCatch& try_catch); | 138 void HandleException(const v8::TryCatch& try_catch); |
127 | 139 |
| 140 static std::string CreateExceptionString(const v8::TryCatch& try_catch); |
| 141 |
128 // Ensure that require_ has been evaluated from require.js. | 142 // Ensure that require_ has been evaluated from require.js. |
129 void EnsureRequireLoaded(); | 143 void EnsureRequireLoaded(); |
130 | 144 |
131 // Run |code| in the current context with the name |name| used for stack | 145 // Run |code| in the current context with the name |name| used for stack |
132 // traces. | 146 // traces. |
133 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, | 147 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, |
134 v8::Handle<v8::String> name); | 148 v8::Handle<v8::String> name); |
135 | 149 |
| 150 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); |
| 151 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); |
| 152 |
| 153 // Sets a lazy field using the specified |getter|. |
| 154 void SetLazyField(v8::Handle<v8::Object> object, |
| 155 const std::string& field, |
| 156 const std::string& module_name, |
| 157 const std::string& module_field, |
| 158 v8::AccessorGetter getter); |
| 159 |
| 160 typedef v8::Handle<v8::Value> (ModuleSystem::*RequireFunction)( |
| 161 const std::string&); |
| 162 // Base implementation of a LazyFieldGetter which uses |require_fn| to require |
| 163 // modules. |
| 164 static v8::Handle<v8::Value> LazyFieldGetterInner( |
| 165 v8::Local<v8::String> property, |
| 166 const v8::AccessorInfo& info, |
| 167 RequireFunction require_function); |
| 168 |
136 // Return the named source file stored in the source map. | 169 // Return the named source file stored in the source map. |
137 // |args[0]| - the name of a source file in source_map_. | 170 // |args[0]| - the name of a source file in source_map_. |
138 v8::Handle<v8::Value> GetSource(v8::Handle<v8::String> source_name); | 171 v8::Handle<v8::Value> GetSource(v8::Handle<v8::String> source_name); |
139 | 172 |
140 // Return an object that contains the native methods defined by the named | 173 // Return an object that contains the native methods defined by the named |
141 // NativeHandler. | 174 // NativeHandler. |
142 // |args[0]| - the name of a native handler object. | 175 // |args[0]| - the name of a native handler object. |
143 v8::Handle<v8::Value> GetNative(const v8::Arguments& args); | 176 v8::Handle<v8::Value> RequireNativeFromString(const std::string& native_name); |
| 177 v8::Handle<v8::Value> RequireNative(const v8::Arguments& args); |
144 | 178 |
145 // Wraps |source| in a (function(require, requireNative, exports) {...}). | 179 // Wraps |source| in a (function(require, requireNative, exports) {...}). |
146 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); | 180 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); |
147 | 181 |
148 // Throws an exception in the calling JS context. | 182 // Throws an exception in the calling JS context. |
149 v8::Handle<v8::Value> ThrowException(const std::string& message); | 183 v8::Handle<v8::Value> ThrowException(const std::string& message); |
150 | 184 |
151 // The context that this ModuleSystem is for. | |
152 ScopedPersistent<v8::Context> context_; | |
153 | |
154 // A map from module names to the JS source for that module. GetSource() | 185 // A map from module names to the JS source for that module. GetSource() |
155 // performs a lookup on this map. | 186 // performs a lookup on this map. |
156 SourceMap* source_map_; | 187 SourceMap* source_map_; |
157 | 188 |
158 // A map from native handler names to native handlers. | 189 // A map from native handler names to native handlers. |
159 NativeHandlerMap native_handler_map_; | 190 NativeHandlerMap native_handler_map_; |
160 | 191 |
161 // When 0, natives are disabled, otherwise indicates how many callers have | 192 // When 0, natives are disabled, otherwise indicates how many callers have |
162 // pinned natives as enabled. | 193 // pinned natives as enabled. |
163 int natives_enabled_; | 194 int natives_enabled_; |
164 | 195 |
165 // Called when an exception is thrown but not caught in JS. | 196 // Called when an exception is thrown but not caught in JS. |
166 scoped_ptr<ExceptionHandler> exception_handler_; | 197 scoped_ptr<ExceptionHandler> exception_handler_; |
167 | 198 |
168 std::set<std::string> overridden_native_handlers_; | 199 std::set<std::string> overridden_native_handlers_; |
169 | 200 |
170 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); | 201 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); |
171 }; | 202 }; |
172 | 203 |
173 } // extensions | 204 } // extensions |
174 | 205 |
175 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ | 206 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ |
OLD | NEW |