OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "bindings/core/v8/V8ThrowDOMException.h" |
| 6 |
| 7 #include "bindings/core/v8/ToV8ForCore.h" |
| 8 #include "bindings/core/v8/V8PrivateProperty.h" |
| 9 #include "bindings/core/v8/V8ThrowException.h" |
| 10 #include "core/dom/DOMException.h" |
| 11 |
| 12 namespace blink { |
| 13 |
| 14 namespace { |
| 15 |
| 16 void DomExceptionStackGetter(v8::Local<v8::Name> name, |
| 17 const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 18 v8::Isolate* isolate = info.GetIsolate(); |
| 19 v8::Local<v8::Value> value; |
| 20 if (info.Data() |
| 21 .As<v8::Object>() |
| 22 ->Get(isolate->GetCurrentContext(), V8AtomicString(isolate, "stack")) |
| 23 .ToLocal(&value)) |
| 24 V8SetReturnValue(info, value); |
| 25 } |
| 26 |
| 27 void DomExceptionStackSetter(v8::Local<v8::Name> name, |
| 28 v8::Local<v8::Value> value, |
| 29 const v8::PropertyCallbackInfo<void>& info) { |
| 30 v8::Maybe<bool> unused = info.Data().As<v8::Object>()->Set( |
| 31 info.GetIsolate()->GetCurrentContext(), |
| 32 V8AtomicString(info.GetIsolate(), "stack"), value); |
| 33 ALLOW_UNUSED_LOCAL(unused); |
| 34 } |
| 35 |
| 36 } // namespace |
| 37 |
| 38 v8::Local<v8::Value> V8ThrowDOMException::CreateDOMException( |
| 39 v8::Isolate* isolate, |
| 40 ExceptionCode exception_code, |
| 41 const String& sanitized_message, |
| 42 const String& unsanitized_message) { |
| 43 DCHECK_GT(exception_code, 0); |
| 44 DCHECK(exception_code == kSecurityError || unsanitized_message.IsNull()); |
| 45 |
| 46 if (isolate->IsExecutionTerminating()) |
| 47 return v8::Local<v8::Value>(); |
| 48 |
| 49 switch (exception_code) { |
| 50 case kV8Error: |
| 51 return V8ThrowException::CreateError(isolate, sanitized_message); |
| 52 case kV8TypeError: |
| 53 return V8ThrowException::CreateTypeError(isolate, sanitized_message); |
| 54 case kV8RangeError: |
| 55 return V8ThrowException::CreateRangeError(isolate, sanitized_message); |
| 56 case kV8SyntaxError: |
| 57 return V8ThrowException::CreateSyntaxError(isolate, sanitized_message); |
| 58 case kV8ReferenceError: |
| 59 return V8ThrowException::CreateReferenceError(isolate, sanitized_message); |
| 60 } |
| 61 |
| 62 DOMException* dom_exception = DOMException::Create( |
| 63 exception_code, sanitized_message, unsanitized_message); |
| 64 v8::Local<v8::Object> exception_obj = |
| 65 ToV8(dom_exception, isolate->GetCurrentContext()->Global(), isolate) |
| 66 .As<v8::Object>(); |
| 67 // Attach an Error object to the DOMException. This is then lazily used to |
| 68 // get the stack value. |
| 69 v8::Local<v8::Value> error = |
| 70 v8::Exception::Error(V8String(isolate, dom_exception->message())); |
| 71 exception_obj |
| 72 ->SetAccessor(isolate->GetCurrentContext(), |
| 73 V8AtomicString(isolate, "stack"), DomExceptionStackGetter, |
| 74 DomExceptionStackSetter, error) |
| 75 .ToChecked(); |
| 76 |
| 77 auto private_error = V8PrivateProperty::GetDOMExceptionError(isolate); |
| 78 private_error.Set(exception_obj, error); |
| 79 |
| 80 return exception_obj; |
| 81 } |
| 82 |
| 83 void V8ThrowDOMException::ThrowDOMException(v8::Isolate* isolate, |
| 84 ExceptionCode exception_code, |
| 85 const String& sanitized_message, |
| 86 const String& unsanitized_message) { |
| 87 v8::Local<v8::Value> dom_exception = CreateDOMException( |
| 88 isolate, exception_code, sanitized_message, unsanitized_message); |
| 89 if (dom_exception.IsEmpty()) |
| 90 return; |
| 91 V8ThrowException::ThrowException(isolate, dom_exception); |
| 92 } |
| 93 |
| 94 } // namespace blink |
OLD | NEW |