| 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 "chrome/renderer/native_handler.h" | 12 #include "chrome/renderer/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 <string> | 16 #include <string> |
| 17 | 17 |
| 18 // A module system for JS similar to node.js' require() function. | 18 // A module system for JS similar to node.js' require() function. |
| 19 // Each module has three variables in the global scope: | 19 // Each module has three variables in the global scope: |
| 20 // - exports, an object returned to dependencies who require() this | 20 // - exports, an object returned to dependencies who require() this |
| 21 // module. | 21 // module. |
| 22 // - require, a function that takes a module name as an argument and returns | 22 // - require, a function that takes a module name as an argument and returns |
| 23 // that module's exports object. | 23 // that module's exports object. |
| 24 // - requireNative, a function that takes the name of a registered | 24 // - requireNative, a function that takes the name of a registered |
| 25 // NativeHandler and returns an object that contains the functions the | 25 // NativeHandler and returns an object that contains the functions the |
| 26 // NativeHandler defines. | 26 // NativeHandler defines. |
| 27 // | 27 // |
| 28 // Each module in a ModuleSystem is executed at most once and its exports | 28 // Each module in a ModuleSystem is executed at most once and its exports |
| 29 // object cached. | 29 // object cached. |
| 30 // |
| 31 // Note that a ModuleSystem must be used only in conjunction with a single |
| 32 // v8::Context. |
| 33 // TODO(koz): Rename this to JavaScriptModuleSystem. |
| 30 class ModuleSystem : public NativeHandler { | 34 class ModuleSystem : public NativeHandler { |
| 31 public: | 35 public: |
| 36 class SourceMap { |
| 37 public: |
| 38 virtual v8::Handle<v8::Value> GetSource(const std::string& name) = 0; |
| 39 virtual bool Contains(const std::string& name) = 0; |
| 40 }; |
| 41 |
| 32 // |source_map| is a weak pointer. | 42 // |source_map| is a weak pointer. |
| 33 explicit ModuleSystem(const std::map<std::string, std::string>* source_map); | 43 explicit ModuleSystem(SourceMap* source_map); |
| 34 virtual ~ModuleSystem(); | 44 virtual ~ModuleSystem(); |
| 35 | 45 |
| 36 // Require the specified module. This is the equivalent of calling | 46 // Require the specified module. This is the equivalent of calling |
| 37 // require('module_name') from the loaded JS files. | 47 // require('module_name') from the loaded JS files. |
| 38 void Require(const std::string& module_name); | 48 void Require(const std::string& module_name); |
| 49 v8::Handle<v8::Value> Require(const v8::Arguments& args); |
| 50 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); |
| 51 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::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 // Executes |code| in the current context with |name| as the filename. |
| 60 void RunString(const std::string& code, const std::string& name); |
| 61 |
| 62 // When false |natives_enabled_| causes calls to GetNative() (the basis of |
| 63 // requireNative() in JS) to throw an exception. |
| 64 void set_natives_enabled(bool natives_enabled) { |
| 65 natives_enabled_ = natives_enabled; |
| 66 } |
| 45 | 67 |
| 46 private: | 68 private: |
| 69 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; |
| 70 |
| 47 // Ensure that require_ has been evaluated from require.js. | 71 // Ensure that require_ has been evaluated from require.js. |
| 48 void EnsureRequireLoaded(); | 72 void EnsureRequireLoaded(); |
| 49 | 73 |
| 50 // Run |code| in the current context. | 74 // Run |code| in the current context with the name |name| used for stack |
| 51 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code); | 75 // traces. |
| 52 | 76 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, |
| 53 // Run the given code in the current context. | 77 v8::Handle<v8::String> name); |
| 54 // |args[0]| - the code to execute. | |
| 55 v8::Handle<v8::Value> Run(const v8::Arguments& args); | |
| 56 | 78 |
| 57 // Return the named source file stored in the source map. | 79 // Return the named source file stored in the source map. |
| 58 // |args[0]| - the name of a source file in source_map_. | 80 // |args[0]| - the name of a source file in source_map_. |
| 59 v8::Handle<v8::Value> GetSource(const v8::Arguments& args); | 81 v8::Handle<v8::Value> GetSource(v8::Handle<v8::String> source_name); |
| 60 | 82 |
| 61 // Return an object that contains the native methods defined by the named | 83 // Return an object that contains the native methods defined by the named |
| 62 // NativeHandler. | 84 // NativeHandler. |
| 63 // |args[0]| - the name of a native handler object. | 85 // |args[0]| - the name of a native handler object. |
| 64 v8::Handle<v8::Value> GetNative(const v8::Arguments& args); | 86 v8::Handle<v8::Value> GetNative(const v8::Arguments& args); |
| 65 | 87 |
| 88 // Wraps |source| in a (function(require, requireNative, exports) {...}). |
| 89 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); |
| 90 |
| 91 // Throws an exception in the calling JS context. |
| 92 v8::Handle<v8::Value> ThrowException(const std::string& message); |
| 93 |
| 66 // A map from module names to the JS source for that module. GetSource() | 94 // A map from module names to the JS source for that module. GetSource() |
| 67 // performs a lookup on this map. | 95 // performs a lookup on this map. |
| 68 const std::map<std::string, std::string>* source_map_; | 96 SourceMap* source_map_; |
| 69 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; | |
| 70 NativeHandlerMap native_handler_map_; | 97 NativeHandlerMap native_handler_map_; |
| 71 v8::Handle<v8::Function> require_; | 98 bool natives_enabled_; |
| 72 }; | 99 }; |
| 73 | 100 |
| 74 #endif // CHROME_RENDERER_MODULE_SYSTEM_H_ | 101 #endif // CHROME_RENDERER_MODULE_SYSTEM_H_ |
| OLD | NEW |