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

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

Issue 9386001: Implement a module system for the extension bindings JS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix compile errors Created 8 years, 9 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_MODULE_SYSTEM_H_ 5 #ifndef CHROME_RENDERER_MODULE_SYSTEM_H_
6 #define CHROME_RENDERER_MODULE_SYSTEM_H_ 6 #define CHROME_RENDERER_MODULE_SYSTEM_H_
7 #pragma once 7 #pragma once
8 8
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/memory/linked_ptr.h" 10 #include "base/memory/linked_ptr.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/string_piece.h"
12 #include "chrome/renderer/native_handler.h" 13 #include "chrome/renderer/native_handler.h"
13 #include "v8/include/v8.h" 14 #include "v8/include/v8.h"
14 15
15 #include <map> 16 #include <map>
16 #include <string> 17 #include <string>
17 18
19 class SourceMap;
20
18 // A module system for JS similar to node.js' require() function. 21 // A module system for JS similar to node.js' require() function.
19 // Each module has three variables in the global scope: 22 // Each module has three variables in the global scope:
20 // - exports, an object returned to dependencies who require() this 23 // - exports, an object returned to dependencies who require() this
21 // module. 24 // module.
22 // - 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
23 // that module's exports object. 26 // that module's exports object.
24 // - requireNative, a function that takes the name of a registered 27 // - requireNative, a function that takes the name of a registered
25 // NativeHandler and returns an object that contains the functions the 28 // NativeHandler and returns an object that contains the functions the
26 // NativeHandler defines. 29 // NativeHandler defines.
27 // 30 //
28 // 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
29 // object cached. 32 // object cached.
Matt Perry 2012/03/01 23:23:06 Could you add a quick comment that explains this i
koz (OOO until 15th September) 2012/03/02 01:13:58 Done.
33 // TODO(koz): Rename this to JavaScriptModuleSystem.
30 class ModuleSystem : public NativeHandler { 34 class ModuleSystem : public NativeHandler {
31 public: 35 public:
32 // |source_map| is a weak pointer. 36 // |source_map| is a weak pointer.
33 explicit ModuleSystem(const std::map<std::string, std::string>* source_map); 37 explicit ModuleSystem(SourceMap* source_map);
34 virtual ~ModuleSystem(); 38 virtual ~ModuleSystem();
35 39
36 // Require the specified module. This is the equivalent of calling 40 // Require the specified module. This is the equivalent of calling
37 // require('module_name') from the loaded JS files. 41 // require('module_name') from the loaded JS files.
38 void Require(const std::string& module_name); 42 void Require(const std::string& module_name);
39 43
40 // Register |native_handler| as a potential target for requireNative(), so 44 // Register |native_handler| as a potential target for requireNative(), so
41 // calls to requireNative(|name|) from JS will return a new object created by 45 // calls to requireNative(|name|) from JS will return a new object created by
42 // |native_handler|. 46 // |native_handler|.
43 void RegisterNativeHandler(const std::string& name, 47 void RegisterNativeHandler(const std::string& name,
44 scoped_ptr<NativeHandler> native_handler); 48 scoped_ptr<NativeHandler> native_handler);
49
50 // Prevents any new JS code from being injected by causing GetSource(), Run()
51 // and GetNative() to throw exceptions.
52 void DisableInjection();
Aaron Boodman 2012/03/01 07:24:26 It's only GetNative() that needs to be disabled; t
koz (OOO until 15th September) 2012/03/02 01:13:58 Done.
45 53
46 private: 54 private:
55 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap;
Aaron Boodman 2012/03/01 07:24:26 .append("\n");
koz (OOO until 15th September) 2012/03/02 01:13:58 Done.
47 // Ensure that require_ has been evaluated from require.js. 56 // Ensure that require_ has been evaluated from require.js.
48 void EnsureRequireLoaded(); 57 void EnsureRequireLoaded();
49 58
50 // Run |code| in the current context. 59 // Run |code| in the current context with the name |name| used for stack
51 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code); 60 // traces.
61 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code,
62 v8::Handle<v8::String> name);
52 63
53 // Run the given code in the current context. 64 // Run the given code in the current context.
54 // |args[0]| - the code to execute. 65 // |args[0]| - the code to execute.
55 v8::Handle<v8::Value> Run(const v8::Arguments& args); 66 v8::Handle<v8::Value> Run(const v8::Arguments& args);
56 67
57 // Return the named source file stored in the source map. 68 // Return the named source file stored in the source map.
58 // |args[0]| - the name of a source file in source_map_. 69 // |args[0]| - the name of a source file in source_map_.
59 v8::Handle<v8::Value> GetSource(const v8::Arguments& args); 70 v8::Handle<v8::Value> GetSource(const v8::Arguments& args);
60 71
61 // Return an object that contains the native methods defined by the named 72 // Return an object that contains the native methods defined by the named
62 // NativeHandler. 73 // NativeHandler.
63 // |args[0]| - the name of a native handler object. 74 // |args[0]| - the name of a native handler object.
64 v8::Handle<v8::Value> GetNative(const v8::Arguments& args); 75 v8::Handle<v8::Value> GetNative(const v8::Arguments& args);
65 76
77 base::StringPiece GetResource(int resource_id);
78
79 // Throws an exception in the calling JS context.
80 v8::Handle<v8::Value> ThrowException(const std::string& message);
81
66 // A map from module names to the JS source for that module. GetSource() 82 // A map from module names to the JS source for that module. GetSource()
67 // performs a lookup on this map. 83 // performs a lookup on this map.
68 const std::map<std::string, std::string>* source_map_; 84 SourceMap* source_map_;
69 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap;
70 NativeHandlerMap native_handler_map_; 85 NativeHandlerMap native_handler_map_;
71 v8::Handle<v8::Function> require_; 86 v8::Handle<v8::Function> require_;
87 bool injection_enabled_;
72 }; 88 };
73 89
74 #endif // CHROME_RENDERER_MODULE_SYSTEM_H_ 90 #endif // CHROME_RENDERER_MODULE_SYSTEM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698