File: group_data_store.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 (100 lines) | stat: -rw-r--r-- 3,696 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
// 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.

#ifndef COMPONENTS_DATA_SHARING_INTERNAL_GROUP_DATA_STORE_H_
#define COMPONENTS_DATA_SHARING_INTERNAL_GROUP_DATA_STORE_H_

#include <optional>
#include <string>
#include <unordered_map>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "base/types/strong_alias.h"
#include "components/data_sharing/internal/protocol/group_data_db.pb.h"
#include "components/data_sharing/public/group_data.h"
#include "components/data_sharing/public/protocol/group_data.pb.h"
#include "components/sqlite_proto/key_value_data.h"
#include "components/sqlite_proto/key_value_table.h"
#include "components/sqlite_proto/proto_table_manager.h"
#include "sql/database.h"

namespace data_sharing_pb {
class GroupData;
}  // namespace data_sharing_pb

namespace data_sharing {

// Opaque (at this level) token that represents the version of the GroupData.
using VersionToken = base::StrongAlias<class VersionTokenTag, std::string>;

// In-memory cache and persistent storage for GroupData.
class GroupDataStore {
 public:
  enum class DBInitStatus {
    kSuccess,
    kFailure,
    kNotLoaded,
  };

  using DBLoadedCallback = base::OnceCallback<void(DBInitStatus)>;

  // Public methods must not be called until `db_loaded_callback` is invoked
  // with kSuccess.
  GroupDataStore(const base::FilePath& db_dir_path,
                 DBLoadedCallback db_loaded_callback);

  GroupDataStore(const GroupDataStore& other) = delete;
  GroupDataStore& operator=(const GroupDataStore& other) = delete;

  GroupDataStore(GroupDataStore&& other) = delete;
  GroupDataStore& operator=(GroupDataStore&& other) = delete;

  ~GroupDataStore();

  void StoreGroupData(const VersionToken& version_token,
                      const base::Time& last_updated_timestamp,
                      const data_sharing_pb::GroupData& group_data_proto);
  void DeleteGroups(const std::vector<GroupId>& groups_ids);

  std::optional<VersionToken> GetGroupVersionToken(
      const GroupId& group_id) const;
  base::Time GetGroupLastUpdatedTimestamp(const GroupId& group_id) const;
  std::optional<GroupData> GetGroupData(const GroupId& group_id) const;
  std::vector<GroupId> GetAllGroupIds() const;

  // Allows test to wait until DB shutdown tasks are complete.
  void SetShutdownCallbackForTesting(base::OnceClosure shutdown_callback);

 private:
  void OnDBReady(DBLoadedCallback db_loaded_callback, DBInitStatus init_status);

  // The following fields hold objects to work with SQLite database. `db_`,
  // `proto_table_manager_` are deleted on db sequence; `group_entity_data_` and
  // `group_entity_table_` are deleted on the main thread, however only after
  // deletion of the rest.
  scoped_refptr<base::SequencedTaskRunner> db_task_runner_;
  std::unique_ptr<sql::Database> db_;
  scoped_refptr<sqlite_proto::ProtoTableManager> proto_table_manager_;
  // Entities are keyed by GroupID.
  std::unique_ptr<sqlite_proto::KeyValueTable<data_sharing_pb::GroupEntity>>
      group_entity_table_;
  std::unique_ptr<sqlite_proto::KeyValueData<data_sharing_pb::GroupEntity>>
      group_entity_data_;

  DBInitStatus db_init_status_ = DBInitStatus::kNotLoaded;

  // Used only for tests to notify that shutdown tasks are completed on the DB
  // sequence.
  base::OnceClosure shutdown_callback_;

  base::WeakPtrFactory<GroupDataStore> weak_ptr_factory_{this};
};

}  // namespace data_sharing

#endif  // COMPONENTS_DATA_SHARING_INTERNAL_GROUP_DATA_STORE_H_