1/*
2 * Copyright (C) 2008-2017 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#include <wtf/HashMap.h>
29#include <wtf/text/StringHash.h>
30
31namespace WebCore {
32
33class ApplicationCacheGroup;
34class ApplicationCacheResource;
35class ResourceRequest;
36
37using FallbackURLVector = Vector<std::pair<URL, URL>>;
38
39class ApplicationCache : public RefCounted<ApplicationCache> {
40public:
41 static Ref<ApplicationCache> create() { return adoptRef(*new ApplicationCache); }
42
43 ~ApplicationCache();
44
45 void addResource(Ref<ApplicationCacheResource>&&);
46
47 void setManifestResource(Ref<ApplicationCacheResource>&&);
48 ApplicationCacheResource* manifestResource() const { return m_manifest; }
49
50 void setGroup(ApplicationCacheGroup*);
51 ApplicationCacheGroup* group() const { return m_group; }
52
53 bool isComplete();
54
55 ApplicationCacheResource* resourceForRequest(const ResourceRequest&);
56 ApplicationCacheResource* resourceForURL(const String& url);
57
58 void setAllowsAllNetworkRequests(bool value) { m_allowAllNetworkRequests = value; }
59 bool allowsAllNetworkRequests() const { return m_allowAllNetworkRequests; }
60 void setOnlineWhitelist(const Vector<URL>& onlineWhitelist);
61 const Vector<URL>& onlineWhitelist() const { return m_onlineWhitelist; }
62 bool isURLInOnlineWhitelist(const URL&); // There is an entry in online whitelist that has the same origin as the resource's URL and that is a prefix match for the resource's URL.
63
64 void setFallbackURLs(const FallbackURLVector&);
65 const FallbackURLVector& fallbackURLs() const { return m_fallbackURLs; }
66 bool urlMatchesFallbackNamespace(const URL&, URL* fallbackURL = nullptr);
67
68#ifndef NDEBUG
69 void dump();
70#endif
71
72 using ResourceMap = HashMap<String, RefPtr<ApplicationCacheResource>>;
73 const ResourceMap& resources() const { return m_resources; }
74
75 void setStorageID(unsigned storageID) { m_storageID = storageID; }
76 unsigned storageID() const { return m_storageID; }
77 void clearStorageID();
78
79 static bool requestIsHTTPOrHTTPSGet(const ResourceRequest&);
80
81 int64_t estimatedSizeInStorage() const { return m_estimatedSizeInStorage; }
82
83private:
84 ApplicationCache();
85
86 ApplicationCacheGroup* m_group { nullptr };
87 ResourceMap m_resources;
88 ApplicationCacheResource* m_manifest { nullptr };
89
90 bool m_allowAllNetworkRequests { false };
91 Vector<URL> m_onlineWhitelist;
92 FallbackURLVector m_fallbackURLs;
93
94 // The total size of the resources belonging to this Application Cache instance.
95 // This is an estimation of the size this Application Cache occupies in the database file.
96 int64_t m_estimatedSizeInStorage { 0 };
97
98 unsigned m_storageID { 0 };
99};
100
101} // namespace WebCore
102