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

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

Issue 9657026: Revert 125801 - Implement a module system for the extension bindings JS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: 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" 8 #include "chrome/renderer/static_v8_external_string_resource.h"
9 #include "grit/renderer_resources.h" 9 #include "grit/renderer_resources.h"
10 #include "ui/base/resource/resource_bundle.h" 10 #include "ui/base/resource/resource_bundle.h"
11 11
12 namespace { 12 namespace {
13 13
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
14 } // namespace 20 } // namespace
15 21
16 ModuleSystem::ModuleSystem(SourceMap* source_map) 22 ModuleSystem::ModuleSystem(const std::map<std::string, std::string>* source_map)
17 : source_map_(source_map), 23 : source_map_(source_map) {
18 natives_enabled_(true) { 24 RouteFunction("Run",
25 base::Bind(&ModuleSystem::Run, base::Unretained(this)));
19 RouteFunction("GetSource", 26 RouteFunction("GetSource",
20 base::Bind(&ModuleSystem::GetSource, base::Unretained(this))); 27 base::Bind(&ModuleSystem::GetSource, base::Unretained(this)));
21 RouteFunction("GetNative", 28 RouteFunction("GetNative",
22 base::Bind(&ModuleSystem::GetNative, base::Unretained(this))); 29 base::Bind(&ModuleSystem::GetNative, base::Unretained(this)));
23 } 30 }
24 31
25 ModuleSystem::~ModuleSystem() { 32 ModuleSystem::~ModuleSystem() {
26 } 33 }
27 34
28 void ModuleSystem::Require(const std::string& module_name) { 35 void ModuleSystem::Require(const std::string& module_name) {
29 EnsureRequireLoaded(); 36 EnsureRequireLoaded();
30 v8::HandleScope handle_scope; 37 v8::HandleScope handle_scope;
31 v8::Handle<v8::Value> argv[] = { 38 v8::Handle<v8::Value> argv[] = {
32 v8::String::New(module_name.c_str()), 39 v8::String::New(module_name.c_str()),
33 }; 40 };
34 require_->Call(v8::Context::GetCurrent()->Global(), arraysize(argv), argv); 41 require_->Call(v8::Context::GetCurrent()->Global(), arraysize(argv), argv);
35 } 42 }
36 43
37 void ModuleSystem::RegisterNativeHandler(const std::string& name, 44 void ModuleSystem::RegisterNativeHandler(const std::string& name,
38 scoped_ptr<NativeHandler> native_handler) { 45 scoped_ptr<NativeHandler> native_handler) {
39 native_handler_map_[name] = 46 native_handler_map_[name] =
40 linked_ptr<NativeHandler>(native_handler.release()); 47 linked_ptr<NativeHandler>(native_handler.release());
41 } 48 }
42 49
43 void ModuleSystem::RunString(const std::string& code, const std::string& name) {
44 v8::HandleScope handle_scope;
45 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str()));
46 }
47
48 void ModuleSystem::EnsureRequireLoaded() { 50 void ModuleSystem::EnsureRequireLoaded() {
49 v8::HandleScope handle_scope; 51 v8::HandleScope handle_scope;
50 if (!require_.IsEmpty()) 52 if (!require_.IsEmpty())
51 return; 53 return;
52 v8::Handle<v8::Object> bootstrap = NewInstance(); 54 v8::Handle<v8::Object> bootstrap = NewInstance();
53 // NOTE v8 takes ownership of the StaticV8ExternalAsciiStringResource. 55 v8::Handle<v8::Value> result = RunString(GetResource(IDR_REQUIRE_JS));
54 v8::Handle<v8::String> source = v8::String::NewExternal(
55 new StaticV8ExternalAsciiStringResource(GetResource(IDR_REQUIRE_JS)));
56 v8::Handle<v8::Value> result = RunString(source,
57 v8::String::New("require.js"));
58 v8::Handle<v8::Function> require_factory = 56 v8::Handle<v8::Function> require_factory =
59 v8::Handle<v8::Function>::Cast(result); 57 v8::Handle<v8::Function>::Cast(result);
60 CHECK(!require_factory.IsEmpty()) 58 CHECK(!require_factory.IsEmpty())
61 << "require.js should define a function that returns a require() " 59 << "require.js should define a function that returns a require() "
62 << "function"; 60 << "function";
63 v8::Handle<v8::Value> argv[] = { 61 v8::Handle<v8::Value> argv[] = {
64 bootstrap, 62 bootstrap,
65 }; 63 };
66 v8::Handle<v8::Value> require = require_factory->Call( 64 v8::Handle<v8::Value> require = require_factory->Call(
67 v8::Context::GetCurrent()->Global(), arraysize(argv), argv); 65 v8::Context::GetCurrent()->Global(), arraysize(argv), argv);
68 require_ = v8::Persistent<v8::Function>::New( 66 require_ = v8::Persistent<v8::Function>::New(
69 v8::Handle<v8::Function>::Cast(require)); 67 v8::Handle<v8::Function>::Cast(require));
70 } 68 }
71 69
72 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, 70 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code) {
73 v8::Handle<v8::String> name) {
74 v8::HandleScope handle_scope; 71 v8::HandleScope handle_scope;
75 return handle_scope.Close(v8::Script::New(code, name)->Run()); 72 return handle_scope.Close(v8::Script::New(code)->Run());
73 }
74
75 v8::Handle<v8::Value> ModuleSystem::Run(const v8::Arguments& args) {
76 CHECK_EQ(1, args.Length());
77 v8::HandleScope handle_scope;
78 return handle_scope.Close(v8::Script::New(args[0]->ToString())->Run());
76 } 79 }
77 80
78 v8::Handle<v8::Value> ModuleSystem::GetSource(const v8::Arguments& args) { 81 v8::Handle<v8::Value> ModuleSystem::GetSource(const v8::Arguments& args) {
79 CHECK_EQ(1, args.Length()); 82 CHECK_EQ(1, args.Length());
80
81 v8::HandleScope handle_scope;
82 std::string module_name = *v8::String::AsciiValue(args[0]->ToString()); 83 std::string module_name = *v8::String::AsciiValue(args[0]->ToString());
83 if (!source_map_->Contains(module_name)) 84 std::map<std::string, std::string>::const_iterator p =
85 source_map_->find(module_name);
86 if (p == source_map_->end())
84 return v8::Undefined(); 87 return v8::Undefined();
85 return handle_scope.Close(source_map_->GetSource(module_name)); 88 return v8::String::New(p->second.c_str());
86 } 89 }
87 90
88 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) { 91 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) {
89 CHECK_EQ(1, args.Length()); 92 CHECK_EQ(1, args.Length());
90 if (!natives_enabled_)
91 return ThrowException("Natives disabled");
92 std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); 93 std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
93 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); 94 NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
94 if (i == native_handler_map_.end()) 95 if (i == native_handler_map_.end())
95 return v8::Undefined(); 96 return v8::Undefined();
96 return i->second->NewInstance(); 97 return i->second->NewInstance();
97 } 98 }
98
99 base::StringPiece ModuleSystem::GetResource(int resourceId) {
100 const ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance();
101 return resource_bundle.GetRawDataResource(resourceId);
102 }
103
104 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) {
105 return v8::ThrowException(v8::String::New(message.c_str()));
106 }
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