1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/browsing_data/core/counters/history_counter.h"
#include <limits.h>
#include <stdint.h>
#include <memory>
#include "base/functional/bind.h"
#include "base/timer/timer.h"
#include "components/browsing_data/core/pref_names.h"
namespace {
static const int64_t kWebHistoryTimeoutSeconds = 10;
}
namespace browsing_data {
HistoryCounter::HistoryCounter(history::HistoryService* history_service,
GetUpdatedWebHistoryServiceCallback callback,
syncer::SyncService* sync_service)
: history_service_(history_service),
web_history_service_callback_(callback),
sync_tracker_(this, sync_service),
has_synced_visits_(false),
local_counting_finished_(false),
web_counting_finished_(false) {
DCHECK(history_service_);
}
HistoryCounter::~HistoryCounter() = default;
void HistoryCounter::OnInitialized() {
sync_tracker_.OnInitialized(base::BindRepeating(
&HistoryCounter::IsHistorySyncEnabled, base::Unretained(this)));
}
bool HistoryCounter::HasTrackedTasksForTesting() {
return cancelable_task_tracker_.HasTrackedTasks();
}
const char* HistoryCounter::GetPrefName() const {
return GetTab() == ClearBrowsingDataTab::BASIC
? browsing_data::prefs::kDeleteBrowsingHistoryBasic
: browsing_data::prefs::kDeleteBrowsingHistory;
}
history::WebHistoryService* HistoryCounter::GetWebHistoryService() {
if (web_history_service_callback_)
return web_history_service_callback_.Run();
return nullptr;
}
void HistoryCounter::Count() {
// Reset the state.
cancelable_task_tracker_.TryCancelAll();
web_history_request_.reset();
weak_ptr_factory_.InvalidateWeakPtrs();
web_history_timeout_.Stop();
has_synced_visits_ = false;
history::WebHistoryService* web_history = GetWebHistoryService();
local_counting_finished_ = false;
web_counting_finished_ = !web_history;
domain_fetching_finished_ = false;
// Count the locally stored items.
// TODO(crbug.com/397187800): Clean up GetHistoryCount logic once
// kDbdRevampDesktop is launched.
history_service_->GetHistoryCount(
GetPeriodStart(), GetPeriodEnd(),
base::BindOnce(&HistoryCounter::OnGetLocalHistoryCount,
weak_ptr_factory_.GetWeakPtr()),
&cancelable_task_tracker_);
history_service_->GetUniqueDomainsVisited(
GetPeriodStart(), GetPeriodEnd(),
base::BindOnce(&HistoryCounter::OnGetUniqueDomains,
weak_ptr_factory_.GetWeakPtr()),
&cancelable_task_tracker_);
if (!web_history) {
return;
}
web_history_timeout_.Start(FROM_HERE,
base::Seconds(kWebHistoryTimeoutSeconds), this,
&HistoryCounter::OnWebHistoryTimeout);
// If the history sync is enabled, test if there is at least one synced item.
history::QueryOptions options;
options.max_count = 1;
options.begin_time = GetPeriodStart();
options.end_time = GetPeriodEnd();
net::PartialNetworkTrafficAnnotationTag partial_traffic_annotation =
net::DefinePartialNetworkTrafficAnnotation("web_history_counter",
"web_history_service",
R"(
semantics {
description:
"If history sync is enabled, this queries history.google.com to "
"determine if there is any synced history. This information is "
"displayed in the Clear Browsing Data dialog."
trigger:
"Checking the 'Browsing history' option in the Clear Browsing Data "
"dialog, or enabling history sync while the dialog is open."
data:
"A version info token to resolve transaction conflicts, and an "
"OAuth2 token authenticating the user."
}
policy {
chrome_policy {
SyncDisabled {
SyncDisabled: true
}
}
})");
web_history_request_ = web_history->QueryHistory(
std::u16string(), options,
base::BindOnce(&HistoryCounter::OnGetWebHistoryCount,
weak_ptr_factory_.GetWeakPtr()),
partial_traffic_annotation);
// TODO(msramek): Include web history count when there is an API for it.
}
void HistoryCounter::OnGetLocalHistoryCount(
history::HistoryCountResult result) {
// Ensure that all callbacks are on the same thread, so that we do not need
// a mutex for `MergeResults`.
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!result.success) {
return;
}
local_result_ = result.count;
local_counting_finished_ = true;
MergeResults();
}
void HistoryCounter::OnGetWebHistoryCount(
history::WebHistoryService::Request* request,
base::optional_ref<const base::Value::Dict> result) {
// Ensure that all callbacks are on the same thread, so that we do not need
// a mutex for `MergeResults`.
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// If the timeout for this request already fired, ignore the result.
if (!web_history_timeout_.IsRunning())
return;
web_history_timeout_.Stop();
// If the query failed, err on the safe side and inform the user that they
// may have history items stored in Sync. Otherwise, we expect at least one
// entry in the "event" list.
if (!result.has_value()) {
has_synced_visits_ = true;
} else if (const base::Value::List* events = result->FindList("event")) {
has_synced_visits_ = !events->empty();
} else {
has_synced_visits_ = false;
}
web_counting_finished_ = true;
MergeResults();
}
void HistoryCounter::OnGetUniqueDomains(history::DomainsVisitedResult result) {
// Ensure that all callbacks are on the same thread, so that we do not need
// a mutex for `MergeResults`.
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
unique_domains_result_ = result.all_visited_domains.size();
last_visited_domain_ = result.all_visited_domains.empty()
? ""
: result.all_visited_domains.front();
domain_fetching_finished_ = true;
MergeResults();
}
void HistoryCounter::OnWebHistoryTimeout() {
// Ensure that all callbacks are on the same thread, so that we do not need
// a mutex for `MergeResults`.
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// If the query timed out, err on the safe side and inform the user that they
// may have history items stored in Sync.
web_history_request_.reset();
has_synced_visits_ = true;
web_counting_finished_ = true;
MergeResults();
}
void HistoryCounter::MergeResults() {
if (!local_counting_finished_ || !web_counting_finished_ ||
!domain_fetching_finished_) {
return;
}
ReportResult(std::make_unique<HistoryResult>(
this, local_result_, sync_tracker_.IsSyncActive(), has_synced_visits_,
last_visited_domain_, unique_domains_result_));
}
bool HistoryCounter::IsHistorySyncEnabled(
const syncer::SyncService* sync_service) {
return !!GetWebHistoryService();
}
HistoryCounter::HistoryResult::HistoryResult(const HistoryCounter* source,
ResultInt value,
bool is_sync_enabled,
bool has_synced_visits,
std::string last_visited_domain,
ResultInt unique_domains_result)
: SyncResult(source, value, is_sync_enabled),
has_synced_visits_(has_synced_visits),
last_visited_domain_(std::move(last_visited_domain)),
unique_domains_result_(unique_domains_result) {}
HistoryCounter::HistoryResult::~HistoryResult() = default;
} // namespace browsing_data
|