OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 29 matching lines...) Expand all Loading... |
40 #include "wtf/Noncopyable.h" | 40 #include "wtf/Noncopyable.h" |
41 #include "wtf/text/WTFString.h" | 41 #include "wtf/text/WTFString.h" |
42 #include <v8.h> | 42 #include <v8.h> |
43 | 43 |
44 namespace blink { | 44 namespace blink { |
45 | 45 |
46 typedef int ExceptionCode; | 46 typedef int ExceptionCode; |
47 class ScriptPromiseResolver; | 47 class ScriptPromiseResolver; |
48 class ScriptState; | 48 class ScriptState; |
49 | 49 |
| 50 // ExceptionState is a scope-like class and provides a way to throw an exception |
| 51 // with an option to cancel it. An exception message may be auto-generated. |
| 52 // You can convert an exception to a reject promise. |
50 class CORE_EXPORT ExceptionState { | 53 class CORE_EXPORT ExceptionState { |
51 WTF_MAKE_NONCOPYABLE(ExceptionState); | 54 WTF_MAKE_NONCOPYABLE(ExceptionState); |
52 USING_FAST_MALLOC(ExceptionState); | 55 USING_FAST_MALLOC(ExceptionState); |
53 public: | 56 public: |
54 enum ContextType { | 57 enum ContextType { |
55 ConstructionContext, | 58 ConstructionContext, |
56 ExecutionContext, | 59 ExecutionContext, |
57 DeletionContext, | 60 DeletionContext, |
58 GetterContext, | 61 GetterContext, |
59 SetterContext, | 62 SetterContext, |
(...skipping 28 matching lines...) Expand all Loading... |
88 } | 91 } |
89 #endif // ENABLE(ASSERT) | 92 #endif // ENABLE(ASSERT) |
90 } | 93 } |
91 | 94 |
92 ExceptionState(ContextType context, const char* propertyName, const char* in
terfaceName, const v8::Local<v8::Object>& creationContext, v8::Isolate* isolate)
// DEPRECATED | 95 ExceptionState(ContextType context, const char* propertyName, const char* in
terfaceName, const v8::Local<v8::Object>& creationContext, v8::Isolate* isolate)
// DEPRECATED |
93 : ExceptionState(isolate, context, interfaceName, propertyName) { } | 96 : ExceptionState(isolate, context, interfaceName, propertyName) { } |
94 | 97 |
95 ExceptionState(ContextType context, const char* interfaceName, const v8::Loc
al<v8::Object>& creationContext, v8::Isolate* isolate) // DEPRECATED | 98 ExceptionState(ContextType context, const char* interfaceName, const v8::Loc
al<v8::Object>& creationContext, v8::Isolate* isolate) // DEPRECATED |
96 : ExceptionState(isolate, context, interfaceName) { } | 99 : ExceptionState(isolate, context, interfaceName) { } |
97 | 100 |
| 101 ~ExceptionState() |
| 102 { |
| 103 if (!m_exception.isEmpty()) { |
| 104 V8ThrowException::throwException(m_isolate, m_exception.newLocal(m_i
solate)); |
| 105 } |
| 106 } |
| 107 |
98 virtual void throwDOMException(const ExceptionCode&, const String& message); | 108 virtual void throwDOMException(const ExceptionCode&, const String& message); |
| 109 virtual void throwRangeError(const String& message); |
| 110 virtual void throwSecurityError(const String& sanitizedMessage, const String
& unsanitizedMessage = String()); |
99 virtual void throwTypeError(const String& message); | 111 virtual void throwTypeError(const String& message); |
100 virtual void throwSecurityError(const String& sanitizedMessage, const String
& unsanitizedMessage = String()); | 112 virtual void rethrowV8Exception(v8::Local<v8::Value>); |
101 virtual void throwRangeError(const String& message); | |
102 | 113 |
103 bool hadException() const { return !m_exception.isEmpty() || m_code; } | 114 bool hadException() const { return m_code; } |
104 void clearException(); | 115 void clearException(); |
105 | 116 |
106 ExceptionCode code() const { return m_code; } | 117 ExceptionCode code() const { return m_code; } |
107 const String& message() const { return m_message; } | 118 const String& message() const { return m_message; } |
108 v8::Local<v8::Value> getException() { DCHECK(!m_exception.isEmpty()); return
m_exception.newLocal(m_isolate); } | 119 v8::Local<v8::Value> getException() |
109 | |
110 bool throwIfNeeded() | |
111 { | 120 { |
112 if (!hadException()) | 121 DCHECK(!m_exception.isEmpty()); |
113 return false; | 122 return m_exception.newLocal(m_isolate); |
114 throwException(); | |
115 return true; | |
116 } | 123 } |
117 | 124 |
118 // This method clears out the exception which |this| has. | 125 // This method clears out the exception which |this| has. |
119 ScriptPromise reject(ScriptState*); | 126 ScriptPromise reject(ScriptState*); |
120 | 127 |
121 // This method clears out the exception which |this| has. | 128 // This method clears out the exception which |this| has. |
122 void reject(ScriptPromiseResolver*); | 129 void reject(ScriptPromiseResolver*); |
123 | 130 |
124 ContextType context() const { return m_context; } | |
125 const char* propertyName() const { return m_propertyName; } | 131 const char* propertyName() const { return m_propertyName; } |
126 const char* interfaceName() const { return m_interfaceName; } | 132 const char* interfaceName() const { return m_interfaceName; } |
127 | 133 |
128 void rethrowV8Exception(v8::Local<v8::Value> value) | |
129 { | |
130 setException(value); | |
131 } | |
132 | |
133 // Might return nullptr. | |
134 v8::Isolate* isolate() const { return m_isolate; } | |
135 | |
136 #if ENABLE(ASSERT) | 134 #if ENABLE(ASSERT) |
137 OnStackObjectChecker& getOnStackObjectChecker() { return m_onStackObjectChec
ker; } | 135 OnStackObjectChecker& getOnStackObjectChecker() { return m_onStackObjectChec
ker; } |
138 #endif | 136 #endif |
139 | 137 |
140 protected: | 138 protected: |
| 139 // An ExceptionCode for the case that an exception is rethrown. In that |
| 140 // case, we cannot determine an exception code. |
| 141 static const int kRethrownException = UnknownError; |
| 142 |
| 143 void setException(ExceptionCode, const String&, v8::Local<v8::Value>); |
| 144 |
| 145 private: |
| 146 String addExceptionContext(const String&) const; |
| 147 |
141 ExceptionCode m_code; | 148 ExceptionCode m_code; |
142 ContextType m_context; | 149 ContextType m_context; |
143 String m_message; | 150 String m_message; |
144 const char* m_propertyName; | 151 const char* m_propertyName; |
145 const char* m_interfaceName; | 152 const char* m_interfaceName; |
146 | 153 // The exception is empty when it was thrown through TrackExceptionState. |
147 private: | |
148 void setException(v8::Local<v8::Value>); | |
149 void throwException(); | |
150 | |
151 String addExceptionContext(const String&) const; | |
152 | |
153 ScopedPersistent<v8::Value> m_exception; | 154 ScopedPersistent<v8::Value> m_exception; |
154 v8::Isolate* m_isolate; | 155 v8::Isolate* m_isolate; |
155 #if ENABLE(ASSERT) | 156 #if ENABLE(ASSERT) |
156 OnStackObjectChecker m_onStackObjectChecker; | 157 OnStackObjectChecker m_onStackObjectChecker; |
157 #endif | 158 #endif |
158 }; | 159 }; |
159 | 160 |
160 // Used if exceptions can/should not be directly thrown. | 161 // NonThrowableExceptionState never allow call sites to throw an exception. |
| 162 // Should be used if an exception must not be thrown. |
161 class CORE_EXPORT NonThrowableExceptionState final : public ExceptionState { | 163 class CORE_EXPORT NonThrowableExceptionState final : public ExceptionState { |
162 WTF_MAKE_NONCOPYABLE(NonThrowableExceptionState); | |
163 public: | 164 public: |
164 NonThrowableExceptionState(): ExceptionState(ExceptionState::UnknownContext,
0, 0, v8::Local<v8::Object>(), v8::Isolate::GetCurrent()) { } | 165 NonThrowableExceptionState() |
| 166 : ExceptionState(nullptr, ExceptionState::UnknownContext, nullptr, nullp
tr) { } |
| 167 |
165 void throwDOMException(const ExceptionCode&, const String& message) override
; | 168 void throwDOMException(const ExceptionCode&, const String& message) override
; |
166 void throwTypeError(const String& message = String()) override; | 169 void throwTypeError(const String& message = String()) override; |
167 void throwSecurityError(const String& sanitizedMessage, const String& unsani
tizedMessage = String()) override; | 170 void throwSecurityError(const String& sanitizedMessage, const String& unsani
tizedMessage = String()) override; |
168 void throwRangeError(const String& message) override; | 171 void throwRangeError(const String& message) override; |
| 172 void rethrowV8Exception(v8::Local<v8::Value>) override; |
169 }; | 173 }; |
170 | 174 |
171 // Used if any exceptions thrown are ignorable. | 175 // TrackExceptionState never actually throws an exception, but just records |
| 176 // whether a call site tried to throw an exception or not. Should be used |
| 177 // if any exceptions must be ignored. |
172 class CORE_EXPORT TrackExceptionState final : public ExceptionState { | 178 class CORE_EXPORT TrackExceptionState final : public ExceptionState { |
173 WTF_MAKE_NONCOPYABLE(TrackExceptionState); | |
174 public: | 179 public: |
175 TrackExceptionState(): ExceptionState(ExceptionState::UnknownContext, 0, 0,
v8::Local<v8::Object>(), v8::Isolate::GetCurrent()) { } | 180 TrackExceptionState() |
| 181 : ExceptionState(nullptr, ExceptionState::UnknownContext, nullptr, nullp
tr) { } |
| 182 ~TrackExceptionState() |
| 183 { |
| 184 // Prevent the base class throw an exception. |
| 185 if (hadException()) { |
| 186 clearException(); |
| 187 } |
| 188 } |
| 189 |
176 void throwDOMException(const ExceptionCode&, const String& message) override
; | 190 void throwDOMException(const ExceptionCode&, const String& message) override
; |
177 void throwTypeError(const String& message = String()) override; | 191 void throwTypeError(const String& message = String()) override; |
178 void throwSecurityError(const String& sanitizedMessage, const String& unsani
tizedMessage = String()) override; | 192 void throwSecurityError(const String& sanitizedMessage, const String& unsani
tizedMessage = String()) override; |
179 void throwRangeError(const String& message) override; | 193 void throwRangeError(const String& message) override; |
| 194 void rethrowV8Exception(v8::Local<v8::Value>) override; |
180 }; | 195 }; |
181 | 196 |
182 } // namespace blink | 197 } // namespace blink |
183 | 198 |
184 #endif // ExceptionState_h | 199 #endif // ExceptionState_h |
OLD | NEW |