1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "webkit/appcache/mock_appcache_storage.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/ref_counted.h"
#include "base/stl_util-inl.h"
#include "webkit/appcache/appcache.h"
#include "webkit/appcache/appcache_entry.h"
#include "webkit/appcache/appcache_group.h"
#include "webkit/appcache/appcache_response.h"
// This is a quick and easy 'mock' implementation of the storage interface
// that doesn't put anything to disk.
//
// We simply add an extra reference to objects when they're put in storage,
// and remove the extra reference when they are removed from storage.
// Responses are never really removed from the in-memory disk cache.
// Delegate callbacks are made asyncly to appropiately mimic what will
// happen with a real disk-backed storage impl that involves IO on a
// background thread.
namespace appcache {
MockAppCacheStorage::MockAppCacheStorage(AppCacheService* service)
: AppCacheStorage(service),
ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
simulate_make_group_obsolete_failure_(false),
simulate_store_group_and_newest_cache_failure_(false),
simulate_find_main_resource_(false),
simulate_find_sub_resource_(false),
simulated_found_cache_id_(kNoCacheId),
simulated_found_network_namespace_(false) {
last_cache_id_ = 0;
last_entry_id_ = 0;
last_group_id_ = 0;
last_response_id_ = 0;
}
MockAppCacheStorage::~MockAppCacheStorage() {
STLDeleteElements(&pending_tasks_);
}
void MockAppCacheStorage::LoadCache(int64 id, Delegate* delegate) {
DCHECK(delegate);
AppCache* cache = working_set_.GetCache(id);
if (ShouldCacheLoadAppearAsync(cache)) {
ScheduleTask(method_factory_.NewRunnableMethod(
&MockAppCacheStorage::ProcessLoadCache,
id, GetOrCreateDelegateReference(delegate)));
return;
}
ProcessLoadCache(id, GetOrCreateDelegateReference(delegate));
}
void MockAppCacheStorage::LoadOrCreateGroup(
const GURL& manifest_url, Delegate* delegate) {
DCHECK(delegate);
AppCacheGroup* group = working_set_.GetGroup(manifest_url);
if (ShouldGroupLoadAppearAsync(group)) {
ScheduleTask(method_factory_.NewRunnableMethod(
&MockAppCacheStorage::ProcessLoadOrCreateGroup,
manifest_url, GetOrCreateDelegateReference(delegate)));
return;
}
ProcessLoadOrCreateGroup(
manifest_url, GetOrCreateDelegateReference(delegate));
}
void MockAppCacheStorage::StoreGroupAndNewestCache(
AppCacheGroup* group, AppCache* newest_cache, Delegate* delegate) {
DCHECK(group && delegate);
DCHECK(newest_cache && newest_cache->is_complete());
// Always make this operation look async.
ScheduleTask(method_factory_.NewRunnableMethod(
&MockAppCacheStorage::ProcessStoreGroupAndNewestCache,
group, newest_cache, GetOrCreateDelegateReference(delegate)));
}
void MockAppCacheStorage::FindResponseForMainRequest(
const GURL& url, Delegate* delegate) {
DCHECK(delegate);
// Always make this operation look async.
ScheduleTask(method_factory_.NewRunnableMethod(
&MockAppCacheStorage::ProcessFindResponseForMainRequest,
url, GetOrCreateDelegateReference(delegate)));
}
void MockAppCacheStorage::FindResponseForSubRequest(
AppCache* cache, const GURL& url,
AppCacheEntry* found_entry, AppCacheEntry* found_fallback_entry,
bool* found_network_namespace) {
DCHECK(cache && cache->is_complete());
// This layer of indirection is here to facilitate testing.
if (simulate_find_sub_resource_) {
*found_entry = simulated_found_entry_;
*found_fallback_entry = simulated_found_fallback_entry_;
*found_network_namespace = simulated_found_network_namespace_;
simulate_find_sub_resource_ = false;
return;
}
cache->FindResponseForRequest(url, found_entry, found_fallback_entry,
found_network_namespace);
}
void MockAppCacheStorage::MarkEntryAsForeign(
const GURL& entry_url, int64 cache_id) {
AppCache* cache = working_set_.GetCache(cache_id);
if (cache) {
AppCacheEntry* entry = cache->GetEntry(entry_url);
DCHECK(entry);
if (entry)
entry->add_types(AppCacheEntry::FOREIGN);
}
// TODO(michaeln): in real storage update in storage, and if this cache is
// being loaded be sure to update the memory cache upon load completion.
}
void MockAppCacheStorage::MakeGroupObsolete(
AppCacheGroup* group, Delegate* delegate) {
DCHECK(group && delegate);
// Always make this method look async.
ScheduleTask(method_factory_.NewRunnableMethod(
&MockAppCacheStorage::ProcessMakeGroupObsolete,
group, GetOrCreateDelegateReference(delegate)));
}
AppCacheResponseReader* MockAppCacheStorage::CreateResponseReader(
const GURL& origin, int64 response_id) {
return new AppCacheResponseReader(response_id, disk_cache());
}
AppCacheResponseWriter* MockAppCacheStorage::CreateResponseWriter(
const GURL& manifest_url) {
return new AppCacheResponseWriter(NewResponseId(), disk_cache());
}
void MockAppCacheStorage::DoomResponses(
const GURL& manifest_url, const std::vector<int64>& response_ids) {
// We don't bother with actually removing responses from the disk-cache,
// just keep track of which ids have been doomed.
std::vector<int64>::const_iterator it = response_ids.begin();
while (it != response_ids.end()) {
doomed_response_ids_.insert(*it);
++it;
}
}
void MockAppCacheStorage::ProcessLoadCache(
int64 id, scoped_refptr<DelegateReference> delegate_ref) {
AppCache* cache = working_set_.GetCache(id);
if (delegate_ref->delegate)
delegate_ref->delegate->OnCacheLoaded(cache, id);
}
void MockAppCacheStorage::ProcessLoadOrCreateGroup(
const GURL& manifest_url, scoped_refptr<DelegateReference> delegate_ref) {
scoped_refptr<AppCacheGroup> group = working_set_.GetGroup(manifest_url);
// Newly created groups are not put in the stored_groups collection
// until StoreGroupAndNewestCache is called.
if (!group)
group = new AppCacheGroup(service_, manifest_url);
if (delegate_ref->delegate)
delegate_ref->delegate->OnGroupLoaded(group, manifest_url);
}
void MockAppCacheStorage::ProcessStoreGroupAndNewestCache(
scoped_refptr<AppCacheGroup> group,
scoped_refptr<AppCache> newest_cache,
scoped_refptr<DelegateReference> delegate_ref) {
if (simulate_store_group_and_newest_cache_failure_) {
if (delegate_ref->delegate)
delegate_ref->delegate->OnGroupAndNewestCacheStored(group, false);
return;
}
AddStoredGroup(group);
AddStoredCache(newest_cache);
group->AddCache(newest_cache);
// Copy the collection prior to removal, on final release
// of a cache the group's collection will change.
AppCacheGroup::Caches copy = group->old_caches();
RemoveStoredCaches(copy);
if (delegate_ref->delegate)
delegate_ref->delegate->OnGroupAndNewestCacheStored(group, true);
// We don't bother with removing responses from 'mock' storage
// TODO(michaeln): for 'real' storage...
// std::set<int64> doomed_responses_ = responses from old caches
// std::set<int64> needed_responses_ = responses from newest cache
// foreach(needed_responses_)
// doomed_responses_.remove(needed_response_);
// DoomResponses(group->manifest_url(), doomed_responses_);
}
void MockAppCacheStorage::ProcessFindResponseForMainRequest(
const GURL& url, scoped_refptr<DelegateReference> delegate_ref) {
// TODO(michaeln): write me when doing AppCacheRequestHandler
// foreach(stored_group) {
// if (group->manifest_url()->origin() != url.GetOrigin())
// continue;
// look for an entry
// look for a fallback namespace
// look for a online namespace
// }
AppCacheEntry found_entry;
AppCacheEntry found_fallback_entry;
int64 found_cache_id = kNoCacheId;
GURL found_manifest_url = GURL();
if (simulate_find_main_resource_) {
found_entry = simulated_found_entry_;
found_fallback_entry = simulated_found_fallback_entry_;
found_cache_id = simulated_found_cache_id_;
found_manifest_url = simulated_found_manifest_url_;
simulate_find_main_resource_ = false;
}
if (delegate_ref->delegate) {
delegate_ref->delegate->OnMainResponseFound(
url, found_entry, found_fallback_entry,
found_cache_id, found_manifest_url);
}
}
void MockAppCacheStorage::ProcessMakeGroupObsolete(
scoped_refptr<AppCacheGroup> group,
scoped_refptr<DelegateReference> delegate_ref) {
if (simulate_make_group_obsolete_failure_) {
if (delegate_ref->delegate)
delegate_ref->delegate->OnGroupMadeObsolete(group, false);
return;
}
RemoveStoredGroup(group);
if (group->newest_complete_cache())
RemoveStoredCache(group->newest_complete_cache());
// Copy the collection prior to removal, on final release
// of a cache the group's collection will change.
AppCacheGroup::Caches copy = group->old_caches();
RemoveStoredCaches(copy);
group->set_obsolete(true);
if (delegate_ref->delegate)
delegate_ref->delegate->OnGroupMadeObsolete(group, true);
}
void MockAppCacheStorage::ScheduleTask(Task* task) {
pending_tasks_.push_back(task);
MessageLoop::current()->PostTask(FROM_HERE,
method_factory_.NewRunnableMethod(
&MockAppCacheStorage::RunOnePendingTask));
}
void MockAppCacheStorage::RunOnePendingTask() {
DCHECK(!pending_tasks_.empty());
Task* task = pending_tasks_.front();
pending_tasks_.pop_front();
task->Run();
delete task;
}
void MockAppCacheStorage::AddStoredCache(AppCache* cache) {
int64 cache_id = cache->cache_id();
if (stored_caches_.find(cache_id) == stored_caches_.end())
stored_caches_.insert(StoredCacheMap::value_type(cache_id, cache));
}
void MockAppCacheStorage::RemoveStoredCache(AppCache* cache) {
// Do not remove from the working set, active caches are still usable
// and may be looked up by id until they fall out of use.
stored_caches_.erase(cache->cache_id());
}
void MockAppCacheStorage::RemoveStoredCaches(
const AppCacheGroup::Caches& caches) {
AppCacheGroup::Caches::const_iterator it = caches.begin();
while (it != caches.end()) {
RemoveStoredCache(*it);
++it;
}
}
void MockAppCacheStorage::AddStoredGroup(AppCacheGroup* group) {
const GURL& url = group->manifest_url();
if (stored_groups_.find(url) == stored_groups_.end())
stored_groups_.insert(StoredGroupMap::value_type(url, group));
}
void MockAppCacheStorage::RemoveStoredGroup(AppCacheGroup* group) {
// Also remove from the working set, caches for an 'obsolete' group
// may linger in use, but the group itself cannot be looked up by
// 'manifest_url' in the working set any longer.
working_set()->RemoveGroup(group);
stored_groups_.erase(group->manifest_url());
}
bool MockAppCacheStorage::ShouldGroupLoadAppearAsync(
const AppCacheGroup* group) {
// We'll have to query the database to see if a group for the
// manifest_url exists on disk. So return true for async.
if (!group)
return true;
// Groups without a newest cache can't have been put to disk yet, so
// we can synchronously return a reference we have in the working set.
if (!group->newest_complete_cache())
return false;
// The LoadGroup interface implies also loading the newest cache, so
// if loading the newest cache should appear async, so too must the
// loading of this group.
if (ShouldCacheLoadAppearAsync(group->newest_complete_cache()))
return true;
// If any of the old caches are "in use", then the group must also
// be memory resident and not require async loading.
const AppCacheGroup::Caches& old_caches = group->old_caches();
AppCacheGroup::Caches::const_iterator it = old_caches.begin();
while (it != old_caches.end()) {
// "in use" caches don't require async loading
if (!ShouldCacheLoadAppearAsync(*it))
return false;
++it;
}
return true;
}
bool MockAppCacheStorage::ShouldCacheLoadAppearAsync(const AppCache* cache) {
if (!cache)
return true;
// If the 'stored' ref is the only ref, real storage will have to load from
// the database.
return IsCacheStored(cache) && cache->HasOneRef();
}
} // namespace appcache
|