OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/offline_pages/offline_page_model.h" | 5 #include "components/offline_pages/offline_page_model.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 bookmark_ids.push_back(id_page_pair.first); | 246 bookmark_ids.push_back(id_page_pair.first); |
247 DeletePagesByBookmarkId( | 247 DeletePagesByBookmarkId( |
248 bookmark_ids, | 248 bookmark_ids, |
249 base::Bind(&OfflinePageModel::OnRemoveAllFilesDoneForClearAll, | 249 base::Bind(&OfflinePageModel::OnRemoveAllFilesDoneForClearAll, |
250 weak_ptr_factory_.GetWeakPtr(), | 250 weak_ptr_factory_.GetWeakPtr(), |
251 callback)); | 251 callback)); |
252 } | 252 } |
253 | 253 |
254 bool OfflinePageModel::HasOfflinePages() const { | 254 bool OfflinePageModel::HasOfflinePages() const { |
255 DCHECK(is_loaded_); | 255 DCHECK(is_loaded_); |
256 return !offline_pages_.empty(); | 256 // Check that at least one page is not marked for deletion. Because we have |
| 257 // pages marked for deletion, we cannot simply invert result of |empty()|. |
| 258 for (const auto& id_page_pair : offline_pages_) { |
| 259 if (!id_page_pair.second.IsMarkedForDeletion()) |
| 260 return true; |
| 261 } |
| 262 return false; |
257 } | 263 } |
258 | 264 |
259 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { | 265 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { |
260 DCHECK(is_loaded_); | 266 DCHECK(is_loaded_); |
261 std::vector<OfflinePageItem> offline_pages; | 267 std::vector<OfflinePageItem> offline_pages; |
262 for (const auto& id_page_pair : offline_pages_) { | 268 for (const auto& id_page_pair : offline_pages_) { |
263 if (id_page_pair.second.IsMarkedForDeletion()) | 269 if (id_page_pair.second.IsMarkedForDeletion()) |
264 continue; | 270 continue; |
265 offline_pages.push_back(id_page_pair.second); | 271 offline_pages.push_back(id_page_pair.second); |
266 } | 272 } |
267 return offline_pages; | 273 return offline_pages; |
268 } | 274 } |
269 | 275 |
270 const std::vector<OfflinePageItem> OfflinePageModel::GetPagesToCleanUp() const { | 276 const std::vector<OfflinePageItem> OfflinePageModel::GetPagesToCleanUp() const { |
271 DCHECK(is_loaded_); | 277 DCHECK(is_loaded_); |
272 std::vector<OfflinePageItem> offline_pages; | 278 std::vector<OfflinePageItem> offline_pages; |
273 base::Time now = base::Time::Now(); | 279 base::Time now = base::Time::Now(); |
274 for (const auto& id_page_pair : offline_pages_) { | 280 for (const auto& id_page_pair : offline_pages_) { |
275 if (!id_page_pair.second.IsMarkedForDeletion() && | 281 if (!id_page_pair.second.IsMarkedForDeletion() && |
276 now - id_page_pair.second.last_access_time > kPageCleanUpThreshold) { | 282 now - id_page_pair.second.last_access_time > kPageCleanUpThreshold) { |
277 offline_pages.push_back(id_page_pair.second); | 283 offline_pages.push_back(id_page_pair.second); |
278 } | 284 } |
279 } | 285 } |
280 return offline_pages; | 286 return offline_pages; |
281 } | 287 } |
282 | 288 |
283 const OfflinePageItem* OfflinePageModel::GetPageByBookmarkId( | 289 const OfflinePageItem* OfflinePageModel::GetPageByBookmarkId( |
284 int64 bookmark_id) const { | 290 int64 bookmark_id) const { |
285 const auto iter = offline_pages_.find(bookmark_id); | 291 const auto iter = offline_pages_.find(bookmark_id); |
286 return iter != offline_pages_.end() ? &(iter->second) : nullptr; | 292 return iter != offline_pages_.end() && !iter->second.IsMarkedForDeletion() |
| 293 ? &(iter->second) |
| 294 : nullptr; |
287 } | 295 } |
288 | 296 |
289 const OfflinePageItem* OfflinePageModel::GetPageByOfflineURL( | 297 const OfflinePageItem* OfflinePageModel::GetPageByOfflineURL( |
290 const GURL& offline_url) const { | 298 const GURL& offline_url) const { |
291 for (auto iter = offline_pages_.begin(); | 299 for (auto iter = offline_pages_.begin(); |
292 iter != offline_pages_.end(); | 300 iter != offline_pages_.end(); |
293 ++iter) { | 301 ++iter) { |
294 if (iter->second.GetOfflineURL() == offline_url) | 302 if (iter->second.GetOfflineURL() == offline_url && |
| 303 !iter->second.IsMarkedForDeletion()) { |
295 return &(iter->second); | 304 return &(iter->second); |
| 305 } |
296 } | 306 } |
297 return nullptr; | 307 return nullptr; |
298 } | 308 } |
299 | 309 |
300 const OfflinePageItem* OfflinePageModel::GetPageByOnlineURL( | 310 const OfflinePageItem* OfflinePageModel::GetPageByOnlineURL( |
301 const GURL& online_url) const { | 311 const GURL& online_url) const { |
302 for (auto iter = offline_pages_.begin(); iter != offline_pages_.end(); | 312 for (auto iter = offline_pages_.begin(); iter != offline_pages_.end(); |
303 ++iter) { | 313 ++iter) { |
304 if (iter->second.url == online_url) | 314 if (iter->second.url == online_url && !iter->second.IsMarkedForDeletion()) |
305 return &(iter->second); | 315 return &(iter->second); |
306 } | 316 } |
307 return nullptr; | 317 return nullptr; |
308 } | 318 } |
309 | 319 |
310 void OfflinePageModel::CheckForExternalFileDeletion() { | 320 void OfflinePageModel::CheckForExternalFileDeletion() { |
311 DCHECK(is_loaded_); | 321 DCHECK(is_loaded_); |
312 | 322 |
313 std::vector<std::pair<int64, base::FilePath>> id_path_pairs; | 323 std::vector<std::pair<int64, base::FilePath>> id_path_pairs; |
314 for (const auto& id_page_pair : offline_pages_) { | 324 for (const auto& id_page_pair : offline_pages_) { |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
689 } | 699 } |
690 | 700 |
691 void OfflinePageModel::CacheLoadedData( | 701 void OfflinePageModel::CacheLoadedData( |
692 const std::vector<OfflinePageItem>& offline_pages) { | 702 const std::vector<OfflinePageItem>& offline_pages) { |
693 offline_pages_.clear(); | 703 offline_pages_.clear(); |
694 for (const auto& offline_page : offline_pages) | 704 for (const auto& offline_page : offline_pages) |
695 offline_pages_[offline_page.bookmark_id] = offline_page; | 705 offline_pages_[offline_page.bookmark_id] = offline_page; |
696 } | 706 } |
697 | 707 |
698 } // namespace offline_pages | 708 } // namespace offline_pages |
OLD | NEW |