Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(561)

Side by Side Diff: components/offline_pages/offline_page_model.cc

Issue 1397233002: [Offline pages] Detecting missing offline copy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Passing only ID and paths for verification Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 9
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
11 #include "base/location.h" 12 #include "base/location.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 #include "base/metrics/histogram_macros.h" 14 #include "base/metrics/histogram_macros.h"
14 #include "base/sequenced_task_runner.h" 15 #include "base/sequenced_task_runner.h"
15 #include "base/time/time.h" 16 #include "base/time/time.h"
16 #include "components/bookmarks/browser/bookmark_model.h" 17 #include "components/bookmarks/browser/bookmark_model.h"
17 #include "components/bookmarks/browser/bookmark_node.h" 18 #include "components/bookmarks/browser/bookmark_node.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 DCHECK(success); 61 DCHECK(success);
61 for (const auto& file_path : paths_to_delete) { 62 for (const auto& file_path : paths_to_delete) {
62 // Make sure delete happens on the left of || so that it is always executed. 63 // Make sure delete happens on the left of || so that it is always executed.
63 *success = base::DeleteFile(file_path, false) || *success; 64 *success = base::DeleteFile(file_path, false) || *success;
64 } 65 }
65 } 66 }
66 67
67 void EmptyDeleteCallback(OfflinePageModel::DeletePageResult /* result */) { 68 void EmptyDeleteCallback(OfflinePageModel::DeletePageResult /* result */) {
68 } 69 }
69 70
71 void ListPagesMissingArchiveFile(
jianli 2015/10/14 22:30:23 FindMissingArchiveFiles?
fgorski 2015/10/15 19:12:29 No, we are not looking for missing archive files,
72 const std::vector<std::pair<int64, base::FilePath>>& id_path_pairs,
73 std::vector<int64>* pages_missing_archive_file) {
74 DCHECK(pages_missing_archive_file);
75
76 for (const auto& id_path : id_path_pairs) {
77 if (!base::PathExists(id_path.second))
jianli 2015/10/14 22:30:23 I think we should also call base::DirectoryExists
fgorski 2015/10/15 19:12:29 Done.
78 pages_missing_archive_file->push_back(id_path.first);
79 }
80 }
81
70 } // namespace 82 } // namespace
71 83
72 OfflinePageModel::OfflinePageModel( 84 OfflinePageModel::OfflinePageModel(
73 scoped_ptr<OfflinePageMetadataStore> store, 85 scoped_ptr<OfflinePageMetadataStore> store,
74 const scoped_refptr<base::SequencedTaskRunner>& task_runner) 86 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
75 : store_(store.Pass()), 87 : store_(store.Pass()),
76 is_loaded_(false), 88 is_loaded_(false),
77 task_runner_(task_runner), 89 task_runner_(task_runner),
78 scoped_observer_(this), 90 scoped_observer_(this),
79 weak_ptr_factory_(this) { 91 weak_ptr_factory_(this) {
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 const GURL& offline_url) const { 208 const GURL& offline_url) const {
197 for (auto iter = offline_pages_.begin(); 209 for (auto iter = offline_pages_.begin();
198 iter != offline_pages_.end(); 210 iter != offline_pages_.end();
199 ++iter) { 211 ++iter) {
200 if (iter->second.GetOfflineURL() == offline_url) 212 if (iter->second.GetOfflineURL() == offline_url)
201 return &(iter->second); 213 return &(iter->second);
202 } 214 }
203 return nullptr; 215 return nullptr;
204 } 216 }
205 217
218 void OfflinePageModel::CheckForExternalFileDeletion() {
219 DCHECK(is_loaded_);
220
221 std::vector<std::pair<int64, base::FilePath>> id_path_pairs;
222 for (const auto& id_page_pair : offline_pages_) {
223 id_path_pairs.push_back(
224 std::make_pair(id_page_pair.first, id_page_pair.second.file_path));
225 }
226
227 std::vector<int64>* pages_missing_archive_file = new std::vector<int64>();
228 task_runner_->PostTaskAndReply(
229 FROM_HERE, base::Bind(&ListPagesMissingArchiveFile, id_path_pairs,
230 pages_missing_archive_file),
231 base::Bind(&OfflinePageModel::OnListPagesMissingArchiveFileDone,
232 weak_ptr_factory_.GetWeakPtr(),
233 base::Owned(pages_missing_archive_file)));
234 }
235
206 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { 236 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() {
207 return store_.get(); 237 return store_.get();
208 } 238 }
209 239
210 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url, 240 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url,
211 int64 bookmark_id, 241 int64 bookmark_id,
212 const SavePageCallback& callback, 242 const SavePageCallback& callback,
213 OfflinePageArchiver* archiver, 243 OfflinePageArchiver* archiver,
214 ArchiverResult archiver_result, 244 ArchiverResult archiver_result,
215 const GURL& url, 245 const GURL& url,
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 for (const auto& offline_page : offline_pages) 324 for (const auto& offline_page : offline_pages)
295 offline_pages_[offline_page.bookmark_id] = offline_page; 325 offline_pages_[offline_page.bookmark_id] = offline_page;
296 } 326 }
297 327
298 // Run all the delayed tasks. 328 // Run all the delayed tasks.
299 for (const auto& delayed_task : delayed_tasks_) 329 for (const auto& delayed_task : delayed_tasks_)
300 delayed_task.Run(); 330 delayed_task.Run();
301 delayed_tasks_.clear(); 331 delayed_tasks_.clear();
302 332
303 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); 333 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this));
334
335 CheckForExternalFileDeletion();
304 } 336 }
305 337
306 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, 338 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback,
307 SavePageResult result) { 339 SavePageResult result) {
308 UMA_HISTOGRAM_ENUMERATION( 340 UMA_HISTOGRAM_ENUMERATION(
309 "OfflinePages.SavePageResult", 341 "OfflinePages.SavePageResult",
310 static_cast<int>(result), 342 static_cast<int>(result),
311 static_cast<int>(SavePageResult::RESULT_COUNT)); 343 static_cast<int>(SavePageResult::RESULT_COUNT));
312 callback.Run(result); 344 callback.Run(result);
313 } 345 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 1, 382 1,
351 base::TimeDelta::FromDays(365).InMinutes(), 383 base::TimeDelta::FromDays(365).InMinutes(),
352 100); 384 100);
353 offline_pages_.erase(iter); 385 offline_pages_.erase(iter);
354 } 386 }
355 // Deleting multiple pages always succeeds when it gets to this point. 387 // Deleting multiple pages always succeeds when it gets to this point.
356 InformDeletePageDone( 388 InformDeletePageDone(
357 callback, 389 callback,
358 (success || bookmark_ids.size() > 1) ? DeletePageResult::SUCCESS 390 (success || bookmark_ids.size() > 1) ? DeletePageResult::SUCCESS
359 : DeletePageResult::STORE_FAILURE); 391 : DeletePageResult::STORE_FAILURE);
392
393 for (int64 bookmark_id : bookmark_ids) {
394 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(bookmark_id));
jianli 2015/10/15 00:24:36 Per the discussion with Ian, I realized that OnRem
fgorski 2015/10/15 19:12:29 OK, let's discuss this.
395 }
360 } 396 }
361 397
362 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, 398 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback,
363 DeletePageResult result) { 399 DeletePageResult result) {
364 UMA_HISTOGRAM_ENUMERATION( 400 UMA_HISTOGRAM_ENUMERATION(
365 "OfflinePages.DeletePageResult", 401 "OfflinePages.DeletePageResult",
366 static_cast<int>(result), 402 static_cast<int>(result),
367 static_cast<int>(DeletePageResult::RESULT_COUNT)); 403 static_cast<int>(DeletePageResult::RESULT_COUNT));
368 callback.Run(result); 404 callback.Run(result);
369 } 405 }
370 406
407 void OfflinePageModel::OnListPagesMissingArchiveFileDone(
408 const std::vector<int64>* pages_missing_archive_file) {
jianli 2015/10/14 22:30:23 pages_missing_archive_file => bookmark_ids_for_mis
fgorski 2015/10/15 19:12:29 Done. Went with ids_of_pages_missing_archive_file
409 DCHECK(pages_missing_archive_file);
410 if (pages_missing_archive_file->empty())
411 return;
412
413 store_->RemoveOfflinePages(
414 *pages_missing_archive_file,
415 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone,
416 weak_ptr_factory_.GetWeakPtr(), *pages_missing_archive_file,
417 base::Bind(&EmptyDeleteCallback)));
418 }
419
371 } // namespace offline_pages 420 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698