| 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 "components/browser_sync/browser/signin_confirmation_helper.h" | |
| 6 | |
| 7 #include "base/strings/string16.h" | |
| 8 #include "base/threading/thread_task_runner_handle.h" | |
| 9 #include "components/history/core/browser/history_backend.h" | |
| 10 #include "components/history/core/browser/history_db_task.h" | |
| 11 #include "components/history/core/browser/history_service.h" | |
| 12 #include "components/history/core/browser/history_types.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // Determines whether there are any typed URLs in a history backend. | |
| 17 class HasTypedURLsTask : public history::HistoryDBTask { | |
| 18 public: | |
| 19 explicit HasTypedURLsTask(const base::Callback<void(bool)>& cb) | |
| 20 : has_typed_urls_(false), cb_(cb) {} | |
| 21 | |
| 22 bool RunOnDBThread(history::HistoryBackend* backend, | |
| 23 history::HistoryDatabase* db) override { | |
| 24 history::URLRows rows; | |
| 25 backend->GetAllTypedURLs(&rows); | |
| 26 if (!rows.empty()) { | |
| 27 DVLOG(1) << "SigninConfirmationHelper: history contains " << rows.size() | |
| 28 << " typed URLs"; | |
| 29 has_typed_urls_ = true; | |
| 30 } | |
| 31 return true; | |
| 32 } | |
| 33 | |
| 34 void DoneRunOnMainThread() override { cb_.Run(has_typed_urls_); } | |
| 35 | |
| 36 private: | |
| 37 ~HasTypedURLsTask() override {} | |
| 38 | |
| 39 bool has_typed_urls_; | |
| 40 base::Callback<void(bool)> cb_; | |
| 41 }; | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 namespace sync_driver { | |
| 46 | |
| 47 SigninConfirmationHelper::SigninConfirmationHelper( | |
| 48 history::HistoryService* history_service, | |
| 49 const base::Callback<void(bool)>& return_result) | |
| 50 : origin_thread_(base::ThreadTaskRunnerHandle::Get()), | |
| 51 history_service_(history_service), | |
| 52 pending_requests_(0), | |
| 53 return_result_(return_result) {} | |
| 54 | |
| 55 SigninConfirmationHelper::~SigninConfirmationHelper() { | |
| 56 DCHECK(origin_thread_->BelongsToCurrentThread()); | |
| 57 } | |
| 58 | |
| 59 void SigninConfirmationHelper::OnHistoryQueryResults( | |
| 60 size_t max_entries, | |
| 61 history::QueryResults* results) { | |
| 62 history::QueryResults owned_results; | |
| 63 results->Swap(&owned_results); | |
| 64 bool too_much_history = owned_results.size() >= max_entries; | |
| 65 if (too_much_history) { | |
| 66 DVLOG(1) << "SigninConfirmationHelper: profile contains " | |
| 67 << owned_results.size() << " history entries"; | |
| 68 } | |
| 69 ReturnResult(too_much_history); | |
| 70 } | |
| 71 | |
| 72 void SigninConfirmationHelper::CheckHasHistory(int max_entries) { | |
| 73 pending_requests_++; | |
| 74 if (!history_service_) { | |
| 75 PostResult(false); | |
| 76 return; | |
| 77 } | |
| 78 history::QueryOptions opts; | |
| 79 opts.max_count = max_entries; | |
| 80 history_service_->QueryHistory( | |
| 81 base::string16(), opts, | |
| 82 base::Bind(&SigninConfirmationHelper::OnHistoryQueryResults, | |
| 83 base::Unretained(this), max_entries), | |
| 84 &task_tracker_); | |
| 85 } | |
| 86 | |
| 87 void SigninConfirmationHelper::CheckHasTypedURLs() { | |
| 88 pending_requests_++; | |
| 89 if (!history_service_) { | |
| 90 PostResult(false); | |
| 91 return; | |
| 92 } | |
| 93 history_service_->ScheduleDBTask( | |
| 94 std::unique_ptr<history::HistoryDBTask>(new HasTypedURLsTask(base::Bind( | |
| 95 &SigninConfirmationHelper::ReturnResult, base::Unretained(this)))), | |
| 96 &task_tracker_); | |
| 97 } | |
| 98 | |
| 99 void SigninConfirmationHelper::PostResult(bool result) { | |
| 100 origin_thread_->PostTask(FROM_HERE, | |
| 101 base::Bind(&SigninConfirmationHelper::ReturnResult, | |
| 102 base::Unretained(this), result)); | |
| 103 } | |
| 104 | |
| 105 void SigninConfirmationHelper::ReturnResult(bool result) { | |
| 106 DCHECK(origin_thread_->BelongsToCurrentThread()); | |
| 107 // Pass |true| into the callback as soon as one of the tasks passes a | |
| 108 // result of |true|, otherwise pass the last returned result. | |
| 109 if (--pending_requests_ == 0 || result) { | |
| 110 return_result_.Run(result); | |
| 111 | |
| 112 // This leaks at shutdown if the HistoryService is destroyed, but | |
| 113 // the process is going to die anyway. | |
| 114 delete this; | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 } // namespace sync_driver | |
| OLD | NEW |