1 // Copyright 2018 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 
5 #ifndef COMPONENTS_CONSENT_AUDITOR_CONSENT_SYNC_BRIDGE_IMPL_H_
6 #define COMPONENTS_CONSENT_AUDITOR_CONSENT_SYNC_BRIDGE_IMPL_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 #include "base/macros.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/optional.h"
17 #include "components/consent_auditor/consent_sync_bridge.h"
18 #include "components/sync/model/model_type_change_processor.h"
19 #include "components/sync/model/model_type_store.h"
20 #include "components/sync/model/model_type_sync_bridge.h"
21 
22 namespace consent_auditor {
23 
24 class ConsentSyncBridgeImpl : public ConsentSyncBridge,
25                               public syncer::ModelTypeSyncBridge {
26  public:
27   ConsentSyncBridgeImpl(
28       syncer::OnceModelTypeStoreFactory store_factory,
29       std::unique_ptr<syncer::ModelTypeChangeProcessor> change_processor);
30   ~ConsentSyncBridgeImpl() override;
31 
32   // ModelTypeSyncBridge implementation.
33   std::unique_ptr<syncer::MetadataChangeList> CreateMetadataChangeList()
34       override;
35   base::Optional<syncer::ModelError> MergeSyncData(
36       std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
37       syncer::EntityChangeList entity_data) override;
38   base::Optional<syncer::ModelError> ApplySyncChanges(
39       std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
40       syncer::EntityChangeList entity_changes) override;
41   void GetData(StorageKeyList storage_keys, DataCallback callback) override;
42   void GetAllDataForDebugging(DataCallback callback) override;
43   std::string GetClientTag(const syncer::EntityData& entity_data) override;
44   std::string GetStorageKey(const syncer::EntityData& entity_data) override;
45   void ApplyStopSyncChanges(std::unique_ptr<syncer::MetadataChangeList>
46                                 delete_metadata_change_list) override;
47 
48   // ConsentSyncBridge implementation.
49   void RecordConsent(
50       std::unique_ptr<sync_pb::UserConsentSpecifics> specifics) override;
51   base::WeakPtr<syncer::ModelTypeControllerDelegate> GetControllerDelegate()
52       override;
53 
54   static std::string GetStorageKeyFromSpecificsForTest(
55       const sync_pb::UserConsentSpecifics& specifics);
56   std::unique_ptr<syncer::ModelTypeStore> StealStoreForTest();
57 
58  private:
59   void RecordConsentImpl(
60       std::unique_ptr<sync_pb::UserConsentSpecifics> specifics);
61   // Record events in the deferred queue and clear the queue.
62   void ProcessQueuedEvents();
63 
64   void OnStoreCreated(const base::Optional<syncer::ModelError>& error,
65                       std::unique_ptr<syncer::ModelTypeStore> store);
66   void OnReadAllMetadata(const base::Optional<syncer::ModelError>& error,
67                          std::unique_ptr<syncer::MetadataBatch> metadata_batch);
68   void OnCommit(const base::Optional<syncer::ModelError>& error);
69   void OnReadData(
70       DataCallback callback,
71       const base::Optional<syncer::ModelError>& error,
72       std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records,
73       std::unique_ptr<syncer::ModelTypeStore::IdList> missing_id_list);
74   void OnReadAllData(
75       DataCallback callback,
76       const base::Optional<syncer::ModelError>& error,
77       std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records);
78 
79   // Resubmit all the consents persisted in the store to sync consents, which
80   // were preserved when sync was disabled. This may resubmit entities that the
81   // processor already knows about (i.e. with metadata), but it is allowed.
82   void ReadAllDataAndResubmit();
83   void OnReadAllDataToResubmit(
84       const base::Optional<syncer::ModelError>& error,
85       std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records);
86 
87   // Persistent storage for in flight consents. Should remain quite small, as we
88   // delete upon commit confirmation. May contain consents without metadata
89   // (e.g. persisted when sync was disabled).
90   std::unique_ptr<syncer::ModelTypeStore> store_;
91 
92   // Used to store consents while the store or change processor are not
93   // ready.
94   std::vector<std::unique_ptr<sync_pb::UserConsentSpecifics>>
95       deferred_consents_while_initializing_;
96 
97   base::WeakPtrFactory<ConsentSyncBridgeImpl> weak_ptr_factory_{this};
98 
99   DISALLOW_COPY_AND_ASSIGN(ConsentSyncBridgeImpl);
100 };
101 
102 }  // namespace consent_auditor
103 
104 #endif  // COMPONENTS_CONSENT_AUDITOR_CONSENT_SYNC_BRIDGE_IMPL_H_
105