| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 #endif // DCHECK_IS_ON() | 97 #endif // DCHECK_IS_ON() |
| 98 } | 98 } |
| 99 | 99 |
| 100 ~ExceptionState() { | 100 ~ExceptionState() { |
| 101 if (!m_exception.isEmpty()) { | 101 if (!m_exception.isEmpty()) { |
| 102 V8ThrowException::throwException(m_isolate, | 102 V8ThrowException::throwException(m_isolate, |
| 103 m_exception.newLocal(m_isolate)); | 103 m_exception.newLocal(m_isolate)); |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 | 106 |
| 107 virtual bool shouldThrow() const; |
| 108 |
| 109 // const char* versions to avoid code bloat from inlined temporary Strings. |
| 107 void throwDOMException(ExceptionCode, const char* message); | 110 void throwDOMException(ExceptionCode, const char* message); |
| 108 void throwRangeError(const char* message); | 111 void throwRangeError(const char* message); |
| 109 void throwSecurityError(const char* sanitizedMessage, | 112 void throwSecurityError(const char* sanitizedMessage, |
| 110 const char* unsanitizedMessage = nullptr); | 113 const char* unsanitizedMessage = nullptr); |
| 111 void throwTypeError(const char* message); | 114 void throwTypeError(const char* message); |
| 112 | 115 |
| 113 virtual void throwDOMException(ExceptionCode, const String& message); | 116 void throwDOMException(ExceptionCode, const String& message); |
| 114 virtual void throwRangeError(const String& message); | 117 void throwRangeError(const String& message); |
| 115 virtual void throwSecurityError(const String& sanitizedMessage, | 118 void throwSecurityError(const String& sanitizedMessage, |
| 116 const String& unsanitizedMessage = String()); | 119 const String& unsanitizedMessage = String()); |
| 117 virtual void throwTypeError(const String& message); | 120 void throwTypeError(const String& message); |
| 118 virtual void rethrowV8Exception(v8::Local<v8::Value>); | 121 |
| 122 void rethrowV8Exception(v8::Local<v8::Value>); |
| 119 | 123 |
| 120 bool hadException() const { return m_code; } | 124 bool hadException() const { return m_code; } |
| 121 void clearException(); | 125 void clearException(); |
| 122 | 126 |
| 123 ExceptionCode code() const { return m_code; } | 127 ExceptionCode code() const { return m_code; } |
| 124 const String& message() const { return m_message; } | 128 const String& message() const { return m_message; } |
| 125 v8::Local<v8::Value> getException() { | 129 v8::Local<v8::Value> getException() { |
| 126 DCHECK(!m_exception.isEmpty()); | 130 DCHECK(!m_exception.isEmpty()); |
| 127 return m_exception.newLocal(m_isolate); | 131 return m_exception.newLocal(m_isolate); |
| 128 } | 132 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 139 | 143 |
| 140 String addExceptionContext(const String&) const; | 144 String addExceptionContext(const String&) const; |
| 141 | 145 |
| 142 protected: | 146 protected: |
| 143 // An ExceptionCode for the case that an exception is rethrown. In that | 147 // An ExceptionCode for the case that an exception is rethrown. In that |
| 144 // case, we cannot determine an exception code. | 148 // case, we cannot determine an exception code. |
| 145 static const int kRethrownException = UnknownError; | 149 static const int kRethrownException = UnknownError; |
| 146 | 150 |
| 147 void setException(ExceptionCode, const String&, v8::Local<v8::Value>); | 151 void setException(ExceptionCode, const String&, v8::Local<v8::Value>); |
| 148 | 152 |
| 149 private: | |
| 150 ExceptionCode m_code; | 153 ExceptionCode m_code; |
| 151 ContextType m_context; | 154 ContextType m_context; |
| 152 String m_message; | 155 String m_message; |
| 153 const char* m_propertyName; | 156 const char* m_propertyName; |
| 154 const char* m_interfaceName; | 157 const char* m_interfaceName; |
| 155 // The exception is empty when it was thrown through | 158 // The exception is empty when it was thrown through |
| 156 // DummyExceptionStateForTesting. | 159 // DummyExceptionStateForTesting. |
| 157 ScopedPersistent<v8::Value> m_exception; | 160 ScopedPersistent<v8::Value> m_exception; |
| 158 v8::Isolate* m_isolate; | 161 v8::Isolate* m_isolate; |
| 159 }; | 162 }; |
| 160 | 163 |
| 161 // NonThrowableExceptionState never allow call sites to throw an exception. | 164 // NonThrowableExceptionState never allow call sites to throw an exception. |
| 162 // Should be used if an exception must not be thrown. | 165 // Should be used if an exception must not be thrown. |
| 163 class CORE_EXPORT NonThrowableExceptionState final : public ExceptionState { | 166 class CORE_EXPORT NonThrowableExceptionState final : public ExceptionState { |
| 164 public: | 167 public: |
| 165 NonThrowableExceptionState(); | 168 NonThrowableExceptionState(); |
| 166 NonThrowableExceptionState(const char*, int); | 169 NonThrowableExceptionState(const char*, int); |
| 167 | 170 |
| 168 void throwDOMException(ExceptionCode, const String& message) override; | |
| 169 void throwTypeError(const String& message) override; | |
| 170 void throwSecurityError(const String& sanitizedMessage, | |
| 171 const String& unsanitizedMessage) override; | |
| 172 void throwRangeError(const String& message) override; | |
| 173 void rethrowV8Exception(v8::Local<v8::Value>) override; | |
| 174 ExceptionState& returnThis() { return *this; } | 171 ExceptionState& returnThis() { return *this; } |
| 175 | 172 |
| 173 // ExceptionState overrides: |
| 174 bool shouldThrow() const override; |
| 175 |
| 176 private: | 176 private: |
| 177 const char* m_file; | 177 const char* m_file; |
| 178 const int m_line; | 178 const int m_line; |
| 179 }; | 179 }; |
| 180 | 180 |
| 181 // Syntax sugar for NonThrowableExceptionState. | 181 // Syntax sugar for NonThrowableExceptionState. |
| 182 // This can be used as a default value of an ExceptionState parameter like this: | 182 // This can be used as a default value of an ExceptionState parameter like this: |
| 183 // | 183 // |
| 184 // Node* removeChild(Node*, ExceptionState& = ASSERT_NO_EXCEPTION); | 184 // Node* removeChild(Node*, ExceptionState& = ASSERT_NO_EXCEPTION); |
| 185 #if DCHECK_IS_ON() | 185 #if DCHECK_IS_ON() |
| 186 #define ASSERT_NO_EXCEPTION \ | 186 #define ASSERT_NO_EXCEPTION \ |
| 187 (::blink::NonThrowableExceptionState(__FILE__, __LINE__).returnThis()) | 187 (::blink::NonThrowableExceptionState(__FILE__, __LINE__).returnThis()) |
| 188 #else | 188 #else |
| 189 #define ASSERT_NO_EXCEPTION \ | 189 #define ASSERT_NO_EXCEPTION \ |
| 190 (::blink::DummyExceptionStateForTesting().returnThis()) | 190 (::blink::DummyExceptionStateForTesting().returnThis()) |
| 191 #endif | 191 #endif |
| 192 | 192 |
| 193 // DummyExceptionStateForTesting ignores all thrown exceptions. You should not | 193 // DummyExceptionStateForTesting ignores all thrown exceptions. You should not |
| 194 // use DummyExceptionStateForTesting in production code, where you need to | 194 // use DummyExceptionStateForTesting in production code, where you need to |
| 195 // handle all exceptions properly. If you really need to ignore exceptions in | 195 // handle all exceptions properly. If you really need to ignore exceptions in |
| 196 // production code for some special reason, explicitly call clearException(). | 196 // production code for some special reason, explicitly call clearException(). |
| 197 class CORE_EXPORT DummyExceptionStateForTesting final : public ExceptionState { | 197 class CORE_EXPORT DummyExceptionStateForTesting final : public ExceptionState { |
| 198 public: | 198 public: |
| 199 DummyExceptionStateForTesting() | 199 DummyExceptionStateForTesting() |
| 200 : ExceptionState(nullptr, | 200 : ExceptionState(nullptr, |
| 201 ExceptionState::UnknownContext, | 201 ExceptionState::UnknownContext, |
| 202 nullptr, | 202 nullptr, |
| 203 nullptr) {} | 203 nullptr) {} |
| 204 ~DummyExceptionStateForTesting() { | 204 ~DummyExceptionStateForTesting() {} |
| 205 // Prevent the base class throw an exception. | 205 |
| 206 if (hadException()) { | |
| 207 clearException(); | |
| 208 } | |
| 209 } | |
| 210 void throwDOMException(ExceptionCode, const String& message) override; | |
| 211 void throwTypeError(const String& message) override; | |
| 212 void throwSecurityError(const String& sanitizedMessage, | |
| 213 const String& unsanitizedMessage) override; | |
| 214 void throwRangeError(const String& message) override; | |
| 215 void rethrowV8Exception(v8::Local<v8::Value>) override; | |
| 216 ExceptionState& returnThis() { return *this; } | 206 ExceptionState& returnThis() { return *this; } |
| 207 |
| 208 // ExceptionState overrides: |
| 209 bool shouldThrow() const override; |
| 217 }; | 210 }; |
| 218 | 211 |
| 219 // Syntax sugar for DummyExceptionStateForTesting. | 212 // Syntax sugar for DummyExceptionStateForTesting. |
| 220 // This can be used as a default value of an ExceptionState parameter like this: | 213 // This can be used as a default value of an ExceptionState parameter like this: |
| 221 // | 214 // |
| 222 // Node* removeChild(Node*, ExceptionState& = IGNORE_EXCEPTION_FOR_TESTING); | 215 // Node* removeChild(Node*, ExceptionState& = IGNORE_EXCEPTION_FOR_TESTING); |
| 223 #define IGNORE_EXCEPTION_FOR_TESTING \ | 216 #define IGNORE_EXCEPTION_FOR_TESTING \ |
| 224 (::blink::DummyExceptionStateForTesting().returnThis()) | 217 (::blink::DummyExceptionStateForTesting().returnThis()) |
| 225 | 218 |
| 226 } // namespace blink | 219 } // namespace blink |
| 227 | 220 |
| 228 #endif // ExceptionState_h | 221 #endif // ExceptionState_h |
| OLD | NEW |