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

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

Issue 1192763002: extensions: Use V8 Maybe APIs in NativeHandler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use MaybeLocal Created 5 years, 5 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/renderer/module_system.h" 5 #include "extensions/renderer/module_system.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 188
189 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() { 189 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() {
190 module_system_->natives_enabled_--; 190 module_system_->natives_enabled_--;
191 CHECK_GE(module_system_->natives_enabled_, 0); 191 CHECK_GE(module_system_->natives_enabled_, 0);
192 } 192 }
193 193
194 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) { 194 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) {
195 exception_handler_->HandleUncaughtException(try_catch); 195 exception_handler_->HandleUncaughtException(try_catch);
196 } 196 }
197 197
198 v8::Local<v8::Value> ModuleSystem::Require(const std::string& module_name) { 198 v8::MaybeLocal<v8::Object> ModuleSystem::Require(
199 if (module_name.size() >= v8::String::kMaxLength) 199 const std::string& module_name) {
200 return v8::Undefined(GetIsolate()); 200 v8::Local<v8::String> v8_module_name;
201 if (!ToV8String(GetIsolate(), module_name, &v8_module_name))
202 return v8::MaybeLocal<v8::Object>();
201 v8::EscapableHandleScope handle_scope(GetIsolate()); 203 v8::EscapableHandleScope handle_scope(GetIsolate());
202 return handle_scope.Escape(RequireForJsInner( 204 v8::Local<v8::Value> value = RequireForJsInner(
203 ToV8StringUnsafe(GetIsolate(), module_name.c_str()))); 205 v8_module_name);
not at google - send to devlin 2015/06/24 16:43:03 This should fit on 1 line; run "git cl format" bef
206 if (value.IsEmpty() || !value->IsObject())
207 return v8::MaybeLocal<v8::Object>();
208 return handle_scope.Escape(value.As<v8::Object>());
204 } 209 }
205 210
206 void ModuleSystem::RequireForJs( 211 void ModuleSystem::RequireForJs(
207 const v8::FunctionCallbackInfo<v8::Value>& args) { 212 const v8::FunctionCallbackInfo<v8::Value>& args) {
208 if (!args[0]->IsString()) { 213 if (!args[0]->IsString()) {
209 NOTREACHED() << "require() called with a non-string argument"; 214 NOTREACHED() << "require() called with a non-string argument";
210 return; 215 return;
211 } 216 }
212 v8::Local<v8::String> module_name = args[0].As<v8::String>(); 217 v8::Local<v8::String> module_name = args[0].As<v8::String>();
213 args.GetReturnValue().Set(RequireForJsInner(module_name)); 218 args.GetReturnValue().Set(RequireForJsInner(module_name));
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 return; 392 return;
388 } 393 }
389 std::string name = *v8::String::Utf8Value(v8_module_name); 394 std::string name = *v8::String::Utf8Value(v8_module_name);
390 395
391 // Switch to our v8 context because we need functions created while running 396 // Switch to our v8 context because we need functions created while running
392 // the require()d module to belong to our context, not the current one. 397 // the require()d module to belong to our context, not the current one.
393 v8::Context::Scope context_scope(context); 398 v8::Context::Scope context_scope(context);
394 NativesEnabledScope natives_enabled_scope(module_system); 399 NativesEnabledScope natives_enabled_scope(module_system);
395 400
396 v8::TryCatch try_catch(info.GetIsolate()); 401 v8::TryCatch try_catch(info.GetIsolate());
397 v8::Local<v8::Value> module_value = (module_system->*require_function)(name); 402 v8::Local<v8::Value> module_value;
398 if (try_catch.HasCaught()) { 403 if (!(module_system->*require_function)(name).ToLocal(&module_value)) {
399 module_system->HandleException(try_catch); 404 module_system->HandleException(try_catch);
400 return; 405 return;
401 } 406 }
402 if (module_value.IsEmpty() || !module_value->IsObject()) {
403 // require_function will have already logged this, we don't need to.
404 return;
405 }
406 407
407 v8::Local<v8::Object> module = v8::Local<v8::Object>::Cast(module_value); 408 v8::Local<v8::Object> module = v8::Local<v8::Object>::Cast(module_value);
408 v8::Local<v8::Value> field_value; 409 v8::Local<v8::Value> field_value;
409 if (!GetProperty(context, parameters, kModuleField, &field_value)) { 410 if (!GetProperty(context, parameters, kModuleField, &field_value)) {
410 module_system->HandleException(try_catch); 411 module_system->HandleException(try_catch);
411 return; 412 return;
412 } 413 }
413 v8::Local<v8::String> field; 414 v8::Local<v8::String> field;
414 if (!field_value->ToString(context).ToLocal(&field)) { 415 if (!field_value->ToString(context).ToLocal(&field)) {
415 module_system->HandleException(try_catch); 416 module_system->HandleException(try_catch);
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 if (!source_map_->Contains(module_name)) 529 if (!source_map_->Contains(module_name))
529 return v8::Undefined(GetIsolate()); 530 return v8::Undefined(GetIsolate());
530 return handle_scope.Escape( 531 return handle_scope.Escape(
531 v8::Local<v8::Value>(source_map_->GetSource(GetIsolate(), module_name))); 532 v8::Local<v8::Value>(source_map_->GetSource(GetIsolate(), module_name)));
532 } 533 }
533 534
534 void ModuleSystem::RequireNative( 535 void ModuleSystem::RequireNative(
535 const v8::FunctionCallbackInfo<v8::Value>& args) { 536 const v8::FunctionCallbackInfo<v8::Value>& args) {
536 CHECK_EQ(1, args.Length()); 537 CHECK_EQ(1, args.Length());
537 std::string native_name = *v8::String::Utf8Value(args[0]); 538 std::string native_name = *v8::String::Utf8Value(args[0]);
538 args.GetReturnValue().Set(RequireNativeFromString(native_name)); 539 v8::Local<v8::Object> object;
540 if (RequireNativeFromString(native_name).ToLocal(&object))
541 args.GetReturnValue().Set(object);
539 } 542 }
540 543
541 v8::Local<v8::Value> ModuleSystem::RequireNativeFromString( 544 v8::MaybeLocal<v8::Object> ModuleSystem::RequireNativeFromString(
542 const std::string& native_name) { 545 const std::string& native_name) {
543 if (natives_enabled_ == 0) { 546 if (natives_enabled_ == 0) {
544 // HACK: if in test throw exception so that we can test the natives-disabled 547 // HACK: if in test throw exception so that we can test the natives-disabled
545 // logic; however, under normal circumstances, this is programmer error so 548 // logic; however, under normal circumstances, this is programmer error so
546 // we could crash. 549 // we could crash.
547 if (exception_handler_) { 550 if (exception_handler_) {
548 return GetIsolate()->ThrowException( 551 GetIsolate()->ThrowException(
549 ToV8StringUnsafe(GetIsolate(), "Natives disabled")); 552 ToV8StringUnsafe(GetIsolate(), "Natives disabled"));
553 return v8::MaybeLocal<v8::Object>();
550 } 554 }
551 Fatal(context_, "Natives disabled for requireNative(" + native_name + ")"); 555 Fatal(context_, "Natives disabled for requireNative(" + native_name + ")");
552 return v8::Undefined(GetIsolate()); 556 return v8::MaybeLocal<v8::Object>();
553 } 557 }
554 558
555 if (overridden_native_handlers_.count(native_name) > 0u) { 559 if (overridden_native_handlers_.count(native_name) > 0u) {
556 return RequireForJsInner( 560 v8::Local<v8::Value> value = RequireForJsInner(
557 ToV8StringUnsafe(GetIsolate(), native_name.c_str())); 561 ToV8StringUnsafe(GetIsolate(), native_name.c_str()));
562 if (value.IsEmpty() || !value->IsObject())
563 return v8::MaybeLocal<v8::Object>();
not at google - send to devlin 2015/06/24 16:43:03 This should probably be a DCHECK since the code is
564 return value.As<v8::Object>();
558 } 565 }
559 566
560 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); 567 NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
561 if (i == native_handler_map_.end()) { 568 if (i == native_handler_map_.end()) {
562 Fatal(context_, 569 Fatal(context_,
563 "Couldn't find native for requireNative(" + native_name + ")"); 570 "Couldn't find native for requireNative(" + native_name + ")");
564 return v8::Undefined(GetIsolate()); 571 return v8::MaybeLocal<v8::Object>();
565 } 572 }
566 return i->second->NewInstance(); 573 return i->second->NewInstance();
567 } 574 }
568 575
569 void ModuleSystem::RequireAsync( 576 void ModuleSystem::RequireAsync(
570 const v8::FunctionCallbackInfo<v8::Value>& args) { 577 const v8::FunctionCallbackInfo<v8::Value>& args) {
571 CHECK_EQ(1, args.Length()); 578 CHECK_EQ(1, args.Length());
572 std::string module_name = *v8::String::Utf8Value(args[0]); 579 std::string module_name = *v8::String::Utf8Value(args[0]);
573 v8::Local<v8::Context> v8_context = context_->v8_context(); 580 v8::Local<v8::Context> v8_context = context_->v8_context();
574 v8::Local<v8::Promise::Resolver> resolver( 581 v8::Local<v8::Promise::Resolver> resolver(
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 745
739 void ModuleSystem::ClobberExistingNativeHandler(const std::string& name) { 746 void ModuleSystem::ClobberExistingNativeHandler(const std::string& name) {
740 NativeHandlerMap::iterator existing_handler = native_handler_map_.find(name); 747 NativeHandlerMap::iterator existing_handler = native_handler_map_.find(name);
741 if (existing_handler != native_handler_map_.end()) { 748 if (existing_handler != native_handler_map_.end()) {
742 clobbered_native_handlers_.push_back(existing_handler->second); 749 clobbered_native_handlers_.push_back(existing_handler->second);
743 native_handler_map_.erase(existing_handler); 750 native_handler_map_.erase(existing_handler);
744 } 751 }
745 } 752 }
746 753
747 } // namespace extensions 754 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698