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_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" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 // v8::Context. | 32 // v8::Context. |
| 33 // TODO(koz): Rename this to JavaScriptModuleSystem. | 33 // TODO(koz): Rename this to JavaScriptModuleSystem. |
| 34 class ModuleSystem : public NativeHandler { | 34 class ModuleSystem : public NativeHandler { |
| 35 public: | 35 public: |
| 36 class SourceMap { | 36 class SourceMap { |
| 37 public: | 37 public: |
| 38 virtual v8::Handle<v8::Value> GetSource(const std::string& name) = 0; | 38 virtual v8::Handle<v8::Value> GetSource(const std::string& name) = 0; |
| 39 virtual bool Contains(const std::string& name) = 0; | 39 virtual bool Contains(const std::string& name) = 0; |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 // Enables native bindings for the duration of its lifetime. | |
| 43 class NativesEnabledScope { | |
| 44 public: | |
| 45 explicit NativesEnabledScope(ModuleSystem* module_system); | |
| 46 ~NativesEnabledScope(); | |
| 47 | |
| 48 private: | |
| 49 ModuleSystem* module_system_; | |
|
not at google - send to devlin
2012/03/26 02:29:49
Come to think of it this is still subtly flawed, s
koz (OOO until 15th September)
2012/03/26 03:58:59
lol, maybe I'll just add the DISALLOW_COPY_AND_ASS
| |
| 50 }; | |
| 51 | |
| 42 // |source_map| is a weak pointer. | 52 // |source_map| is a weak pointer. |
| 43 explicit ModuleSystem(SourceMap* source_map); | 53 explicit ModuleSystem(SourceMap* source_map); |
| 44 virtual ~ModuleSystem(); | 54 virtual ~ModuleSystem(); |
| 45 | 55 |
| 46 // Require the specified module. This is the equivalent of calling | 56 // Require the specified module. This is the equivalent of calling |
| 47 // require('module_name') from the loaded JS files. | 57 // require('module_name') from the loaded JS files. |
| 48 void Require(const std::string& module_name); | 58 void Require(const std::string& module_name); |
| 49 v8::Handle<v8::Value> Require(const v8::Arguments& args); | 59 v8::Handle<v8::Value> Require(const v8::Arguments& args); |
| 50 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); | 60 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); |
| 51 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); | 61 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); |
| 52 | 62 |
| 53 // Register |native_handler| as a potential target for requireNative(), so | 63 // Register |native_handler| as a potential target for requireNative(), so |
| 54 // calls to requireNative(|name|) from JS will return a new object created by | 64 // calls to requireNative(|name|) from JS will return a new object created by |
| 55 // |native_handler|. | 65 // |native_handler|. |
| 56 void RegisterNativeHandler(const std::string& name, | 66 void RegisterNativeHandler(const std::string& name, |
| 57 scoped_ptr<NativeHandler> native_handler); | 67 scoped_ptr<NativeHandler> native_handler); |
| 58 | 68 |
| 59 // Executes |code| in the current context with |name| as the filename. | 69 // Executes |code| in the current context with |name| as the filename. |
| 60 void RunString(const std::string& code, const std::string& name); | 70 void RunString(const std::string& code, const std::string& name); |
| 61 | 71 |
| 62 // When false |natives_enabled_| causes calls to GetNative() (the basis of | 72 // Retrieves the lazily defined field specified by |property|. |
| 63 // requireNative() in JS) to throw an exception. | 73 static v8::Handle<v8::Value> LazyFieldGetter(v8::Local<v8::String> property, |
| 64 void set_natives_enabled(bool natives_enabled) { | 74 const v8::AccessorInfo& info); |
| 65 natives_enabled_ = natives_enabled; | |
| 66 } | |
| 67 | |
| 68 static v8::Handle<v8::Value> GetterRouter(v8::Local<v8::String> property, | |
| 69 const v8::AccessorInfo& info); | |
| 70 | 75 |
| 71 // Make |object|.|field| lazily evaluate to the result of | 76 // Make |object|.|field| lazily evaluate to the result of |
| 72 // require(|module_name|)[|module_field|]. | 77 // require(|module_name|)[|module_field|]. |
| 73 void SetLazyField(v8::Handle<v8::Object> object, | 78 void SetLazyField(v8::Handle<v8::Object> object, |
| 74 const std::string& field, | 79 const std::string& field, |
| 75 const std::string& module_name, | 80 const std::string& module_name, |
| 76 const std::string& module_field); | 81 const std::string& module_field); |
| 77 | 82 |
| 78 private: | 83 private: |
| 79 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; | 84 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 98 // Wraps |source| in a (function(require, requireNative, exports) {...}). | 103 // Wraps |source| in a (function(require, requireNative, exports) {...}). |
| 99 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); | 104 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); |
| 100 | 105 |
| 101 // Throws an exception in the calling JS context. | 106 // Throws an exception in the calling JS context. |
| 102 v8::Handle<v8::Value> ThrowException(const std::string& message); | 107 v8::Handle<v8::Value> ThrowException(const std::string& message); |
| 103 | 108 |
| 104 // A map from module names to the JS source for that module. GetSource() | 109 // A map from module names to the JS source for that module. GetSource() |
| 105 // performs a lookup on this map. | 110 // performs a lookup on this map. |
| 106 SourceMap* source_map_; | 111 SourceMap* source_map_; |
| 107 NativeHandlerMap native_handler_map_; | 112 NativeHandlerMap native_handler_map_; |
| 108 bool natives_enabled_; | 113 // When 0, natives are disabled, otherwise indicates how many callers have |
| 114 // pinned natives as enabled. | |
| 115 int natives_enabled_; | |
| 109 }; | 116 }; |
| 110 | 117 |
| 111 #endif // CHROME_RENDERER_MODULE_SYSTEM_H_ | 118 #endif // CHROME_RENDERER_MODULE_SYSTEM_H_ |
| OLD | NEW |