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