1/*
2 * Copyright (C) 2006 Apple Inc. All rights reserved.
3 * Copyright (C) 2016 Canon Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <wtf/URL.h>
30#include <wtf/text/WTFString.h>
31
32namespace WebCore {
33
34class ResourceError;
35
36WEBCORE_EXPORT extern const char* const errorDomainWebKitInternal; // Used for errors that won't be exposed to clients.
37WEBCORE_EXPORT extern const char* const errorDomainWebKitServiceWorker; // Used for errors that happen when loading a resource from a service worker.
38
39class ResourceErrorBase {
40public:
41 WEBCORE_EXPORT ResourceError isolatedCopy() const;
42
43 const String& domain() const { lazyInit(); return m_domain; }
44 int errorCode() const { lazyInit(); return m_errorCode; }
45 const URL& failingURL() const { lazyInit(); return m_failingURL; }
46 const String& localizedDescription() const { lazyInit(); return m_localizedDescription; }
47
48 enum class Type : uint8_t {
49 Null,
50 General,
51 AccessControl,
52 Cancellation,
53 Timeout
54 };
55
56 bool isNull() const { return m_type == Type::Null; }
57 bool isGeneral() const { return m_type == Type::General; }
58 bool isAccessControl() const { return m_type == Type::AccessControl; }
59 bool isCancellation() const { return m_type == Type::Cancellation; }
60 bool isTimeout() const { return m_type == Type::Timeout; }
61
62 static bool compare(const ResourceError&, const ResourceError&);
63
64 WEBCORE_EXPORT void setType(Type);
65 Type type() const { return m_type; }
66
67protected:
68 ResourceErrorBase(Type type) : m_type(type) { }
69
70 ResourceErrorBase(const String& domain, int errorCode, const URL& failingURL, const String& localizedDescription, Type type)
71 : m_domain(domain)
72 , m_failingURL(failingURL)
73 , m_localizedDescription(localizedDescription)
74 , m_errorCode(errorCode)
75 , m_type(type)
76 {
77 }
78
79 WEBCORE_EXPORT void lazyInit() const;
80
81 // The ResourceError subclass may "shadow" this method to lazily initialize platform specific fields
82 void platformLazyInit() {}
83
84 // The ResourceError subclass may "shadow" this method to compare platform specific fields
85 static bool platformCompare(const ResourceError&, const ResourceError&) { return true; }
86
87 String m_domain;
88 URL m_failingURL;
89 String m_localizedDescription;
90 int m_errorCode { 0 };
91 Type m_type { Type::General };
92
93private:
94 const ResourceError& asResourceError() const;
95};
96
97inline bool operator==(const ResourceError& a, const ResourceError& b) { return ResourceErrorBase::compare(a, b); }
98inline bool operator!=(const ResourceError& a, const ResourceError& b) { return !(a == b); }
99
100} // namespace WebCore
101