| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 iter != entries_.end(); ++iter) { | 75 iter != entries_.end(); ++iter) { |
| 76 if (iter->second.response_id() == response_id) | 76 if (iter->second.response_id() == response_id) |
| 77 return &iter->second; | 77 return &iter->second; |
| 78 } | 78 } |
| 79 return NULL; | 79 return NULL; |
| 80 } | 80 } |
| 81 | 81 |
| 82 GURL AppCache::GetFallbackEntryUrl(const GURL& namespace_url) const { | 82 GURL AppCache::GetFallbackEntryUrl(const GURL& namespace_url) const { |
| 83 size_t count = fallback_namespaces_.size(); | 83 size_t count = fallback_namespaces_.size(); |
| 84 for (size_t i = 0; i < count; ++i) { | 84 for (size_t i = 0; i < count; ++i) { |
| 85 if (fallback_namespaces_[i].first == namespace_url) | 85 if (fallback_namespaces_[i].namespace_url == namespace_url) |
| 86 return fallback_namespaces_[i].second; | 86 return fallback_namespaces_[i].target_url; |
| 87 } | 87 } |
| 88 NOTREACHED(); | 88 NOTREACHED(); |
| 89 return GURL(); | 89 return GURL(); |
| 90 } | 90 } |
| 91 | 91 |
| 92 namespace { | 92 namespace { |
| 93 bool SortByLength( | 93 bool SortNamespacesByLength( |
| 94 const FallbackNamespace& lhs, const FallbackNamespace& rhs) { | 94 const Namespace& lhs, const Namespace& rhs) { |
| 95 return lhs.first.spec().length() > rhs.first.spec().length(); | 95 return lhs.namespace_url.spec().length() > rhs.namespace_url.spec().length(); |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 | 98 |
| 99 void AppCache::InitializeWithManifest(Manifest* manifest) { | 99 void AppCache::InitializeWithManifest(Manifest* manifest) { |
| 100 DCHECK(manifest); | 100 DCHECK(manifest); |
| 101 intercept_namespaces_.swap(manifest->intercept_namespaces); |
| 101 fallback_namespaces_.swap(manifest->fallback_namespaces); | 102 fallback_namespaces_.swap(manifest->fallback_namespaces); |
| 102 online_whitelist_namespaces_.swap(manifest->online_whitelist_namespaces); | 103 online_whitelist_namespaces_.swap(manifest->online_whitelist_namespaces); |
| 103 online_whitelist_all_ = manifest->online_whitelist_all; | 104 online_whitelist_all_ = manifest->online_whitelist_all; |
| 104 | 105 |
| 105 // Sort the fallback namespaces by url string length, longest to shortest, | 106 // Sort the namespaces by url string length, longest to shortest, |
| 106 // since longer matches trump when matching a url to a namespace. | 107 // since longer matches trump when matching a url to a namespace. |
| 108 std::sort(intercept_namespaces_.begin(), intercept_namespaces_.end(), |
| 109 SortNamespacesByLength); |
| 107 std::sort(fallback_namespaces_.begin(), fallback_namespaces_.end(), | 110 std::sort(fallback_namespaces_.begin(), fallback_namespaces_.end(), |
| 108 SortByLength); | 111 SortNamespacesByLength); |
| 109 } | 112 } |
| 110 | 113 |
| 111 void AppCache::InitializeWithDatabaseRecords( | 114 void AppCache::InitializeWithDatabaseRecords( |
| 112 const AppCacheDatabase::CacheRecord& cache_record, | 115 const AppCacheDatabase::CacheRecord& cache_record, |
| 113 const std::vector<AppCacheDatabase::EntryRecord>& entries, | 116 const std::vector<AppCacheDatabase::EntryRecord>& entries, |
| 114 const std::vector<AppCacheDatabase::FallbackNameSpaceRecord>& fallbacks, | 117 const std::vector<AppCacheDatabase::NamespaceRecord>& intercepts, |
| 118 const std::vector<AppCacheDatabase::NamespaceRecord>& fallbacks, |
| 115 const std::vector<AppCacheDatabase::OnlineWhiteListRecord>& whitelists) { | 119 const std::vector<AppCacheDatabase::OnlineWhiteListRecord>& whitelists) { |
| 116 DCHECK(cache_id_ == cache_record.cache_id); | 120 DCHECK(cache_id_ == cache_record.cache_id); |
| 117 online_whitelist_all_ = cache_record.online_wildcard; | 121 online_whitelist_all_ = cache_record.online_wildcard; |
| 118 update_time_ = cache_record.update_time; | 122 update_time_ = cache_record.update_time; |
| 119 | 123 |
| 120 for (size_t i = 0; i < entries.size(); ++i) { | 124 for (size_t i = 0; i < entries.size(); ++i) { |
| 121 const AppCacheDatabase::EntryRecord& entry = entries.at(i); | 125 const AppCacheDatabase::EntryRecord& entry = entries.at(i); |
| 122 AddEntry(entry.url, AppCacheEntry(entry.flags, entry.response_id, | 126 AddEntry(entry.url, AppCacheEntry(entry.flags, entry.response_id, |
| 123 entry.response_size)); | 127 entry.response_size)); |
| 124 } | 128 } |
| 125 DCHECK(cache_size_ == cache_record.cache_size); | 129 DCHECK(cache_size_ == cache_record.cache_size); |
| 126 | 130 |
| 131 for (size_t i = 0; i < intercepts.size(); ++i) { |
| 132 const AppCacheDatabase::NamespaceRecord& intercept = intercepts.at(i); |
| 133 intercept_namespaces_.push_back( |
| 134 Namespace(INTERCEPT_NAMESPACE, intercept.namespace_url, intercept.target
_url)); |
| 135 } |
| 136 |
| 127 for (size_t i = 0; i < fallbacks.size(); ++i) { | 137 for (size_t i = 0; i < fallbacks.size(); ++i) { |
| 128 const AppCacheDatabase::FallbackNameSpaceRecord& fallback = fallbacks.at(i); | 138 const AppCacheDatabase::NamespaceRecord& fallback = fallbacks.at(i); |
| 129 fallback_namespaces_.push_back( | 139 fallback_namespaces_.push_back( |
| 130 FallbackNamespace(fallback.namespace_url, fallback.fallback_entry_url)); | 140 Namespace(FALLBACK_NAMESPACE, fallback.namespace_url, fallback.target_ur
l)); |
| 131 } | 141 } |
| 132 | 142 |
| 133 // Sort the fallback namespaces by url string length, longest to shortest, | 143 // Sort the fallback namespaces by url string length, longest to shortest, |
| 134 // since longer matches trump when matching a url to a namespace. | 144 // since longer matches trump when matching a url to a namespace. |
| 145 std::sort(intercept_namespaces_.begin(), intercept_namespaces_.end(), |
| 146 SortNamespacesByLength); |
| 135 std::sort(fallback_namespaces_.begin(), fallback_namespaces_.end(), | 147 std::sort(fallback_namespaces_.begin(), fallback_namespaces_.end(), |
| 136 SortByLength); | 148 SortNamespacesByLength); |
| 137 | 149 |
| 138 if (!online_whitelist_all_) { | 150 if (!online_whitelist_all_) { |
| 139 for (size_t i = 0; i < whitelists.size(); ++i) { | 151 for (size_t i = 0; i < whitelists.size(); ++i) { |
| 140 online_whitelist_namespaces_.push_back(whitelists.at(i).namespace_url); | 152 online_whitelist_namespaces_.push_back(whitelists.at(i).namespace_url); |
| 141 } | 153 } |
| 142 } | 154 } |
| 143 } | 155 } |
| 144 | 156 |
| 145 void AppCache::ToDatabaseRecords( | 157 void AppCache::ToDatabaseRecords( |
| 146 const AppCacheGroup* group, | 158 const AppCacheGroup* group, |
| 147 AppCacheDatabase::CacheRecord* cache_record, | 159 AppCacheDatabase::CacheRecord* cache_record, |
| 148 std::vector<AppCacheDatabase::EntryRecord>* entries, | 160 std::vector<AppCacheDatabase::EntryRecord>* entries, |
| 149 std::vector<AppCacheDatabase::FallbackNameSpaceRecord>* fallbacks, | 161 std::vector<AppCacheDatabase::NamespaceRecord>* intercepts, |
| 162 std::vector<AppCacheDatabase::NamespaceRecord>* fallbacks, |
| 150 std::vector<AppCacheDatabase::OnlineWhiteListRecord>* whitelists) { | 163 std::vector<AppCacheDatabase::OnlineWhiteListRecord>* whitelists) { |
| 151 DCHECK(group && cache_record && entries && fallbacks && whitelists); | 164 DCHECK(group && cache_record && entries && fallbacks && whitelists); |
| 152 DCHECK(entries->empty() && fallbacks->empty() && whitelists->empty()); | 165 DCHECK(entries->empty() && fallbacks->empty() && whitelists->empty()); |
| 153 | 166 |
| 154 cache_record->cache_id = cache_id_; | 167 cache_record->cache_id = cache_id_; |
| 155 cache_record->group_id = group->group_id(); | 168 cache_record->group_id = group->group_id(); |
| 156 cache_record->online_wildcard = online_whitelist_all_; | 169 cache_record->online_wildcard = online_whitelist_all_; |
| 157 cache_record->update_time = update_time_; | 170 cache_record->update_time = update_time_; |
| 158 cache_record->cache_size = 0; | 171 cache_record->cache_size = 0; |
| 159 | 172 |
| 160 for (EntryMap::const_iterator iter = entries_.begin(); | 173 for (EntryMap::const_iterator iter = entries_.begin(); |
| 161 iter != entries_.end(); ++iter) { | 174 iter != entries_.end(); ++iter) { |
| 162 entries->push_back(AppCacheDatabase::EntryRecord()); | 175 entries->push_back(AppCacheDatabase::EntryRecord()); |
| 163 AppCacheDatabase::EntryRecord& record = entries->back(); | 176 AppCacheDatabase::EntryRecord& record = entries->back(); |
| 164 record.url = iter->first; | 177 record.url = iter->first; |
| 165 record.cache_id = cache_id_; | 178 record.cache_id = cache_id_; |
| 166 record.flags = iter->second.types(); | 179 record.flags = iter->second.types(); |
| 167 record.response_id = iter->second.response_id(); | 180 record.response_id = iter->second.response_id(); |
| 168 record.response_size = iter->second.response_size(); | 181 record.response_size = iter->second.response_size(); |
| 169 cache_record->cache_size += record.response_size; | 182 cache_record->cache_size += record.response_size; |
| 170 } | 183 } |
| 171 | 184 |
| 172 GURL origin = group->manifest_url().GetOrigin(); | 185 GURL origin = group->manifest_url().GetOrigin(); |
| 173 | 186 |
| 174 for (size_t i = 0; i < fallback_namespaces_.size(); ++i) { | 187 for (size_t i = 0; i < intercept_namespaces_.size(); ++i) { |
| 175 fallbacks->push_back(AppCacheDatabase::FallbackNameSpaceRecord()); | 188 intercepts->push_back(AppCacheDatabase::NamespaceRecord()); |
| 176 AppCacheDatabase::FallbackNameSpaceRecord& record = fallbacks->back(); | 189 AppCacheDatabase::NamespaceRecord& record = intercepts->back(); |
| 177 record.cache_id = cache_id_; | 190 record.cache_id = cache_id_; |
| 178 record.origin = origin; | 191 record.origin = origin; |
| 179 record.namespace_url = fallback_namespaces_[i].first; | 192 record.type = INTERCEPT_NAMESPACE; |
| 180 record.fallback_entry_url = fallback_namespaces_[i].second; | 193 record.namespace_url = intercept_namespaces_[i].namespace_url; |
| 194 record.target_url = intercept_namespaces_[i].target_url; |
| 195 } |
| 196 |
| 197 for (size_t i = 0; i < fallback_namespaces_.size(); ++i) { |
| 198 fallbacks->push_back(AppCacheDatabase::NamespaceRecord()); |
| 199 AppCacheDatabase::NamespaceRecord& record = fallbacks->back(); |
| 200 record.cache_id = cache_id_; |
| 201 record.origin = origin; |
| 202 record.type = FALLBACK_NAMESPACE; |
| 203 record.namespace_url = fallback_namespaces_[i].namespace_url; |
| 204 record.target_url = fallback_namespaces_[i].target_url; |
| 181 } | 205 } |
| 182 | 206 |
| 183 if (!online_whitelist_all_) { | 207 if (!online_whitelist_all_) { |
| 184 for (size_t i = 0; i < online_whitelist_namespaces_.size(); ++i) { | 208 for (size_t i = 0; i < online_whitelist_namespaces_.size(); ++i) { |
| 185 whitelists->push_back(AppCacheDatabase::OnlineWhiteListRecord()); | 209 whitelists->push_back(AppCacheDatabase::OnlineWhiteListRecord()); |
| 186 AppCacheDatabase::OnlineWhiteListRecord& record = whitelists->back(); | 210 AppCacheDatabase::OnlineWhiteListRecord& record = whitelists->back(); |
| 187 record.cache_id = cache_id_; | 211 record.cache_id = cache_id_; |
| 188 record.namespace_url = online_whitelist_namespaces_[i]; | 212 record.namespace_url = online_whitelist_namespaces_[i]; |
| 189 } | 213 } |
| 190 } | 214 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 209 if (entry) { | 233 if (entry) { |
| 210 *found_entry = *entry; | 234 *found_entry = *entry; |
| 211 return true; | 235 return true; |
| 212 } | 236 } |
| 213 | 237 |
| 214 if ((*found_network_namespace = | 238 if ((*found_network_namespace = |
| 215 IsInNetworkNamespace(url_no_ref, online_whitelist_namespaces_))) { | 239 IsInNetworkNamespace(url_no_ref, online_whitelist_namespaces_))) { |
| 216 return true; | 240 return true; |
| 217 } | 241 } |
| 218 | 242 |
| 219 FallbackNamespace* fallback_namespace = FindFallbackNamespace(url_no_ref); | 243 const Namespace* intercept_namespace = FindInterceptNamespace(url_no_ref); |
| 220 if (fallback_namespace) { | 244 if (intercept_namespace) { |
| 221 entry = GetEntry(fallback_namespace->second); | 245 entry = GetEntry(intercept_namespace->target_url); |
| 222 DCHECK(entry); | 246 DCHECK(entry); |
| 223 *found_fallback_entry = *entry; | 247 *found_entry = *entry; |
| 224 *found_fallback_namespace = fallback_namespace->first; | |
| 225 return true; | 248 return true; |
| 226 } | 249 } |
| 227 | 250 |
| 251 const Namespace* fallback_namespace = FindFallbackNamespace(url_no_ref); |
| 252 if (fallback_namespace) { |
| 253 entry = GetEntry(fallback_namespace->target_url); |
| 254 DCHECK(entry); |
| 255 *found_fallback_entry = *entry; |
| 256 *found_fallback_namespace = fallback_namespace->namespace_url; |
| 257 return true; |
| 258 } |
| 259 |
| 228 *found_network_namespace = online_whitelist_all_; | 260 *found_network_namespace = online_whitelist_all_; |
| 229 return *found_network_namespace; | 261 return *found_network_namespace; |
| 230 } | 262 } |
| 231 | 263 |
| 232 FallbackNamespace* AppCache::FindFallbackNamespace(const GURL& url) { | 264 const Namespace* AppCache::FindNamespace( |
| 233 size_t count = fallback_namespaces_.size(); | 265 const NamespaceVector& namespaces, const GURL& url) { |
| 266 size_t count = namespaces.size(); |
| 234 for (size_t i = 0; i < count; ++i) { | 267 for (size_t i = 0; i < count; ++i) { |
| 235 if (StartsWithASCII( | 268 if (StartsWithASCII( |
| 236 url.spec(), fallback_namespaces_[i].first.spec(), true)) { | 269 url.spec(), namespaces[i].namespace_url.spec(), true)) { |
| 237 return &fallback_namespaces_[i]; | 270 return &namespaces[i]; |
| 238 } | 271 } |
| 239 } | 272 } |
| 240 return NULL; | 273 return NULL; |
| 241 } | 274 } |
| 242 | 275 |
| 243 void AppCache::ToResourceInfoVector(AppCacheResourceInfoVector* infos) const { | 276 void AppCache::ToResourceInfoVector(AppCacheResourceInfoVector* infos) const { |
| 244 DCHECK(infos && infos->empty()); | 277 DCHECK(infos && infos->empty()); |
| 245 for (EntryMap::const_iterator iter = entries_.begin(); | 278 for (EntryMap::const_iterator iter = entries_.begin(); |
| 246 iter != entries_.end(); ++iter) { | 279 iter != entries_.end(); ++iter) { |
| 247 infos->push_back(AppCacheResourceInfo()); | 280 infos->push_back(AppCacheResourceInfo()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 265 // structures and algorithms that can be applied here and above. | 298 // structures and algorithms that can be applied here and above. |
| 266 size_t count = namespaces.size(); | 299 size_t count = namespaces.size(); |
| 267 for (size_t i = 0; i < count; ++i) { | 300 for (size_t i = 0; i < count; ++i) { |
| 268 if (StartsWithASCII(url.spec(), namespaces[i].spec(), true)) | 301 if (StartsWithASCII(url.spec(), namespaces[i].spec(), true)) |
| 269 return true; | 302 return true; |
| 270 } | 303 } |
| 271 return false; | 304 return false; |
| 272 } | 305 } |
| 273 | 306 |
| 274 } // namespace appcache | 307 } // namespace appcache |
| OLD | NEW |