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