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 "webkit/appcache/mock_appcache_storage.h" | 5 #include "webkit/appcache/mock_appcache_storage.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
10 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
11 #include "webkit/appcache/appcache.h" | 12 #include "webkit/appcache/appcache.h" |
12 #include "webkit/appcache/appcache_entry.h" | 13 #include "webkit/appcache/appcache_entry.h" |
13 #include "webkit/appcache/appcache_group.h" | 14 #include "webkit/appcache/appcache_group.h" |
14 #include "webkit/appcache/appcache_response.h" | 15 #include "webkit/appcache/appcache_response.h" |
15 #include "webkit/appcache/appcache_service.h" | 16 #include "webkit/appcache/appcache_service.h" |
16 | 17 |
17 // This is a quick and easy 'mock' implementation of the storage interface | 18 // This is a quick and easy 'mock' implementation of the storage interface |
18 // that doesn't put anything to disk. | 19 // that doesn't put anything to disk. |
19 // | 20 // |
20 // We simply add an extra reference to objects when they're put in storage, | 21 // We simply add an extra reference to objects when they're put in storage, |
21 // and remove the extra reference when they are removed from storage. | 22 // and remove the extra reference when they are removed from storage. |
22 // Responses are never really removed from the in-memory disk cache. | 23 // Responses are never really removed from the in-memory disk cache. |
23 // Delegate callbacks are made asyncly to appropiately mimic what will | 24 // Delegate callbacks are made asyncly to appropiately mimic what will |
24 // happen with a real disk-backed storage impl that involves IO on a | 25 // happen with a real disk-backed storage impl that involves IO on a |
25 // background thread. | 26 // background thread. |
26 | 27 |
27 namespace appcache { | 28 namespace appcache { |
28 | 29 |
29 MockAppCacheStorage::MockAppCacheStorage(AppCacheService* service) | 30 MockAppCacheStorage::MockAppCacheStorage(AppCacheService* service) |
30 : AppCacheStorage(service), | 31 : AppCacheStorage(service), |
31 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 32 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
32 simulate_make_group_obsolete_failure_(false), | 33 simulate_make_group_obsolete_failure_(false), |
33 simulate_store_group_and_newest_cache_failure_(false), | 34 simulate_store_group_and_newest_cache_failure_(false), |
34 simulate_find_main_resource_(false), | 35 simulate_find_main_resource_(false), |
35 simulate_find_sub_resource_(false), | 36 simulate_find_sub_resource_(false), |
36 simulated_found_cache_id_(kNoCacheId), | 37 simulated_found_cache_id_(kNoCacheId), |
37 simulated_found_group_id_(0), | 38 simulated_found_group_id_(0), |
38 simulated_found_network_namespace_(false) { | 39 simulated_found_network_namespace_(false) { |
39 last_cache_id_ = 0; | 40 last_cache_id_ = 0; |
40 last_group_id_ = 0; | 41 last_group_id_ = 0; |
41 last_response_id_ = 0; | 42 last_response_id_ = 0; |
42 } | 43 } |
43 | 44 |
44 MockAppCacheStorage::~MockAppCacheStorage() { | 45 MockAppCacheStorage::~MockAppCacheStorage() { |
45 STLDeleteElements(&pending_tasks_); | |
46 } | 46 } |
47 | 47 |
48 void MockAppCacheStorage::GetAllInfo(Delegate* delegate) { | 48 void MockAppCacheStorage::GetAllInfo(Delegate* delegate) { |
49 ScheduleTask(method_factory_.NewRunnableMethod( | 49 ScheduleTask( |
50 &MockAppCacheStorage::ProcessGetAllInfo, | 50 base::Bind(&MockAppCacheStorage::ProcessGetAllInfo, |
51 make_scoped_refptr(GetOrCreateDelegateReference(delegate)))); | 51 weak_factory_.GetWeakPtr(), |
| 52 make_scoped_refptr(GetOrCreateDelegateReference(delegate)))); |
52 } | 53 } |
53 | 54 |
54 void MockAppCacheStorage::LoadCache(int64 id, Delegate* delegate) { | 55 void MockAppCacheStorage::LoadCache(int64 id, Delegate* delegate) { |
55 DCHECK(delegate); | 56 DCHECK(delegate); |
56 AppCache* cache = working_set_.GetCache(id); | 57 AppCache* cache = working_set_.GetCache(id); |
57 if (ShouldCacheLoadAppearAsync(cache)) { | 58 if (ShouldCacheLoadAppearAsync(cache)) { |
58 ScheduleTask(method_factory_.NewRunnableMethod( | 59 ScheduleTask( |
59 &MockAppCacheStorage::ProcessLoadCache, | 60 base::Bind(&MockAppCacheStorage::ProcessLoadCache, |
60 id, make_scoped_refptr(GetOrCreateDelegateReference(delegate)))); | 61 weak_factory_.GetWeakPtr(), id, |
| 62 make_scoped_refptr(GetOrCreateDelegateReference(delegate)))); |
61 return; | 63 return; |
62 } | 64 } |
63 ProcessLoadCache(id, GetOrCreateDelegateReference(delegate)); | 65 ProcessLoadCache(id, GetOrCreateDelegateReference(delegate)); |
64 } | 66 } |
65 | 67 |
66 void MockAppCacheStorage::LoadOrCreateGroup( | 68 void MockAppCacheStorage::LoadOrCreateGroup( |
67 const GURL& manifest_url, Delegate* delegate) { | 69 const GURL& manifest_url, Delegate* delegate) { |
68 DCHECK(delegate); | 70 DCHECK(delegate); |
69 AppCacheGroup* group = working_set_.GetGroup(manifest_url); | 71 AppCacheGroup* group = working_set_.GetGroup(manifest_url); |
70 if (ShouldGroupLoadAppearAsync(group)) { | 72 if (ShouldGroupLoadAppearAsync(group)) { |
71 ScheduleTask(method_factory_.NewRunnableMethod( | 73 ScheduleTask( |
72 &MockAppCacheStorage::ProcessLoadOrCreateGroup, | 74 base::Bind(&MockAppCacheStorage::ProcessLoadOrCreateGroup, |
73 manifest_url, | 75 weak_factory_.GetWeakPtr(), manifest_url, |
74 make_scoped_refptr(GetOrCreateDelegateReference(delegate)))); | 76 make_scoped_refptr(GetOrCreateDelegateReference(delegate)))); |
75 return; | 77 return; |
76 } | 78 } |
77 ProcessLoadOrCreateGroup( | 79 ProcessLoadOrCreateGroup( |
78 manifest_url, GetOrCreateDelegateReference(delegate)); | 80 manifest_url, GetOrCreateDelegateReference(delegate)); |
79 } | 81 } |
80 | 82 |
81 void MockAppCacheStorage::StoreGroupAndNewestCache( | 83 void MockAppCacheStorage::StoreGroupAndNewestCache( |
82 AppCacheGroup* group, AppCache* newest_cache, Delegate* delegate) { | 84 AppCacheGroup* group, AppCache* newest_cache, Delegate* delegate) { |
83 DCHECK(group && delegate && newest_cache); | 85 DCHECK(group && delegate && newest_cache); |
84 | 86 |
85 // Always make this operation look async. | 87 // Always make this operation look async. |
86 ScheduleTask(method_factory_.NewRunnableMethod( | 88 ScheduleTask( |
87 &MockAppCacheStorage::ProcessStoreGroupAndNewestCache, | 89 base::Bind(&MockAppCacheStorage::ProcessStoreGroupAndNewestCache, |
88 make_scoped_refptr(group), | 90 weak_factory_.GetWeakPtr(), make_scoped_refptr(group), |
89 make_scoped_refptr(newest_cache), | 91 make_scoped_refptr(newest_cache), |
90 make_scoped_refptr(GetOrCreateDelegateReference(delegate)))); | 92 make_scoped_refptr(GetOrCreateDelegateReference(delegate)))); |
91 } | 93 } |
92 | 94 |
93 void MockAppCacheStorage::FindResponseForMainRequest( | 95 void MockAppCacheStorage::FindResponseForMainRequest( |
94 const GURL& url, const GURL& preferred_manifest_url, Delegate* delegate) { | 96 const GURL& url, const GURL& preferred_manifest_url, Delegate* delegate) { |
95 DCHECK(delegate); | 97 DCHECK(delegate); |
96 | 98 |
97 // Note: MockAppCacheStorage does not respect the preferred_manifest_url. | 99 // Note: MockAppCacheStorage does not respect the preferred_manifest_url. |
98 | 100 |
99 // Always make this operation look async. | 101 // Always make this operation look async. |
100 ScheduleTask(method_factory_.NewRunnableMethod( | 102 ScheduleTask( |
101 &MockAppCacheStorage::ProcessFindResponseForMainRequest, | 103 base::Bind(&MockAppCacheStorage::ProcessFindResponseForMainRequest, |
102 url, | 104 weak_factory_.GetWeakPtr(), url, |
103 make_scoped_refptr(GetOrCreateDelegateReference(delegate)))); | 105 make_scoped_refptr(GetOrCreateDelegateReference(delegate)))); |
104 } | 106 } |
105 | 107 |
106 void MockAppCacheStorage::FindResponseForSubRequest( | 108 void MockAppCacheStorage::FindResponseForSubRequest( |
107 AppCache* cache, const GURL& url, | 109 AppCache* cache, const GURL& url, |
108 AppCacheEntry* found_entry, AppCacheEntry* found_fallback_entry, | 110 AppCacheEntry* found_entry, AppCacheEntry* found_fallback_entry, |
109 bool* found_network_namespace) { | 111 bool* found_network_namespace) { |
110 DCHECK(cache && cache->is_complete()); | 112 DCHECK(cache && cache->is_complete()); |
111 | 113 |
112 // This layer of indirection is here to facilitate testing. | 114 // This layer of indirection is here to facilitate testing. |
113 if (simulate_find_sub_resource_) { | 115 if (simulate_find_sub_resource_) { |
(...skipping 19 matching lines...) Expand all Loading... |
133 if (entry) | 135 if (entry) |
134 entry->add_types(AppCacheEntry::FOREIGN); | 136 entry->add_types(AppCacheEntry::FOREIGN); |
135 } | 137 } |
136 } | 138 } |
137 | 139 |
138 void MockAppCacheStorage::MakeGroupObsolete( | 140 void MockAppCacheStorage::MakeGroupObsolete( |
139 AppCacheGroup* group, Delegate* delegate) { | 141 AppCacheGroup* group, Delegate* delegate) { |
140 DCHECK(group && delegate); | 142 DCHECK(group && delegate); |
141 | 143 |
142 // Always make this method look async. | 144 // Always make this method look async. |
143 ScheduleTask(method_factory_.NewRunnableMethod( | 145 ScheduleTask( |
144 &MockAppCacheStorage::ProcessMakeGroupObsolete, | 146 base::Bind(&MockAppCacheStorage::ProcessMakeGroupObsolete, |
145 make_scoped_refptr(group), | 147 weak_factory_.GetWeakPtr(), make_scoped_refptr(group), |
146 make_scoped_refptr(GetOrCreateDelegateReference(delegate)))); | 148 make_scoped_refptr(GetOrCreateDelegateReference(delegate)))); |
147 } | 149 } |
148 | 150 |
149 AppCacheResponseReader* MockAppCacheStorage::CreateResponseReader( | 151 AppCacheResponseReader* MockAppCacheStorage::CreateResponseReader( |
150 const GURL& manifest_url, int64 group_id, int64 response_id) { | 152 const GURL& manifest_url, int64 group_id, int64 response_id) { |
151 if (simulated_reader_.get()) | 153 if (simulated_reader_.get()) |
152 return simulated_reader_.release(); | 154 return simulated_reader_.release(); |
153 return new AppCacheResponseReader(response_id, group_id, disk_cache()); | 155 return new AppCacheResponseReader(response_id, group_id, disk_cache()); |
154 } | 156 } |
155 | 157 |
156 AppCacheResponseWriter* MockAppCacheStorage::CreateResponseWriter( | 158 AppCacheResponseWriter* MockAppCacheStorage::CreateResponseWriter( |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 | 400 |
399 // Also remove from the working set, caches for an 'obsolete' group | 401 // Also remove from the working set, caches for an 'obsolete' group |
400 // may linger in use, but the group itself cannot be looked up by | 402 // may linger in use, but the group itself cannot be looked up by |
401 // 'manifest_url' in the working set any longer. | 403 // 'manifest_url' in the working set any longer. |
402 working_set()->RemoveGroup(group); | 404 working_set()->RemoveGroup(group); |
403 | 405 |
404 if (delegate_ref->delegate) | 406 if (delegate_ref->delegate) |
405 delegate_ref->delegate->OnGroupMadeObsolete(group, true); | 407 delegate_ref->delegate->OnGroupMadeObsolete(group, true); |
406 } | 408 } |
407 | 409 |
408 void MockAppCacheStorage::ScheduleTask(Task* task) { | 410 void MockAppCacheStorage::ScheduleTask(const base::Closure& task) { |
409 pending_tasks_.push_back(task); | 411 pending_tasks_.push_back(task); |
410 MessageLoop::current()->PostTask(FROM_HERE, | 412 MessageLoop::current()->PostTask( |
411 method_factory_.NewRunnableMethod( | 413 FROM_HERE, |
412 &MockAppCacheStorage::RunOnePendingTask)); | 414 base::Bind(&MockAppCacheStorage::RunOnePendingTask, |
| 415 weak_factory_.GetWeakPtr())); |
413 } | 416 } |
414 | 417 |
415 void MockAppCacheStorage::RunOnePendingTask() { | 418 void MockAppCacheStorage::RunOnePendingTask() { |
416 DCHECK(!pending_tasks_.empty()); | 419 DCHECK(!pending_tasks_.empty()); |
417 Task* task = pending_tasks_.front(); | 420 base::Closure task = pending_tasks_.front(); |
418 pending_tasks_.pop_front(); | 421 pending_tasks_.pop_front(); |
419 task->Run(); | 422 task.Run(); |
420 delete task; | |
421 } | 423 } |
422 | 424 |
423 void MockAppCacheStorage::AddStoredCache(AppCache* cache) { | 425 void MockAppCacheStorage::AddStoredCache(AppCache* cache) { |
424 int64 cache_id = cache->cache_id(); | 426 int64 cache_id = cache->cache_id(); |
425 if (stored_caches_.find(cache_id) == stored_caches_.end()) { | 427 if (stored_caches_.find(cache_id) == stored_caches_.end()) { |
426 stored_caches_.insert( | 428 stored_caches_.insert( |
427 StoredCacheMap::value_type(cache_id, make_scoped_refptr(cache))); | 429 StoredCacheMap::value_type(cache_id, make_scoped_refptr(cache))); |
428 } | 430 } |
429 } | 431 } |
430 | 432 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 bool MockAppCacheStorage::ShouldCacheLoadAppearAsync(const AppCache* cache) { | 493 bool MockAppCacheStorage::ShouldCacheLoadAppearAsync(const AppCache* cache) { |
492 if (!cache) | 494 if (!cache) |
493 return true; | 495 return true; |
494 | 496 |
495 // If the 'stored' ref is the only ref, real storage will have to load from | 497 // If the 'stored' ref is the only ref, real storage will have to load from |
496 // the database. | 498 // the database. |
497 return IsCacheStored(cache) && cache->HasOneRef(); | 499 return IsCacheStored(cache) && cache->HasOneRef(); |
498 } | 500 } |
499 | 501 |
500 } // namespace appcache | 502 } // namespace appcache |
OLD | NEW |