initial import
[vuplus_webkit] / Source / WebCore / loader / appcache / ApplicationCacheStorage.h
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
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);
73
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.
76
77     ApplicationCacheGroup* findOrCreateCacheGroup(const KURL& manifestURL);
78     ApplicationCacheGroup* findInMemoryCacheGroup(const KURL& manifestURL) const;
79     void cacheGroupDestroyed(ApplicationCacheGroup*);
80     void cacheGroupMadeObsolete(ApplicationCacheGroup*);
81         
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*);
86
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*);
89     
90     void empty();
91     
92     static bool storeCopyOfCache(const String& cacheDirectory, ApplicationCacheHost*);
93
94     bool manifestURLs(Vector<KURL>* urls);
95     bool cacheGroupSize(const String& manifestURL, int64_t* size);
96     bool deleteCacheGroup(const String& manifestURL);
97     void vacuumDatabaseFile();
98
99     void getOriginsWithCache(HashSet<RefPtr<SecurityOrigin>, SecurityOriginHash>&);
100     void deleteAllEntries();
101
102     static int64_t unknownQuota() { return -1; }
103     static int64_t noQuota() { return std::numeric_limits<int64_t>::max(); }
104 private:
105     ApplicationCacheStorage();
106     PassRefPtr<ApplicationCache> loadCache(unsigned storageID);
107     ApplicationCacheGroup* loadCacheGroup(const KURL& manifestURL);
108     
109     typedef StorageIDJournal<ApplicationCacheResource> ResourceStorageIDJournal;
110     typedef StorageIDJournal<ApplicationCacheGroup> GroupStorageIDJournal;
111
112     bool store(ApplicationCacheGroup*, GroupStorageIDJournal*);
113     bool store(ApplicationCache*, ResourceStorageIDJournal*);
114     bool store(ApplicationCacheResource*, unsigned cacheStorageID);
115
116     bool ensureOriginRecord(const SecurityOrigin*);
117     bool shouldStoreResourceAsFlatFile(ApplicationCacheResource*);
118     void deleteTables();
119     bool writeDataToUniqueFileInDirectory(SharedBuffer*, const String& directory, String& outFilename, const String& fileExtension);
120
121     void loadManifestHostHashes();
122     
123     void verifySchemaVersion();
124     
125     void openDatabase(bool createIfDoesNotExist);
126     
127     bool executeStatement(SQLiteStatement&);
128     bool executeSQLCommand(const String&);
129
130     void checkForMaxSizeReached();
131     void checkForDeletedResources();
132     long long flatFileAreaSize();
133     
134     String m_cacheDirectory;
135     String m_cacheFile;
136
137     int64_t m_maximumSize;
138     bool m_isMaximumSizeReached;
139
140     int64_t m_defaultOriginQuota;
141
142     SQLiteDatabase m_database;
143
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;
147     
148     typedef HashMap<String, ApplicationCacheGroup*> CacheGroupMap;
149     CacheGroupMap m_cachesInMemory; // Excludes obsolete cache groups.
150
151     friend ApplicationCacheStorage& cacheStorage();
152 };
153  
154 ApplicationCacheStorage& cacheStorage();
155     
156 } // namespace WebCore
157
158 #endif // ENABLE(OFFLINE_WEB_APPLICATIONS)
159
160 #endif // ApplicationCacheStorage_h