client_tag_based_model_type_processor.cc 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. // Copyright 2014 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/sync/model/client_tag_based_model_type_processor.h"
  5. #include <set>
  6. #include <utility>
  7. #include <vector>
  8. #include "base/bind.h"
  9. #include "base/location.h"
  10. #include "base/logging.h"
  11. #include "base/metrics/histogram_functions.h"
  12. #include "base/metrics/histogram_macros.h"
  13. #include "base/strings/stringprintf.h"
  14. #include "base/threading/sequenced_task_runner_handle.h"
  15. #include "base/trace_event/memory_usage_estimator.h"
  16. #include "components/sync/base/data_type_histogram.h"
  17. #include "components/sync/base/features.h"
  18. #include "components/sync/base/model_type.h"
  19. #include "components/sync/base/time.h"
  20. #include "components/sync/engine/commit_queue.h"
  21. #include "components/sync/engine/data_type_activation_response.h"
  22. #include "components/sync/engine/model_type_processor_metrics.h"
  23. #include "components/sync/engine/model_type_processor_proxy.h"
  24. #include "components/sync/model/client_tag_based_remote_update_handler.h"
  25. #include "components/sync/model/model_type_change_processor.h"
  26. #include "components/sync/model/processor_entity.h"
  27. #include "components/sync/model/type_entities_count.h"
  28. #include "components/sync/protocol/entity_data.h"
  29. #include "components/sync/protocol/entity_metadata.pb.h"
  30. #include "components/sync/protocol/model_type_state.pb.h"
  31. #include "components/sync/protocol/proto_value_conversions.h"
  32. namespace syncer {
  33. namespace {
  34. const char kErrorSiteHistogramPrefix[] = "Sync.ModelTypeErrorSite.";
  35. size_t CountDuplicateClientTags(const EntityMetadataMap& metadata_map) {
  36. size_t count = 0u;
  37. std::set<std::string> client_tag_hashes;
  38. for (const auto& [storage_key, metadata] : metadata_map) {
  39. const std::string& client_tag_hash = metadata->client_tag_hash();
  40. if (client_tag_hashes.find(client_tag_hash) != client_tag_hashes.end()) {
  41. count++;
  42. }
  43. client_tag_hashes.insert(client_tag_hash);
  44. }
  45. return count;
  46. }
  47. } // namespace
  48. ClientTagBasedModelTypeProcessor::ClientTagBasedModelTypeProcessor(
  49. ModelType type,
  50. const base::RepeatingClosure& dump_stack)
  51. : type_(type), bridge_(nullptr), dump_stack_(dump_stack) {
  52. ResetState(CLEAR_METADATA);
  53. }
  54. ClientTagBasedModelTypeProcessor::~ClientTagBasedModelTypeProcessor() {
  55. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  56. }
  57. void ClientTagBasedModelTypeProcessor::OnSyncStarting(
  58. const DataTypeActivationRequest& request,
  59. StartCallback start_callback) {
  60. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  61. DVLOG(1) << "Sync is starting for " << ModelTypeToDebugString(type_);
  62. DCHECK(request.IsValid()) << ModelTypeToDebugString(type_);
  63. DCHECK(start_callback) << ModelTypeToDebugString(type_);
  64. DCHECK(!start_callback_) << ModelTypeToDebugString(type_);
  65. DCHECK(!IsConnected()) << ModelTypeToDebugString(type_);
  66. start_callback_ = std::move(start_callback);
  67. activation_request_ = request;
  68. // Notify the bridge sync is starting before calling the |start_callback_|
  69. // which in turn creates the worker.
  70. bridge_->OnSyncStarting(request);
  71. ConnectIfReady();
  72. }
  73. void ClientTagBasedModelTypeProcessor::OnModelStarting(
  74. ModelTypeSyncBridge* bridge) {
  75. DCHECK(bridge);
  76. bridge_ = bridge;
  77. }
  78. void ClientTagBasedModelTypeProcessor::ModelReadyToSync(
  79. std::unique_ptr<MetadataBatch> batch) {
  80. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  81. DCHECK(!entity_tracker_);
  82. DCHECK(!model_ready_to_sync_);
  83. model_ready_to_sync_ = true;
  84. // The model already experienced an error; abort;
  85. if (model_error_)
  86. return;
  87. if (batch->GetModelTypeState().initial_sync_done()) {
  88. EntityMetadataMap metadata_map(batch->TakeAllMetadata());
  89. if (CheckForInvalidPersistedMetadata(metadata_map)) {
  90. entity_tracker_ = std::make_unique<ProcessorEntityTracker>(
  91. batch->GetModelTypeState(), std::move(metadata_map));
  92. }
  93. } else {
  94. // In older versions of the binary, commit-only types did not persist
  95. // initial_sync_done(). So this branch can be exercised for commit-only
  96. // types exactly once as an upgrade flow.
  97. // TODO(crbug.com/872360): This DCHECK can currently trigger if the user's
  98. // persisted Sync metadata is in an inconsistent state.
  99. DCHECK(CommitOnlyTypes().Has(type_) || batch->TakeAllMetadata().empty())
  100. << ModelTypeToDebugString(type_);
  101. }
  102. DCHECK(model_ready_to_sync_);
  103. ConnectIfReady();
  104. }
  105. bool ClientTagBasedModelTypeProcessor::IsAllowingChanges() const {
  106. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  107. // Changes can be handled correctly even before pending data is loaded.
  108. return model_ready_to_sync_;
  109. }
  110. void ClientTagBasedModelTypeProcessor::ConnectIfReady() {
  111. if (!start_callback_) {
  112. return;
  113. }
  114. if (model_error_) {
  115. activation_request_.error_handler.Run(model_error_.value());
  116. start_callback_.Reset();
  117. return;
  118. }
  119. if (!model_ready_to_sync_) {
  120. return;
  121. }
  122. CheckForInvalidPersistedModelTypeState();
  123. auto activation_response = std::make_unique<DataTypeActivationResponse>();
  124. if (!entity_tracker_) {
  125. sync_pb::ModelTypeState model_type_state;
  126. model_type_state.mutable_progress_marker()->set_data_type_id(
  127. GetSpecificsFieldNumberFromModelType(type_));
  128. model_type_state.set_cache_guid(activation_request_.cache_guid);
  129. model_type_state.set_authenticated_account_id(
  130. activation_request_.authenticated_account_id.ToString());
  131. if (CommitOnlyTypes().Has(type_)) {
  132. // For commit-only types, no updates are expected and hence we can
  133. // consider initial_sync_done(), reflecting that sync is enabled.
  134. model_type_state.set_initial_sync_done(true);
  135. OnFullUpdateReceived(model_type_state, UpdateResponseDataList());
  136. DCHECK(entity_tracker_);
  137. } else {
  138. activation_response->model_type_state = model_type_state;
  139. }
  140. }
  141. if (entity_tracker_) {
  142. activation_response->model_type_state = entity_tracker_->model_type_state();
  143. }
  144. DCHECK_EQ(activation_response->model_type_state.cache_guid(),
  145. activation_request_.cache_guid);
  146. activation_response->type_processor =
  147. std::make_unique<ModelTypeProcessorProxy>(
  148. weak_ptr_factory_for_worker_.GetWeakPtr(),
  149. base::SequencedTaskRunnerHandle::Get());
  150. // Defer invoking of |start_callback_| to avoid synchronous call from the
  151. // |bridge_|. It might cause a situation when inside the ModelReadyToSync()
  152. // another methods of the bridge eventually were called. This behavior would
  153. // be complicated and be unexpected in some bridges.
  154. // See crbug.com/1055584 for more details.
  155. base::SequencedTaskRunnerHandle::Get()->PostTask(
  156. FROM_HERE, base::BindOnce(std::move(start_callback_),
  157. std::move(activation_response)));
  158. }
  159. bool ClientTagBasedModelTypeProcessor::IsConnected() const {
  160. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  161. return !!worker_;
  162. }
  163. void ClientTagBasedModelTypeProcessor::OnSyncStopping(
  164. SyncStopMetadataFate metadata_fate) {
  165. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  166. // Disabling sync for a type never happens before the model is ready to sync.
  167. DCHECK(model_ready_to_sync_);
  168. DCHECK(!start_callback_);
  169. switch (metadata_fate) {
  170. case KEEP_METADATA: {
  171. bridge_->ApplyStopSyncChanges(
  172. /*delete_metadata_change_list=*/nullptr);
  173. // The model is still ready to sync (with the same |bridge_|) and same
  174. // sync metadata.
  175. ResetState(KEEP_METADATA);
  176. DCHECK(model_ready_to_sync_);
  177. break;
  178. }
  179. case CLEAR_METADATA: {
  180. ClearAllTrackedMetadataAndResetState();
  181. DCHECK(model_ready_to_sync_);
  182. break;
  183. }
  184. }
  185. DCHECK(!IsConnected());
  186. }
  187. void ClientTagBasedModelTypeProcessor::ClearAllTrackedMetadataAndResetState() {
  188. std::unique_ptr<MetadataChangeList> change_list;
  189. // All changes before the initial sync is done are ignored and in fact they
  190. // were never persisted by the bridge (prior to MergeSyncData), so no
  191. // entities should be tracking.
  192. //
  193. // Clear metadata if MergeSyncData() was called before.
  194. if (entity_tracker_) {
  195. change_list = bridge_->CreateMetadataChangeList();
  196. std::vector<const ProcessorEntity*> entities =
  197. entity_tracker_->GetAllEntitiesIncludingTombstones();
  198. for (const ProcessorEntity* entity : entities) {
  199. change_list->ClearMetadata(entity->storage_key());
  200. }
  201. change_list->ClearModelTypeState();
  202. }
  203. ClearAllMetadataAndResetStateImpl(std::move(change_list));
  204. }
  205. void ClientTagBasedModelTypeProcessor::ClearAllProvidedMetadataAndResetState(
  206. const EntityMetadataMap& metadata_map) {
  207. std::unique_ptr<MetadataChangeList> change_list =
  208. bridge_->CreateMetadataChangeList();
  209. for (const auto& [storage_key, metadata] : metadata_map) {
  210. change_list->ClearMetadata(storage_key);
  211. }
  212. change_list->ClearModelTypeState();
  213. ClearAllMetadataAndResetStateImpl(std::move(change_list));
  214. }
  215. void ClientTagBasedModelTypeProcessor::ClearAllMetadataAndResetStateImpl(
  216. std::unique_ptr<MetadataChangeList> change_list) {
  217. bridge_->ApplyStopSyncChanges(std::move(change_list));
  218. // Reset all the internal state of the processor.
  219. ResetState(CLEAR_METADATA);
  220. if (activation_request_.IsValid()) {
  221. // If OnSyncStarting() already was called, notify the bridge again.
  222. bridge_->OnSyncStarting(activation_request_);
  223. }
  224. }
  225. bool ClientTagBasedModelTypeProcessor::IsTrackingMetadata() const {
  226. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  227. return entity_tracker_ != nullptr;
  228. }
  229. std::string ClientTagBasedModelTypeProcessor::TrackedAccountId() const {
  230. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  231. // Returning non-empty here despite !IsTrackingMetadata() has weird semantics,
  232. // e.g. initial updates are being fetched but we haven't received the response
  233. // (i.e. prior to exercising MergeSyncData()). Let's be cautious and hide the
  234. // account ID.
  235. if (!IsTrackingMetadata()) {
  236. return "";
  237. }
  238. return entity_tracker_->model_type_state().authenticated_account_id();
  239. }
  240. std::string ClientTagBasedModelTypeProcessor::TrackedCacheGuid() const {
  241. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  242. // Returning non-empty here despite !IsTrackingMetadata() has weird semantics,
  243. // e.g. initial updates are being fetched but we haven't received the response
  244. // (i.e. prior to exercising MergeSyncData()). Let's be cautious and hide the
  245. // cache GUID.
  246. if (!IsTrackingMetadata()) {
  247. return "";
  248. }
  249. return entity_tracker_->model_type_state().cache_guid();
  250. }
  251. void ClientTagBasedModelTypeProcessor::ReportError(const ModelError& error) {
  252. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  253. ReportErrorImpl(error, ErrorSite::kBridgeInitiated);
  254. }
  255. void ClientTagBasedModelTypeProcessor::ReportErrorImpl(const ModelError& error,
  256. ErrorSite site) {
  257. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  258. // Ignore all errors after the first.
  259. if (model_error_) {
  260. return;
  261. }
  262. model_error_ = error;
  263. const std::string type_suffix = ModelTypeToHistogramSuffix(type_);
  264. base::UmaHistogramEnumeration(kErrorSiteHistogramPrefix + type_suffix, site);
  265. if (dump_stack_) {
  266. // Upload a stack trace if possible.
  267. dump_stack_.Run();
  268. }
  269. if (IsConnected()) {
  270. DisconnectSync();
  271. }
  272. // Shouldn't connect anymore.
  273. start_callback_.Reset();
  274. if (activation_request_.IsValid()) {
  275. // Tell sync about the error.
  276. activation_request_.error_handler.Run(error);
  277. }
  278. // If the error handler isn't ready yet, we defer reporting the error until it
  279. // becomes available which happens in ConnectIfReady() upon OnSyncStarting().
  280. }
  281. absl::optional<ModelError> ClientTagBasedModelTypeProcessor::GetError() const {
  282. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  283. return model_error_;
  284. }
  285. base::WeakPtr<ModelTypeControllerDelegate>
  286. ClientTagBasedModelTypeProcessor::GetControllerDelegate() {
  287. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  288. return weak_ptr_factory_for_controller_.GetWeakPtr();
  289. }
  290. void ClientTagBasedModelTypeProcessor::ConnectSync(
  291. std::unique_ptr<CommitQueue> worker) {
  292. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  293. DVLOG(1) << "Successfully connected " << ModelTypeToDebugString(type_);
  294. worker_ = std::move(worker);
  295. NudgeForCommitIfNeeded();
  296. }
  297. void ClientTagBasedModelTypeProcessor::DisconnectSync() {
  298. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  299. DCHECK(IsConnected());
  300. DVLOG(1) << "Disconnecting sync for " << ModelTypeToDebugString(type_);
  301. weak_ptr_factory_for_worker_.InvalidateWeakPtrs();
  302. worker_.reset();
  303. if (entity_tracker_) {
  304. entity_tracker_->ClearTransientSyncState();
  305. }
  306. }
  307. void ClientTagBasedModelTypeProcessor::Put(
  308. const std::string& storage_key,
  309. std::unique_ptr<EntityData> data,
  310. MetadataChangeList* metadata_change_list) {
  311. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  312. DCHECK(IsAllowingChanges());
  313. DCHECK(data);
  314. DCHECK(!data->is_deleted());
  315. DCHECK(!data->name.empty());
  316. DCHECK(!data->specifics.has_encrypted());
  317. DCHECK(!storage_key.empty());
  318. DCHECK_EQ(type_, GetModelTypeFromSpecifics(data->specifics));
  319. if (!entity_tracker_) {
  320. // Ignore changes before the initial sync is done.
  321. return;
  322. }
  323. // |data->specifics| is about to be committed, and therefore represents the
  324. // imminent server-side state in most cases.
  325. sync_pb::EntitySpecifics trimmed_specifics =
  326. bridge_->TrimRemoteSpecificsForCaching(data->specifics);
  327. ProcessorEntity* entity =
  328. entity_tracker_->GetEntityForStorageKey(storage_key);
  329. if (entity == nullptr) {
  330. // The bridge is creating a new entity. The bridge may or may not populate
  331. // |data->client_tag_hash|, so let's ask for the client tag if needed.
  332. if (data->client_tag_hash.value().empty()) {
  333. DCHECK(bridge_->SupportsGetClientTag());
  334. data->client_tag_hash =
  335. ClientTagHash::FromUnhashed(type_, bridge_->GetClientTag(*data));
  336. } else if (bridge_->SupportsGetClientTag()) {
  337. // If the Put() call already included the client tag, let's verify that
  338. // it's consistent with the bridge's regular GetClientTag() function (if
  339. // supported by the bridge).
  340. DCHECK_EQ(
  341. data->client_tag_hash,
  342. ClientTagHash::FromUnhashed(type_, bridge_->GetClientTag(*data)));
  343. }
  344. // If another entity exists for the same client_tag_hash, it could be the
  345. // case that the bridge has deleted this entity but the tombstone hasn't
  346. // been sent to the server yet, and the bridge is trying to re-create this
  347. // entity with a new storage key. In such case, we should reuse the existing
  348. // entity.
  349. entity = entity_tracker_->GetEntityForTagHash(data->client_tag_hash);
  350. if (entity != nullptr) {
  351. DCHECK(storage_key != entity->storage_key());
  352. if (!entity->metadata().is_deleted()) {
  353. // The bridge overrides an entity that is not deleted. This is
  354. // unexpected but the processor tolerates it. It is very likely a
  355. // metadata orphan; report it to metrics.
  356. UMA_HISTOGRAM_ENUMERATION("Sync.ModelTypeOrphanMetadata.Put",
  357. ModelTypeHistogramValue(type_));
  358. }
  359. // Remove the old storage key from the tracker and the corresponding
  360. // metadata record.
  361. metadata_change_list->ClearMetadata(entity->storage_key());
  362. entity_tracker_->UpdateOrOverrideStorageKey(data->client_tag_hash,
  363. storage_key);
  364. entity->RecordLocalUpdate(std::move(data), std::move(trimmed_specifics));
  365. } else {
  366. if (data->creation_time.is_null())
  367. data->creation_time = base::Time::Now();
  368. if (data->modification_time.is_null())
  369. data->modification_time = data->creation_time;
  370. entity = entity_tracker_->AddUnsyncedLocal(storage_key, std::move(data),
  371. std::move(trimmed_specifics));
  372. }
  373. } else if (entity->MatchesData(*data)) {
  374. // Ignore changes that don't actually change anything.
  375. return;
  376. } else {
  377. entity->RecordLocalUpdate(std::move(data), std::move(trimmed_specifics));
  378. }
  379. DCHECK(entity->IsUnsynced());
  380. metadata_change_list->UpdateMetadata(storage_key, entity->metadata());
  381. NudgeForCommitIfNeeded();
  382. }
  383. void ClientTagBasedModelTypeProcessor::Delete(
  384. const std::string& storage_key,
  385. MetadataChangeList* metadata_change_list) {
  386. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  387. DCHECK(IsAllowingChanges());
  388. if (!entity_tracker_) {
  389. // Ignore changes before the initial sync is done.
  390. return;
  391. }
  392. ProcessorEntity* entity =
  393. entity_tracker_->GetEntityForStorageKey(storage_key);
  394. if (entity == nullptr) {
  395. // Missing is as good as deleted as far as the model is concerned.
  396. return;
  397. }
  398. if (entity->RecordLocalDeletion())
  399. metadata_change_list->UpdateMetadata(storage_key, entity->metadata());
  400. else
  401. RemoveEntity(entity->storage_key(), metadata_change_list);
  402. NudgeForCommitIfNeeded();
  403. }
  404. void ClientTagBasedModelTypeProcessor::UpdateStorageKey(
  405. const EntityData& entity_data,
  406. const std::string& storage_key,
  407. MetadataChangeList* metadata_change_list) {
  408. const ClientTagHash& client_tag_hash = entity_data.client_tag_hash;
  409. DCHECK(!client_tag_hash.value().empty());
  410. DCHECK(!storage_key.empty());
  411. DCHECK(!bridge_->SupportsGetStorageKey());
  412. DCHECK(entity_tracker_);
  413. const ProcessorEntity* entity =
  414. entity_tracker_->GetEntityForTagHash(client_tag_hash);
  415. DCHECK(entity);
  416. DCHECK(entity->storage_key().empty());
  417. entity_tracker_->UpdateOrOverrideStorageKey(client_tag_hash, storage_key);
  418. metadata_change_list->UpdateMetadata(storage_key, entity->metadata());
  419. }
  420. void ClientTagBasedModelTypeProcessor::UntrackEntityForStorageKey(
  421. const std::string& storage_key) {
  422. if (!entity_tracker_) {
  423. // Ignore changes before the initial sync is done.
  424. return;
  425. }
  426. entity_tracker_->RemoveEntityForStorageKey(storage_key);
  427. }
  428. void ClientTagBasedModelTypeProcessor::UntrackEntityForClientTagHash(
  429. const ClientTagHash& client_tag_hash) {
  430. DCHECK(!client_tag_hash.value().empty());
  431. if (!entity_tracker_) {
  432. // Ignore changes before the initial sync is done.
  433. return;
  434. }
  435. entity_tracker_->RemoveEntityForClientTagHash(client_tag_hash);
  436. }
  437. std::vector<std::string>
  438. ClientTagBasedModelTypeProcessor::GetAllTrackedStorageKeys() const {
  439. std::vector<std::string> storage_keys;
  440. if (entity_tracker_) {
  441. for (const ProcessorEntity* entity :
  442. entity_tracker_->GetAllEntitiesIncludingTombstones()) {
  443. storage_keys.push_back(entity->storage_key());
  444. }
  445. }
  446. return storage_keys;
  447. }
  448. bool ClientTagBasedModelTypeProcessor::IsEntityUnsynced(
  449. const std::string& storage_key) {
  450. if (!entity_tracker_) {
  451. return false;
  452. }
  453. const ProcessorEntity* entity =
  454. entity_tracker_->GetEntityForStorageKey(storage_key);
  455. if (entity == nullptr) {
  456. return false;
  457. }
  458. return entity->IsUnsynced();
  459. }
  460. base::Time ClientTagBasedModelTypeProcessor::GetEntityCreationTime(
  461. const std::string& storage_key) const {
  462. if (!entity_tracker_) {
  463. return base::Time();
  464. }
  465. const ProcessorEntity* entity =
  466. entity_tracker_->GetEntityForStorageKey(storage_key);
  467. if (entity == nullptr) {
  468. return base::Time();
  469. }
  470. return ProtoTimeToTime(entity->metadata().creation_time());
  471. }
  472. base::Time ClientTagBasedModelTypeProcessor::GetEntityModificationTime(
  473. const std::string& storage_key) const {
  474. if (!entity_tracker_) {
  475. return base::Time();
  476. }
  477. const ProcessorEntity* entity =
  478. entity_tracker_->GetEntityForStorageKey(storage_key);
  479. if (entity == nullptr) {
  480. return base::Time();
  481. }
  482. return ProtoTimeToTime(entity->metadata().modification_time());
  483. }
  484. void ClientTagBasedModelTypeProcessor::NudgeForCommitIfNeeded() {
  485. // Don't bother sending anything if there's no one to send to.
  486. if (!IsConnected())
  487. return;
  488. // Don't send anything if the type is not ready to handle commits.
  489. if (!entity_tracker_)
  490. return;
  491. // Nudge worker if there are any entities with local changes.0
  492. if (entity_tracker_->HasLocalChanges())
  493. worker_->NudgeForCommit();
  494. }
  495. void ClientTagBasedModelTypeProcessor::GetLocalChanges(
  496. size_t max_entries,
  497. GetLocalChangesCallback callback) {
  498. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  499. DCHECK_GT(max_entries, 0U);
  500. // If there is a model error, it must have been reported already but hasn't
  501. // reached the sync engine yet. In this case return directly to avoid
  502. // interactions with the bridge.
  503. // In some cases local changes may be requested before entity tracker is
  504. // loaded. Just invoke the callback with empty list.
  505. if (model_error_ || !entity_tracker_) {
  506. std::move(callback).Run(CommitRequestDataList());
  507. return;
  508. }
  509. std::vector<const ProcessorEntity*> entities =
  510. entity_tracker_->GetAllEntitiesIncludingTombstones();
  511. std::vector<std::string> entities_requiring_data;
  512. for (const ProcessorEntity* entity : entities) {
  513. if (entity->RequiresCommitData()) {
  514. entities_requiring_data.push_back(entity->storage_key());
  515. }
  516. }
  517. if (!entities_requiring_data.empty()) {
  518. // Make a copy for the callback so that we can check if everything was
  519. // loaded successfully.
  520. std::unordered_set<std::string> storage_keys_to_load(
  521. entities_requiring_data.begin(), entities_requiring_data.end());
  522. bridge_->GetData(
  523. std::move(entities_requiring_data),
  524. base::BindOnce(&ClientTagBasedModelTypeProcessor::OnPendingDataLoaded,
  525. weak_ptr_factory_for_worker_.GetWeakPtr(), max_entries,
  526. std::move(callback), std::move(storage_keys_to_load)));
  527. } else {
  528. // All commit data can be available in memory for those entries passed in
  529. // the .put() method.
  530. CommitLocalChanges(max_entries, std::move(callback));
  531. }
  532. }
  533. void ClientTagBasedModelTypeProcessor::OnCommitCompleted(
  534. const sync_pb::ModelTypeState& model_type_state,
  535. const CommitResponseDataList& committed_response_list,
  536. const FailedCommitResponseDataList& error_response_list) {
  537. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  538. DCHECK(!model_error_);
  539. DCHECK(entity_tracker_)
  540. << "Received commit response when entity tracker is null. Type: "
  541. << ModelTypeToDebugString(type_);
  542. // |error_response_list| is ignored, because all errors are treated as
  543. // transientand the processor with eventually retry.
  544. std::unique_ptr<MetadataChangeList> metadata_change_list =
  545. bridge_->CreateMetadataChangeList();
  546. EntityChangeList entity_change_list;
  547. entity_tracker_->set_model_type_state(model_type_state);
  548. metadata_change_list->UpdateModelTypeState(
  549. entity_tracker_->model_type_state());
  550. for (const CommitResponseData& data : committed_response_list) {
  551. ProcessorEntity* entity =
  552. entity_tracker_->GetEntityForTagHash(data.client_tag_hash);
  553. if (entity == nullptr) {
  554. NOTREACHED() << "Received commit response for missing item."
  555. << " type: " << ModelTypeToDebugString(type_)
  556. << " client_tag_hash: " << data.client_tag_hash;
  557. continue;
  558. }
  559. entity->ReceiveCommitResponse(data, CommitOnlyTypes().Has(type_));
  560. if (CommitOnlyTypes().Has(type_)) {
  561. if (!entity->IsUnsynced()) {
  562. entity_change_list.push_back(
  563. EntityChange::CreateDelete(entity->storage_key()));
  564. RemoveEntity(entity->storage_key(), metadata_change_list.get());
  565. }
  566. // If unsynced, we could theoretically update persisted metadata to have
  567. // more accurate bookkeeping. However, this wouldn't actually do anything
  568. // useful, we still need to commit again, and we're not going to include
  569. // any of the changing metadata in the commit message. So skip updating
  570. // metadata.
  571. } else if (entity->CanClearMetadata()) {
  572. RemoveEntity(entity->storage_key(), metadata_change_list.get());
  573. } else {
  574. metadata_change_list->UpdateMetadata(entity->storage_key(),
  575. entity->metadata());
  576. }
  577. }
  578. // Entities not mentioned in response_list weren't committed. We should reset
  579. // their commit_requested_sequence_number so they are committed again on next
  580. // sync cycle.
  581. // TODO(crbug.com/740757): Iterating over all entities is inefficient. It is
  582. // better to remember in GetLocalChanges which entities are being committed
  583. // and adjust only them. Alternatively we can make worker return commit status
  584. // for all entities, not just successful ones and use that to lookup entities
  585. // to clear.
  586. entity_tracker_->ClearTransientSyncState();
  587. absl::optional<ModelError> error = bridge_->ApplySyncChanges(
  588. std::move(metadata_change_list), std::move(entity_change_list));
  589. if (!error_response_list.empty()) {
  590. bridge_->OnCommitAttemptErrors(error_response_list);
  591. }
  592. if (error) {
  593. ReportErrorImpl(*error, ErrorSite::kApplyUpdatesOnCommitResponse);
  594. }
  595. }
  596. // Returns whether the state has a version_watermark based GC directive, which
  597. // tells us to clear all sync data that's stored locally.
  598. bool HasClearAllDirective(const sync_pb::ModelTypeState& model_type_state) {
  599. return model_type_state.progress_marker()
  600. .gc_directive()
  601. .has_version_watermark();
  602. }
  603. void ClientTagBasedModelTypeProcessor::OnCommitFailed(
  604. SyncCommitError commit_error) {
  605. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  606. switch (bridge_->OnCommitAttemptFailed(commit_error)) {
  607. case ModelTypeSyncBridge::CommitAttemptFailedBehavior::
  608. kShouldRetryOnNextCycle:
  609. // Entities weren't committed. Reset their
  610. // |commit_requested_sequence_number| to commit them again on next sync
  611. // cycle.
  612. entity_tracker_->ClearTransientSyncState();
  613. break;
  614. case ModelTypeSyncBridge::CommitAttemptFailedBehavior::
  615. kDontRetryOnNextCycle:
  616. // Do nothing and leave all entities in a transient state.
  617. break;
  618. }
  619. }
  620. void ClientTagBasedModelTypeProcessor::OnUpdateReceived(
  621. const sync_pb::ModelTypeState& model_type_state,
  622. UpdateResponseDataList updates) {
  623. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  624. DCHECK(model_ready_to_sync_);
  625. DCHECK(!model_error_);
  626. const bool is_initial_sync = !IsTrackingMetadata();
  627. LogUpdatesReceivedByProcessorHistogram(type_, is_initial_sync,
  628. updates.size());
  629. if (!ValidateUpdate(model_type_state, updates)) {
  630. return;
  631. }
  632. absl::optional<ModelError> error;
  633. // We call OnFullUpdateReceived when it's the first sync cycle, or when
  634. // we get a garbage collection directive from the server telling us to clear
  635. // all data by version watermark.
  636. // This means that if we receive a version watermark based GC directive, we
  637. // always clear all data. We do this to allow the server to replace all data
  638. // on the client, without having to know exactly which entities the client
  639. // has.
  640. const bool treating_as_full_update =
  641. is_initial_sync || HasClearAllDirective(model_type_state);
  642. if (treating_as_full_update) {
  643. error = OnFullUpdateReceived(model_type_state, std::move(updates));
  644. } else {
  645. error = OnIncrementalUpdateReceived(model_type_state, std::move(updates));
  646. }
  647. if (error) {
  648. ReportErrorImpl(*error, treating_as_full_update
  649. ? ErrorSite::kApplyFullUpdates
  650. : ErrorSite::kApplyIncrementalUpdates);
  651. return;
  652. }
  653. if (is_initial_sync) {
  654. base::TimeDelta configuration_duration =
  655. base::Time::Now() - activation_request_.configuration_start_time;
  656. base::UmaHistogramCustomTimes(
  657. base::StringPrintf(
  658. "Sync.ModelTypeConfigurationTime.%s.%s",
  659. (activation_request_.sync_mode == SyncMode::kTransportOnly)
  660. ? "Ephemeral"
  661. : "Persistent",
  662. ModelTypeToHistogramSuffix(type_)),
  663. configuration_duration,
  664. /*min=*/base::Milliseconds(1),
  665. /*min=*/base::Seconds(60),
  666. /*buckets=*/50);
  667. }
  668. DCHECK(entity_tracker_);
  669. // If there were entities with empty storage keys, they should have been
  670. // updated by bridge as part of ApplySyncChanges.
  671. DCHECK(entity_tracker_->AllStorageKeysPopulated());
  672. // There may be new reasons to commit by the time this function is done.
  673. NudgeForCommitIfNeeded();
  674. }
  675. bool ClientTagBasedModelTypeProcessor::ValidateUpdate(
  676. const sync_pb::ModelTypeState& model_type_state,
  677. const UpdateResponseDataList& updates) {
  678. if (!entity_tracker_) {
  679. // Due to uss_migrator, initial sync (when migrating from non-USS) does not
  680. // contain any gc directives. Thus, we cannot expect the conditions below to
  681. // be satisfied. It is okay to skip the check as for an initial sync, the gc
  682. // directive does not make any semantical difference.
  683. return true;
  684. }
  685. if (HasClearAllDirective(model_type_state) &&
  686. bridge_->SupportsIncrementalUpdates()) {
  687. ReportErrorImpl(ModelError(FROM_HERE,
  688. "Received an update with version watermark for "
  689. "bridge that supports incremental updates"),
  690. ErrorSite::kSupportsIncrementalUpdatesMismatch);
  691. return false;
  692. } else if (!HasClearAllDirective(model_type_state) &&
  693. !bridge_->SupportsIncrementalUpdates() && !updates.empty()) {
  694. // We receive an update without clear all directive from the server to
  695. // indicate no data has changed. This contradicts with the list of updates
  696. // being non-empty, the bridge cannot handle it and we need to fail here.
  697. // (If the last condition does not hold true and the list of updates is
  698. // empty, we still need to pass the empty update to the bridge because the
  699. // progress marker might have changed.)
  700. ReportErrorImpl(ModelError(FROM_HERE,
  701. "Received a non-empty update without version "
  702. "watermark for bridge that does not support "
  703. "incremental updates"),
  704. ErrorSite::kSupportsIncrementalUpdatesMismatch);
  705. return false;
  706. }
  707. return true;
  708. }
  709. absl::optional<ModelError>
  710. ClientTagBasedModelTypeProcessor::OnFullUpdateReceived(
  711. const sync_pb::ModelTypeState& model_type_state,
  712. UpdateResponseDataList updates) {
  713. std::unique_ptr<MetadataChangeList> metadata_changes =
  714. bridge_->CreateMetadataChangeList();
  715. DCHECK(model_ready_to_sync_);
  716. // Check that the worker correctly marked initial sync as done for this
  717. // update.
  718. DCHECK(model_type_state.initial_sync_done());
  719. // Ensure that this is the initial sync, and it was not already marked done.
  720. DCHECK(HasClearAllDirective(model_type_state) || !entity_tracker_);
  721. if (entity_tracker_ && HasClearAllDirective(model_type_state)) {
  722. ExpireAllEntries(metadata_changes.get());
  723. entity_tracker_->set_model_type_state(model_type_state);
  724. }
  725. if (!entity_tracker_) {
  726. entity_tracker_ = std::make_unique<ProcessorEntityTracker>(
  727. model_type_state, EntityMetadataMap());
  728. }
  729. // TODO(crbug.com/1041888): the comment below may be wrong in case where a
  730. // datatype supports non-incremental updates and local updates are
  731. // acceptable.
  732. // Given that we either just removed all existing sync entities (in the full
  733. // update case).
  734. DCHECK(!entity_tracker_->size());
  735. metadata_changes->UpdateModelTypeState(entity_tracker_->model_type_state());
  736. EntityChangeList entity_data;
  737. for (syncer::UpdateResponseData& update : updates) {
  738. const ClientTagHash& client_tag_hash = update.entity.client_tag_hash;
  739. if (client_tag_hash.value().empty()) {
  740. // Ignore updates missing a client tag hash (e.g. permanent nodes).
  741. continue;
  742. }
  743. if (update.entity.is_deleted()) {
  744. SyncRecordModelTypeUpdateDropReason(
  745. UpdateDropReason::kTombstoneInFullUpdate, type_);
  746. DLOG(WARNING) << "Ignoring tombstone found during initial update: "
  747. << "client_tag_hash = " << client_tag_hash << " for "
  748. << ModelTypeToDebugString(type_);
  749. continue;
  750. }
  751. if (bridge_->SupportsGetClientTag() &&
  752. client_tag_hash != ClientTagHash::FromUnhashed(
  753. type_, bridge_->GetClientTag(update.entity))) {
  754. SyncRecordModelTypeUpdateDropReason(
  755. UpdateDropReason::kInconsistentClientTag, type_);
  756. DLOG(WARNING) << "Received unexpected client tag hash: "
  757. << client_tag_hash << " for "
  758. << ModelTypeToDebugString(type_);
  759. continue;
  760. }
  761. std::string storage_key;
  762. if (bridge_->SupportsGetStorageKey()) {
  763. storage_key = bridge_->GetStorageKey(update.entity);
  764. if (storage_key.empty()) {
  765. SyncRecordModelTypeUpdateDropReason(
  766. UpdateDropReason::kCannotGenerateStorageKey, type_);
  767. DLOG(WARNING) << "Received entity with invalid update for "
  768. << ModelTypeToDebugString(type_);
  769. continue;
  770. }
  771. }
  772. #if DCHECK_IS_ON()
  773. // TODO(crbug.com/872360): The CreateEntity() call below assumes that no
  774. // entity with this client_tag_hash exists already, but in some cases it
  775. // does.
  776. if (entity_tracker_->GetEntityForTagHash(client_tag_hash)) {
  777. DLOG(ERROR) << "Received duplicate client_tag_hash " << client_tag_hash
  778. << " for " << ModelTypeToDebugString(type_);
  779. }
  780. #endif // DCHECK_IS_ON()
  781. ProcessorEntity* entity = entity_tracker_->AddRemote(
  782. storage_key, update,
  783. bridge_->TrimRemoteSpecificsForCaching(update.entity.specifics));
  784. entity_data.push_back(
  785. EntityChange::CreateAdd(storage_key, std::move(update.entity)));
  786. if (!storage_key.empty())
  787. metadata_changes->UpdateMetadata(storage_key, entity->metadata());
  788. }
  789. // Let the bridge handle associating and merging the data.
  790. absl::optional<ModelError> error = bridge_->MergeSyncData(
  791. std::move(metadata_changes), std::move(entity_data));
  792. return error;
  793. }
  794. absl::optional<ModelError>
  795. ClientTagBasedModelTypeProcessor::OnIncrementalUpdateReceived(
  796. const sync_pb::ModelTypeState& model_type_state,
  797. UpdateResponseDataList updates) {
  798. DCHECK(model_ready_to_sync_);
  799. DCHECK(model_type_state.initial_sync_done());
  800. DCHECK(entity_tracker_);
  801. ClientTagBasedRemoteUpdateHandler updates_handler(type_, bridge_,
  802. entity_tracker_.get());
  803. return updates_handler.ProcessIncrementalUpdate(model_type_state,
  804. std::move(updates));
  805. }
  806. void ClientTagBasedModelTypeProcessor::OnPendingDataLoaded(
  807. size_t max_entries,
  808. GetLocalChangesCallback callback,
  809. std::unordered_set<std::string> storage_keys_to_load,
  810. std::unique_ptr<DataBatch> data_batch) {
  811. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  812. // The model already experienced an error; abort;
  813. if (model_error_)
  814. return;
  815. ConsumeDataBatch(std::move(storage_keys_to_load), std::move(data_batch));
  816. CommitLocalChanges(max_entries, std::move(callback));
  817. }
  818. void ClientTagBasedModelTypeProcessor::ConsumeDataBatch(
  819. std::unordered_set<std::string> storage_keys_to_load,
  820. std::unique_ptr<DataBatch> data_batch) {
  821. DCHECK(entity_tracker_);
  822. while (data_batch->HasNext()) {
  823. auto [storage_key, data] = data_batch->Next();
  824. storage_keys_to_load.erase(storage_key);
  825. ProcessorEntity* entity =
  826. entity_tracker_->GetEntityForStorageKey(storage_key);
  827. // If the entity wasn't deleted or updated with new commit.
  828. if (entity != nullptr && entity->RequiresCommitData()) {
  829. // SetCommitData will update EntityData's fields with values from
  830. // metadata.
  831. entity->SetCommitData(std::move(data));
  832. }
  833. }
  834. // Detect failed loads that shouldn't have failed.
  835. std::vector<std::string> storage_keys_to_untrack;
  836. for (const std::string& storage_key : storage_keys_to_load) {
  837. const ProcessorEntity* entity =
  838. entity_tracker_->GetEntityForStorageKey(storage_key);
  839. if (entity == nullptr || entity->metadata().is_deleted()) {
  840. // Skip entities that are not tracked any more or already marked for
  841. // deletion.
  842. continue;
  843. }
  844. // This scenario indicates a bug in the bridge, which didn't properly
  845. // propagate a local deletion to the processor, either in the form of
  846. // Delete() or UntrackEntity(). As a workaround to avoid negative side
  847. // effects of this inconsistent state, we treat it as if UntrackEntity()
  848. // had been called.
  849. storage_keys_to_untrack.push_back(storage_key);
  850. UMA_HISTOGRAM_ENUMERATION("Sync.ModelTypeOrphanMetadata.GetData",
  851. ModelTypeHistogramValue(type_));
  852. }
  853. if (storage_keys_to_untrack.empty()) {
  854. return;
  855. }
  856. DCHECK(model_ready_to_sync_);
  857. DCHECK(IsTrackingMetadata());
  858. std::unique_ptr<MetadataChangeList> metadata_changes =
  859. bridge_->CreateMetadataChangeList();
  860. for (const std::string& storage_key : storage_keys_to_untrack) {
  861. UntrackEntityForStorageKey(storage_key);
  862. metadata_changes->ClearMetadata(storage_key);
  863. }
  864. bridge_->ApplySyncChanges(std::move(metadata_changes), EntityChangeList());
  865. }
  866. void ClientTagBasedModelTypeProcessor::CommitLocalChanges(
  867. size_t max_entries,
  868. GetLocalChangesCallback callback) {
  869. DCHECK(!model_error_);
  870. DCHECK(entity_tracker_);
  871. // Prepares entities commit request data for entities which are
  872. // out of sync with the sync thread.
  873. CommitRequestDataList commit_requests;
  874. // TODO(rlarocque): Do something smarter than iterate here.
  875. std::vector<ProcessorEntity*> entities =
  876. entity_tracker_->GetEntitiesWithLocalChanges(max_entries);
  877. for (ProcessorEntity* entity : entities) {
  878. if (entity->RequiresCommitRequest() && !entity->RequiresCommitData()) {
  879. auto request = std::make_unique<CommitRequestData>();
  880. entity->InitializeCommitRequestData(request.get());
  881. commit_requests.push_back(std::move(request));
  882. if (commit_requests.size() >= max_entries) {
  883. break;
  884. }
  885. }
  886. }
  887. std::move(callback).Run(std::move(commit_requests));
  888. }
  889. size_t ClientTagBasedModelTypeProcessor::EstimateMemoryUsage() const {
  890. using base::trace_event::EstimateMemoryUsage;
  891. size_t memory_usage = 0;
  892. if (entity_tracker_) {
  893. memory_usage += entity_tracker_->EstimateMemoryUsage();
  894. }
  895. if (bridge_) {
  896. memory_usage += bridge_->EstimateSyncOverheadMemoryUsage();
  897. }
  898. return memory_usage;
  899. }
  900. bool ClientTagBasedModelTypeProcessor::HasLocalChangesForTest() const {
  901. return entity_tracker_ && entity_tracker_->HasLocalChanges();
  902. }
  903. bool ClientTagBasedModelTypeProcessor::IsTrackingEntityForTest(
  904. const std::string& storage_key) const {
  905. return entity_tracker_ &&
  906. entity_tracker_->GetEntityForStorageKey(storage_key) != nullptr;
  907. }
  908. bool ClientTagBasedModelTypeProcessor::IsModelReadyToSyncForTest() const {
  909. return model_ready_to_sync_;
  910. }
  911. void ClientTagBasedModelTypeProcessor::ExpireAllEntries(
  912. MetadataChangeList* metadata_changes) {
  913. DCHECK(metadata_changes);
  914. DCHECK(entity_tracker_);
  915. std::vector<std::string> storage_key_to_be_deleted;
  916. for (const ProcessorEntity* entity :
  917. entity_tracker_->GetAllEntitiesIncludingTombstones()) {
  918. if (!entity->IsUnsynced()) {
  919. storage_key_to_be_deleted.push_back(entity->storage_key());
  920. }
  921. }
  922. for (const std::string& key : storage_key_to_be_deleted) {
  923. RemoveEntity(key, metadata_changes);
  924. }
  925. }
  926. void ClientTagBasedModelTypeProcessor::RemoveEntity(
  927. const std::string& storage_key,
  928. MetadataChangeList* metadata_change_list) {
  929. DCHECK(!storage_key.empty());
  930. DCHECK(entity_tracker_);
  931. DCHECK(entity_tracker_->GetEntityForStorageKey(storage_key));
  932. metadata_change_list->ClearMetadata(storage_key);
  933. entity_tracker_->RemoveEntityForStorageKey(storage_key);
  934. }
  935. void ClientTagBasedModelTypeProcessor::ResetState(
  936. SyncStopMetadataFate metadata_fate) {
  937. // This should reset all mutable fields (except for |bridge_|).
  938. worker_.reset();
  939. switch (metadata_fate) {
  940. case KEEP_METADATA:
  941. break;
  942. case CLEAR_METADATA:
  943. entity_tracker_.reset();
  944. break;
  945. }
  946. // Do not let any delayed callbacks to be called.
  947. weak_ptr_factory_for_worker_.InvalidateWeakPtrs();
  948. }
  949. void ClientTagBasedModelTypeProcessor::GetAllNodesForDebugging(
  950. AllNodesCallback callback) {
  951. if (!bridge_)
  952. return;
  953. bridge_->GetAllDataForDebugging(base::BindOnce(
  954. &ClientTagBasedModelTypeProcessor::MergeDataWithMetadataForDebugging,
  955. weak_ptr_factory_for_worker_.GetWeakPtr(), std::move(callback)));
  956. }
  957. void ClientTagBasedModelTypeProcessor::MergeDataWithMetadataForDebugging(
  958. AllNodesCallback callback,
  959. std::unique_ptr<DataBatch> batch) {
  960. base::Value::List all_nodes;
  961. std::string type_string = ModelTypeToDebugString(type_);
  962. while (batch->HasNext()) {
  963. auto [storage_key, data] = batch->Next();
  964. // There is an overlap between EntityData fields from the bridge and
  965. // EntityMetadata fields from the processor's entity, metadata is
  966. // the authoritative source of truth.
  967. const ProcessorEntity* entity =
  968. entity_tracker_->GetEntityForStorageKey(storage_key);
  969. // |entity| could be null if there are some unapplied changes.
  970. if (entity != nullptr) {
  971. const sync_pb::EntityMetadata& metadata = entity->metadata();
  972. // Set id value as the legacy Directory implementation, "s" means server.
  973. data->id = "s" + metadata.server_id();
  974. data->creation_time = ProtoTimeToTime(metadata.creation_time());
  975. data->modification_time = ProtoTimeToTime(metadata.modification_time());
  976. data->client_tag_hash =
  977. ClientTagHash::FromHashed(metadata.client_tag_hash());
  978. }
  979. base::Value::Dict node = data->ToDictionaryValue();
  980. node.Set("modelType", type_string);
  981. // Copy the whole metadata message into the dictionary (if existing).
  982. if (entity != nullptr) {
  983. node.Set("metadata", base::Value::FromUniquePtrValue(
  984. EntityMetadataToValue(entity->metadata())));
  985. }
  986. all_nodes.Append(std::move(node));
  987. }
  988. // Create a permanent folder for this data type. Since sync server no longer
  989. // creates root folders, and USS won't migrate root folders from the
  990. // Directory, we create root folders for each data type here.
  991. base::Value::Dict rootnode;
  992. // Function isTypeRootNode in sync_node_browser.js use PARENT_ID and
  993. // UNIQUE_SERVER_TAG to check if the node is root node. isChildOf in
  994. // sync_node_browser.js uses modelType to check if root node is parent of real
  995. // data node. NON_UNIQUE_NAME will be the name of node to display.
  996. rootnode.Set("PARENT_ID", "r");
  997. rootnode.Set("UNIQUE_SERVER_TAG", type_string);
  998. rootnode.Set("IS_DIR", true);
  999. rootnode.Set("modelType", type_string);
  1000. rootnode.Set("NON_UNIQUE_NAME", type_string);
  1001. all_nodes.Append(std::move(rootnode));
  1002. std::move(callback).Run(type_, std::move(all_nodes));
  1003. }
  1004. bool ClientTagBasedModelTypeProcessor::CheckForInvalidPersistedMetadata(
  1005. const EntityMetadataMap& metadata_map) {
  1006. size_t count_of_duplicates = CountDuplicateClientTags(metadata_map);
  1007. if (count_of_duplicates == 0u)
  1008. return true;
  1009. // Metadata entities with duplicate client tag hashes most likely arise
  1010. // from metadata orphans; report their count to metrics.
  1011. for (size_t i = 0; i < count_of_duplicates; i++) {
  1012. UMA_HISTOGRAM_ENUMERATION("Sync.ModelTypeOrphanMetadata.ModelReadyToSync",
  1013. ModelTypeHistogramValue(type_));
  1014. }
  1015. ClearAllProvidedMetadataAndResetState(metadata_map);
  1016. // Not having `entity_tracker_` results in doing the initial sync again.
  1017. DCHECK(!entity_tracker_);
  1018. return false;
  1019. }
  1020. void ClientTagBasedModelTypeProcessor::
  1021. CheckForInvalidPersistedModelTypeState() {
  1022. if (!entity_tracker_) {
  1023. return;
  1024. }
  1025. const sync_pb::ModelTypeState& model_type_state =
  1026. entity_tracker_->model_type_state();
  1027. // Check for a mismatch in authenticated account id. The id can change after
  1028. // restart (and this does not mean the account has changed, this is checked
  1029. // later here by cache_guid mismatch). Easy to fix in place.
  1030. if (model_type_state.authenticated_account_id() !=
  1031. activation_request_.authenticated_account_id.ToString()) {
  1032. sync_pb::ModelTypeState update_model_type_state = model_type_state;
  1033. update_model_type_state.set_authenticated_account_id(
  1034. activation_request_.authenticated_account_id.ToString());
  1035. entity_tracker_->set_model_type_state(update_model_type_state);
  1036. }
  1037. // Check for deeper issues where we need to restart sync for this type.
  1038. const bool valid_cache_guid =
  1039. model_type_state.cache_guid() == activation_request_.cache_guid;
  1040. // Check for a mismatch between the cache guid or the data type id stored
  1041. // in |model_type_state_| and the one received from sync. A mismatch indicates
  1042. // that the stored metadata are invalid (e.g. has been manipulated) and
  1043. // don't belong to the current syncing client.
  1044. const bool valid_data_type_id =
  1045. model_type_state.progress_marker().data_type_id() ==
  1046. GetSpecificsFieldNumberFromModelType(type_);
  1047. if (valid_cache_guid && valid_data_type_id) {
  1048. return;
  1049. }
  1050. ClearAllTrackedMetadataAndResetState();
  1051. // Not having `entity_tracker_` results in doing the initial sync again.
  1052. DCHECK(!entity_tracker_);
  1053. }
  1054. void ClientTagBasedModelTypeProcessor::GetTypeEntitiesCountForDebugging(
  1055. base::OnceCallback<void(const TypeEntitiesCount&)> callback) const {
  1056. TypeEntitiesCount count(type_);
  1057. if (entity_tracker_) {
  1058. count.entities = entity_tracker_->size();
  1059. count.non_tombstone_entities = entity_tracker_->CountNonTombstoneEntries();
  1060. }
  1061. std::move(callback).Run(count);
  1062. }
  1063. void ClientTagBasedModelTypeProcessor::RecordMemoryUsageAndCountsHistograms() {
  1064. SyncRecordModelTypeMemoryHistogram(type_, EstimateMemoryUsage());
  1065. const size_t non_tombstone_entries_count =
  1066. entity_tracker_ == nullptr ? 0
  1067. : entity_tracker_->CountNonTombstoneEntries();
  1068. SyncRecordModelTypeCountHistogram(type_, non_tombstone_entries_count);
  1069. }
  1070. const sync_pb::EntitySpecifics&
  1071. ClientTagBasedModelTypeProcessor::GetPossiblyTrimmedRemoteSpecifics(
  1072. const std::string& storage_key) const {
  1073. DCHECK(entity_tracker_);
  1074. DCHECK(!storage_key.empty());
  1075. if (!base::FeatureList::IsEnabled(
  1076. syncer::kCacheBaseEntitySpecificsInMetadata)) {
  1077. return sync_pb::EntitySpecifics::default_instance();
  1078. }
  1079. ProcessorEntity* entity =
  1080. entity_tracker_->GetEntityForStorageKey(storage_key);
  1081. if (entity == nullptr) {
  1082. return sync_pb::EntitySpecifics::default_instance();
  1083. }
  1084. return entity->metadata().possibly_trimmed_base_specifics();
  1085. }
  1086. } // namespace syncer