| 1 | /* |
| 2 | * Copyright (C) 2015, 2016 Apple 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 |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #if ENABLE(INDEXED_DATABASE) |
| 29 | |
| 30 | #include "DOMException.h" |
| 31 | #include "ExceptionCode.h" |
| 32 | #include <wtf/text/WTFString.h> |
| 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | class IDBError { |
| 37 | public: |
| 38 | WEBCORE_EXPORT explicit IDBError(Optional<ExceptionCode> = WTF::nullopt, const String& message = { }); |
| 39 | |
| 40 | static IDBError userDeleteError() |
| 41 | { |
| 42 | return IDBError { UnknownError, "Database deleted by request of the user"_s }; |
| 43 | } |
| 44 | |
| 45 | static IDBError serverConnectionLostError() |
| 46 | { |
| 47 | return IDBError { UnknownError, "Connection to Indexed Database server lost. Refresh the page to try again"_s }; |
| 48 | } |
| 49 | |
| 50 | RefPtr<DOMException> toDOMException() const; |
| 51 | |
| 52 | Optional<ExceptionCode> code() const { return m_code; } |
| 53 | String name() const; |
| 54 | String message() const; |
| 55 | |
| 56 | bool isNull() const { return !m_code; } |
| 57 | |
| 58 | IDBError isolatedCopy() const; |
| 59 | |
| 60 | template<class Encoder> void encode(Encoder&) const; |
| 61 | template<class Decoder> static bool decode(Decoder&, IDBError&); |
| 62 | |
| 63 | private: |
| 64 | Optional<ExceptionCode> m_code; |
| 65 | String m_message; |
| 66 | }; |
| 67 | |
| 68 | template<class Encoder> |
| 69 | void IDBError::encode(Encoder& encoder) const |
| 70 | { |
| 71 | if (m_code) { |
| 72 | encoder << true; |
| 73 | encoder.encodeEnum(m_code.value()); |
| 74 | } else |
| 75 | encoder << false; |
| 76 | encoder << m_message; |
| 77 | } |
| 78 | |
| 79 | template<class Decoder> |
| 80 | bool IDBError::decode(Decoder& decoder, IDBError& error) |
| 81 | { |
| 82 | bool hasCode = false; |
| 83 | if (!decoder.decode(hasCode)) |
| 84 | return false; |
| 85 | |
| 86 | if (hasCode) { |
| 87 | ExceptionCode ec; |
| 88 | if (!decoder.decodeEnum(ec)) |
| 89 | return false; |
| 90 | error.m_code = ec; |
| 91 | } else |
| 92 | error.m_code = WTF::nullopt; |
| 93 | |
| 94 | if (!decoder.decode(error.m_message)) |
| 95 | return false; |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | } // namespace WebCore |
| 101 | |
| 102 | #endif // ENABLE(INDEXED_DATABASE) |
| 103 | |