2 * Copyright (C) 2008, 2010, 2011 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef ApplicationCacheStorage_h
27 #define ApplicationCacheStorage_h
29 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
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>
40 class ApplicationCache;
41 class ApplicationCacheGroup;
42 class ApplicationCacheHost;
43 class ApplicationCacheResource;
46 class StorageIDJournal;
49 class ApplicationCacheStorage {
50 WTF_MAKE_NONCOPYABLE(ApplicationCacheStorage); WTF_MAKE_FAST_ALLOCATED;
55 DiskOrOperationFailure
58 void setCacheDirectory(const String&);
59 const String& cacheDirectory() const;
61 void setMaximumSize(int64_t size);
62 int64_t maximumSize() const;
63 bool isMaximumSizeReached() const;
64 int64_t spaceNeeded(int64_t cacheToSave);
66 int64_t defaultOriginQuota() const { return m_defaultOriginQuota; }
67 void setDefaultOriginQuota(int64_t quota);
68 bool calculateUsageForOrigin(const SecurityOrigin*, int64_t& usage);
69 bool calculateQuotaForOrigin(const SecurityOrigin*, int64_t& quota);
70 bool calculateRemainingSizeForOriginExcludingCache(const SecurityOrigin*, ApplicationCache*, int64_t& remainingSize);
71 bool storeUpdatedQuotaForOrigin(const SecurityOrigin*, int64_t quota);
72 bool checkOriginQuota(ApplicationCacheGroup*, ApplicationCache* oldCache, ApplicationCache* newCache, int64_t& totalSpaceNeeded);
74 ApplicationCacheGroup* cacheGroupForURL(const KURL&); // Cache to load a main resource from.
75 ApplicationCacheGroup* fallbackCacheGroupForURL(const KURL&); // Cache that has a fallback entry to load a main resource from if normal loading fails.
77 ApplicationCacheGroup* findOrCreateCacheGroup(const KURL& manifestURL);
78 ApplicationCacheGroup* findInMemoryCacheGroup(const KURL& manifestURL) const;
79 void cacheGroupDestroyed(ApplicationCacheGroup*);
80 void cacheGroupMadeObsolete(ApplicationCacheGroup*);
82 bool storeNewestCache(ApplicationCacheGroup*, ApplicationCache* oldCache, FailureReason& failureReason);
83 bool storeNewestCache(ApplicationCacheGroup*); // Updates the cache group, but doesn't remove old cache.
84 bool store(ApplicationCacheResource*, ApplicationCache*);
85 bool storeUpdatedType(ApplicationCacheResource*, ApplicationCache*);
87 // Removes the group if the cache to be removed is the newest one (so, storeNewestCache() needs to be called beforehand when updating).
88 void remove(ApplicationCache*);
92 static bool storeCopyOfCache(const String& cacheDirectory, ApplicationCacheHost*);
94 bool manifestURLs(Vector<KURL>* urls);
95 bool cacheGroupSize(const String& manifestURL, int64_t* size);
96 bool deleteCacheGroup(const String& manifestURL);
97 void vacuumDatabaseFile();
99 void getOriginsWithCache(HashSet<RefPtr<SecurityOrigin>, SecurityOriginHash>&);
100 void deleteAllEntries();
102 static int64_t unknownQuota() { return -1; }
103 static int64_t noQuota() { return std::numeric_limits<int64_t>::max(); }
105 ApplicationCacheStorage();
106 PassRefPtr<ApplicationCache> loadCache(unsigned storageID);
107 ApplicationCacheGroup* loadCacheGroup(const KURL& manifestURL);
109 typedef StorageIDJournal<ApplicationCacheResource> ResourceStorageIDJournal;
110 typedef StorageIDJournal<ApplicationCacheGroup> GroupStorageIDJournal;
112 bool store(ApplicationCacheGroup*, GroupStorageIDJournal*);
113 bool store(ApplicationCache*, ResourceStorageIDJournal*);
114 bool store(ApplicationCacheResource*, unsigned cacheStorageID);
116 bool ensureOriginRecord(const SecurityOrigin*);
117 bool shouldStoreResourceAsFlatFile(ApplicationCacheResource*);
119 bool writeDataToUniqueFileInDirectory(SharedBuffer*, const String& directory, String& outFilename, const String& fileExtension);
121 void loadManifestHostHashes();
123 void verifySchemaVersion();
125 void openDatabase(bool createIfDoesNotExist);
127 bool executeStatement(SQLiteStatement&);
128 bool executeSQLCommand(const String&);
130 void checkForMaxSizeReached();
131 void checkForDeletedResources();
132 long long flatFileAreaSize();
134 String m_cacheDirectory;
137 int64_t m_maximumSize;
138 bool m_isMaximumSizeReached;
140 int64_t m_defaultOriginQuota;
142 SQLiteDatabase m_database;
144 // In order to quickly determine if a given resource exists in an application cache,
145 // we keep a hash set of the hosts of the manifest URLs of all non-obsolete cache groups.
146 HashCountedSet<unsigned, AlreadyHashed> m_cacheHostSet;
148 typedef HashMap<String, ApplicationCacheGroup*> CacheGroupMap;
149 CacheGroupMap m_cachesInMemory; // Excludes obsolete cache groups.
151 friend ApplicationCacheStorage& cacheStorage();
154 ApplicationCacheStorage& cacheStorage();
156 } // namespace WebCore
158 #endif // ENABLE(OFFLINE_WEB_APPLICATIONS)
160 #endif // ApplicationCacheStorage_h