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_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. |
| 33 // |
| 34 // Note that a ModuleSystem must be used only in conjunction with a single |
| 35 // v8::Context. |
| 36 // TODO(koz): Rename this to JavaScriptModuleSystem. |
30 class ModuleSystem : public NativeHandler { | 37 class ModuleSystem : public NativeHandler { |
31 public: | 38 public: |
| 39 class SourceMap { |
| 40 public: |
| 41 virtual v8::Handle<v8::Value> GetSource(const std::string& name) = 0; |
| 42 virtual bool Contains(const std::string& name) = 0; |
| 43 }; |
| 44 |
32 // |source_map| is a weak pointer. | 45 // |source_map| is a weak pointer. |
33 explicit ModuleSystem(const std::map<std::string, std::string>* source_map); | 46 explicit ModuleSystem(SourceMap* source_map); |
34 virtual ~ModuleSystem(); | 47 virtual ~ModuleSystem(); |
35 | 48 |
36 // Require the specified module. This is the equivalent of calling | 49 // Require the specified module. This is the equivalent of calling |
37 // require('module_name') from the loaded JS files. | 50 // require('module_name') from the loaded JS files. |
38 void Require(const std::string& module_name); | 51 void Require(const std::string& module_name); |
39 | 52 |
40 // Register |native_handler| as a potential target for requireNative(), so | 53 // Register |native_handler| as a potential target for requireNative(), so |
41 // calls to requireNative(|name|) from JS will return a new object created by | 54 // calls to requireNative(|name|) from JS will return a new object created by |
42 // |native_handler|. | 55 // |native_handler|. |
43 void RegisterNativeHandler(const std::string& name, | 56 void RegisterNativeHandler(const std::string& name, |
44 scoped_ptr<NativeHandler> native_handler); | 57 scoped_ptr<NativeHandler> native_handler); |
| 58 |
| 59 // When false |natives_enabled_| causes calls to GetNative() (the basis of |
| 60 // requireNative() in JS) to throw an exception. |
| 61 void set_natives_enabled(bool natives_enabled) { |
| 62 natives_enabled_ = natives_enabled; |
| 63 } |
45 | 64 |
46 private: | 65 private: |
| 66 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; |
| 67 |
47 // Ensure that require_ has been evaluated from require.js. | 68 // Ensure that require_ has been evaluated from require.js. |
48 void EnsureRequireLoaded(); | 69 void EnsureRequireLoaded(); |
49 | 70 |
50 // Run |code| in the current context. | 71 // Run |code| in the current context with the name |name| used for stack |
51 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code); | 72 // traces. |
52 | 73 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, |
53 // Run the given code in the current context. | 74 v8::Handle<v8::String> name); |
54 // |args[0]| - the code to execute. | |
55 v8::Handle<v8::Value> Run(const v8::Arguments& args); | |
56 | 75 |
57 // Return the named source file stored in the source map. | 76 // Return the named source file stored in the source map. |
58 // |args[0]| - the name of a source file in source_map_. | 77 // |args[0]| - the name of a source file in source_map_. |
59 v8::Handle<v8::Value> GetSource(const v8::Arguments& args); | 78 v8::Handle<v8::Value> GetSource(const v8::Arguments& args); |
60 | 79 |
61 // Return an object that contains the native methods defined by the named | 80 // Return an object that contains the native methods defined by the named |
62 // NativeHandler. | 81 // NativeHandler. |
63 // |args[0]| - the name of a native handler object. | 82 // |args[0]| - the name of a native handler object. |
64 v8::Handle<v8::Value> GetNative(const v8::Arguments& args); | 83 v8::Handle<v8::Value> GetNative(const v8::Arguments& args); |
65 | 84 |
| 85 base::StringPiece GetResource(int resource_id); |
| 86 |
| 87 // Throws an exception in the calling JS context. |
| 88 v8::Handle<v8::Value> ThrowException(const std::string& message); |
| 89 |
66 // A map from module names to the JS source for that module. GetSource() | 90 // A map from module names to the JS source for that module. GetSource() |
67 // performs a lookup on this map. | 91 // performs a lookup on this map. |
68 const std::map<std::string, std::string>* source_map_; | 92 SourceMap* source_map_; |
69 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; | |
70 NativeHandlerMap native_handler_map_; | 93 NativeHandlerMap native_handler_map_; |
71 v8::Handle<v8::Function> require_; | 94 v8::Handle<v8::Function> require_; |
| 95 bool natives_enabled_; |
72 }; | 96 }; |
73 | 97 |
74 #endif // CHROME_RENDERER_MODULE_SYSTEM_H_ | 98 #endif // CHROME_RENDERER_MODULE_SYSTEM_H_ |
OLD | NEW |