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/appcache_group.h" | 5 #include "webkit/appcache/appcache_group.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
| 9 #include "base/bind.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
11 #include "webkit/appcache/appcache.h" | 12 #include "webkit/appcache/appcache.h" |
12 #include "webkit/appcache/appcache_host.h" | 13 #include "webkit/appcache/appcache_host.h" |
13 #include "webkit/appcache/appcache_service.h" | 14 #include "webkit/appcache/appcache_service.h" |
14 #include "webkit/appcache/appcache_storage.h" | 15 #include "webkit/appcache/appcache_storage.h" |
15 #include "webkit/appcache/appcache_update_job.h" | 16 #include "webkit/appcache/appcache_update_job.h" |
16 | 17 |
17 namespace appcache { | 18 namespace appcache { |
18 | 19 |
(...skipping 19 matching lines...) Expand all Loading... |
38 const GURL& manifest_url, | 39 const GURL& manifest_url, |
39 int64 group_id) | 40 int64 group_id) |
40 : group_id_(group_id), | 41 : group_id_(group_id), |
41 manifest_url_(manifest_url), | 42 manifest_url_(manifest_url), |
42 update_status_(IDLE), | 43 update_status_(IDLE), |
43 is_obsolete_(false), | 44 is_obsolete_(false), |
44 is_being_deleted_(false), | 45 is_being_deleted_(false), |
45 newest_complete_cache_(NULL), | 46 newest_complete_cache_(NULL), |
46 update_job_(NULL), | 47 update_job_(NULL), |
47 service_(service), | 48 service_(service), |
48 restart_update_task_(NULL), | |
49 is_in_dtor_(false) { | 49 is_in_dtor_(false) { |
50 service_->storage()->working_set()->AddGroup(this); | 50 service_->storage()->working_set()->AddGroup(this); |
51 host_observer_.reset(new HostObserver(this)); | 51 host_observer_.reset(new HostObserver(this)); |
52 } | 52 } |
53 | 53 |
54 AppCacheGroup::~AppCacheGroup() { | 54 AppCacheGroup::~AppCacheGroup() { |
55 DCHECK(old_caches_.empty()); | 55 DCHECK(old_caches_.empty()); |
56 DCHECK(!newest_complete_cache_); | 56 DCHECK(!newest_complete_cache_); |
57 DCHECK(!restart_update_task_); | 57 DCHECK(restart_update_task_.IsCancelled()); |
58 DCHECK(queued_updates_.empty()); | 58 DCHECK(queued_updates_.empty()); |
59 | 59 |
60 is_in_dtor_ = true; | 60 is_in_dtor_ = true; |
61 | 61 |
62 if (update_job_) | 62 if (update_job_) |
63 delete update_job_; | 63 delete update_job_; |
64 DCHECK_EQ(IDLE, update_status_); | 64 DCHECK_EQ(IDLE, update_status_); |
65 | 65 |
66 service_->storage()->working_set()->RemoveGroup(this); | 66 service_->storage()->working_set()->RemoveGroup(this); |
67 service_->storage()->DeleteResponses( | 67 service_->storage()->DeleteResponses( |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 DCHECK(!is_obsolete() && !is_being_deleted()); | 159 DCHECK(!is_obsolete() && !is_being_deleted()); |
160 if (is_in_dtor_) | 160 if (is_in_dtor_) |
161 return; | 161 return; |
162 | 162 |
163 if (!update_job_) | 163 if (!update_job_) |
164 update_job_ = new AppCacheUpdateJob(service_, this); | 164 update_job_ = new AppCacheUpdateJob(service_, this); |
165 | 165 |
166 update_job_->StartUpdate(host, new_master_resource); | 166 update_job_->StartUpdate(host, new_master_resource); |
167 | 167 |
168 // Run queued update immediately as an update has been started manually. | 168 // Run queued update immediately as an update has been started manually. |
169 if (restart_update_task_) { | 169 if (!restart_update_task_.IsCancelled()) { |
170 restart_update_task_->Cancel(); | 170 restart_update_task_.Cancel(); |
171 restart_update_task_ = NULL; | |
172 RunQueuedUpdates(); | 171 RunQueuedUpdates(); |
173 } | 172 } |
174 } | 173 } |
175 | 174 |
176 void AppCacheGroup::CancelUpdate() { | 175 void AppCacheGroup::CancelUpdate() { |
177 if (update_job_) { | 176 if (update_job_) { |
178 delete update_job_; | 177 delete update_job_; |
179 DCHECK(!update_job_); | 178 DCHECK(!update_job_); |
180 DCHECK_EQ(IDLE, update_status_); | 179 DCHECK_EQ(IDLE, update_status_); |
181 } | 180 } |
182 } | 181 } |
183 | 182 |
184 void AppCacheGroup::QueueUpdate(AppCacheHost* host, | 183 void AppCacheGroup::QueueUpdate(AppCacheHost* host, |
185 const GURL& new_master_resource) { | 184 const GURL& new_master_resource) { |
186 DCHECK(update_job_ && host && !new_master_resource.is_empty()); | 185 DCHECK(update_job_ && host && !new_master_resource.is_empty()); |
187 queued_updates_.insert(QueuedUpdates::value_type(host, new_master_resource)); | 186 queued_updates_.insert(QueuedUpdates::value_type(host, new_master_resource)); |
188 | 187 |
189 // Need to know when host is destroyed. | 188 // Need to know when host is destroyed. |
190 host->AddObserver(host_observer_.get()); | 189 host->AddObserver(host_observer_.get()); |
191 | 190 |
192 // If host is already observing for updates, move host to queued observers | 191 // If host is already observing for updates, move host to queued observers |
193 // list so that host is not notified when the current update completes. | 192 // list so that host is not notified when the current update completes. |
194 if (FindObserver(host, observers_)) { | 193 if (FindObserver(host, observers_)) { |
195 observers_.RemoveObserver(host); | 194 observers_.RemoveObserver(host); |
196 queued_observers_.AddObserver(host); | 195 queued_observers_.AddObserver(host); |
197 } | 196 } |
198 } | 197 } |
199 | 198 |
200 void AppCacheGroup::RunQueuedUpdates() { | 199 void AppCacheGroup::RunQueuedUpdates() { |
201 if (restart_update_task_) | 200 if (!restart_update_task_.IsCancelled()) |
202 restart_update_task_ = NULL; | 201 restart_update_task_.Cancel(); |
203 | 202 |
204 if (queued_updates_.empty()) | 203 if (queued_updates_.empty()) |
205 return; | 204 return; |
206 | 205 |
207 QueuedUpdates updates_to_run; | 206 QueuedUpdates updates_to_run; |
208 queued_updates_.swap(updates_to_run); | 207 queued_updates_.swap(updates_to_run); |
209 DCHECK(queued_updates_.empty()); | 208 DCHECK(queued_updates_.empty()); |
210 | 209 |
211 for (QueuedUpdates::iterator it = updates_to_run.begin(); | 210 for (QueuedUpdates::iterator it = updates_to_run.begin(); |
212 it != updates_to_run.end(); ++it) { | 211 it != updates_to_run.end(); ++it) { |
213 AppCacheHost* host = it->first; | 212 AppCacheHost* host = it->first; |
214 host->RemoveObserver(host_observer_.get()); | 213 host->RemoveObserver(host_observer_.get()); |
215 if (FindObserver(host, queued_observers_)) { | 214 if (FindObserver(host, queued_observers_)) { |
216 queued_observers_.RemoveObserver(host); | 215 queued_observers_.RemoveObserver(host); |
217 observers_.AddObserver(host); | 216 observers_.AddObserver(host); |
218 } | 217 } |
219 | 218 |
220 if (!is_obsolete() && !is_being_deleted()) | 219 if (!is_obsolete() && !is_being_deleted()) |
221 StartUpdateWithNewMasterEntry(host, it->second); | 220 StartUpdateWithNewMasterEntry(host, it->second); |
222 } | 221 } |
223 } | 222 } |
224 | 223 |
225 bool AppCacheGroup::FindObserver(UpdateObserver* find_me, | 224 bool AppCacheGroup::FindObserver(UpdateObserver* find_me, |
226 const ObserverList<UpdateObserver>& observer_list) { | 225 const ObserverList<UpdateObserver>& observer_list) { |
227 return observer_list.HasObserver(find_me); | 226 return observer_list.HasObserver(find_me); |
228 } | 227 } |
229 | 228 |
230 void AppCacheGroup::ScheduleUpdateRestart(int delay_ms) { | 229 void AppCacheGroup::ScheduleUpdateRestart(int delay_ms) { |
231 DCHECK(!restart_update_task_); | 230 DCHECK(restart_update_task_.IsCancelled()); |
232 restart_update_task_ = | 231 restart_update_task_.Reset( |
233 NewRunnableMethod(this, &AppCacheGroup::RunQueuedUpdates); | 232 base::Bind(&AppCacheGroup::RunQueuedUpdates, this)); |
234 MessageLoop::current()->PostDelayedTask(FROM_HERE, restart_update_task_, | 233 MessageLoop::current()->PostDelayedTask( |
235 delay_ms); | 234 FROM_HERE, restart_update_task_.callback(), delay_ms); |
236 } | 235 } |
237 | 236 |
238 void AppCacheGroup::HostDestructionImminent(AppCacheHost* host) { | 237 void AppCacheGroup::HostDestructionImminent(AppCacheHost* host) { |
239 queued_updates_.erase(host); | 238 queued_updates_.erase(host); |
240 if (queued_updates_.empty() && restart_update_task_) { | 239 if (queued_updates_.empty() && !restart_update_task_.IsCancelled()) |
241 restart_update_task_->Cancel(); | 240 restart_update_task_.Cancel(); |
242 restart_update_task_ = NULL; | |
243 } | |
244 } | 241 } |
245 | 242 |
246 void AppCacheGroup::SetUpdateStatus(UpdateStatus status) { | 243 void AppCacheGroup::SetUpdateStatus(UpdateStatus status) { |
247 if (status == update_status_) | 244 if (status == update_status_) |
248 return; | 245 return; |
249 | 246 |
250 update_status_ = status; | 247 update_status_ = status; |
251 | 248 |
252 if (status != IDLE) { | 249 if (status != IDLE) { |
253 DCHECK(update_job_); | 250 DCHECK(update_job_); |
254 } else { | 251 } else { |
255 update_job_ = NULL; | 252 update_job_ = NULL; |
256 | 253 |
257 // Observers may release us in these callbacks, so we protect against | 254 // Observers may release us in these callbacks, so we protect against |
258 // deletion by adding an extra ref in this scope (but only if we're not | 255 // deletion by adding an extra ref in this scope (but only if we're not |
259 // in our destructor). | 256 // in our destructor). |
260 scoped_refptr<AppCacheGroup> protect(is_in_dtor_ ? NULL : this); | 257 scoped_refptr<AppCacheGroup> protect(is_in_dtor_ ? NULL : this); |
261 FOR_EACH_OBSERVER(UpdateObserver, observers_, OnUpdateComplete(this)); | 258 FOR_EACH_OBSERVER(UpdateObserver, observers_, OnUpdateComplete(this)); |
262 if (!queued_updates_.empty()) | 259 if (!queued_updates_.empty()) |
263 ScheduleUpdateRestart(kUpdateRestartDelayMs); | 260 ScheduleUpdateRestart(kUpdateRestartDelayMs); |
264 } | 261 } |
265 } | 262 } |
266 | 263 |
267 } // namespace appcache | 264 } // namespace appcache |
OLD | NEW |