1 /*
2  * Copyright (C) 2008, 2010, 2011 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 ApplicationCacheStorage_h
27 #define ApplicationCacheStorage_h
28 
29 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
30 
31 #include "PlatformString.h"
32 #include "SecurityOriginHash.h"
33 #include "SQLiteDatabase.h"
34 #include <wtf/HashCountedSet.h>
35 #include <wtf/HashSet.h>
36 #include <wtf/text/StringHash.h>
37 
38 namespace WebCore {
39 
40 class ApplicationCache;
41 class ApplicationCacheGroup;
42 class ApplicationCacheHost;
43 class ApplicationCacheResource;
44 class KURL;
45 template <class T>
46 class StorageIDJournal;
47 class SecurityOrigin;
48 
49 class ApplicationCacheStorage {
50     WTF_MAKE_NONCOPYABLE(ApplicationCacheStorage); WTF_MAKE_FAST_ALLOCATED;
51 public:
52     enum FailureReason {
53         OriginQuotaReached,
54         TotalQuotaReached,
55         DiskOrOperationFailure
56     };
57 
58     void setCacheDirectory(const String&);
59     const String& cacheDirectory() const;
60 
61     void setMaximumSize(int64_t size);
62     int64_t maximumSize() const;
63     bool isMaximumSizeReached() const;
64     int64_t spaceNeeded(int64_t cacheToSave);
65 
defaultOriginQuota()66     int64_t defaultOriginQuota() const { return m_defaultOriginQuota; }
67     void setDefaultOriginQuota(int64_t quota);
68     bool usageForOrigin(const SecurityOrigin*, int64_t& usage);
69     bool quotaForOrigin(const SecurityOrigin*, int64_t& quota);
70     bool remainingSizeForOriginExcludingCache(const SecurityOrigin*, ApplicationCache*, int64_t& remainingSize);
71     bool storeUpdatedQuotaForOrigin(const SecurityOrigin*, int64_t quota);
72 
73     ApplicationCacheGroup* cacheGroupForURL(const KURL&); // Cache to load a main resource from.
74     ApplicationCacheGroup* fallbackCacheGroupForURL(const KURL&); // Cache that has a fallback entry to load a main resource from if normal loading fails.
75 
76     ApplicationCacheGroup* findOrCreateCacheGroup(const KURL& manifestURL);
77     ApplicationCacheGroup* findInMemoryCacheGroup(const KURL& manifestURL) const;
78     void cacheGroupDestroyed(ApplicationCacheGroup*);
79     void cacheGroupMadeObsolete(ApplicationCacheGroup*);
80 
81     bool storeNewestCache(ApplicationCacheGroup*, ApplicationCache* oldCache, FailureReason& failureReason);
82     bool storeNewestCache(ApplicationCacheGroup*); // Updates the cache group, but doesn't remove old cache.
83     bool store(ApplicationCacheResource*, ApplicationCache*);
84     bool storeUpdatedType(ApplicationCacheResource*, ApplicationCache*);
85 
86     // Removes the group if the cache to be removed is the newest one (so, storeNewestCache() needs to be called beforehand when updating).
87     void remove(ApplicationCache*);
88 
89     void empty();
90 
91     static bool storeCopyOfCache(const String& cacheDirectory, ApplicationCacheHost*);
92 
93     bool manifestURLs(Vector<KURL>* urls);
94     bool cacheGroupSize(const String& manifestURL, int64_t* size);
95     bool deleteCacheGroup(const String& manifestURL);
96     void vacuumDatabaseFile();
97 
98     void getOriginsWithCache(HashSet<RefPtr<SecurityOrigin>, SecurityOriginHash>&);
99     void deleteAllEntries();
100 
unknownQuota()101     static int64_t unknownQuota() { return -1; }
noQuota()102     static int64_t noQuota() { return std::numeric_limits<int64_t>::max(); }
103 private:
104     ApplicationCacheStorage();
105     PassRefPtr<ApplicationCache> loadCache(unsigned storageID);
106     ApplicationCacheGroup* loadCacheGroup(const KURL& manifestURL);
107 
108     typedef StorageIDJournal<ApplicationCacheResource> ResourceStorageIDJournal;
109     typedef StorageIDJournal<ApplicationCacheGroup> GroupStorageIDJournal;
110 
111     bool store(ApplicationCacheGroup*, GroupStorageIDJournal*);
112     bool store(ApplicationCache*, ResourceStorageIDJournal*);
113     bool store(ApplicationCacheResource*, unsigned cacheStorageID);
114 
115     bool ensureOriginRecord(const SecurityOrigin*);
116     bool shouldStoreResourceAsFlatFile(ApplicationCacheResource*);
117     void deleteTables();
118     bool writeDataToUniqueFileInDirectory(SharedBuffer*, const String& directory, String& outFilename, const String& fileExtension);
119 
120     void loadManifestHostHashes();
121 
122     void verifySchemaVersion();
123 
124     void openDatabase(bool createIfDoesNotExist);
125 
126     bool executeStatement(SQLiteStatement&);
127     bool executeSQLCommand(const String&);
128 
129     void checkForMaxSizeReached();
130     void checkForDeletedResources();
131     long long flatFileAreaSize();
132 
133     String m_cacheDirectory;
134     String m_cacheFile;
135 
136     int64_t m_maximumSize;
137     bool m_isMaximumSizeReached;
138 
139     int64_t m_defaultOriginQuota;
140 
141     SQLiteDatabase m_database;
142 
143     // In order to quickly determine if a given resource exists in an application cache,
144     // we keep a hash set of the hosts of the manifest URLs of all non-obsolete cache groups.
145     HashCountedSet<unsigned, AlreadyHashed> m_cacheHostSet;
146 
147     typedef HashMap<String, ApplicationCacheGroup*> CacheGroupMap;
148     CacheGroupMap m_cachesInMemory; // Excludes obsolete cache groups.
149 
150     friend ApplicationCacheStorage& cacheStorage();
151 };
152 
153 ApplicationCacheStorage& cacheStorage();
154 
155 } // namespace WebCore
156 
157 #endif // ENABLE(OFFLINE_WEB_APPLICATIONS)
158 
159 #endif // ApplicationCacheStorage_h
160