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