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