OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "webkit/appcache/appcache.h" | 7 #include "webkit/appcache/appcache.h" |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "webkit/appcache/appcache_group.h" | 11 #include "webkit/appcache/appcache_group.h" |
12 #include "webkit/appcache/appcache_host.h" | 12 #include "webkit/appcache/appcache_host.h" |
13 #include "webkit/appcache/appcache_interfaces.h" | 13 #include "webkit/appcache/appcache_interfaces.h" |
14 #include "webkit/appcache/appcache_service.h" | 14 #include "webkit/appcache/appcache_service.h" |
15 #include "webkit/appcache/appcache_storage.h" | 15 #include "webkit/appcache/appcache_storage.h" |
16 | 16 |
17 namespace appcache { | 17 namespace appcache { |
18 | 18 |
19 AppCache::AppCache(AppCacheService *service, int64 cache_id) | 19 AppCache::AppCache(AppCacheService *service, int64 cache_id) |
20 : cache_id_(cache_id), | 20 : cache_id_(cache_id), |
21 owning_group_(NULL), | 21 owning_group_(NULL), |
22 online_whitelist_all_(false), | 22 online_whitelist_all_(false), |
23 is_complete_(false), | 23 is_complete_(false), |
| 24 cache_size_(0), |
24 service_(service) { | 25 service_(service) { |
25 service_->storage()->working_set()->AddCache(this); | 26 service_->storage()->working_set()->AddCache(this); |
26 } | 27 } |
27 | 28 |
28 AppCache::~AppCache() { | 29 AppCache::~AppCache() { |
29 DCHECK(associated_hosts_.empty()); | 30 DCHECK(associated_hosts_.empty()); |
30 if (owning_group_) { | 31 if (owning_group_) { |
31 DCHECK(is_complete_); | 32 DCHECK(is_complete_); |
32 owning_group_->RemoveCache(this); | 33 owning_group_->RemoveCache(this); |
33 } | 34 } |
(...skipping 10 matching lines...) Expand all Loading... |
44 entries_.insert(EntryMap::value_type(url, entry)); | 45 entries_.insert(EntryMap::value_type(url, entry)); |
45 } | 46 } |
46 | 47 |
47 bool AppCache::AddOrModifyEntry(const GURL& url, const AppCacheEntry& entry) { | 48 bool AppCache::AddOrModifyEntry(const GURL& url, const AppCacheEntry& entry) { |
48 std::pair<EntryMap::iterator, bool> ret = | 49 std::pair<EntryMap::iterator, bool> ret = |
49 entries_.insert(EntryMap::value_type(url, entry)); | 50 entries_.insert(EntryMap::value_type(url, entry)); |
50 | 51 |
51 // Entry already exists. Merge the types of the new and existing entries. | 52 // Entry already exists. Merge the types of the new and existing entries. |
52 if (!ret.second) | 53 if (!ret.second) |
53 ret.first->second.add_types(entry.types()); | 54 ret.first->second.add_types(entry.types()); |
54 | 55 else |
| 56 cache_size_ += entry.response_size(); // New entry. Add to cache size. |
55 return ret.second; | 57 return ret.second; |
56 } | 58 } |
57 | 59 |
58 AppCacheEntry* AppCache::GetEntry(const GURL& url) { | 60 AppCacheEntry* AppCache::GetEntry(const GURL& url) { |
59 EntryMap::iterator it = entries_.find(url); | 61 EntryMap::iterator it = entries_.find(url); |
60 return (it != entries_.end()) ? &(it->second) : NULL; | 62 return (it != entries_.end()) ? &(it->second) : NULL; |
61 } | 63 } |
62 | 64 |
63 namespace { | 65 namespace { |
64 | |
65 bool SortByLength( | 66 bool SortByLength( |
66 const FallbackNamespace& lhs, const FallbackNamespace& rhs) { | 67 const FallbackNamespace& lhs, const FallbackNamespace& rhs) { |
67 return lhs.first.spec().length() > rhs.first.spec().length(); | 68 return lhs.first.spec().length() > rhs.first.spec().length(); |
68 } | 69 } |
69 | |
70 } | 70 } |
71 | 71 |
72 void AppCache::InitializeWithManifest(Manifest* manifest) { | 72 void AppCache::InitializeWithManifest(Manifest* manifest) { |
73 DCHECK(manifest); | 73 DCHECK(manifest); |
74 fallback_namespaces_.swap(manifest->fallback_namespaces); | 74 fallback_namespaces_.swap(manifest->fallback_namespaces); |
75 online_whitelist_namespaces_.swap(manifest->online_whitelist_namespaces); | 75 online_whitelist_namespaces_.swap(manifest->online_whitelist_namespaces); |
76 online_whitelist_all_ = manifest->online_whitelist_all; | 76 online_whitelist_all_ = manifest->online_whitelist_all; |
77 | 77 |
78 // Sort the fallback namespaces by url string length, longest to shortest, | 78 // Sort the fallback namespaces by url string length, longest to shortest, |
79 // since longer matches trump when matching a url to a namespace. | 79 // since longer matches trump when matching a url to a namespace. |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 if (!online_whitelist_all_) { | 155 if (!online_whitelist_all_) { |
156 for (size_t i = 0; i < online_whitelist_namespaces_.size(); ++i) { | 156 for (size_t i = 0; i < online_whitelist_namespaces_.size(); ++i) { |
157 whitelists->push_back(AppCacheDatabase::OnlineWhiteListRecord()); | 157 whitelists->push_back(AppCacheDatabase::OnlineWhiteListRecord()); |
158 AppCacheDatabase::OnlineWhiteListRecord& record = whitelists->back(); | 158 AppCacheDatabase::OnlineWhiteListRecord& record = whitelists->back(); |
159 record.cache_id = cache_id_; | 159 record.cache_id = cache_id_; |
160 record.namespace_url = online_whitelist_namespaces_[i]; | 160 record.namespace_url = online_whitelist_namespaces_[i]; |
161 } | 161 } |
162 } | 162 } |
163 } | 163 } |
164 | 164 |
165 | |
166 bool AppCache::FindResponseForRequest(const GURL& url, | 165 bool AppCache::FindResponseForRequest(const GURL& url, |
167 AppCacheEntry* found_entry, AppCacheEntry* found_fallback_entry, | 166 AppCacheEntry* found_entry, AppCacheEntry* found_fallback_entry, |
168 GURL* found_fallback_namespace, bool* found_network_namespace) { | 167 GURL* found_fallback_namespace, bool* found_network_namespace) { |
169 // Ignore fragments when looking up URL in the cache. | 168 // Ignore fragments when looking up URL in the cache. |
170 GURL url_no_ref; | 169 GURL url_no_ref; |
171 if (url.has_ref()) { | 170 if (url.has_ref()) { |
172 GURL::Replacements replacements; | 171 GURL::Replacements replacements; |
173 replacements.ClearRef(); | 172 replacements.ClearRef(); |
174 url_no_ref = url.ReplaceComponents(replacements); | 173 url_no_ref = url.ReplaceComponents(replacements); |
175 } else { | 174 } else { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 for (size_t i = 0; i < count; ++i) { | 214 for (size_t i = 0; i < count; ++i) { |
216 if (StartsWithASCII( | 215 if (StartsWithASCII( |
217 url.spec(), online_whitelist_namespaces_[i].spec(), true)) { | 216 url.spec(), online_whitelist_namespaces_[i].spec(), true)) { |
218 return true; | 217 return true; |
219 } | 218 } |
220 } | 219 } |
221 return false; | 220 return false; |
222 } | 221 } |
223 | 222 |
224 } // namespace appcache | 223 } // namespace appcache |
OLD | NEW |