Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: chrome/renderer/module_system.h

Issue 9835039: Make app_custom_bindings.js lazily evaluated so it doesn't execute on every page load. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/renderer/extensions/extension_dispatcher.cc ('k') | chrome/renderer/module_system.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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_;
50 DISALLOW_COPY_AND_ASSIGN(NativesEnabledScope);
51 };
52
42 // |source_map| is a weak pointer. 53 // |source_map| is a weak pointer.
43 explicit ModuleSystem(SourceMap* source_map); 54 explicit ModuleSystem(SourceMap* source_map);
44 virtual ~ModuleSystem(); 55 virtual ~ModuleSystem();
45 56
46 // Require the specified module. This is the equivalent of calling 57 // Require the specified module. This is the equivalent of calling
47 // require('module_name') from the loaded JS files. 58 // require('module_name') from the loaded JS files.
48 void Require(const std::string& module_name); 59 void Require(const std::string& module_name);
49 v8::Handle<v8::Value> Require(const v8::Arguments& args); 60 v8::Handle<v8::Value> Require(const v8::Arguments& args);
50 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args); 61 v8::Handle<v8::Value> RequireForJs(const v8::Arguments& args);
51 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name); 62 v8::Handle<v8::Value> RequireForJsInner(v8::Handle<v8::String> module_name);
52 63
53 // Register |native_handler| as a potential target for requireNative(), so 64 // Register |native_handler| as a potential target for requireNative(), so
54 // calls to requireNative(|name|) from JS will return a new object created by 65 // calls to requireNative(|name|) from JS will return a new object created by
55 // |native_handler|. 66 // |native_handler|.
56 void RegisterNativeHandler(const std::string& name, 67 void RegisterNativeHandler(const std::string& name,
57 scoped_ptr<NativeHandler> native_handler); 68 scoped_ptr<NativeHandler> native_handler);
58 69
59 // Executes |code| in the current context with |name| as the filename. 70 // Executes |code| in the current context with |name| as the filename.
60 void RunString(const std::string& code, const std::string& name); 71 void RunString(const std::string& code, const std::string& name);
61 72
62 // When false |natives_enabled_| causes calls to GetNative() (the basis of 73 // Retrieves the lazily defined field specified by |property|.
63 // requireNative() in JS) to throw an exception. 74 static v8::Handle<v8::Value> LazyFieldGetter(v8::Local<v8::String> property,
64 void set_natives_enabled(bool natives_enabled) { 75 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 76
71 // Make |object|.|field| lazily evaluate to the result of 77 // Make |object|.|field| lazily evaluate to the result of
72 // require(|module_name|)[|module_field|]. 78 // require(|module_name|)[|module_field|].
73 void SetLazyField(v8::Handle<v8::Object> object, 79 void SetLazyField(v8::Handle<v8::Object> object,
74 const std::string& field, 80 const std::string& field,
75 const std::string& module_name, 81 const std::string& module_name,
76 const std::string& module_field); 82 const std::string& module_field);
77 83
78 private: 84 private:
79 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap; 85 typedef std::map<std::string, linked_ptr<NativeHandler> > NativeHandlerMap;
(...skipping 18 matching lines...) Expand all
98 // Wraps |source| in a (function(require, requireNative, exports) {...}). 104 // Wraps |source| in a (function(require, requireNative, exports) {...}).
99 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source); 105 v8::Handle<v8::String> WrapSource(v8::Handle<v8::String> source);
100 106
101 // Throws an exception in the calling JS context. 107 // Throws an exception in the calling JS context.
102 v8::Handle<v8::Value> ThrowException(const std::string& message); 108 v8::Handle<v8::Value> ThrowException(const std::string& message);
103 109
104 // A map from module names to the JS source for that module. GetSource() 110 // A map from module names to the JS source for that module. GetSource()
105 // performs a lookup on this map. 111 // performs a lookup on this map.
106 SourceMap* source_map_; 112 SourceMap* source_map_;
107 NativeHandlerMap native_handler_map_; 113 NativeHandlerMap native_handler_map_;
108 bool natives_enabled_; 114 // When 0, natives are disabled, otherwise indicates how many callers have
115 // pinned natives as enabled.
116 int natives_enabled_;
117
118 DISALLOW_COPY_AND_ASSIGN(ModuleSystem);
109 }; 119 };
110 120
111 #endif // CHROME_RENDERER_MODULE_SYSTEM_H_ 121 #endif // CHROME_RENDERER_MODULE_SYSTEM_H_
OLDNEW
« no previous file with comments | « chrome/renderer/extensions/extension_dispatcher.cc ('k') | chrome/renderer/module_system.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698