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_EXTENSIONS_MODULE_SYSTEM_H_ | 5 #ifndef CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ |
6 #define CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ | 6 #define CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ |
7 | 7 |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/memory/linked_ptr.h" | 9 #include "base/memory/linked_ptr.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 private: | 58 private: |
59 ModuleSystem* module_system_; | 59 ModuleSystem* module_system_; |
60 DISALLOW_COPY_AND_ASSIGN(NativesEnabledScope); | 60 DISALLOW_COPY_AND_ASSIGN(NativesEnabledScope); |
61 }; | 61 }; |
62 | 62 |
63 // |source_map| is a weak pointer. | 63 // |source_map| is a weak pointer. |
64 ModuleSystem(v8::Handle<v8::Context> context, SourceMap* source_map); | 64 ModuleSystem(v8::Handle<v8::Context> context, SourceMap* source_map); |
65 virtual ~ModuleSystem(); | 65 virtual ~ModuleSystem(); |
66 | 66 |
67 // Returns true if the current context has a ModuleSystem installed in it. | |
68 static bool IsPresentInCurrentContext(); | |
69 | |
70 // Dumps the debug info from |try_catch| to LOG(ERROR). | 67 // Dumps the debug info from |try_catch| to LOG(ERROR). |
71 static void DumpException(const v8::TryCatch& try_catch); | 68 static void DumpException(const v8::TryCatch& try_catch); |
72 | 69 |
73 // Require the specified module. This is the equivalent of calling | 70 // Require the specified module. This is the equivalent of calling |
74 // require('module_name') from the loaded JS files. | 71 // require('module_name') from the loaded JS files. |
75 v8::Handle<v8::Value> Require(const std::string& module_name); | 72 v8::Handle<v8::Value> Require(const std::string& module_name); |
76 v8::Handle<v8::Value> Require(const v8::Arguments& args); | 73 v8::Handle<v8::Value> Require(const v8::Arguments& args); |
77 | 74 |
78 // Calls the specified method exported by the specified module. This is | 75 // Calls the specified method exported by the specified module. This is |
79 // equivalent to calling require('module_name').method_name() from JS. | 76 // equivalent to calling require('module_name').method_name() from JS. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 // requireNative(|module_name|)[|module_field|]. | 120 // requireNative(|module_name|)[|module_field|]. |
124 void SetNativeLazyField(v8::Handle<v8::Object> object, | 121 void SetNativeLazyField(v8::Handle<v8::Object> object, |
125 const std::string& field, | 122 const std::string& field, |
126 const std::string& module_name, | 123 const std::string& module_name, |
127 const std::string& module_field); | 124 const std::string& module_field); |
128 | 125 |
129 void set_exception_handler(scoped_ptr<ExceptionHandler> handler) { | 126 void set_exception_handler(scoped_ptr<ExceptionHandler> handler) { |
130 exception_handler_ = handler.Pass(); | 127 exception_handler_ = handler.Pass(); |
131 } | 128 } |
132 | 129 |
133 virtual bool Invalidate() OVERRIDE; | 130 protected: |
| 131 friend class ChromeV8Context; |
| 132 virtual void Invalidate() OVERRIDE; |
134 | 133 |
135 private: | 134 private: |
136 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; | 135 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; |
137 | 136 |
138 // Called when an exception is thrown but not caught. | 137 // Called when an exception is thrown but not caught. |
139 void HandleException(const v8::TryCatch& try_catch); | 138 void HandleException(const v8::TryCatch& try_catch); |
140 | 139 |
141 static std::string CreateExceptionString(const v8::TryCatch& try_catch); | 140 static std::string CreateExceptionString(const v8::TryCatch& try_catch); |
142 | 141 |
143 // Ensure that require_ has been evaluated from require.js. | 142 // Ensure that require_ has been evaluated from require.js. |
144 void EnsureRequireLoaded(); | 143 void EnsureRequireLoaded(); |
145 | 144 |
146 // Run |code| in the current context with the name |name| used for stack | 145 // Run |code| in the current context with the name |name| used for stack |
147 // traces. | 146 // traces. |
148 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, | 147 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, |
149 v8::Handle<v8::String> name); | 148 v8::Handle<v8::String> name); |
150 | 149 |
151 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); | 150 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); |
152 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); | 151 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); |
153 | 152 |
154 // Sets a lazy field using the specified |getter|. | 153 // Sets a lazy field using the specified |getter|. |
155 void SetLazyField(v8::Handle<v8::Object> object, | 154 void SetLazyField(v8::Handle<v8::Object> object, |
156 const std::string& field, | 155 const std::string& field, |
157 const std::string& module_name, | 156 const std::string& module_name, |
158 const std::string& module_field, | 157 const std::string& module_field, |
159 v8::AccessorGetter getter); | 158 v8::AccessorGetter getter); |
160 | 159 |
161 typedef v8::Handle<v8::Value> (ModuleSystem::*GetModuleFunc)( | 160 typedef v8::Handle<v8::Value> (ModuleSystem::*RequireFunction)( |
162 const std::string&); | 161 const std::string&); |
163 // Base implementation of a LazyFieldGetter that can be customized by passing | 162 // Base implementation of a LazyFieldGetter which uses |require_fn| to require |
164 // in a |get_module| function. | 163 // modules. |
165 static v8::Handle<v8::Value> LazyFieldGetterInner( | 164 static v8::Handle<v8::Value> LazyFieldGetterInner( |
166 v8::Local<v8::String> property, | 165 v8::Local<v8::String> property, |
167 const v8::AccessorInfo& info, | 166 const v8::AccessorInfo& info, |
168 GetModuleFunc get_module); | 167 RequireFunction require_function); |
169 | 168 |
170 // Return the named source file stored in the source map. | 169 // Return the named source file stored in the source map. |
171 // |args[0]| - the name of a source file in source_map_. | 170 // |args[0]| - the name of a source file in source_map_. |
172 v8::Handle<v8::Value> GetSource(v8::Handle<v8::String> source_name); | 171 v8::Handle<v8::Value> GetSource(v8::Handle<v8::String> source_name); |
173 | 172 |
174 // Return an object that contains the native methods defined by the named | 173 // Return an object that contains the native methods defined by the named |
175 // NativeHandler. | 174 // NativeHandler. |
176 // |args[0]| - the name of a native handler object. | 175 // |args[0]| - the name of a native handler object. |
177 v8::Handle<v8::Value> RequireNativeFromString(const std::string& native_name); | 176 v8::Handle<v8::Value> RequireNativeFromString(const std::string& native_name); |
178 v8::Handle<v8::Value> RequireNative(const v8::Arguments& args); | 177 v8::Handle<v8::Value> RequireNative(const v8::Arguments& args); |
179 | 178 |
180 // Wraps |source| in a (function(require, requireNative, exports) {...}). | 179 // Wraps |source| in a (function(require, requireNative, exports) {...}). |
181 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); | 180 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); |
182 | 181 |
183 // Throws an exception in the calling JS context. | 182 // Throws an exception in the calling JS context. |
184 v8::Handle<v8::Value> ThrowException(const std::string& message); | 183 v8::Handle<v8::Value> ThrowException(const std::string& message); |
185 | 184 |
186 // A map from module names to the JS source for that module. GetSource() | 185 // A map from module names to the JS source for that module. GetSource() |
187 // performs a lookup on this map. | 186 // performs a lookup on this map. |
188 SourceMap* source_map_; | 187 SourceMap* source_map_; |
189 | 188 |
190 // A map from native handler names to native handlers. | 189 // A map from native handler names to native handlers. |
191 NativeHandlerMap native_handler_map_; | 190 NativeHandlerMap native_handler_map_; |
192 | 191 |
193 // When 0, natives are disabled, otherwise indicates how many callers have | 192 // When 0, natives are disabled, otherwise indicates how many callers have |
194 // pinned natives as enabled. | 193 // pinned natives as enabled. |
195 int natives_enabled_; | 194 int natives_enabled_; |
196 | 195 |
197 // Set to false if |context_| has been deleted and this should not be used. | |
198 bool is_valid_; | |
199 | |
200 // Called when an exception is thrown but not caught in JS. | 196 // Called when an exception is thrown but not caught in JS. |
201 scoped_ptr<ExceptionHandler> exception_handler_; | 197 scoped_ptr<ExceptionHandler> exception_handler_; |
202 | 198 |
203 std::set<std::string> overridden_native_handlers_; | 199 std::set<std::string> overridden_native_handlers_; |
204 | 200 |
205 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); | 201 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); |
206 }; | 202 }; |
207 | 203 |
208 } // extensions | 204 } // extensions |
209 | 205 |
210 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ | 206 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ |
OLD | NEW |