When Retrier succeeds, record errors it encountered.
[chromium-blink-merge.git] / webkit / appcache / appcache.h
blobfee31697e4822b41aa752b7c00565d27ce96fb93
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef WEBKIT_APPCACHE_APPCACHE_H_
6 #define WEBKIT_APPCACHE_APPCACHE_H_
8 #include <map>
9 #include <set>
10 #include <vector>
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/time.h"
15 #include "googleurl/src/gurl.h"
16 #include "webkit/appcache/appcache_database.h"
17 #include "webkit/appcache/appcache_entry.h"
18 #include "webkit/appcache/manifest_parser.h"
19 #include "webkit/storage/webkit_storage_export.h"
21 namespace appcache {
23 class AppCacheGroup;
24 class AppCacheHost;
25 class AppCacheStorage;
27 // Set of cached resources for an application. A cache exists as long as a
28 // host is associated with it, the cache is in an appcache group or the
29 // cache is being created during an appcache upate.
30 class WEBKIT_STORAGE_EXPORT AppCache : public base::RefCounted<AppCache> {
31 public:
32 typedef std::map<GURL, AppCacheEntry> EntryMap;
33 typedef std::set<AppCacheHost*> AppCacheHosts;
35 AppCache(AppCacheStorage* storage, int64 cache_id);
37 int64 cache_id() const { return cache_id_; }
39 AppCacheGroup* owning_group() const { return owning_group_; }
41 bool is_complete() const { return is_complete_; }
42 void set_complete(bool value) { is_complete_ = value; }
44 // Adds a new entry. Entry must not already be in cache.
45 void AddEntry(const GURL& url, const AppCacheEntry& entry);
47 // Adds a new entry or modifies an existing entry by merging the types
48 // of the new entry with the existing entry. Returns true if a new entry
49 // is added, false if the flags are merged into an existing entry.
50 bool AddOrModifyEntry(const GURL& url, const AppCacheEntry& entry);
52 // Removes an entry from the EntryMap, the URL must be in the set.
53 void RemoveEntry(const GURL& url);
55 // Do not store the returned object as it could be deleted anytime.
56 AppCacheEntry* GetEntry(const GURL& url);
57 const AppCacheEntry* GetEntryWithResponseId(int64 response_id);
59 const EntryMap& entries() const { return entries_; }
61 // Returns the URL of the resource used as entry for 'namespace_url'.
62 GURL GetFallbackEntryUrl(const GURL& namespace_url) const {
63 return GetNamespaceEntryUrl(fallback_namespaces_, namespace_url);
65 GURL GetInterceptEntryUrl(const GURL& namespace_url) const {
66 return GetNamespaceEntryUrl(intercept_namespaces_, namespace_url);
69 AppCacheHosts& associated_hosts() { return associated_hosts_; }
71 bool IsNewerThan(AppCache* cache) const {
72 // TODO(michaeln): revisit, the system clock can be set
73 // back in time which would confuse this logic.
74 if (update_time_ > cache->update_time_)
75 return true;
77 // Tie breaker. Newer caches have a larger cache ID.
78 if (update_time_ == cache->update_time_)
79 return cache_id_ > cache->cache_id_;
81 return false;
84 base::Time update_time() const { return update_time_; }
86 int64 cache_size() const { return cache_size_; }
88 void set_update_time(base::Time ticks) { update_time_ = ticks; }
90 // Initializes the cache with information in the manifest.
91 // Do not use the manifest after this call.
92 void InitializeWithManifest(Manifest* manifest);
94 // Initializes the cache with the information in the database records.
95 void InitializeWithDatabaseRecords(
96 const AppCacheDatabase::CacheRecord& cache_record,
97 const std::vector<AppCacheDatabase::EntryRecord>& entries,
98 const std::vector<AppCacheDatabase::NamespaceRecord>& intercepts,
99 const std::vector<AppCacheDatabase::NamespaceRecord>& fallbacks,
100 const std::vector<AppCacheDatabase::OnlineWhiteListRecord>& whitelists);
102 // Returns the database records to be stored in the AppCacheDatabase
103 // to represent this cache.
104 void ToDatabaseRecords(
105 const AppCacheGroup* group,
106 AppCacheDatabase::CacheRecord* cache_record,
107 std::vector<AppCacheDatabase::EntryRecord>* entries,
108 std::vector<AppCacheDatabase::NamespaceRecord>* intercepts,
109 std::vector<AppCacheDatabase::NamespaceRecord>* fallbacks,
110 std::vector<AppCacheDatabase::OnlineWhiteListRecord>* whitelists);
112 bool FindResponseForRequest(const GURL& url,
113 AppCacheEntry* found_entry, GURL* found_intercept_namespace,
114 AppCacheEntry* found_fallback_entry, GURL* found_fallback_namespace,
115 bool* found_network_namespace);
117 // Populates the 'infos' vector with an element per entry in the appcache.
118 void ToResourceInfoVector(AppCacheResourceInfoVector* infos) const;
120 static const Namespace* FindNamespace(
121 const NamespaceVector& namespaces,
122 const GURL& url);
124 private:
125 friend class AppCacheGroup;
126 friend class AppCacheHost;
127 friend class AppCacheStorageImplTest;
128 friend class AppCacheUpdateJobTest;
129 friend class base::RefCounted<AppCache>;
131 ~AppCache();
133 // Use AppCacheGroup::Add/RemoveCache() to manipulate owning group.
134 void set_owning_group(AppCacheGroup* group) { owning_group_ = group; }
136 // FindResponseForRequest helpers
137 const Namespace* FindInterceptNamespace(const GURL& url) {
138 return FindNamespace(intercept_namespaces_, url);
140 const Namespace* FindFallbackNamespace(const GURL& url) {
141 return FindNamespace(fallback_namespaces_, url);
143 bool IsInNetworkNamespace(const GURL& url) {
144 return FindNamespace(online_whitelist_namespaces_, url) != NULL;
147 GURL GetNamespaceEntryUrl(const NamespaceVector& namespaces,
148 const GURL& namespace_url) const;
150 // Use AppCacheHost::Associate*Cache() to manipulate host association.
151 void AssociateHost(AppCacheHost* host) {
152 associated_hosts_.insert(host);
154 void UnassociateHost(AppCacheHost* host);
156 const int64 cache_id_;
157 scoped_refptr<AppCacheGroup> owning_group_;
158 AppCacheHosts associated_hosts_;
160 EntryMap entries_; // contains entries of all types
162 NamespaceVector intercept_namespaces_;
163 NamespaceVector fallback_namespaces_;
164 NamespaceVector online_whitelist_namespaces_;
165 bool online_whitelist_all_;
167 bool is_complete_;
169 // when this cache was last updated
170 base::Time update_time_;
172 int64 cache_size_;
174 // to notify storage when cache is deleted
175 AppCacheStorage* storage_;
177 FRIEND_TEST_ALL_PREFIXES(AppCacheTest, InitializeWithManifest);
178 FRIEND_TEST_ALL_PREFIXES(AppCacheTest, ToFromDatabaseRecords);
179 DISALLOW_COPY_AND_ASSIGN(AppCache);
182 } // namespace appcache
184 #endif // WEBKIT_APPCACHE_APPCACHE_H_