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

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

Issue 11571014: Lazy load chrome.* APIs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more progress Created 7 years, 11 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
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/extensions/module_system.h" 5 #include "chrome/renderer/extensions/module_system.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup pression.h" 8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup pression.h"
9 9
10 namespace { 10 namespace {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 stack_trace.assign(*stack_value, stack_value.length()); 96 stack_trace.assign(*stack_value, stack_value.length());
97 else 97 else
98 stack_trace = "<could not convert stack trace to string>"; 98 stack_trace = "<could not convert stack trace to string>";
99 } 99 }
100 100
101 LOG(ERROR) << "[" << resource_name << "(" << message->GetLineNumber() << ")] " 101 LOG(ERROR) << "[" << resource_name << "(" << message->GetLineNumber() << ")] "
102 << error_message 102 << error_message
103 << "{" << stack_trace << "}"; 103 << "{" << stack_trace << "}";
104 } 104 }
105 105
106 void ModuleSystem::Require(const std::string& module_name) { 106 v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) {
107 v8::HandleScope handle_scope; 107 v8::HandleScope handle_scope;
108 RequireForJsInner(v8::String::New(module_name.c_str())); 108 return handle_scope.Close(
109 RequireForJsInner(v8::String::New(module_name.c_str())));
109 } 110 }
110 111
111 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { 112 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) {
112 v8::HandleScope handle_scope; 113 v8::HandleScope handle_scope;
113 v8::Handle<v8::String> module_name = args[0]->ToString(); 114 v8::Handle<v8::String> module_name = args[0]->ToString();
114 return handle_scope.Close(RequireForJsInner(module_name)); 115 return handle_scope.Close(RequireForJsInner(module_name));
115 } 116 }
116 117
117 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( 118 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner(
118 v8::Handle<v8::String> module_name) { 119 v8::Handle<v8::String> module_name) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 const std::string& method_name) { 162 const std::string& method_name) {
162 v8::HandleScope handle_scope; 163 v8::HandleScope handle_scope;
163 v8::Local<v8::Value> module = 164 v8::Local<v8::Value> module =
164 v8::Local<v8::Value>::New( 165 v8::Local<v8::Value>::New(
165 RequireForJsInner(v8::String::New(module_name.c_str()))); 166 RequireForJsInner(v8::String::New(module_name.c_str())));
166 if (module.IsEmpty() || !module->IsObject()) 167 if (module.IsEmpty() || !module->IsObject())
167 return; 168 return;
168 v8::Local<v8::Value> value = 169 v8::Local<v8::Value> value =
169 v8::Handle<v8::Object>::Cast(module)->Get( 170 v8::Handle<v8::Object>::Cast(module)->Get(
170 v8::String::New(method_name.c_str())); 171 v8::String::New(method_name.c_str()));
172 CallModuleMethod(value, 0, NULL);
173 }
174
175 v8::Handle<v8::Value> ModuleSystem::CallModuleMethod(
176 v8::Local<v8::Value> value,
177 int argc,
178 v8::Handle<v8::Value> argv[]) {
171 if (value.IsEmpty() || !value->IsFunction()) 179 if (value.IsEmpty() || !value->IsFunction())
172 return; 180 return v8::Handle<v8::Value>();
173 v8::Handle<v8::Function> func = 181 v8::Handle<v8::Function> func =
174 v8::Handle<v8::Function>::Cast(value); 182 v8::Handle<v8::Function>::Cast(value);
175 // TODO(jeremya/koz): refer to context_ here, not the current context. 183 // TODO(jeremya/koz): refer to context_ here, not the current context.
176 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); 184 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
185 v8::Handle<v8::Value> ret;
177 { 186 {
178 WebKit::WebScopedMicrotaskSuppression suppression; 187 WebKit::WebScopedMicrotaskSuppression suppression;
179 v8::TryCatch try_catch; 188 v8::TryCatch try_catch;
180 try_catch.SetCaptureMessage(true); 189 try_catch.SetCaptureMessage(true);
181 func->Call(global, 0, NULL); 190 ret = func->Call(global, argc, argv);
182 if (try_catch.HasCaught()) 191 if (try_catch.HasCaught())
183 HandleException(try_catch); 192 HandleException(try_catch);
184 } 193 }
194 return ret;
195 }
196
197 bool ModuleSystem::HasNativeHandler(const std::string& name) {
198 return native_handler_map_.find(name) != native_handler_map_.end();
185 } 199 }
186 200
187 void ModuleSystem::RegisterNativeHandler(const std::string& name, 201 void ModuleSystem::RegisterNativeHandler(const std::string& name,
188 scoped_ptr<NativeHandler> native_handler) { 202 scoped_ptr<NativeHandler> native_handler) {
189 native_handler_map_[name] = 203 native_handler_map_[name] =
190 linked_ptr<NativeHandler>(native_handler.release()); 204 linked_ptr<NativeHandler>(native_handler.release());
191 } 205 }
192 206
193 void ModuleSystem::OverrideNativeHandler(const std::string& name) { 207 void ModuleSystem::OverrideNativeHandler(const std::string& name) {
194 overridden_native_handlers_.insert(name); 208 overridden_native_handlers_.insert(name);
195 } 209 }
196 210
197 void ModuleSystem::RunString(const std::string& code, const std::string& name) { 211 void ModuleSystem::RunString(const std::string& code, const std::string& name) {
198 v8::HandleScope handle_scope; 212 v8::HandleScope handle_scope;
199 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str())); 213 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str()));
200 } 214 }
201 215
202 // static 216 // static
217 v8::Handle<v8::Object> ModuleSystem::GetModule(
218 ModuleSystem* module_system, v8::Handle<v8::Object> parameters) {
219 NativesEnabledScope scope(module_system);
220 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(
221 module_system->RequireForJsInner(
222 parameters->Get(v8::String::New(kModuleName))->ToString()));
223 return module;
224 }
225
226 // static
227 v8::Handle<v8::Object> ModuleSystem::GetNativeModule(
228 ModuleSystem* module_system, v8::Handle<v8::Object> parameters) {
229 NativesEnabledScope scope(module_system);
230 std::string name = *v8::String::AsciiValue(
231 parameters->Get(v8::String::New(kModuleName))->ToString());
232 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(
233 module_system->GetNativeFromString(name));
234 return module;
235 }
236
237 // static
238 v8::Handle<v8::Value> ModuleSystem::NativeLazyFieldGetter(
239 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
240 return BaseLazyFieldGetter(property, info, &ModuleSystem::GetNativeModule);
241 }
242
243 // static
203 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter( 244 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter(
204 v8::Local<v8::String> property, const v8::AccessorInfo& info) { 245 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
246 return BaseLazyFieldGetter(property, info, &ModuleSystem::GetModule);
247 }
248
249 // static
250 v8::Handle<v8::Value> ModuleSystem::BaseLazyFieldGetter(
251 v8::Local<v8::String> property,
252 const v8::AccessorInfo& info,
253 GetModuleFunc get_module) {
205 CHECK(!info.Data().IsEmpty()); 254 CHECK(!info.Data().IsEmpty());
206 CHECK(info.Data()->IsObject()); 255 CHECK(info.Data()->IsObject());
207 v8::HandleScope handle_scope; 256 v8::HandleScope handle_scope;
208 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); 257 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data());
209 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); 258 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
210 v8::Handle<v8::Value> module_system_value = 259 v8::Handle<v8::Value> module_system_value =
211 global->GetHiddenValue(v8::String::New(kModuleSystem)); 260 global->GetHiddenValue(v8::String::New(kModuleSystem));
212 if (module_system_value->IsUndefined()) { 261 if (module_system_value->IsUndefined()) {
213 // ModuleSystem has been deleted. 262 // ModuleSystem has been deleted.
214 return v8::Undefined(); 263 return v8::Undefined();
215 } 264 }
216 ModuleSystem* module_system = static_cast<ModuleSystem*>( 265 ModuleSystem* module_system = static_cast<ModuleSystem*>(
217 v8::Handle<v8::External>::Cast(module_system_value)->Value()); 266 v8::Handle<v8::External>::Cast(module_system_value)->Value());
218 267
219 v8::Handle<v8::Object> module; 268 v8::Handle<v8::Object> module = get_module(module_system, parameters);
220 {
221 NativesEnabledScope scope(module_system);
222 module = v8::Handle<v8::Object>::Cast(module_system->RequireForJsInner(
223 parameters->Get(v8::String::New(kModuleName))->ToString()));
224 }
225 if (module.IsEmpty()) 269 if (module.IsEmpty())
226 return handle_scope.Close(v8::Handle<v8::Value>()); 270 return handle_scope.Close(v8::Handle<v8::Value>());
227 271
228 v8::Handle<v8::String> field = 272 v8::Handle<v8::String> field =
229 parameters->Get(v8::String::New(kModuleField))->ToString(); 273 parameters->Get(v8::String::New(kModuleField))->ToString();
230 274
231 return handle_scope.Close(module->Get(field)); 275 return handle_scope.Close(module->Get(field));
232 } 276 }
233 277
234 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, 278 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
235 const std::string& field, 279 const std::string& field,
236 const std::string& module_name, 280 const std::string& module_name,
237 const std::string& module_field) { 281 const std::string& module_field) {
282 SetLazyField(object, field, module_name, module_field,
283 &ModuleSystem::LazyFieldGetter);
284 }
285
286 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
287 const std::string& field,
288 const std::string& module_name,
289 const std::string& module_field,
290 v8::AccessorGetter getter) {
238 v8::HandleScope handle_scope; 291 v8::HandleScope handle_scope;
239 v8::Handle<v8::Object> parameters = v8::Object::New(); 292 v8::Handle<v8::Object> parameters = v8::Object::New();
240 parameters->Set(v8::String::New(kModuleName), 293 parameters->Set(v8::String::New(kModuleName),
241 v8::String::New(module_name.c_str())); 294 v8::String::New(module_name.c_str()));
242 parameters->Set(v8::String::New(kModuleField), 295 parameters->Set(v8::String::New(kModuleField),
243 v8::String::New(module_field.c_str())); 296 v8::String::New(module_field.c_str()));
244 297
245 object->SetAccessor(v8::String::New(field.c_str()), 298 object->SetAccessor(v8::String::New(field.c_str()),
246 &ModuleSystem::LazyFieldGetter, 299 getter,
247 NULL, 300 NULL,
248 parameters); 301 parameters);
249 } 302 }
250 303
304 void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object,
305 const std::string& field,
306 const std::string& module_name,
307 const std::string& module_field) {
308 SetLazyField(object, field, module_name, module_field,
309 &ModuleSystem::NativeLazyFieldGetter);
310 }
311
251 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, 312 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
252 v8::Handle<v8::String> name) { 313 v8::Handle<v8::String> name) {
253 v8::HandleScope handle_scope; 314 v8::HandleScope handle_scope;
254 WebKit::WebScopedMicrotaskSuppression suppression; 315 WebKit::WebScopedMicrotaskSuppression suppression;
255 v8::Handle<v8::Value> result; 316 v8::Handle<v8::Value> result;
256 v8::TryCatch try_catch; 317 v8::TryCatch try_catch;
257 try_catch.SetCaptureMessage(true); 318 try_catch.SetCaptureMessage(true);
258 v8::Handle<v8::Script> script(v8::Script::New(code, name)); 319 v8::Handle<v8::Script> script(v8::Script::New(code, name));
259 if (try_catch.HasCaught()) { 320 if (try_catch.HasCaught()) {
260 HandleException(try_catch); 321 HandleException(try_catch);
(...skipping 11 matching lines...) Expand all
272 v8::Handle<v8::String> source_name) { 333 v8::Handle<v8::String> source_name) {
273 v8::HandleScope handle_scope; 334 v8::HandleScope handle_scope;
274 std::string module_name = *v8::String::AsciiValue(source_name); 335 std::string module_name = *v8::String::AsciiValue(source_name);
275 if (!source_map_->Contains(module_name)) 336 if (!source_map_->Contains(module_name))
276 return v8::Undefined(); 337 return v8::Undefined();
277 return handle_scope.Close(source_map_->GetSource(module_name)); 338 return handle_scope.Close(source_map_->GetSource(module_name));
278 } 339 }
279 340
280 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) { 341 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) {
281 CHECK_EQ(1, args.Length()); 342 CHECK_EQ(1, args.Length());
343 std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
344 return GetNativeFromString(native_name);
345 }
346
347 v8::Handle<v8::Value> ModuleSystem::GetNativeFromString(
348 const std::string& native_name) {
282 if (natives_enabled_ == 0) 349 if (natives_enabled_ == 0)
283 return ThrowException("Natives disabled"); 350 return ThrowException("Natives disabled");
284 std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
285 if (overridden_native_handlers_.count(native_name) > 0u) 351 if (overridden_native_handlers_.count(native_name) > 0u)
286 return RequireForJs(args); 352 return RequireForJsInner(v8::String::New(native_name.c_str()));
287 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); 353 NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
288 if (i == native_handler_map_.end()) 354 if (i == native_handler_map_.end())
289 return v8::Undefined(); 355 return v8::Undefined();
290 return i->second->NewInstance(); 356 return i->second->NewInstance();
291 } 357 }
292 358
293 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { 359 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) {
294 v8::HandleScope handle_scope; 360 v8::HandleScope handle_scope;
295 v8::Handle<v8::String> left = 361 v8::Handle<v8::String> left =
296 v8::String::New("(function(require, requireNative, exports) {"); 362 v8::String::New("(function(require, requireNative, exports) {");
297 v8::Handle<v8::String> right = v8::String::New("\n})"); 363 v8::Handle<v8::String> right = v8::String::New("\n})");
298 return handle_scope.Close( 364 return handle_scope.Close(
299 v8::String::Concat(left, v8::String::Concat(source, right))); 365 v8::String::Concat(left, v8::String::Concat(source, right)));
300 } 366 }
301 367
302 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { 368 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) {
303 return v8::ThrowException(v8::String::New(message.c_str())); 369 return v8::ThrowException(v8::String::New(message.c_str()));
304 } 370 }
305 371
306 } // extensions 372 } // extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698