Chromium Code Reviews| 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 // Calls the specified method exported by the specified module. This is | 79 // Calls the specified method exported by the specified module. This is |
| 80 // equivalent to calling require('module_name').method_name() from JS. | 80 // equivalent to calling require('module_name').method_name() from JS. |
| 81 void CallModuleMethod(const std::string& module_name, | 81 void CallModuleMethod(const std::string& module_name, |
| 82 const std::string& method_name); | 82 const std::string& method_name); |
| 83 | 83 |
| 84 // Register |native_handler| as a potential target for requireNative(), so | 84 // Register |native_handler| as a potential target for requireNative(), so |
| 85 // calls to requireNative(|name|) from JS will return a new object created by | 85 // calls to requireNative(|name|) from JS will return a new object created by |
| 86 // |native_handler|. | 86 // |native_handler|. |
| 87 void RegisterNativeHandler(const std::string& name, | 87 void RegisterNativeHandler(const std::string& name, |
| 88 scoped_ptr<NativeHandler> native_handler); | 88 scoped_ptr<NativeHandler> native_handler); |
| 89 bool HasNativeHandler(const std::string& name); | |
| 89 | 90 |
| 90 // Causes requireNative(|name|) to look for its module in |source_map_| | 91 // Causes requireNative(|name|) to look for its module in |source_map_| |
| 91 // instead of using a registered native handler. This can be used in unit | 92 // instead of using a registered native handler. This can be used in unit |
| 92 // tests to mock out native modules. | 93 // tests to mock out native modules. |
| 93 void OverrideNativeHandler(const std::string& name); | 94 void OverrideNativeHandler(const std::string& name); |
| 94 | 95 |
| 95 // Executes |code| in the current context with |name| as the filename. | 96 // Executes |code| in the current context with |name| as the filename. |
| 96 void RunString(const std::string& code, const std::string& name); | 97 void RunString(const std::string& code, const std::string& name); |
| 97 | 98 |
| 98 // Retrieves the lazily defined field specified by |property|. | 99 // Retrieves the lazily defined field specified by |property|. |
| 99 static v8::Handle<v8::Value> LazyFieldGetter(v8::Local<v8::String> property, | 100 static v8::Handle<v8::Value> LazyFieldGetter(v8::Local<v8::String> property, |
| 100 const v8::AccessorInfo& info); | 101 const v8::AccessorInfo& info); |
| 101 | 102 |
| 102 // Make |object|.|field| lazily evaluate to the result of | 103 // Make |object|.|field| lazily evaluate to the result of |
| 104 // require(|module_name|)[|module_field|](|arg|). | |
|
not at google - send to devlin
2012/12/13 22:26:40
This is pretty cool, though see comment in schema_
| |
| 105 void SetLazyField(v8::Handle<v8::Object> object, | |
| 106 const std::string& field, | |
| 107 const std::string& module_name, | |
| 108 const std::string& module_field, | |
| 109 const std::string& arg); | |
| 110 | |
| 111 // Make |object|.|field| lazily evaluate to the result of | |
| 103 // require(|module_name|)[|module_field|]. | 112 // require(|module_name|)[|module_field|]. |
| 104 void SetLazyField(v8::Handle<v8::Object> object, | 113 void SetLazyField(v8::Handle<v8::Object> object, |
| 105 const std::string& field, | 114 const std::string& field, |
| 106 const std::string& module_name, | 115 const std::string& module_name, |
| 107 const std::string& module_field); | 116 const std::string& module_field); |
| 108 | 117 |
| 109 void set_exception_handler(scoped_ptr<ExceptionHandler> handler) { | 118 void set_exception_handler(scoped_ptr<ExceptionHandler> handler) { |
| 110 exception_handler_ = handler.Pass(); | 119 exception_handler_ = handler.Pass(); |
| 111 } | 120 } |
| 112 | 121 |
| 113 private: | 122 private: |
| 114 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; | 123 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; |
| 115 | 124 |
| 116 // Called when an exception is thrown but not caught. | 125 // Called when an exception is thrown but not caught. |
| 117 void HandleException(const v8::TryCatch& try_catch); | 126 void HandleException(const v8::TryCatch& try_catch); |
| 118 | 127 |
| 119 // Ensure that require_ has been evaluated from require.js. | 128 // Ensure that require_ has been evaluated from require.js. |
| 120 void EnsureRequireLoaded(); | 129 void EnsureRequireLoaded(); |
| 121 | 130 |
| 122 // Run |code| in the current context with the name |name| used for stack | 131 // Run |code| in the current context with the name |name| used for stack |
| 123 // traces. | 132 // traces. |
| 124 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, | 133 v8::Handle<v8::Value> RunString(v8::Handle<v8::String> code, |
| 125 v8::Handle<v8::String> name); | 134 v8::Handle<v8::String> name); |
| 126 | 135 |
| 136 // Call |value| with arguments |argv| and return the result. | |
| 137 v8::Handle<v8::Value> CallModuleMethod(v8::Local<v8::Value> value, | |
| 138 int argc, | |
| 139 v8::Handle<v8::Value> argv[]); | |
| 140 | |
| 141 | |
| 127 // Return the named source file stored in the source map. | 142 // Return the named source file stored in the source map. |
| 128 // |args[0]| - the name of a source file in source_map_. | 143 // |args[0]| - the name of a source file in source_map_. |
| 129 v8::Handle<v8::Value> GetSource(v8::Handle<v8::String> source_name); | 144 v8::Handle<v8::Value> GetSource(v8::Handle<v8::String> source_name); |
| 130 | 145 |
| 131 // Return an object that contains the native methods defined by the named | 146 // Return an object that contains the native methods defined by the named |
| 132 // NativeHandler. | 147 // NativeHandler. |
| 133 // |args[0]| - the name of a native handler object. | 148 // |args[0]| - the name of a native handler object. |
| 134 v8::Handle<v8::Value> GetNative(const v8::Arguments& args); | 149 v8::Handle<v8::Value> GetNative(const v8::Arguments& args); |
| 135 | 150 |
| 136 // Wraps |source| in a (function(require, requireNative, exports) {...}). | 151 // Wraps |source| in a (function(require, requireNative, exports) {...}). |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 157 scoped_ptr<ExceptionHandler> exception_handler_; | 172 scoped_ptr<ExceptionHandler> exception_handler_; |
| 158 | 173 |
| 159 std::set<std::string> overridden_native_handlers_; | 174 std::set<std::string> overridden_native_handlers_; |
| 160 | 175 |
| 161 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); | 176 DISALLOW_COPY_AND_ASSIGN(ModuleSystem); |
| 162 }; | 177 }; |
| 163 | 178 |
| 164 } // extensions | 179 } // extensions |
| 165 | 180 |
| 166 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ | 181 #endif // CHROME_RENDERER_EXTENSIONS_MODULE_SYSTEM_H_ |
| OLD | NEW |