1/*
2 * Copyright (C) 2008 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#ifndef ApplicationCache_h
27#define ApplicationCache_h
28
29#include <wtf/HashMap.h>
30#include <wtf/HashSet.h>
31#include <wtf/PassRefPtr.h>
32#include <wtf/RefCounted.h>
33#include <wtf/text/StringHash.h>
34#include <wtf/text/WTFString.h>
35
36namespace WebCore {
37
38class ApplicationCacheGroup;
39class ApplicationCacheResource;
40class DocumentLoader;
41class URL;
42class ResourceRequest;
43class SecurityOrigin;
44
45typedef Vector<std::pair<URL, URL>> FallbackURLVector;
46
47class ApplicationCache : public RefCounted<ApplicationCache> {
48public:
49    static PassRefPtr<ApplicationCache> create() { return adoptRef(new ApplicationCache); }
50
51    static void deleteCacheForOrigin(SecurityOrigin*);
52
53    ~ApplicationCache();
54
55    void addResource(PassRefPtr<ApplicationCacheResource> resource);
56    unsigned removeResource(const String& url);
57
58    void setManifestResource(PassRefPtr<ApplicationCacheResource> manifest);
59    ApplicationCacheResource* manifestResource() const { return m_manifest; }
60
61    void setGroup(ApplicationCacheGroup*);
62    ApplicationCacheGroup* group() const { return m_group; }
63
64    bool isComplete() const;
65
66    ApplicationCacheResource* resourceForRequest(const ResourceRequest&);
67    ApplicationCacheResource* resourceForURL(const String& url);
68
69    void setAllowsAllNetworkRequests(bool value) { m_allowAllNetworkRequests = value; }
70    bool allowsAllNetworkRequests() const { return m_allowAllNetworkRequests; }
71    void setOnlineWhitelist(const Vector<URL>& onlineWhitelist);
72    const Vector<URL>& onlineWhitelist() const { return m_onlineWhitelist; }
73    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.
74
75    void setFallbackURLs(const FallbackURLVector&);
76    const FallbackURLVector& fallbackURLs() const { return m_fallbackURLs; }
77    bool urlMatchesFallbackNamespace(const URL&, URL* fallbackURL = 0);
78
79#ifndef NDEBUG
80    void dump();
81#endif
82
83    typedef HashMap<String, RefPtr<ApplicationCacheResource>> ResourceMap;
84    const ResourceMap& resources() const { return m_resources; }
85
86    void setStorageID(unsigned storageID) { m_storageID = storageID; }
87    unsigned storageID() const { return m_storageID; }
88    void clearStorageID();
89
90    static bool requestIsHTTPOrHTTPSGet(const ResourceRequest&);
91
92    static int64_t diskUsageForOrigin(SecurityOrigin*);
93
94    int64_t estimatedSizeInStorage() const { return m_estimatedSizeInStorage; }
95
96private:
97    ApplicationCache();
98
99    ApplicationCacheGroup* m_group;
100    ResourceMap m_resources;
101    ApplicationCacheResource* m_manifest;
102
103    bool m_allowAllNetworkRequests;
104    Vector<URL> m_onlineWhitelist;
105    FallbackURLVector m_fallbackURLs;
106
107    // The total size of the resources belonging to this Application Cache instance.
108    // This is an estimation of the size this Application Cache occupies in the
109    // database file.
110    int64_t m_estimatedSizeInStorage;
111
112    unsigned m_storageID;
113};
114
115} // namespace WebCore
116
117#endif // ApplicationCache_h
118