Chromium Code Reviews| 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 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 DCHECK(success); | 60 DCHECK(success); |
| 61 for (const auto& file_path : paths_to_delete) { | 61 for (const auto& file_path : paths_to_delete) { |
| 62 // Make sure delete happens on the left of || so that it is always executed. | 62 // Make sure delete happens on the left of || so that it is always executed. |
| 63 *success = base::DeleteFile(file_path, false) || *success; | 63 *success = base::DeleteFile(file_path, false) || *success; |
| 64 } | 64 } |
| 65 } | 65 } |
| 66 | 66 |
| 67 void EmptyDeleteCallback(OfflinePageModel::DeletePageResult /* result */) { | 67 void EmptyDeleteCallback(OfflinePageModel::DeletePageResult /* result */) { |
| 68 } | 68 } |
| 69 | 69 |
| 70 void ListPagesMissingOfflineCopy( | |
|
jianli
2015/10/13 00:17:53
ValidateArchiveFiles?
fgorski
2015/10/13 17:39:58
Changed to ListPagesMissingArchiveFile since that
| |
| 71 const std::vector<OfflinePageItem>& offline_pages, | |
| 72 std::vector<int64>* pages_missing_offline_copy) { | |
| 73 DCHECK(pages_missing_offline_copy); | |
| 74 | |
| 75 for (const auto& offline_page : offline_pages) { | |
| 76 if (!base::PathExists(offline_page.file_path)) | |
| 77 pages_missing_offline_copy->push_back(offline_page.bookmark_id); | |
| 78 } | |
| 79 } | |
| 80 | |
| 70 } // namespace | 81 } // namespace |
| 71 | 82 |
| 72 OfflinePageModel::OfflinePageModel( | 83 OfflinePageModel::OfflinePageModel( |
| 73 scoped_ptr<OfflinePageMetadataStore> store, | 84 scoped_ptr<OfflinePageMetadataStore> store, |
| 74 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 85 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 75 : store_(store.Pass()), | 86 : store_(store.Pass()), |
| 76 is_loaded_(false), | 87 is_loaded_(false), |
| 77 task_runner_(task_runner), | 88 task_runner_(task_runner), |
| 78 scoped_observer_(this), | 89 scoped_observer_(this), |
| 79 weak_ptr_factory_(this) { | 90 weak_ptr_factory_(this) { |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 const GURL& offline_url) const { | 207 const GURL& offline_url) const { |
| 197 for (auto iter = offline_pages_.begin(); | 208 for (auto iter = offline_pages_.begin(); |
| 198 iter != offline_pages_.end(); | 209 iter != offline_pages_.end(); |
| 199 ++iter) { | 210 ++iter) { |
| 200 if (iter->second.GetOfflineURL() == offline_url) | 211 if (iter->second.GetOfflineURL() == offline_url) |
| 201 return &(iter->second); | 212 return &(iter->second); |
| 202 } | 213 } |
| 203 return nullptr; | 214 return nullptr; |
| 204 } | 215 } |
| 205 | 216 |
| 217 void OfflinePageModel::CheckMetadataConsistency() { | |
| 218 DCHECK(is_loaded_); | |
| 219 | |
| 220 std::vector<OfflinePageItem> offline_pages; | |
| 221 for (const auto& id_page_pair : offline_pages_) | |
|
jianli
2015/10/13 00:17:54
I think it might be better to combine this logic w
fgorski
2015/10/13 17:39:58
I don't understand what exactly is your expectatio
| |
| 222 offline_pages.push_back(id_page_pair.second); | |
| 223 | |
| 224 std::vector<int64>* pages_missing_offline_copy = new std::vector<int64>(); | |
| 225 task_runner_->PostTaskAndReply( | |
| 226 FROM_HERE, base::Bind(&ListPagesMissingOfflineCopy, offline_pages, | |
| 227 pages_missing_offline_copy), | |
| 228 base::Bind(&OfflinePageModel::OnListPagesMissingOfflineCopy, | |
| 229 weak_ptr_factory_.GetWeakPtr(), | |
| 230 base::Owned(pages_missing_offline_copy))); | |
| 231 } | |
| 232 | |
| 206 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { | 233 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { |
| 207 return store_.get(); | 234 return store_.get(); |
| 208 } | 235 } |
| 209 | 236 |
| 210 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url, | 237 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url, |
| 211 int64 bookmark_id, | 238 int64 bookmark_id, |
| 212 const SavePageCallback& callback, | 239 const SavePageCallback& callback, |
| 213 OfflinePageArchiver* archiver, | 240 OfflinePageArchiver* archiver, |
| 214 ArchiverResult archiver_result, | 241 ArchiverResult archiver_result, |
| 215 const GURL& url, | 242 const GURL& url, |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 294 for (const auto& offline_page : offline_pages) | 321 for (const auto& offline_page : offline_pages) |
| 295 offline_pages_[offline_page.bookmark_id] = offline_page; | 322 offline_pages_[offline_page.bookmark_id] = offline_page; |
| 296 } | 323 } |
| 297 | 324 |
| 298 // Run all the delayed tasks. | 325 // Run all the delayed tasks. |
| 299 for (const auto& delayed_task : delayed_tasks_) | 326 for (const auto& delayed_task : delayed_tasks_) |
| 300 delayed_task.Run(); | 327 delayed_task.Run(); |
| 301 delayed_tasks_.clear(); | 328 delayed_tasks_.clear(); |
| 302 | 329 |
| 303 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); | 330 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); |
| 331 | |
| 332 CheckMetadataConsistency(); | |
| 304 } | 333 } |
| 305 | 334 |
| 306 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, | 335 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, |
| 307 SavePageResult result) { | 336 SavePageResult result) { |
| 308 UMA_HISTOGRAM_ENUMERATION( | 337 UMA_HISTOGRAM_ENUMERATION( |
| 309 "OfflinePages.SavePageResult", | 338 "OfflinePages.SavePageResult", |
| 310 static_cast<int>(result), | 339 static_cast<int>(result), |
| 311 static_cast<int>(SavePageResult::RESULT_COUNT)); | 340 static_cast<int>(SavePageResult::RESULT_COUNT)); |
| 312 callback.Run(result); | 341 callback.Run(result); |
| 313 } | 342 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 350 1, | 379 1, |
| 351 base::TimeDelta::FromDays(365).InMinutes(), | 380 base::TimeDelta::FromDays(365).InMinutes(), |
| 352 100); | 381 100); |
| 353 offline_pages_.erase(iter); | 382 offline_pages_.erase(iter); |
| 354 } | 383 } |
| 355 // Deleting multiple pages always succeeds when it gets to this point. | 384 // Deleting multiple pages always succeeds when it gets to this point. |
| 356 InformDeletePageDone( | 385 InformDeletePageDone( |
| 357 callback, | 386 callback, |
| 358 (success || bookmark_ids.size() > 1) ? DeletePageResult::SUCCESS | 387 (success || bookmark_ids.size() > 1) ? DeletePageResult::SUCCESS |
| 359 : DeletePageResult::STORE_FAILURE); | 388 : DeletePageResult::STORE_FAILURE); |
| 389 | |
| 390 for (int64 bookmark_id : bookmark_ids) { | |
|
jianli
2015/10/13 00:17:53
nit: brackets are not needed
fgorski
2015/10/13 17:39:58
Not true:
a) FOR_EACH_OBSERVER is in fact multi-li
| |
| 391 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(bookmark_id)); | |
| 392 } | |
| 360 } | 393 } |
| 361 | 394 |
| 362 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, | 395 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, |
| 363 DeletePageResult result) { | 396 DeletePageResult result) { |
| 364 UMA_HISTOGRAM_ENUMERATION( | 397 UMA_HISTOGRAM_ENUMERATION( |
| 365 "OfflinePages.DeletePageResult", | 398 "OfflinePages.DeletePageResult", |
| 366 static_cast<int>(result), | 399 static_cast<int>(result), |
| 367 static_cast<int>(DeletePageResult::RESULT_COUNT)); | 400 static_cast<int>(DeletePageResult::RESULT_COUNT)); |
| 368 callback.Run(result); | 401 callback.Run(result); |
| 369 } | 402 } |
| 370 | 403 |
| 404 void OfflinePageModel::OnListPagesMissingOfflineCopy( | |
| 405 const std::vector<int64>* pages_missing_offline_copy) { | |
| 406 DCHECK(pages_missing_offline_copy); | |
| 407 if (pages_missing_offline_copy->empty()) | |
| 408 return; | |
| 409 | |
| 410 store_->RemoveOfflinePages( | |
| 411 *pages_missing_offline_copy, | |
| 412 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, | |
| 413 weak_ptr_factory_.GetWeakPtr(), *pages_missing_offline_copy, | |
| 414 base::Bind(&EmptyDeleteCallback))); | |
| 415 } | |
| 416 | |
| 371 } // namespace offline_pages | 417 } // namespace offline_pages |
| OLD | NEW |