Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: webkit/appcache/appcache.cc

Issue 8396013: AppCache INTERCEPT namespace. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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,
135 intercept.namespace_url,
136 intercept.target_url));
137 }
138
127 for (size_t i = 0; i < fallbacks.size(); ++i) { 139 for (size_t i = 0; i < fallbacks.size(); ++i) {
128 const AppCacheDatabase::FallbackNameSpaceRecord& fallback = fallbacks.at(i); 140 const AppCacheDatabase::NamespaceRecord& fallback = fallbacks.at(i);
129 fallback_namespaces_.push_back( 141 fallback_namespaces_.push_back(
130 FallbackNamespace(fallback.namespace_url, fallback.fallback_entry_url)); 142 Namespace(FALLBACK_NAMESPACE,
143 fallback.namespace_url,
144 fallback.target_url));
131 } 145 }
132 146
133 // Sort the fallback namespaces by url string length, longest to shortest, 147 // Sort the fallback namespaces by url string length, longest to shortest,
134 // since longer matches trump when matching a url to a namespace. 148 // since longer matches trump when matching a url to a namespace.
149 std::sort(intercept_namespaces_.begin(), intercept_namespaces_.end(),
150 SortNamespacesByLength);
135 std::sort(fallback_namespaces_.begin(), fallback_namespaces_.end(), 151 std::sort(fallback_namespaces_.begin(), fallback_namespaces_.end(),
136 SortByLength); 152 SortNamespacesByLength);
137 153
138 if (!online_whitelist_all_) { 154 if (!online_whitelist_all_) {
139 for (size_t i = 0; i < whitelists.size(); ++i) { 155 for (size_t i = 0; i < whitelists.size(); ++i) {
140 online_whitelist_namespaces_.push_back(whitelists.at(i).namespace_url); 156 online_whitelist_namespaces_.push_back(whitelists.at(i).namespace_url);
141 } 157 }
142 } 158 }
143 } 159 }
144 160
145 void AppCache::ToDatabaseRecords( 161 void AppCache::ToDatabaseRecords(
146 const AppCacheGroup* group, 162 const AppCacheGroup* group,
147 AppCacheDatabase::CacheRecord* cache_record, 163 AppCacheDatabase::CacheRecord* cache_record,
148 std::vector<AppCacheDatabase::EntryRecord>* entries, 164 std::vector<AppCacheDatabase::EntryRecord>* entries,
149 std::vector<AppCacheDatabase::FallbackNameSpaceRecord>* fallbacks, 165 std::vector<AppCacheDatabase::NamespaceRecord>* intercepts,
166 std::vector<AppCacheDatabase::NamespaceRecord>* fallbacks,
150 std::vector<AppCacheDatabase::OnlineWhiteListRecord>* whitelists) { 167 std::vector<AppCacheDatabase::OnlineWhiteListRecord>* whitelists) {
151 DCHECK(group && cache_record && entries && fallbacks && whitelists); 168 DCHECK(group && cache_record && entries && fallbacks && whitelists);
152 DCHECK(entries->empty() && fallbacks->empty() && whitelists->empty()); 169 DCHECK(entries->empty() && fallbacks->empty() && whitelists->empty());
153 170
154 cache_record->cache_id = cache_id_; 171 cache_record->cache_id = cache_id_;
155 cache_record->group_id = group->group_id(); 172 cache_record->group_id = group->group_id();
156 cache_record->online_wildcard = online_whitelist_all_; 173 cache_record->online_wildcard = online_whitelist_all_;
157 cache_record->update_time = update_time_; 174 cache_record->update_time = update_time_;
158 cache_record->cache_size = 0; 175 cache_record->cache_size = 0;
159 176
160 for (EntryMap::const_iterator iter = entries_.begin(); 177 for (EntryMap::const_iterator iter = entries_.begin();
161 iter != entries_.end(); ++iter) { 178 iter != entries_.end(); ++iter) {
162 entries->push_back(AppCacheDatabase::EntryRecord()); 179 entries->push_back(AppCacheDatabase::EntryRecord());
163 AppCacheDatabase::EntryRecord& record = entries->back(); 180 AppCacheDatabase::EntryRecord& record = entries->back();
164 record.url = iter->first; 181 record.url = iter->first;
165 record.cache_id = cache_id_; 182 record.cache_id = cache_id_;
166 record.flags = iter->second.types(); 183 record.flags = iter->second.types();
167 record.response_id = iter->second.response_id(); 184 record.response_id = iter->second.response_id();
168 record.response_size = iter->second.response_size(); 185 record.response_size = iter->second.response_size();
169 cache_record->cache_size += record.response_size; 186 cache_record->cache_size += record.response_size;
170 } 187 }
171 188
172 GURL origin = group->manifest_url().GetOrigin(); 189 GURL origin = group->manifest_url().GetOrigin();
173 190
174 for (size_t i = 0; i < fallback_namespaces_.size(); ++i) { 191 for (size_t i = 0; i < intercept_namespaces_.size(); ++i) {
175 fallbacks->push_back(AppCacheDatabase::FallbackNameSpaceRecord()); 192 intercepts->push_back(AppCacheDatabase::NamespaceRecord());
176 AppCacheDatabase::FallbackNameSpaceRecord& record = fallbacks->back(); 193 AppCacheDatabase::NamespaceRecord& record = intercepts->back();
177 record.cache_id = cache_id_; 194 record.cache_id = cache_id_;
178 record.origin = origin; 195 record.origin = origin;
179 record.namespace_url = fallback_namespaces_[i].first; 196 record.type = INTERCEPT_NAMESPACE;
180 record.fallback_entry_url = fallback_namespaces_[i].second; 197 record.namespace_url = intercept_namespaces_[i].namespace_url;
198 record.target_url = intercept_namespaces_[i].target_url;
199 }
200
201 for (size_t i = 0; i < fallback_namespaces_.size(); ++i) {
202 fallbacks->push_back(AppCacheDatabase::NamespaceRecord());
203 AppCacheDatabase::NamespaceRecord& record = fallbacks->back();
204 record.cache_id = cache_id_;
205 record.origin = origin;
206 record.type = FALLBACK_NAMESPACE;
207 record.namespace_url = fallback_namespaces_[i].namespace_url;
208 record.target_url = fallback_namespaces_[i].target_url;
181 } 209 }
182 210
183 if (!online_whitelist_all_) { 211 if (!online_whitelist_all_) {
184 for (size_t i = 0; i < online_whitelist_namespaces_.size(); ++i) { 212 for (size_t i = 0; i < online_whitelist_namespaces_.size(); ++i) {
185 whitelists->push_back(AppCacheDatabase::OnlineWhiteListRecord()); 213 whitelists->push_back(AppCacheDatabase::OnlineWhiteListRecord());
186 AppCacheDatabase::OnlineWhiteListRecord& record = whitelists->back(); 214 AppCacheDatabase::OnlineWhiteListRecord& record = whitelists->back();
187 record.cache_id = cache_id_; 215 record.cache_id = cache_id_;
188 record.namespace_url = online_whitelist_namespaces_[i]; 216 record.namespace_url = online_whitelist_namespaces_[i];
189 } 217 }
190 } 218 }
(...skipping 18 matching lines...) Expand all
209 if (entry) { 237 if (entry) {
210 *found_entry = *entry; 238 *found_entry = *entry;
211 return true; 239 return true;
212 } 240 }
213 241
214 if ((*found_network_namespace = 242 if ((*found_network_namespace =
215 IsInNetworkNamespace(url_no_ref, online_whitelist_namespaces_))) { 243 IsInNetworkNamespace(url_no_ref, online_whitelist_namespaces_))) {
216 return true; 244 return true;
217 } 245 }
218 246
219 FallbackNamespace* fallback_namespace = FindFallbackNamespace(url_no_ref); 247 const Namespace* intercept_namespace = FindInterceptNamespace(url_no_ref);
220 if (fallback_namespace) { 248 if (intercept_namespace) {
221 entry = GetEntry(fallback_namespace->second); 249 entry = GetEntry(intercept_namespace->target_url);
222 DCHECK(entry); 250 DCHECK(entry);
223 *found_fallback_entry = *entry; 251 *found_entry = *entry;
224 *found_fallback_namespace = fallback_namespace->first;
225 return true; 252 return true;
226 } 253 }
227 254
255 const Namespace* fallback_namespace = FindFallbackNamespace(url_no_ref);
256 if (fallback_namespace) {
257 entry = GetEntry(fallback_namespace->target_url);
258 DCHECK(entry);
259 *found_fallback_entry = *entry;
260 *found_fallback_namespace = fallback_namespace->namespace_url;
261 return true;
262 }
263
228 *found_network_namespace = online_whitelist_all_; 264 *found_network_namespace = online_whitelist_all_;
229 return *found_network_namespace; 265 return *found_network_namespace;
230 } 266 }
231 267
232 FallbackNamespace* AppCache::FindFallbackNamespace(const GURL& url) { 268 const Namespace* AppCache::FindNamespace(
233 size_t count = fallback_namespaces_.size(); 269 const NamespaceVector& namespaces, const GURL& url) {
270 size_t count = namespaces.size();
234 for (size_t i = 0; i < count; ++i) { 271 for (size_t i = 0; i < count; ++i) {
235 if (StartsWithASCII( 272 if (StartsWithASCII(
236 url.spec(), fallback_namespaces_[i].first.spec(), true)) { 273 url.spec(), namespaces[i].namespace_url.spec(), true)) {
237 return &fallback_namespaces_[i]; 274 return &namespaces[i];
238 } 275 }
239 } 276 }
240 return NULL; 277 return NULL;
241 } 278 }
242 279
243 void AppCache::ToResourceInfoVector(AppCacheResourceInfoVector* infos) const { 280 void AppCache::ToResourceInfoVector(AppCacheResourceInfoVector* infos) const {
244 DCHECK(infos && infos->empty()); 281 DCHECK(infos && infos->empty());
245 for (EntryMap::const_iterator iter = entries_.begin(); 282 for (EntryMap::const_iterator iter = entries_.begin();
246 iter != entries_.end(); ++iter) { 283 iter != entries_.end(); ++iter) {
247 infos->push_back(AppCacheResourceInfo()); 284 infos->push_back(AppCacheResourceInfo());
(...skipping 17 matching lines...) Expand all
265 // structures and algorithms that can be applied here and above. 302 // structures and algorithms that can be applied here and above.
266 size_t count = namespaces.size(); 303 size_t count = namespaces.size();
267 for (size_t i = 0; i < count; ++i) { 304 for (size_t i = 0; i < count; ++i) {
268 if (StartsWithASCII(url.spec(), namespaces[i].spec(), true)) 305 if (StartsWithASCII(url.spec(), namespaces[i].spec(), true))
269 return true; 306 return true;
270 } 307 }
271 return false; 308 return false;
272 } 309 }
273 310
274 } // namespace appcache 311 } // namespace appcache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698