#include "components/history/core/browser/sync/history_sync_bridge.h"
#include <optional>
#include <vector>
#include "base/auto_reset.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "components/history/core/browser/history_types.h"
#include "components/history/core/browser/sync/history_sync_metadata_database.h"
#include "components/history/core/browser/sync/visit_id_remapper.h"
#include "components/history/core/browser/url_row.h"
#include "components/history/core/browser/visit_annotations_database.h"
#include "components/sync/base/page_transition_conversion.h"
#include "components/sync/model/conflict_resolution.h"
#include "components/sync/model/data_type_local_change_processor.h"
#include "components/sync/model/entity_change.h"
#include "components/sync/model/metadata_batch.h"
#include "components/sync/model/metadata_change_list.h"
#include "components/sync/model/mutable_data_batch.h"
#include "components/sync/model/sync_metadata_store_change_list.h"
#include "components/sync/protocol/history_specifics.pb.h"
#include "ui/base/page_transition_types.h"
namespace history {
namespace {
constexpr base::TimeDelta kMaxWriteToTheFuture = …;
constexpr int kMaxRedirectsPerEntity = …;
constexpr int kMaxFaviconUrlSizeToSync = …;
bool ShouldSync(const GURL& url) { … }
std::string GetStorageKeyFromVisitRow(const VisitRow& row) { … }
enum class SyncHistoryDatabaseError { … };
void RecordDatabaseError(SyncHistoryDatabaseError error) { … }
base::Time GetVisitTime(const sync_pb::HistorySpecifics& specifics) { … }
sync_pb::SyncEnums::BrowserType BrowserTypeToProto(
VisitContextAnnotations::BrowserType type) { … }
VisitContextAnnotations::BrowserType BrowserTypeFromProto(
sync_pb::SyncEnums::BrowserType type) { … }
VisitContentAnnotations::PasswordState PasswordStateFromProto(
sync_pb::SyncEnums::PasswordState password_state) { … }
sync_pb::SyncEnums::PasswordState PasswordStateToProto(
VisitContentAnnotations::PasswordState password_state) { … }
VisitRow MakeVisitRow(const sync_pb::HistorySpecifics& specifics,
int redirect_index) { … }
std::optional<VisitContextAnnotations> MakeContextAnnotations(
const sync_pb::HistorySpecifics& specifics,
int redirect_index) { … }
std::optional<VisitContentAnnotations> MakeContentAnnotations(
const sync_pb::HistorySpecifics& specifics,
int redirect_index) { … }
std::unique_ptr<syncer::EntityData> MakeEntityData(
const std::string& local_cache_guid,
const std::vector<AnnotatedVisit>& redirect_visits,
bool redirect_chain_middle_trimmed,
const GURL& referrer_url,
const std::vector<GURL>& favicon_urls,
int64_t local_cluster_id,
std::vector<VisitID>* included_visit_ids,
std::optional<std::string> app_id) { … }
bool SpecificsContainsOnlyValidURLs(
const sync_pb::HistorySpecifics& specifics,
const HistoryBackendForSync* history_backend) { … }
enum class SpecificsError { … };
std::optional<SpecificsError> GetSpecificsError(
const sync_pb::HistorySpecifics& specifics,
const HistoryBackendForSync* history_backend) { … }
void RecordSpecificsError(SpecificsError error) { … }
}
HistorySyncBridge::HistorySyncBridge(
HistoryBackendForSync* history_backend,
HistorySyncMetadataDatabase* sync_metadata_database,
std::unique_ptr<syncer::DataTypeLocalChangeProcessor> change_processor)
: … { … }
HistorySyncBridge::~HistorySyncBridge() = default;
std::unique_ptr<syncer::MetadataChangeList>
HistorySyncBridge::CreateMetadataChangeList() { … }
std::optional<syncer::ModelError> HistorySyncBridge::MergeFullSyncData(
std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
syncer::EntityChangeList entity_data) { … }
std::optional<syncer::ModelError>
HistorySyncBridge::ApplyIncrementalSyncChanges(
std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
syncer::EntityChangeList entity_changes) { … }
void HistorySyncBridge::ApplyDisableSyncChanges(
std::unique_ptr<syncer::MetadataChangeList> delete_metadata_change_list) { … }
std::unique_ptr<syncer::DataBatch> HistorySyncBridge::GetDataForCommit(
StorageKeyList storage_keys) { … }
std::unique_ptr<syncer::DataBatch> HistorySyncBridge::GetDataImpl(
StorageKeyList storage_keys) { … }
std::unique_ptr<syncer::DataBatch> HistorySyncBridge::GetAllDataForDebugging() { … }
std::string HistorySyncBridge::GetClientTag(
const syncer::EntityData& entity_data) { … }
std::string HistorySyncBridge::GetStorageKey(
const syncer::EntityData& entity_data) { … }
syncer::ConflictResolution HistorySyncBridge::ResolveConflict(
const std::string& storage_key,
const syncer::EntityData& remote_data) const { … }
void HistorySyncBridge::OnURLVisited(HistoryBackend* history_backend,
const URLRow& url_row,
const VisitRow& visit_row) { … }
void HistorySyncBridge::OnURLsModified(HistoryBackend* history_backend,
const URLRows& changed_urls,
bool is_from_expiration) { … }
void HistorySyncBridge::OnHistoryDeletions(HistoryBackend* history_backend,
bool all_history,
bool expired,
const URLRows& deleted_rows,
const std::set<GURL>& favicon_urls) { … }
void HistorySyncBridge::OnVisitUpdated(const VisitRow& visit_row,
VisitUpdateReason reason) { … }
void HistorySyncBridge::OnVisitDeleted(const VisitRow& visit_row) { … }
void HistorySyncBridge::SetSyncTransportState(
syncer::SyncService::TransportState state) { … }
void HistorySyncBridge::OnDatabaseError() { … }
void HistorySyncBridge::LoadMetadata() { … }
bool HistorySyncBridge::ShouldCommitRightNow() const { … }
void HistorySyncBridge::MaybeCommit(const VisitRow& visit_row) { … }
std::vector<std::unique_ptr<syncer::EntityData>>
HistorySyncBridge::QueryRedirectChainAndMakeEntityData(
const VisitRow& final_visit,
std::vector<VisitID>* included_visit_ids) { … }
GURL HistorySyncBridge::GetURLForVisit(VisitID visit_id) { … }
bool HistorySyncBridge::AddEntityInBackend(
VisitIDRemapper* id_remapper,
const sync_pb::HistorySpecifics& specifics) { … }
bool HistorySyncBridge::UpdateEntityInBackend(
VisitIDRemapper* id_remapper,
const sync_pb::HistorySpecifics& specifics) { … }
void HistorySyncBridge::UntrackAndClearMetadataForAllEntities() { … }
void HistorySyncBridge::UntrackAndClearMetadataForSyncedEntities() { … }
std::string HistorySyncBridge::GetLocalCacheGuid() const { … }
}