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

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

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

Powered by Google App Engine
This is Rietveld 408576698