• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ApplicationCacheStorage_h
27 #define ApplicationCacheStorage_h
28 
29 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
30 
31 #include "PlatformString.h"
32 #include "SQLiteDatabase.h"
33 #include "StringHash.h"
34 
35 #include <wtf/HashCountedSet.h>
36 
37 namespace WebCore {
38 
39 class ApplicationCache;
40 class ApplicationCacheHost;
41 class ApplicationCacheGroup;
42 class ApplicationCacheResource;
43 class KURL;
44 template <class T>
45 class StorageIDJournal;
46 
47 class ApplicationCacheStorage {
48 public:
49     void setCacheDirectory(const String&);
50     const String& cacheDirectory() const;
51 
52     void setMaximumSize(int64_t size);
53     int64_t maximumSize() const;
54     bool isMaximumSizeReached() const;
55     int64_t spaceNeeded(int64_t cacheToSave);
56 
57     ApplicationCacheGroup* cacheGroupForURL(const KURL&); // Cache to load a main resource from.
58     ApplicationCacheGroup* fallbackCacheGroupForURL(const KURL&); // Cache that has a fallback entry to load a main resource from if normal loading fails.
59 
60     ApplicationCacheGroup* findOrCreateCacheGroup(const KURL& manifestURL);
61     void cacheGroupDestroyed(ApplicationCacheGroup*);
62     void cacheGroupMadeObsolete(ApplicationCacheGroup*);
63 
64     bool storeNewestCache(ApplicationCacheGroup*); // Updates the cache group, but doesn't remove old cache.
65     bool store(ApplicationCacheResource*, ApplicationCache*);
66     bool storeUpdatedType(ApplicationCacheResource*, ApplicationCache*);
67 
68     // Removes the group if the cache to be removed is the newest one (so, storeNewestCache() needs to be called beforehand when updating).
69     void remove(ApplicationCache*);
70 
71     void empty();
72 
73     static bool storeCopyOfCache(const String& cacheDirectory, ApplicationCacheHost*);
74 
75     bool manifestURLs(Vector<KURL>* urls);
76     bool cacheGroupSize(const String& manifestURL, int64_t* size);
77     bool deleteCacheGroup(const String& manifestURL);
78     void vacuumDatabaseFile();
79 private:
80     ApplicationCacheStorage();
81     PassRefPtr<ApplicationCache> loadCache(unsigned storageID);
82     ApplicationCacheGroup* loadCacheGroup(const KURL& manifestURL);
83 
84     typedef StorageIDJournal<ApplicationCacheResource> ResourceStorageIDJournal;
85     typedef StorageIDJournal<ApplicationCacheGroup> GroupStorageIDJournal;
86 
87     bool store(ApplicationCacheGroup*, GroupStorageIDJournal*);
88     bool store(ApplicationCache*, ResourceStorageIDJournal*);
89     bool store(ApplicationCacheResource*, unsigned cacheStorageID);
90 
91     void loadManifestHostHashes();
92 
93     void verifySchemaVersion();
94 
95     void openDatabase(bool createIfDoesNotExist);
96 
97     bool executeStatement(SQLiteStatement&);
98     bool executeSQLCommand(const String&);
99 
100     void checkForMaxSizeReached();
101 
102     String m_cacheDirectory;
103     String m_cacheFile;
104 
105     int64_t m_maximumSize;
106     bool m_isMaximumSizeReached;
107 
108     SQLiteDatabase m_database;
109 
110     // In order to quickly determine if a given resource exists in an application cache,
111     // we keep a hash set of the hosts of the manifest URLs of all non-obsolete cache groups.
112     HashCountedSet<unsigned, AlreadyHashed> m_cacheHostSet;
113 
114     typedef HashMap<String, ApplicationCacheGroup*> CacheGroupMap;
115     CacheGroupMap m_cachesInMemory; // Excludes obsolete cache groups.
116 
117     friend ApplicationCacheStorage& cacheStorage();
118 };
119 
120 ApplicationCacheStorage& cacheStorage();
121 
122 } // namespace WebCore
123 
124 #endif // ENABLE(OFFLINE_WEB_APPLICATIONS)
125 
126 #endif // ApplicationCacheStorage_h
127