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
|
// Copyright 2024 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/ash/floating_sso/floating_sso_sync_bridge.h"
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "base/check_deref.h"
#include "base/logging.h"
#include "chrome/browser/ash/floating_sso/cookie_sync_conversions.h"
#include "components/sync/base/data_type.h"
#include "components/sync/base/deletion_origin.h"
#include "components/sync/model/conflict_resolution.h"
#include "components/sync/model/data_type_local_change_processor.h"
#include "components/sync/model/data_type_store.h"
#include "components/sync/model/data_type_sync_bridge.h"
#include "components/sync/model/metadata_batch.h"
#include "components/sync/model/model_error.h"
#include "components/sync/model/mutable_data_batch.h"
#include "components/sync/protocol/cookie_specifics.pb.h"
#include "components/sync/protocol/entity_data.h"
#include "net/cookies/canonical_cookie.h"
namespace ash::floating_sso {
namespace {
std::unique_ptr<syncer::EntityData> CreateEntityData(
const sync_pb::CookieSpecifics& specifics) {
auto entity_data = std::make_unique<syncer::EntityData>();
entity_data->specifics.mutable_cookie()->CopyFrom(specifics);
entity_data->name = specifics.unique_key();
return entity_data;
}
} // namespace
FloatingSsoSyncBridge::FloatingSsoSyncBridge(
std::unique_ptr<syncer::DataTypeLocalChangeProcessor> change_processor,
syncer::OnceDataTypeStoreFactory create_store_callback)
: syncer::DataTypeSyncBridge(std::move(change_processor)) {
StoreWithCache::CreateAndLoad(
std::move(create_store_callback), syncer::COOKIES,
base::BindOnce(&FloatingSsoSyncBridge::OnStoreCreated,
weak_ptr_factory_.GetWeakPtr()));
}
FloatingSsoSyncBridge::~FloatingSsoSyncBridge() {
if (!deferred_cookie_additions_.empty() ||
!deferred_cookie_deletions_.empty()) {
DVLOG(1) << "Non-empty event queue at shutdown.";
}
}
std::unique_ptr<syncer::MetadataChangeList>
FloatingSsoSyncBridge::CreateMetadataChangeList() {
return syncer::DataTypeStore::WriteBatch::CreateMetadataChangeList();
}
std::optional<syncer::ModelError> FloatingSsoSyncBridge::MergeFullSyncData(
std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
syncer::EntityChangeList remote_entities) {
const CookieSpecificsEntries& in_memory_data = store_->in_memory_data();
std::set<std::string> local_keys_to_upload;
for (const auto& [key, specifics] : in_memory_data) {
local_keys_to_upload.insert(key);
}
// Go through `remote_entities` and filter out entities conflicting with local
// data if the local data should be preferred according to `ResolveConflict`.
// When remote data should be preferred, remove corresponding key from
// `local_keys_to_upload`.
std::erase_if(remote_entities,
[&](const std::unique_ptr<syncer::EntityChange>& change) {
auto it = local_keys_to_upload.find(change->storage_key());
if (it != local_keys_to_upload.end()) {
syncer::ConflictResolution result =
ResolveConflict(*it, change->data());
if (result == syncer::ConflictResolution::kUseLocal) {
return true;
} else {
CHECK_EQ(result, syncer::ConflictResolution::kUseRemote);
local_keys_to_upload.erase(it);
return false;
}
}
return false;
});
// Send entities corresponding to `local_keys_to_upload` to Sync server.
for (const std::string& storage_key : local_keys_to_upload) {
change_processor()->Put(storage_key,
CreateEntityData(in_memory_data.at(storage_key)),
metadata_change_list.get());
}
// Add remote entities to local data.
std::optional<syncer::ModelError> result = ApplyIncrementalSyncChanges(
std::move(metadata_change_list), std::move(remote_entities));
OnMergeFullSyncDataFinished();
return result;
}
std::optional<syncer::ModelError>
FloatingSsoSyncBridge::ApplyIncrementalSyncChanges(
std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
syncer::EntityChangeList entity_changes) {
std::vector<net::CanonicalCookie> added_or_updated;
std::vector<net::CanonicalCookie> deleted;
std::unique_ptr<StoreWithCache::WriteBatch> batch =
store_->CreateWriteBatch();
for (const std::unique_ptr<syncer::EntityChange>& change : entity_changes) {
switch (change->type()) {
case syncer::EntityChange::ACTION_ADD:
case syncer::EntityChange::ACTION_UPDATE: {
const sync_pb::CookieSpecifics& specifics =
change->data().specifics.cookie();
// We save `specifics` locally only when we don't fail to convert it to
// a `cookie` here. Alternatively we could still store `specifics` in
// the store and then try to create a cookie again in case of a Chrome
// update. We don't do this because: (1) in the targeted enterprise
// use case we expect affected devices to be on the same Chrome version
// and (2) the disparity between client-side and server-side states will
// not last long due to short TTL for cookies in Sync.
if (std::unique_ptr<net::CanonicalCookie> cookie =
FromSyncProto(specifics);
cookie) {
added_or_updated.push_back(*cookie);
batch->WriteData(change->storage_key(), specifics);
}
break;
}
case syncer::EntityChange::ACTION_DELETE: {
const CookieSpecificsEntries& in_memory_data = store_->in_memory_data();
auto it = in_memory_data.find(change->storage_key());
if (it == in_memory_data.end()) {
// Nothing to delete in the local store.
break;
}
batch->DeleteData(change->storage_key());
if (std::unique_ptr<net::CanonicalCookie> cookie =
FromSyncProto(it->second);
cookie) {
deleted.push_back(*cookie);
}
break;
}
}
}
batch->TakeMetadataChangesFrom(std::move(metadata_change_list));
CommitToStore(std::move(batch));
for (Observer& observer : observers_) {
// No need to notify about empty lists of changes.
if (!added_or_updated.empty()) {
observer.OnCookiesAddedOrUpdatedRemotely(added_or_updated);
}
if (!deleted.empty()) {
observer.OnCookiesRemovedRemotely(deleted);
}
}
return {};
}
std::string FloatingSsoSyncBridge::GetStorageKey(
const syncer::EntityData& entity_data) const {
return GetClientTag(entity_data);
}
std::string FloatingSsoSyncBridge::GetClientTag(
const syncer::EntityData& entity_data) const {
return entity_data.specifics.cookie().unique_key();
}
std::unique_ptr<syncer::DataBatch> FloatingSsoSyncBridge::GetDataForCommit(
StorageKeyList storage_keys) {
auto batch = std::make_unique<syncer::MutableDataBatch>();
const CookieSpecificsEntries& in_memory_data = store_->in_memory_data();
for (const std::string& storage_key : storage_keys) {
auto it = in_memory_data.find(storage_key);
if (it != in_memory_data.end()) {
batch->Put(it->first, CreateEntityData(it->second));
}
}
return batch;
}
std::unique_ptr<syncer::DataBatch>
FloatingSsoSyncBridge::GetAllDataForDebugging() {
auto batch = std::make_unique<syncer::MutableDataBatch>();
for (const auto& entry : store_->in_memory_data()) {
batch->Put(entry.first, CreateEntityData(entry.second));
}
return batch;
}
syncer::ConflictResolution FloatingSsoSyncBridge::ResolveConflict(
const std::string& storage_key,
const syncer::EntityData& remote_data) const {
if (keep_local_cookie_keys_.contains(storage_key)) {
return syncer::ConflictResolution::kUseLocal;
}
return syncer::DataTypeSyncBridge::ResolveConflict(storage_key, remote_data);
}
const FloatingSsoSyncBridge::CookieSpecificsEntries&
FloatingSsoSyncBridge::CookieSpecificsInStore() const {
return CHECK_DEREF(store_.get()).in_memory_data();
}
bool FloatingSsoSyncBridge::IsInitialDataReadFinishedForTest() const {
return is_initial_data_read_finished_;
}
void FloatingSsoSyncBridge::SetOnStoreCommitCallbackForTest(
base::RepeatingClosure callback) {
on_store_commit_callback_for_test_ = std::move(callback);
}
void FloatingSsoSyncBridge::OnStoreCreated(
const std::optional<syncer::ModelError>& error,
std::unique_ptr<StoreWithCache> store,
std::unique_ptr<syncer::MetadataBatch> metadata_batch) {
if (error) {
change_processor()->ReportError(*error);
deferred_cookie_additions_.clear();
deferred_cookie_deletions_.clear();
return;
}
CHECK(store);
store_ = std::move(store);
change_processor()->ModelReadyToSync(std::move(metadata_batch));
is_initial_data_read_finished_ = true;
ProcessQueuedCookies();
}
void FloatingSsoSyncBridge::ProcessQueuedCookies() {
// Add all new cookies. The two queues should not overlap.
for (const auto& [key, cookie] : deferred_cookie_additions_) {
if (deferred_cookie_deletions_.contains(key)) {
DVLOG(1) << "Cookie present in both addition and deletion queues. Will "
"perform delete.";
} else {
AddOrUpdateCookie(cookie);
}
}
// Delete queued cookies.
for (const auto& storage_key : deferred_cookie_deletions_) {
DeleteCookieWithKey(storage_key);
}
deferred_cookie_additions_.clear();
deferred_cookie_deletions_.clear();
}
void FloatingSsoSyncBridge::OnStoreCommit(
const std::optional<syncer::ModelError>& error) {
if (on_store_commit_callback_for_test_) {
on_store_commit_callback_for_test_.Run();
}
if (error) {
change_processor()->ReportError(*error);
}
}
void FloatingSsoSyncBridge::CommitToStore(
std::unique_ptr<StoreWithCache::WriteBatch> batch) {
store_->CommitWriteBatch(std::move(batch),
base::BindOnce(&FloatingSsoSyncBridge::OnStoreCommit,
weak_ptr_factory_.GetWeakPtr()));
}
bool FloatingSsoSyncBridge::IsCookieInStore(
const std::string& storage_key) const {
return store_->in_memory_data().contains(storage_key);
}
void FloatingSsoSyncBridge::AddOrUpdateCookie(
const net::CanonicalCookie& cookie) {
std::optional<std::string> serialization_result = SerializedKey(cookie);
if (!serialization_result.has_value()) {
return;
}
const std::string& storage_key = serialization_result.value();
if (!is_initial_data_read_finished_) {
deferred_cookie_additions_[storage_key] = cookie;
deferred_cookie_deletions_.erase(storage_key);
return;
}
std::optional<sync_pb::CookieSpecifics> specifics = ToSyncProto(cookie);
if (!specifics.has_value()) {
return;
}
// Check if an identical cookie already exists in the store, to avoid sending
// no-op changes to Sync.
const CookieSpecificsEntries& in_store_specifics = CookieSpecificsInStore();
if (auto it = in_store_specifics.find(specifics->unique_key());
it != in_store_specifics.end()) {
const sync_pb::CookieSpecifics& local_specifics = it->second;
std::unique_ptr<net::CanonicalCookie> in_store_cookie =
FromSyncProto(local_specifics);
if (in_store_cookie && in_store_cookie->HasEquivalentDataMembers(cookie)) {
return;
}
}
std::unique_ptr<StoreWithCache::WriteBatch> batch =
store_->CreateWriteBatch();
// Add/update this entry to the store and model.
change_processor()->Put(storage_key, CreateEntityData(specifics.value()),
batch->GetMetadataChangeList());
batch->WriteData(storage_key, specifics.value());
CommitToStore(std::move(batch));
}
void FloatingSsoSyncBridge::DeleteCookie(const net::CanonicalCookie& cookie) {
std::optional<std::string> serialization_result = SerializedKey(cookie);
if (!serialization_result.has_value()) {
return;
}
const std::string& storage_key = serialization_result.value();
if (!is_initial_data_read_finished_) {
deferred_cookie_deletions_.insert(storage_key);
deferred_cookie_additions_.erase(storage_key);
return;
}
DeleteCookieWithKey(storage_key);
}
void FloatingSsoSyncBridge::DeleteCookieWithKey(
const std::string& storage_key) {
// Check if the key is present in the store, to avoid sending no-op changes to
// Sync.
if (!IsCookieInStore(storage_key)) {
return;
}
std::unique_ptr<StoreWithCache::WriteBatch> batch =
store_->CreateWriteBatch();
change_processor()->Delete(storage_key, syncer::DeletionOrigin::Unspecified(),
batch->GetMetadataChangeList());
batch->DeleteData(storage_key);
CommitToStore(std::move(batch));
}
void FloatingSsoSyncBridge::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void FloatingSsoSyncBridge::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void FloatingSsoSyncBridge::AddToLocallyPreferredCookies(
const std::string& storage_key) {
keep_local_cookie_keys_.insert(storage_key);
}
void FloatingSsoSyncBridge::SetOnMergeFullSyncDataCallback(
base::OnceClosure callback) {
if (merge_full_sync_data_finished_) {
std::move(callback).Run();
return;
}
on_merge_full_sync_data_callback_ = std::move(callback);
}
void FloatingSsoSyncBridge::OnMergeFullSyncDataFinished() {
if (on_merge_full_sync_data_callback_) {
std::move(on_merge_full_sync_data_callback_).Run();
}
merge_full_sync_data_finished_ = true;
}
} // namespace ash::floating_sso
|