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

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

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

Powered by Google App Engine
This is Rietveld 408576698