| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #ifndef ExceptionState_h |
| 32 #define ExceptionState_h |
| 33 |
| 34 #include "bindings/common/ScriptPromise.h" |
| 35 #include "core/dom/ExceptionCode.h" |
| 36 #include "wtf/Noncopyable.h" |
| 37 #include "wtf/text/WTFString.h" |
| 38 #include <v8.h> |
| 39 |
| 40 namespace blink { |
| 41 |
| 42 typedef int ExceptionCode; |
| 43 class ScriptState; |
| 44 |
| 45 class ExceptionState { |
| 46 public: |
| 47 virtual void throwDOMException(const ExceptionCode&, const String& message)
= 0; |
| 48 virtual void throwTypeError(const String& message) = 0; |
| 49 virtual void throwSecurityError(const String& sanitizedMessage, const String
& unsanitizedMessage = String()) = 0; |
| 50 virtual void throwRangeError(const String& message) = 0; |
| 51 |
| 52 // FIXME: Remove use in SerializedScriptValue. |
| 53 virtual void rethrowV8Exception(v8::Handle<v8::Value> value) = 0; |
| 54 |
| 55 virtual void throwException() = 0; |
| 56 virtual void clearException() = 0; |
| 57 |
| 58 virtual ScriptPromise reject(ScriptState*) = 0; |
| 59 |
| 60 bool throwIfNeeded() |
| 61 { |
| 62 if (hadException()) { |
| 63 throwException(); |
| 64 return true; |
| 65 } |
| 66 return false; |
| 67 } |
| 68 |
| 69 ExceptionCode code() const { return m_code; } |
| 70 const String& message() const { return m_message; } |
| 71 bool hadException() const { return m_hadException || m_code; } |
| 72 |
| 73 virtual bool isV8ExceptionState() const { return false; } |
| 74 |
| 75 protected: |
| 76 ExceptionState() : m_code(0), m_hadException(false) { } |
| 77 void setMessage(const String& message) { m_message = message; } |
| 78 void setCode(ExceptionCode code) { m_code = code; } |
| 79 void setHadException(bool exception) { m_hadException = exception; } |
| 80 |
| 81 private: |
| 82 ExceptionCode m_code; |
| 83 String m_message; |
| 84 bool m_hadException; |
| 85 }; |
| 86 |
| 87 // Used if exceptions can/should not be directly thrown. |
| 88 class NonThrowableExceptionState FINAL : public ExceptionState { |
| 89 public: |
| 90 NonThrowableExceptionState() : ExceptionState() { } |
| 91 virtual void throwDOMException(const ExceptionCode& ec, const String& messag
e) OVERRIDE { |
| 92 ASSERT_NOT_REACHED(); |
| 93 setCode(ec); |
| 94 setMessage(message); |
| 95 setHadException(true); |
| 96 } |
| 97 virtual void throwTypeError(const String& message = String()) OVERRIDE { |
| 98 ASSERT_NOT_REACHED(); |
| 99 setCode(V8TypeError); |
| 100 setMessage(message); |
| 101 setHadException(true); |
| 102 } |
| 103 virtual void throwSecurityError(const String& sanitizedMessage, const String
& unsanitizedMessage = String()) OVERRIDE{ |
| 104 ASSERT_NOT_REACHED(); |
| 105 setCode(SecurityError); |
| 106 setMessage(sanitizedMessage); |
| 107 setHadException(true); |
| 108 } |
| 109 virtual void throwRangeError(const String& message = String()) OVERRIDE { |
| 110 ASSERT_NOT_REACHED(); |
| 111 setCode(V8RangeError); |
| 112 setMessage(message); |
| 113 setHadException(true); |
| 114 } |
| 115 virtual void rethrowV8Exception(v8::Handle<v8::Value> value) OVERRIDE { |
| 116 ASSERT_NOT_REACHED(); |
| 117 setHadException(true); |
| 118 } |
| 119 virtual void throwException() OVERRIDE FINAL { |
| 120 ASSERT_NOT_REACHED(); |
| 121 } |
| 122 virtual void clearException() OVERRIDE FINAL { |
| 123 ASSERT_NOT_REACHED(); |
| 124 setCode(0); |
| 125 setHadException(false); |
| 126 } |
| 127 virtual ScriptPromise reject(ScriptState* state) OVERRIDE FINAL { |
| 128 ASSERT_NOT_REACHED(); |
| 129 return ScriptPromise::empty(state); |
| 130 } |
| 131 }; |
| 132 |
| 133 // Used if any exceptions thrown are ignorable. |
| 134 class TrackExceptionState FINAL : public ExceptionState { |
| 135 public: |
| 136 TrackExceptionState() : ExceptionState() { } |
| 137 virtual void throwDOMException(const ExceptionCode& ec, const String& messag
e) OVERRIDE { |
| 138 setCode(ec); |
| 139 setMessage(message); |
| 140 setHadException(true); |
| 141 } |
| 142 virtual void throwTypeError(const String& message = String()) OVERRIDE { |
| 143 setCode(V8TypeError); |
| 144 setMessage(message); |
| 145 setHadException(true); |
| 146 } |
| 147 virtual void throwSecurityError(const String& sanitizedMessage, const String
& unsanitizedMessage = String()) OVERRIDE { |
| 148 setCode(SecurityError); |
| 149 setMessage(sanitizedMessage); |
| 150 setHadException(true); |
| 151 } |
| 152 virtual void throwRangeError(const String& message = String()) OVERRIDE { |
| 153 setCode(V8RangeError); |
| 154 setMessage(message); |
| 155 setHadException(true); |
| 156 } |
| 157 virtual void rethrowV8Exception(v8::Handle<v8::Value> value) OVERRIDE { |
| 158 setHadException(true); |
| 159 } |
| 160 virtual void throwException() OVERRIDE FINAL { |
| 161 ASSERT_NOT_REACHED(); |
| 162 } |
| 163 virtual void clearException() OVERRIDE FINAL { |
| 164 setCode(0); |
| 165 setHadException(false); |
| 166 } |
| 167 virtual ScriptPromise reject(ScriptState* state) OVERRIDE FINAL { |
| 168 ASSERT_NOT_REACHED(); |
| 169 return ScriptPromise::empty(state); |
| 170 } |
| 171 }; |
| 172 |
| 173 } // namespace blink |
| 174 |
| 175 #endif // ExceptionState_h |
| OLD | NEW |