OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/history/content_visit_delegate.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "chrome/browser/history/history_backend.h" |
| 10 #include "chrome/browser/history/history_service.h" |
| 11 #include "components/history/core/browser/history_database.h" |
| 12 #include "components/history/core/browser/history_db_task.h" |
| 13 #include "components/visitedlink/browser/visitedlink_master.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 // URLIterator from std::vector<GURL> |
| 19 class URLIteratorFromURLs : public visitedlink::VisitedLinkMaster::URLIterator { |
| 20 public: |
| 21 explicit URLIteratorFromURLs(const std::vector<GURL>& urls) |
| 22 : itr_(urls.begin()), end_(urls.end()) {} |
| 23 |
| 24 // visitedlink::VisitedLinkMaster::URLIterator implementation. |
| 25 const GURL& NextURL() override { return *(itr_++); } |
| 26 bool HasNextURL() const override { return itr_ != end_; } |
| 27 |
| 28 private: |
| 29 std::vector<GURL>::const_iterator itr_; |
| 30 std::vector<GURL>::const_iterator end_; |
| 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(URLIteratorFromURLs); |
| 33 }; |
| 34 |
| 35 // IterateUrlsDBTask bridge HistoryBackend::URLEnumerator to |
| 36 // visitedlink::VisitedLinkDelegate::URLEnumerator. |
| 37 class IterateUrlsDBTask : public history::HistoryDBTask { |
| 38 public: |
| 39 explicit IterateUrlsDBTask(const scoped_refptr< |
| 40 visitedlink::VisitedLinkDelegate::URLEnumerator>& enumerator); |
| 41 ~IterateUrlsDBTask() override; |
| 42 |
| 43 private: |
| 44 // Implementation of history::HistoryDBTask. |
| 45 bool RunOnDBThread(history::HistoryBackend* backend, |
| 46 history::HistoryDatabase* db) override; |
| 47 void DoneRunOnMainThread() override; |
| 48 |
| 49 scoped_refptr<visitedlink::VisitedLinkDelegate::URLEnumerator> enumerator_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(IterateUrlsDBTask); |
| 52 }; |
| 53 |
| 54 IterateUrlsDBTask::IterateUrlsDBTask(const scoped_refptr< |
| 55 visitedlink::VisitedLinkDelegate::URLEnumerator>& enumerator) |
| 56 : enumerator_(enumerator) { |
| 57 } |
| 58 |
| 59 IterateUrlsDBTask::~IterateUrlsDBTask() { |
| 60 } |
| 61 |
| 62 bool IterateUrlsDBTask::RunOnDBThread(history::HistoryBackend* backend, |
| 63 history::HistoryDatabase* db) { |
| 64 bool success = false; |
| 65 if (db) { |
| 66 history::HistoryDatabase::URLEnumerator iter; |
| 67 if (db->InitURLEnumeratorForEverything(&iter)) { |
| 68 history::URLRow row; |
| 69 while (iter.GetNextURL(&row)) |
| 70 enumerator_->OnURL(row.url()); |
| 71 success = true; |
| 72 } |
| 73 } |
| 74 enumerator_->OnComplete(success); |
| 75 return true; |
| 76 } |
| 77 |
| 78 void IterateUrlsDBTask::DoneRunOnMainThread() { |
| 79 } |
| 80 |
| 81 } // namespace |
| 82 |
| 83 ContentVisitDelegate::ContentVisitDelegate( |
| 84 content::BrowserContext* browser_context) |
| 85 : history_service_(nullptr), |
| 86 visitedlink_master_( |
| 87 new visitedlink::VisitedLinkMaster(browser_context, this, true)) { |
| 88 } |
| 89 |
| 90 ContentVisitDelegate::~ContentVisitDelegate() { |
| 91 } |
| 92 |
| 93 bool ContentVisitDelegate::Init(HistoryService* history_service) { |
| 94 DCHECK(history_service); |
| 95 history_service_ = history_service; |
| 96 return visitedlink_master_->Init(); |
| 97 } |
| 98 |
| 99 void ContentVisitDelegate::AddURL(const GURL& url) { |
| 100 visitedlink_master_->AddURL(url); |
| 101 } |
| 102 |
| 103 void ContentVisitDelegate::AddURLs(const std::vector<GURL>& urls) { |
| 104 visitedlink_master_->AddURLs(urls); |
| 105 } |
| 106 |
| 107 void ContentVisitDelegate::DeleteURLs(const std::vector<GURL>& urls) { |
| 108 URLIteratorFromURLs iterator(urls); |
| 109 visitedlink_master_->DeleteURLs(&iterator); |
| 110 } |
| 111 |
| 112 void ContentVisitDelegate::DeleteAllURLs() { |
| 113 visitedlink_master_->DeleteAllURLs(); |
| 114 } |
| 115 |
| 116 void ContentVisitDelegate::RebuildTable( |
| 117 const scoped_refptr<URLEnumerator>& enumerator) { |
| 118 DCHECK(history_service_); |
| 119 scoped_ptr<history::HistoryDBTask> task(new IterateUrlsDBTask(enumerator)); |
| 120 history_service_->ScheduleDBTask(task.Pass(), &task_tracker_); |
| 121 } |
OLD | NEW |