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