File: power_bookmark_sync_bridge.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (125 lines) | stat: -rw-r--r-- 4,898 bytes parent folder | download | duplicates (5)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_POWER_BOOKMARKS_STORAGE_POWER_BOOKMARK_SYNC_BRIDGE_H_
#define COMPONENTS_POWER_BOOKMARKS_STORAGE_POWER_BOOKMARK_SYNC_BRIDGE_H_

#include "base/uuid.h"
#include "components/sync/model/data_type_sync_bridge.h"

namespace syncer {
class ModelError;
}  // namespace syncer

namespace power_bookmarks {

class Power;
class PowerBookmarkSyncMetadataDatabase;

// Transaction wraps a database transaction. When it's out of scope the
// underlying transaction will be cancelled if not committed.
// TODO(crbug.com/40247772): Find a better layout for this class.
class Transaction {
 public:
  virtual bool Commit() = 0;
  virtual ~Transaction() = default;
};

// PowerBookmarkSyncBridge is responsible for syncing all powers to different
// devices. It runs on the same thread as the power bookmark database
// implementation.
class PowerBookmarkSyncBridge : public syncer::DataTypeSyncBridge {
 public:
  // Delegate interface PowerBookmarkSyncBridge needs from the backend.
  class Delegate {
   public:
    // Get all the powers from the database.
    virtual std::vector<std::unique_ptr<Power>> GetAllPowers() = 0;

    // Get powers for the given guids.
    virtual std::vector<std::unique_ptr<Power>> GetPowersForGUIDs(
        const std::vector<std::string>& guids) = 0;

    // Get power for the given guid.
    virtual std::unique_ptr<Power> GetPowerForGUID(const std::string& guid) = 0;

    // Create a power if not exists or merge existing the power in the database.
    virtual bool CreateOrMergePowerFromSync(const Power& power) = 0;

    // Delete a power from the database.
    virtual bool DeletePowerFromSync(const std::string& guid) = 0;

    // Get the database to store power bookmarks metadata.
    virtual PowerBookmarkSyncMetadataDatabase* GetSyncMetadataDatabase() = 0;

    // Start a transaction. This is used to make sure power bookmark data
    // and metadata are stored atomically.
    virtual std::unique_ptr<Transaction> BeginTransaction() = 0;

    // Notify the backend if powers are changed.
    virtual void NotifyPowersChanged() {}
  };

  PowerBookmarkSyncBridge(
      PowerBookmarkSyncMetadataDatabase* meta_db,
      Delegate* delegate,
      std::unique_ptr<syncer::DataTypeLocalChangeProcessor> change_processor);

  PowerBookmarkSyncBridge(const PowerBookmarkSyncBridge&) = delete;
  PowerBookmarkSyncBridge& operator=(const PowerBookmarkSyncBridge&) = delete;

  ~PowerBookmarkSyncBridge() override;

  void Init();

  // syncer::DataTypeSyncBridge:
  std::unique_ptr<syncer::MetadataChangeList> CreateMetadataChangeList()
      override;
  std::optional<syncer::ModelError> MergeFullSyncData(
      std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
      syncer::EntityChangeList entity_changes) override;
  std::optional<syncer::ModelError> ApplyIncrementalSyncChanges(
      std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
      syncer::EntityChangeList entity_changes) override;
  std::string GetStorageKey(
      const syncer::EntityData& entity_data) const override;
  std::string GetClientTag(
      const syncer::EntityData& entity_data) const override;
  bool IsEntityDataValid(const syncer::EntityData& entity_data) const override;
  std::unique_ptr<syncer::DataBatch> GetDataForCommit(
      StorageKeyList storage_keys) override;
  std::unique_ptr<syncer::DataBatch> GetAllDataForDebugging() override;

  void SendPowerToSync(const Power& power);
  void NotifySyncForDeletion(const std::string& guid);

  void ReportError(const syncer::ModelError& error);
  bool initialized() { return initialized_; }

 private:
  // Create a change list to store metadata inside the power bookmark database.
  // This method should be called inside a transaction because Chrome sync
  // requires saving data and metadata atomically. Also need to transfer the
  // meta_data_change_list from the InMemoryMetadataChangeList created by
  // CreateMetadataChangeList() within the transaction created in
  // MergeFullSyncData() and ApplyIncrementalSyncChanges().
  std::unique_ptr<syncer::MetadataChangeList>
  CreateMetadataChangeListInTransaction();

  // Helper function called by both `MergeFullSyncData` with
  // is_initial_merge=true and `ApplyIncrementalSyncChanges` with
  // is_initial_merge=false.
  std::optional<syncer::ModelError> ApplyChanges(
      std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
      syncer::EntityChangeList& entity_changes,
      bool is_initial_merge);

  const raw_ptr<PowerBookmarkSyncMetadataDatabase, DanglingUntriaged> meta_db_;
  const raw_ptr<Delegate> delegate_;
  bool initialized_ = false;
};

}  // namespace power_bookmarks

#endif  // COMPONENTS_POWER_BOOKMARKS_STORAGE_POWER_BOOKMARK_SYNC_BRIDGE_H_