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/native_handler.h" |
12 #include "chrome/renderer/extensions/object_backed_native_handler.h" | 12 #include "chrome/renderer/extensions/object_backed_native_handler.h" |
13 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
14 | 14 |
15 #include <map> | 15 #include <map> |
16 #include <set> | 16 #include <set> |
17 #include <string> | 17 #include <string> |
18 #include <vector> | 18 #include <vector> |
19 | 19 |
20 namespace extensions { | 20 namespace extensions { |
21 | 21 |
| 22 class ChromeV8Context; |
| 23 |
22 // A module system for JS similar to node.js' require() function. | 24 // A module system for JS similar to node.js' require() function. |
23 // Each module has three variables in the global scope: | 25 // Each module has three variables in the global scope: |
24 // - exports, an object returned to dependencies who require() this | 26 // - exports, an object returned to dependencies who require() this |
25 // module. | 27 // module. |
26 // - 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 |
27 // that module's exports object. | 29 // that module's exports object. |
28 // - requireNative, a function that takes the name of a registered | 30 // - requireNative, a function that takes the name of a registered |
29 // NativeHandler and returns an object that contains the functions the | 31 // NativeHandler and returns an object that contains the functions the |
30 // NativeHandler defines. | 32 // NativeHandler defines. |
31 // | 33 // |
32 // Each module in a ModuleSystem is executed at most once and its exports | 34 // Each module in a ModuleSystem is executed at most once and its exports |
33 // object cached. | 35 // object cached. |
34 // | 36 // |
35 // Note that a ModuleSystem must be used only in conjunction with a single | 37 // Note that a ModuleSystem must be used only in conjunction with a single |
36 // v8::Context. | 38 // v8::Context. |
37 // TODO(koz): Rename this to JavaScriptModuleSystem. | 39 // TODO(koz): Rename this to JavaScriptModuleSystem. |
38 class ModuleSystem : public ObjectBackedNativeHandler { | 40 class ModuleSystem : public ObjectBackedNativeHandler { |
39 public: | 41 public: |
40 class SourceMap { | 42 class SourceMap { |
41 public: | 43 public: |
42 virtual ~SourceMap() {} | 44 virtual ~SourceMap() {} |
43 virtual v8::Handle<v8::Value> GetSource(const std::string& name) = 0; | 45 virtual v8::Handle<v8::Value> GetSource(const std::string& name) = 0; |
44 virtual bool Contains(const std::string& name) = 0; | 46 virtual bool Contains(const std::string& name) = 0; |
45 }; | 47 }; |
46 | 48 |
47 class ExceptionHandler { | 49 class ExceptionHandler { |
48 public: | 50 public: |
49 virtual ~ExceptionHandler() {} | 51 virtual ~ExceptionHandler() {} |
50 virtual void HandleUncaughtException() = 0; | 52 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) = 0; |
| 53 |
| 54 protected: |
| 55 // Formats |try_catch| as a nice string. |
| 56 std::string CreateExceptionString(const v8::TryCatch& try_catch); |
51 }; | 57 }; |
52 | 58 |
53 // Enables native bindings for the duration of its lifetime. | 59 // Enables native bindings for the duration of its lifetime. |
54 class NativesEnabledScope { | 60 class NativesEnabledScope { |
55 public: | 61 public: |
56 explicit NativesEnabledScope(ModuleSystem* module_system); | 62 explicit NativesEnabledScope(ModuleSystem* module_system); |
57 ~NativesEnabledScope(); | 63 ~NativesEnabledScope(); |
58 | 64 |
59 private: | 65 private: |
60 ModuleSystem* module_system_; | 66 ModuleSystem* module_system_; |
61 DISALLOW_COPY_AND_ASSIGN(NativesEnabledScope); | 67 DISALLOW_COPY_AND_ASSIGN(NativesEnabledScope); |
62 }; | 68 }; |
63 | 69 |
64 // |source_map| is a weak pointer. | 70 // |source_map| is a weak pointer. |
65 ModuleSystem(ChromeV8Context* context, SourceMap* source_map); | 71 ModuleSystem(ChromeV8Context* context, SourceMap* source_map); |
66 virtual ~ModuleSystem(); | 72 virtual ~ModuleSystem(); |
67 | 73 |
68 // Require the specified module. This is the equivalent of calling | 74 // Require the specified module. This is the equivalent of calling |
69 // require('module_name') from the loaded JS files. | 75 // require('module_name') from the loaded JS files. |
70 v8::Handle<v8::Value> Require(const std::string& module_name); | 76 v8::Handle<v8::Value> Require(const std::string& module_name); |
71 v8::Handle<v8::Value> Require(const v8::Arguments& args); | 77 v8::Handle<v8::Value> Require(const v8::Arguments& args); |
72 | 78 |
73 // Calls the specified method exported by the specified module. This is | 79 // Calls the specified method exported by the specified module. This is |
74 // equivalent to calling require('module_name').method_name() from JS. | 80 // equivalent to calling require('module_name').method_name() from JS. |
75 v8::Local<v8::Value> CallModuleMethod(const std::string& module_name, | 81 v8::Local<v8::Value> CallModuleMethod(const std::string& module_name, |
76 const std::string& method_name); | 82 const std::string& method_name); |
77 | |
78 // Calls the specified method exported by the specified module. This is | |
79 // equivalent to calling require('module_name').method_name(args) from JS. | |
80 v8::Local<v8::Value> CallModuleMethod( | 83 v8::Local<v8::Value> CallModuleMethod( |
81 const std::string& module_name, | 84 const std::string& module_name, |
82 const std::string& method_name, | 85 const std::string& method_name, |
83 std::vector<v8::Handle<v8::Value> >* args); | 86 std::vector<v8::Handle<v8::Value> >* args); |
| 87 v8::Local<v8::Value> CallModuleMethod( |
| 88 const std::string& module_name, |
| 89 const std::string& method_name, |
| 90 int argc, |
| 91 v8::Handle<v8::Value> argv[]); |
84 | 92 |
85 // Register |native_handler| as a potential target for requireNative(), so | 93 // Register |native_handler| as a potential target for requireNative(), so |
86 // calls to requireNative(|name|) from JS will return a new object created by | 94 // calls to requireNative(|name|) from JS will return a new object created by |
87 // |native_handler|. | 95 // |native_handler|. |
88 void RegisterNativeHandler(const std::string& name, | 96 void RegisterNativeHandler(const std::string& name, |
89 scoped_ptr<NativeHandler> native_handler); | 97 scoped_ptr<NativeHandler> native_handler); |
90 | 98 |
91 // Causes requireNative(|name|) to look for its module in |source_map_| | 99 // Causes requireNative(|name|) to look for its module in |source_map_| |
92 // instead of using a registered native handler. This can be used in unit | 100 // instead of using a registered native handler. This can be used in unit |
93 // tests to mock out native modules. | 101 // tests to mock out native modules. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 | 179 |
172 // 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 |
173 // NativeHandler. | 181 // NativeHandler. |
174 // |args[0]| - the name of a native handler object. | 182 // |args[0]| - the name of a native handler object. |
175 v8::Handle<v8::Value> RequireNativeFromString(const std::string& native_name); | 183 v8::Handle<v8::Value> RequireNativeFromString(const std::string& native_name); |
176 v8::Handle<v8::Value> RequireNative(const v8::Arguments& args); | 184 v8::Handle<v8::Value> RequireNative(const v8::Arguments& args); |
177 | 185 |
178 // Wraps |source| in a (function(require, requireNative, exports) {...}). | 186 // Wraps |source| in a (function(require, requireNative, exports) {...}). |
179 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); | 187 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); |
180 | 188 |
| 189 ChromeV8Context* context_; |
| 190 |
181 // A map from module names to the JS source for that module. GetSource() | 191 // A map from module names to the JS source for that module. GetSource() |
182 // performs a lookup on this map. | 192 // performs a lookup on this map. |
183 SourceMap* source_map_; | 193 SourceMap* source_map_; |
184 | 194 |
185 // A map from native handler names to native handlers. | 195 // A map from native handler names to native handlers. |
186 NativeHandlerMap native_handler_map_; | 196 NativeHandlerMap native_handler_map_; |
187 | 197 |
188 // When 0, natives are disabled, otherwise indicates how many callers have | 198 // When 0, natives are disabled, otherwise indicates how many callers have |
189 // pinned natives as enabled. | 199 // pinned natives as enabled. |
190 int natives_enabled_; | 200 int natives_enabled_; |
191 | 201 |
192 // Called when an exception is thrown but not caught in JS. | 202 // Called when an exception is thrown but not caught in JS. Overridable by |
193 // Non-NULL in tests only. | 203 // tests. |
194 scoped_ptr<ExceptionHandler> exception_handler_; | 204 scoped_ptr<ExceptionHandler> exception_handler_; |
195 | 205 |
196 std::set<std::string> overridden_native_handlers_; | 206 std::set<std::string> overridden_native_handlers_; |
197 | 207 |
198 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); | 208 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); |
199 }; | 209 }; |
200 | 210 |
201 } // extensions | 211 } // extensions |
202 | 212 |
203 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ | 213 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ |
OLD | NEW |