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 <memory> |
9 #include <set> | 10 #include <set> |
10 #include <string> | 11 #include <string> |
11 #include <utility> | 12 #include <utility> |
12 #include <vector> | 13 #include <vector> |
13 | 14 |
14 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
16 #include "base/memory/scoped_ptr.h" | |
17 #include "extensions/renderer/native_handler.h" | 17 #include "extensions/renderer/native_handler.h" |
18 #include "extensions/renderer/object_backed_native_handler.h" | 18 #include "extensions/renderer/object_backed_native_handler.h" |
19 #include "gin/modules/module_registry_observer.h" | 19 #include "gin/modules/module_registry_observer.h" |
20 #include "v8/include/v8.h" | 20 #include "v8/include/v8.h" |
21 | 21 |
22 namespace extensions { | 22 namespace extensions { |
23 | 23 |
24 class ScriptContext; | 24 class ScriptContext; |
25 | 25 |
26 // A module system for JS similar to node.js' require() function. | 26 // A module system for JS similar to node.js' require() function. |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 std::vector<v8::Local<v8::Value>>* args); | 99 std::vector<v8::Local<v8::Value>>* args); |
100 v8::Local<v8::Value> CallModuleMethod(const std::string& module_name, | 100 v8::Local<v8::Value> CallModuleMethod(const std::string& module_name, |
101 const std::string& method_name, | 101 const std::string& method_name, |
102 int argc, | 102 int argc, |
103 v8::Local<v8::Value> argv[]); | 103 v8::Local<v8::Value> argv[]); |
104 | 104 |
105 // Register |native_handler| as a potential target for requireNative(), so | 105 // Register |native_handler| as a potential target for requireNative(), so |
106 // calls to requireNative(|name|) from JS will return a new object created by | 106 // calls to requireNative(|name|) from JS will return a new object created by |
107 // |native_handler|. | 107 // |native_handler|. |
108 void RegisterNativeHandler(const std::string& name, | 108 void RegisterNativeHandler(const std::string& name, |
109 scoped_ptr<NativeHandler> native_handler); | 109 std::unique_ptr<NativeHandler> native_handler); |
110 | 110 |
111 // Causes requireNative(|name|) to look for its module in |source_map_| | 111 // Causes requireNative(|name|) to look for its module in |source_map_| |
112 // instead of using a registered native handler. This can be used in unit | 112 // instead of using a registered native handler. This can be used in unit |
113 // tests to mock out native modules. | 113 // tests to mock out native modules. |
114 void OverrideNativeHandlerForTest(const std::string& name); | 114 void OverrideNativeHandlerForTest(const std::string& name); |
115 | 115 |
116 // Executes |code| in the current context with |name| as the filename. | 116 // Executes |code| in the current context with |name| as the filename. |
117 void RunString(const std::string& code, const std::string& name); | 117 void RunString(const std::string& code, const std::string& name); |
118 | 118 |
119 // Make |object|.|field| lazily evaluate to the result of | 119 // Make |object|.|field| lazily evaluate to the result of |
(...skipping 15 matching lines...) Expand all Loading... |
135 | 135 |
136 // Make |object|.|field| lazily evaluate to the result of | 136 // Make |object|.|field| lazily evaluate to the result of |
137 // requireNative(|module_name|)[|module_field|]. | 137 // requireNative(|module_name|)[|module_field|]. |
138 // TODO(kalman): Same as above. | 138 // TODO(kalman): Same as above. |
139 void SetNativeLazyField(v8::Local<v8::Object> object, | 139 void SetNativeLazyField(v8::Local<v8::Object> object, |
140 const std::string& field, | 140 const std::string& field, |
141 const std::string& module_name, | 141 const std::string& module_name, |
142 const std::string& module_field); | 142 const std::string& module_field); |
143 | 143 |
144 // Passes exceptions to |handler| rather than console::Fatal. | 144 // Passes exceptions to |handler| rather than console::Fatal. |
145 void SetExceptionHandlerForTest(scoped_ptr<ExceptionHandler> handler) { | 145 void SetExceptionHandlerForTest(std::unique_ptr<ExceptionHandler> handler) { |
146 exception_handler_ = std::move(handler); | 146 exception_handler_ = std::move(handler); |
147 } | 147 } |
148 | 148 |
149 protected: | 149 protected: |
150 friend class ModuleSystemTestEnvironment; | 150 friend class ModuleSystemTestEnvironment; |
151 friend class ScriptContext; | 151 friend class ScriptContext; |
152 void Invalidate() override; | 152 void Invalidate() override; |
153 | 153 |
154 private: | 154 private: |
155 typedef std::map<std::string, scoped_ptr<NativeHandler>> NativeHandlerMap; | 155 typedef std::map<std::string, std::unique_ptr<NativeHandler>> |
| 156 NativeHandlerMap; |
156 | 157 |
157 // Retrieves the lazily defined field specified by |property|. | 158 // Retrieves the lazily defined field specified by |property|. |
158 static void LazyFieldGetter(v8::Local<v8::Name> property, | 159 static void LazyFieldGetter(v8::Local<v8::Name> property, |
159 const v8::PropertyCallbackInfo<v8::Value>& info); | 160 const v8::PropertyCallbackInfo<v8::Value>& info); |
160 // Retrieves the lazily defined field specified by |property| on a native | 161 // Retrieves the lazily defined field specified by |property| on a native |
161 // object. | 162 // object. |
162 static void NativeLazyFieldGetter( | 163 static void NativeLazyFieldGetter( |
163 v8::Local<v8::Name> property, | 164 v8::Local<v8::Name> property, |
164 const v8::PropertyCallbackInfo<v8::Value>& info); | 165 const v8::PropertyCallbackInfo<v8::Value>& info); |
165 | 166 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 v8::Local<v8::String> WrapSource(v8::Local<v8::String> source); | 198 v8::Local<v8::String> WrapSource(v8::Local<v8::String> source); |
198 | 199 |
199 // NativeHandler implementation which returns the private area of an Object. | 200 // NativeHandler implementation which returns the private area of an Object. |
200 void Private(const v8::FunctionCallbackInfo<v8::Value>& args); | 201 void Private(const v8::FunctionCallbackInfo<v8::Value>& args); |
201 | 202 |
202 // Loads and runs a Javascript module. | 203 // Loads and runs a Javascript module. |
203 v8::Local<v8::Value> LoadModule(const std::string& module_name); | 204 v8::Local<v8::Value> LoadModule(const std::string& module_name); |
204 | 205 |
205 // Invoked when a module is loaded in response to a requireAsync call. | 206 // Invoked when a module is loaded in response to a requireAsync call. |
206 // Resolves |resolver| with |value|. | 207 // Resolves |resolver| with |value|. |
207 void OnModuleLoaded(scoped_ptr<v8::Global<v8::Promise::Resolver>> resolver, | 208 void OnModuleLoaded( |
208 v8::Local<v8::Value> value); | 209 std::unique_ptr<v8::Global<v8::Promise::Resolver>> resolver, |
| 210 v8::Local<v8::Value> value); |
209 | 211 |
210 // gin::ModuleRegistryObserver overrides. | 212 // gin::ModuleRegistryObserver overrides. |
211 void OnDidAddPendingModule( | 213 void OnDidAddPendingModule( |
212 const std::string& id, | 214 const std::string& id, |
213 const std::vector<std::string>& dependencies) override; | 215 const std::vector<std::string>& dependencies) override; |
214 | 216 |
215 // Marks any existing NativeHandler named |name| as clobbered. | 217 // Marks any existing NativeHandler named |name| as clobbered. |
216 // See |clobbered_native_handlers_|. | 218 // See |clobbered_native_handlers_|. |
217 void ClobberExistingNativeHandler(const std::string& name); | 219 void ClobberExistingNativeHandler(const std::string& name); |
218 | 220 |
219 ScriptContext* context_; | 221 ScriptContext* context_; |
220 | 222 |
221 // A map from module names to the JS source for that module. GetSource() | 223 // A map from module names to the JS source for that module. GetSource() |
222 // performs a lookup on this map. | 224 // performs a lookup on this map. |
223 const SourceMap* const source_map_; | 225 const SourceMap* const source_map_; |
224 | 226 |
225 // A map from native handler names to native handlers. | 227 // A map from native handler names to native handlers. |
226 NativeHandlerMap native_handler_map_; | 228 NativeHandlerMap native_handler_map_; |
227 | 229 |
228 // When 0, natives are disabled, otherwise indicates how many callers have | 230 // When 0, natives are disabled, otherwise indicates how many callers have |
229 // pinned natives as enabled. | 231 // pinned natives as enabled. |
230 int natives_enabled_; | 232 int natives_enabled_; |
231 | 233 |
232 // Called when an exception is thrown but not caught in JS. Overridable by | 234 // Called when an exception is thrown but not caught in JS. Overridable by |
233 // tests. | 235 // tests. |
234 scoped_ptr<ExceptionHandler> exception_handler_; | 236 std::unique_ptr<ExceptionHandler> exception_handler_; |
235 | 237 |
236 // A set of native handlers that should actually be require()d as non-native | 238 // A set of native handlers that should actually be require()d as non-native |
237 // handlers. This is used for tests to mock out native handlers in JS. | 239 // handlers. This is used for tests to mock out native handlers in JS. |
238 std::set<std::string> overridden_native_handlers_; | 240 std::set<std::string> overridden_native_handlers_; |
239 | 241 |
240 // A list of NativeHandlers that have been clobbered, either due to | 242 // A list of NativeHandlers that have been clobbered, either due to |
241 // registering a NativeHandler when one was already registered with the same | 243 // registering a NativeHandler when one was already registered with the same |
242 // name, or due to OverrideNativeHandlerForTest. This is needed so that they | 244 // name, or due to OverrideNativeHandlerForTest. This is needed so that they |
243 // can be later Invalidated. It should only happen in tests. | 245 // can be later Invalidated. It should only happen in tests. |
244 std::vector<scoped_ptr<NativeHandler>> clobbered_native_handlers_; | 246 std::vector<std::unique_ptr<NativeHandler>> clobbered_native_handlers_; |
245 | 247 |
246 base::WeakPtrFactory<ModuleSystem> weak_factory_; | 248 base::WeakPtrFactory<ModuleSystem> weak_factory_; |
247 | 249 |
248 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); | 250 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); |
249 }; | 251 }; |
250 | 252 |
251 } // namespace extensions | 253 } // namespace extensions |
252 | 254 |
253 #endif // EXTENSIONS_RENDERER_MODULE_SYSTEM_H_ | 255 #endif // EXTENSIONS_RENDERER_MODULE_SYSTEM_H_ |
OLD | NEW |