signin_confirmation_helper.cc 3.8 KB

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