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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/test/integration/history_helper.h"
#include <memory>
#include <ostream>
#include <utility>
#include "base/memory/raw_ptr.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/time/time.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "components/history/core/browser/history_backend.h"
#include "components/history/core/browser/history_backend_observer.h"
#include "components/history/core/browser/history_database.h"
#include "components/history/core/browser/history_db_task.h"
#include "components/history/core/browser/history_service.h"
#include "components/sync/protocol/history_specifics.pb.h"
#include "components/sync/protocol/proto_value_conversions.h"
namespace sync_pb {
// Makes the GMock matchers print out a readable version of the protobuf.
void PrintTo(const HistorySpecifics& history, std::ostream* os) {
base::Value serialized = syncer::HistorySpecificsToValue(history);
*os << serialized;
}
} // namespace sync_pb
namespace history {
// Makes the GMock matchers print out a readable version of a VisitRow.
void PrintTo(const VisitRow& row, std::ostream* os) {
*os << "[ VisitID: " << row.visit_id << ", Duration: " << row.visit_duration
<< " ]";
}
} // namespace history
namespace history_helper {
namespace {
class FlushHistoryDBQueueTask : public history::HistoryDBTask {
public:
explicit FlushHistoryDBQueueTask(base::WaitableEvent* event)
: wait_event_(event) {}
bool RunOnDBThread(history::HistoryBackend* backend,
history::HistoryDatabase* db) override {
wait_event_->Signal();
return true;
}
void DoneRunOnMainThread() override {}
private:
~FlushHistoryDBQueueTask() override = default;
const raw_ptr<base::WaitableEvent> wait_event_;
};
class GetUrlTask : public history::HistoryDBTask {
public:
GetUrlTask(const GURL& url,
history::URLRow* row,
bool* found,
base::WaitableEvent* event)
: url_(url), row_(row), wait_event_(event), found_(found) {}
bool RunOnDBThread(history::HistoryBackend* backend,
history::HistoryDatabase* db) override {
// Fetch the typed URLs.
*found_ = backend->GetURL(url_, row_);
wait_event_->Signal();
return true;
}
void DoneRunOnMainThread() override {}
private:
~GetUrlTask() override = default;
const GURL url_;
const raw_ptr<history::URLRow> row_;
const raw_ptr<base::WaitableEvent> wait_event_;
const raw_ptr<bool> found_;
};
class GetUrlByIdTask : public history::HistoryDBTask {
public:
GetUrlByIdTask(history::URLID url_id,
history::URLRow* row,
bool* found,
base::WaitableEvent* event)
: url_id_(url_id), row_(row), wait_event_(event), found_(found) {}
bool RunOnDBThread(history::HistoryBackend* backend,
history::HistoryDatabase* db) override {
// Fetch the typed URLs.
*found_ = backend->GetURLByID(url_id_, row_);
wait_event_->Signal();
return true;
}
void DoneRunOnMainThread() override {}
private:
~GetUrlByIdTask() override = default;
const history::URLID url_id_;
const raw_ptr<history::URLRow> row_;
const raw_ptr<base::WaitableEvent> wait_event_;
const raw_ptr<bool> found_;
};
class GetVisitsTask : public history::HistoryDBTask {
public:
GetVisitsTask(history::URLID id,
history::VisitVector* visits,
base::WaitableEvent* event)
: id_(id), visits_(visits), wait_event_(event) {}
bool RunOnDBThread(history::HistoryBackend* backend,
history::HistoryDatabase* db) override {
// Fetch the visits.
backend->GetVisitsForURL(id_, visits_);
wait_event_->Signal();
return true;
}
void DoneRunOnMainThread() override {}
private:
~GetVisitsTask() override = default;
const history::URLID id_;
const raw_ptr<history::VisitVector> visits_;
const raw_ptr<base::WaitableEvent> wait_event_;
};
class GetAnnotatedVisitsTask : public history::HistoryDBTask {
public:
GetAnnotatedVisitsTask(history::URLID id,
std::vector<history::AnnotatedVisit>* visits,
base::WaitableEvent* event)
: id_(id), annotated_visits_(visits), wait_event_(event) {}
bool RunOnDBThread(history::HistoryBackend* backend,
history::HistoryDatabase* db) override {
// Fetch the visits.
history::VisitVector basic_visits;
backend->GetVisitsForURL(id_, &basic_visits);
*annotated_visits_ = backend->ToAnnotatedVisitsFromRows(
basic_visits, /*compute_redirect_chain_start_properties=*/false);
wait_event_->Signal();
return true;
}
void DoneRunOnMainThread() override {}
private:
~GetAnnotatedVisitsTask() override = default;
const history::URLID id_;
const raw_ptr<std::vector<history::AnnotatedVisit>> annotated_visits_;
const raw_ptr<base::WaitableEvent> wait_event_;
};
class GetRedirectChainTask : public history::HistoryDBTask {
public:
GetRedirectChainTask(const history::VisitRow& final_visit,
history::VisitVector* visits,
base::WaitableEvent* event)
: final_visit_(final_visit), visits_(visits), wait_event_(event) {}
bool RunOnDBThread(history::HistoryBackend* backend,
history::HistoryDatabase* db) override {
// Fetch the visits.
*visits_ = backend->GetRedirectChain(final_visit_);
wait_event_->Signal();
return true;
}
void DoneRunOnMainThread() override {}
private:
~GetRedirectChainTask() override = default;
const history::VisitRow final_visit_;
const raw_ptr<history::VisitVector> visits_;
const raw_ptr<base::WaitableEvent> wait_event_;
};
// Waits for the history DB thread to finish executing its current set of
// tasks.
void WaitForHistoryDBThread(int index) {
base::CancelableTaskTracker tracker;
history::HistoryService* service =
HistoryServiceFactory::GetForProfileWithoutCreating(
sync_datatype_helper::test()->GetProfile(index));
base::WaitableEvent wait_event(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
service->ScheduleDBTask(FROM_HERE,
std::unique_ptr<history::HistoryDBTask>(
new FlushHistoryDBQueueTask(&wait_event)),
&tracker);
wait_event.Wait();
}
// Creates a URLRow in the specified HistoryService with the passed transition
// type.
void AddToHistory(history::HistoryService* service,
const GURL& url,
ui::PageTransition transition,
history::VisitSource source,
const base::Time& timestamp) {
service->AddPage(url, timestamp, /*context_id=*/0,
/*nav_entry_id=*/1234,
/*referrer=*/GURL(), history::RedirectList(), transition,
source, /*did_replace_entry=*/false);
}
bool GetUrlFromHistoryService(history::HistoryService* service,
const GURL& url,
history::URLRow* row) {
base::CancelableTaskTracker tracker;
base::WaitableEvent wait_event(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
bool found = false;
service->ScheduleDBTask(FROM_HERE,
std::unique_ptr<history::HistoryDBTask>(
new GetUrlTask(url, row, &found, &wait_event)),
&tracker);
wait_event.Wait();
return found;
}
bool GetUrlFromHistoryService(history::HistoryService* service,
history::URLID url_id,
history::URLRow* row) {
base::CancelableTaskTracker tracker;
base::WaitableEvent wait_event(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
bool found = false;
service->ScheduleDBTask(
FROM_HERE,
std::unique_ptr<history::HistoryDBTask>(
new GetUrlByIdTask(url_id, row, &found, &wait_event)),
&tracker);
wait_event.Wait();
return found;
}
history::VisitVector GetVisitsFromHistoryService(
history::HistoryService* service,
history::URLID id) {
base::CancelableTaskTracker tracker;
base::WaitableEvent wait_event(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
history::VisitVector visits;
service->ScheduleDBTask(FROM_HERE,
std::unique_ptr<history::HistoryDBTask>(
new GetVisitsTask(id, &visits, &wait_event)),
&tracker);
wait_event.Wait();
return visits;
}
std::vector<history::AnnotatedVisit> GetAnnotatedVisitsFromHistoryService(
history::HistoryService* service,
history::URLID id) {
base::CancelableTaskTracker tracker;
base::WaitableEvent wait_event(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
std::vector<history::AnnotatedVisit> visits;
service->ScheduleDBTask(
FROM_HERE,
std::unique_ptr<history::HistoryDBTask>(
new GetAnnotatedVisitsTask(id, &visits, &wait_event)),
&tracker);
wait_event.Wait();
return visits;
}
history::VisitVector GetRedirectChainFromHistoryService(
history::HistoryService* service,
history::VisitRow final_visit) {
base::CancelableTaskTracker tracker;
base::WaitableEvent wait_event(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
history::VisitVector visits;
service->ScheduleDBTask(
FROM_HERE,
std::unique_ptr<history::HistoryDBTask>(
new GetRedirectChainTask(final_visit, &visits, &wait_event)),
&tracker);
wait_event.Wait();
return visits;
}
history::HistoryService* GetHistoryServiceFromClient(int index) {
return HistoryServiceFactory::GetForProfileWithoutCreating(
sync_datatype_helper::test()->GetProfile(index));
}
static base::Time* g_timestamp = nullptr;
base::Time GetUniqueTimestamp() {
// The history subsystem doesn't like identical timestamps for page visits,
// and it will massage the visit timestamps if we try to use identical
// values, which can lead to spurious errors. So make sure all timestamps
// are unique.
if (!g_timestamp) {
g_timestamp = new base::Time(base::Time::Now());
}
base::Time original = *g_timestamp;
*g_timestamp += base::Milliseconds(1);
return original;
}
std::vector<sync_pb::HistorySpecifics> SyncEntitiesToHistorySpecifics(
std::vector<sync_pb::SyncEntity> entities) {
std::vector<sync_pb::HistorySpecifics> history;
for (sync_pb::SyncEntity& entity : entities) {
DCHECK(entity.specifics().has_history());
history.push_back(std::move(entity.specifics().history()));
}
return history;
}
} // namespace
bool GetUrlFromClient(int index, const GURL& url, history::URLRow* row) {
history::HistoryService* service = GetHistoryServiceFromClient(index);
return GetUrlFromHistoryService(service, url, row);
}
bool GetUrlFromClient(int index, history::URLID url_id, history::URLRow* row) {
history::HistoryService* service = GetHistoryServiceFromClient(index);
return GetUrlFromHistoryService(service, url_id, row);
}
history::VisitVector GetVisitsFromClient(int index, history::URLID id) {
history::HistoryService* service = GetHistoryServiceFromClient(index);
return GetVisitsFromHistoryService(service, id);
}
history::VisitVector GetVisitsForURLFromClient(int index, const GURL& url) {
history::HistoryService* service = GetHistoryServiceFromClient(index);
history::URLRow url_row;
if (!GetUrlFromHistoryService(service, url, &url_row)) {
return history::VisitVector();
}
return GetVisitsFromHistoryService(service, url_row.id());
}
std::vector<history::AnnotatedVisit> GetAnnotatedVisitsFromClient(
int index,
history::URLID id) {
history::HistoryService* service = GetHistoryServiceFromClient(index);
return GetAnnotatedVisitsFromHistoryService(service, id);
}
std::vector<history::AnnotatedVisit> GetAnnotatedVisitsForURLFromClient(
int index,
const GURL& url) {
history::HistoryService* service = GetHistoryServiceFromClient(index);
history::URLRow url_row;
if (!GetUrlFromHistoryService(service, url, &url_row)) {
return std::vector<history::AnnotatedVisit>();
}
return GetAnnotatedVisitsFromHistoryService(service, url_row.id());
}
history::VisitVector GetRedirectChainFromClient(int index,
history::VisitRow final_visit) {
history::HistoryService* service = GetHistoryServiceFromClient(index);
return GetRedirectChainFromHistoryService(service, final_visit);
}
void AddUrlToHistory(int index, const GURL& url) {
AddUrlToHistoryWithTransition(index, url, ui::PAGE_TRANSITION_TYPED,
history::SOURCE_BROWSED);
}
void AddUrlToHistoryWithTransition(int index,
const GURL& url,
ui::PageTransition transition,
history::VisitSource source) {
AddUrlToHistoryWithTimestamp(index, url, transition, source,
GetUniqueTimestamp());
}
void AddUrlToHistoryWithTimestamp(int index,
const GURL& url,
ui::PageTransition transition,
history::VisitSource source,
const base::Time& timestamp) {
AddToHistory(GetHistoryServiceFromClient(index), url, transition, source,
timestamp);
if (sync_datatype_helper::test()->UseVerifier()) {
AddToHistory(HistoryServiceFactory::GetForProfile(
sync_datatype_helper::test()->verifier(),
ServiceAccessType::IMPLICIT_ACCESS),
url, transition, source, timestamp);
}
// Wait until the AddPage() request has completed so we know the change has
// filtered down to the sync observers (don't need to wait for the
// verifier profile since it doesn't sync).
WaitForHistoryDBThread(index);
}
LocalHistoryMatchChecker::LocalHistoryMatchChecker(
int profile_index,
syncer::SyncServiceImpl* service,
const std::map<GURL, Matcher>& matchers)
: SingleClientStatusChangeChecker(service),
profile_index_(profile_index),
matchers_(matchers) {}
LocalHistoryMatchChecker::~LocalHistoryMatchChecker() = default;
bool LocalHistoryMatchChecker::IsExitConditionSatisfied(std::ostream* os) {
for (const auto& [url, matcher] : matchers_) {
history::VisitVector visits =
GetVisitsForURLFromClient(profile_index_, url);
testing::StringMatchResultListener result_listener;
const bool matches =
testing::ExplainMatchResult(matcher, visits, &result_listener);
*os << result_listener.str();
if (!matches) {
return false;
}
}
return true;
}
void LocalHistoryMatchChecker::OnSyncCycleCompleted(syncer::SyncService* sync) {
CheckExitCondition();
}
ServerHistoryMatchChecker::ServerHistoryMatchChecker(const Matcher& matcher)
: matcher_(matcher) {}
ServerHistoryMatchChecker::~ServerHistoryMatchChecker() = default;
void ServerHistoryMatchChecker::OnCommit(
syncer::DataTypeSet committed_data_types) {
if (committed_data_types.Has(syncer::HISTORY)) {
CheckExitCondition();
}
}
bool ServerHistoryMatchChecker::IsExitConditionSatisfied(std::ostream* os) {
std::vector<sync_pb::HistorySpecifics> entities =
SyncEntitiesToHistorySpecifics(
fake_server()->GetSyncEntitiesByDataType(syncer::HISTORY));
testing::StringMatchResultListener result_listener;
const bool matches =
testing::ExplainMatchResult(matcher_, entities, &result_listener);
*os << result_listener.str();
return matches;
}
} // namespace history_helper
|