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

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

Issue 9386001: Implement a module system for the extension bindings JS. (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/module_system.h ('k') | chrome/renderer/module_system_unittest.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 #include "chrome/renderer/module_system.h" 5 #include "chrome/renderer/module_system.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "chrome/renderer/static_v8_external_string_resource.h"
9 #include "grit/renderer_resources.h"
10 #include "ui/base/resource/resource_bundle.h"
11 8
12 namespace { 9 namespace {
13 10
14 v8::Handle<v8::String> GetResource(int resourceId) {
15 const ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance();
16 return v8::String::NewExternal(new StaticV8ExternalAsciiStringResource(
17 resource_bundle.GetRawDataResource(resourceId)));
18 }
19
20 } // namespace 11 } // namespace
21 12
22 ModuleSystem::ModuleSystem(const std::map<std::string, std::string>* source_map) 13 ModuleSystem::ModuleSystem(SourceMap* source_map)
23 : source_map_(source_map) { 14 : source_map_(source_map),
24 RouteFunction("Run", 15 natives_enabled_(true) {
25 base::Bind(&ModuleSystem::Run, base::Unretained(this))); 16 RouteFunction("require",
26 RouteFunction("GetSource", 17 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this)));
27 base::Bind(&ModuleSystem::GetSource, base::Unretained(this))); 18 RouteFunction("requireNative",
28 RouteFunction("GetNative",
29 base::Bind(&ModuleSystem::GetNative, base::Unretained(this))); 19 base::Bind(&ModuleSystem::GetNative, base::Unretained(this)));
20
21 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
22 global->SetHiddenValue(v8::String::New("modules"), v8::Object::New());
30 } 23 }
31 24
32 ModuleSystem::~ModuleSystem() { 25 ModuleSystem::~ModuleSystem() {
33 } 26 }
34 27
35 void ModuleSystem::Require(const std::string& module_name) { 28 void ModuleSystem::Require(const std::string& module_name) {
36 EnsureRequireLoaded();
37 v8::HandleScope handle_scope; 29 v8::HandleScope handle_scope;
38 v8::Handle<v8::Value> argv[] = { 30 RequireForJsInner(v8::String::New(module_name.c_str()));
39 v8::String::New(module_name.c_str()), 31 }
32
33 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) {
34 v8::HandleScope handle_scope;
35 v8::Handle<v8::String> module_name = args[0]->ToString();
36 return handle_scope.Close(RequireForJsInner(module_name));
37 }
38
39 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner(
40 v8::Handle<v8::String> module_name) {
41 v8::HandleScope handle_scope;
42 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
43 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(
44 global->GetHiddenValue(v8::String::New("modules"))));
45 v8::Handle<v8::Value> exports(modules->Get(module_name));
46 if (!exports->IsUndefined())
47 return handle_scope.Close(exports);
48 v8::Handle<v8::Value> source(GetSource(module_name));
49 if (source->IsUndefined())
50 return handle_scope.Close(v8::Undefined());
51 v8::Handle<v8::String> wrapped_source(WrapSource(
52 v8::Handle<v8::String>::Cast(source)));
53 v8::Handle<v8::Function> func =
54 v8::Handle<v8::Function>::Cast(RunString(wrapped_source, module_name));
55 exports = v8::Object::New();
56 v8::Handle<v8::Object> natives(NewInstance());
57 v8::Handle<v8::Value> args[] = {
58 natives->Get(v8::String::NewSymbol("require")),
59 natives->Get(v8::String::NewSymbol("requireNative")),
60 exports,
40 }; 61 };
41 require_->Call(v8::Context::GetCurrent()->Global(), arraysize(argv), argv); 62 func->Call(global, 3, args);
63 modules->Set(module_name, exports);
64 return handle_scope.Close(exports);
42 } 65 }
43 66
44 void ModuleSystem::RegisterNativeHandler(const std::string& name, 67 void ModuleSystem::RegisterNativeHandler(const std::string& name,
45 scoped_ptr<NativeHandler> native_handler) { 68 scoped_ptr<NativeHandler> native_handler) {
46 native_handler_map_[name] = 69 native_handler_map_[name] =
47 linked_ptr<NativeHandler>(native_handler.release()); 70 linked_ptr<NativeHandler>(native_handler.release());
48 } 71 }
49 72
50 void ModuleSystem::EnsureRequireLoaded() { 73 void ModuleSystem::RunString(const std::string& code, const std::string& name) {
51 v8::HandleScope handle_scope; 74 v8::HandleScope handle_scope;
52 if (!require_.IsEmpty()) 75 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str()));
53 return;
54 v8::Handle<v8::Object> bootstrap = NewInstance();
55 v8::Handle<v8::Value> result = RunString(GetResource(IDR_REQUIRE_JS));
56 v8::Handle<v8::Function> require_factory =
57 v8::Handle<v8::Function>::Cast(result);
58 CHECK(!require_factory.IsEmpty())
59 << "require.js should define a function that returns a require() "
60 << "function";
61 v8::Handle<v8::Value> argv[] = {
62 bootstrap,
63 };
64 v8::Handle<v8::Value> require = require_factory->Call(
65 v8::Context::GetCurrent()->Global(), arraysize(argv), argv);
66 require_ = v8::Persistent<v8::Function>::New(
67 v8::Handle<v8::Function>::Cast(require));
68 } 76 }
69 77
70 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code) { 78
79 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
80 v8::Handle<v8::String> name) {
71 v8::HandleScope handle_scope; 81 v8::HandleScope handle_scope;
72 return handle_scope.Close(v8::Script::New(code)->Run()); 82 return handle_scope.Close(v8::Script::New(code, name)->Run());
73 } 83 }
74 84
75 v8::Handle<v8::Value> ModuleSystem::Run(const v8::Arguments& args) { 85 v8::Handle<v8::Value> ModuleSystem::GetSource(
76 CHECK_EQ(1, args.Length()); 86 v8::Handle<v8::String> source_name) {
77 v8::HandleScope handle_scope; 87 v8::HandleScope handle_scope;
78 return handle_scope.Close(v8::Script::New(args[0]->ToString())->Run()); 88 std::string module_name = *v8::String::AsciiValue(source_name);
79 } 89 if (!source_map_->Contains(module_name))
80
81 v8::Handle<v8::Value> ModuleSystem::GetSource(const v8::Arguments& args) {
82 CHECK_EQ(1, args.Length());
83 std::string module_name = *v8::String::AsciiValue(args[0]->ToString());
84 std::map<std::string, std::string>::const_iterator p =
85 source_map_->find(module_name);
86 if (p == source_map_->end())
87 return v8::Undefined(); 90 return v8::Undefined();
88 return v8::String::New(p->second.c_str()); 91 return handle_scope.Close(source_map_->GetSource(module_name));
89 } 92 }
90 93
91 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) { 94 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) {
92 CHECK_EQ(1, args.Length()); 95 CHECK_EQ(1, args.Length());
96 if (!natives_enabled_)
97 return ThrowException("Natives disabled");
93 std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); 98 std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
94 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); 99 NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
95 if (i == native_handler_map_.end()) 100 if (i == native_handler_map_.end())
96 return v8::Undefined(); 101 return v8::Undefined();
97 return i->second->NewInstance(); 102 return i->second->NewInstance();
98 } 103 }
104
105 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) {
106 v8::HandleScope handle_scope;
107 v8::Handle<v8::String> left =
108 v8::String::New("(function(require, requireNative, exports) {");
109 v8::Handle<v8::String> right = v8::String::New("\n})");
110 return handle_scope.Close(
111 v8::String::Concat(left, v8::String::Concat(source, right)));
112 }
113
114 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) {
115 return v8::ThrowException(v8::String::New(message.c_str()));
116 }
OLDNEW
« no previous file with comments | « chrome/renderer/module_system.h ('k') | chrome/renderer/module_system_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698