File: syncable_settings_storage.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (138 lines) | stat: -rw-r--r-- 5,253 bytes parent folder | download | duplicates (4)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNCABLE_SETTINGS_STORAGE_H_
#define CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNCABLE_SETTINGS_STORAGE_H_

#include <stddef.h>

#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "base/values.h"
#include "chrome/browser/extensions/api/storage/setting_sync_data.h"
#include "components/sync/model/syncable_service.h"
#include "components/value_store/value_store.h"
#include "extensions/browser/api/storage/settings_observer.h"
#include "extensions/common/extension_id.h"

namespace syncer {
class ModelError;
}  // namespace syncer

namespace extensions {

class SettingsSyncProcessor;

// Decorates a ValueStore with sync behaviour.
class SyncableSettingsStorage : public value_store::ValueStore {
 public:
  SyncableSettingsStorage(SequenceBoundSettingsChangedCallback observer,
                          const ExtensionId& extension_id,
                          // Ownership taken.
                          value_store::ValueStore* delegate,
                          syncer::DataType sync_type,
                          const syncer::SyncableService::StartSyncFlare& flare);

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

  ~SyncableSettingsStorage() override;

  // ValueStore implementation.
  size_t GetBytesInUse(const std::string& key) override;
  size_t GetBytesInUse(const std::vector<std::string>& keys) override;
  size_t GetBytesInUse() override;
  ReadResult GetKeys() override;
  ReadResult Get(const std::string& key) override;
  ReadResult Get(const std::vector<std::string>& keys) override;
  ReadResult Get() override;
  WriteResult Set(WriteOptions options,
                  const std::string& key,
                  const base::Value& value) override;
  WriteResult Set(WriteOptions options,
                  const base::Value::Dict& values) override;
  WriteResult Remove(const std::string& key) override;
  WriteResult Remove(const std::vector<std::string>& keys) override;
  WriteResult Clear() override;

  // Sync-related methods, analogous to those on SyncableService (handled by
  // ExtensionSettings), but with looser guarantees about when the methods
  // can be called.

  // Starts syncing this storage area. Must only be called if sync isn't
  // already active.
  // `sync_state` is the current state of the extension settings in sync.
  // `sync_processor` is used to write out any changes.
  // Returns any error when trying to sync, or std::nullopt on success.
  std::optional<syncer::ModelError> StartSyncing(
      base::Value::Dict sync_state,
      std::unique_ptr<SettingsSyncProcessor> sync_processor);

  // Stops syncing this storage area. May be called at any time (idempotent).
  void StopSyncing();

  // Pushes a list of sync changes into this storage area. May be called at any
  // time, changes will be ignored if sync isn't active.
  // Returns any error when trying to sync, or std::nullopt on success.
  std::optional<syncer::ModelError> ProcessSyncChanges(
      std::unique_ptr<SettingSyncDataList> sync_changes);

 private:
  // Sends the changes from `result` to sync if it's enabled.
  void SyncResultIfEnabled(const value_store::ValueStore::WriteResult& result);

  // Analyze the result returned by a call to the delegate, and take appropriate
  // measures.
  template <class T>
  T HandleResult(T result);

  // Sends all local settings to sync. This assumes that there are no settings
  // in sync yet.
  // Returns any error when trying to sync, or std::nullopt on success.
  std::optional<syncer::ModelError> SendLocalSettingsToSync(
      base::Value::Dict local_state);

  // Overwrites local state with sync state.
  // Returns any error when trying to sync, or std::nullopt on success.
  std::optional<syncer::ModelError> OverwriteLocalSettingsWithSync(
      base::Value::Dict sync_state,
      base::Value::Dict local_state);

  // Called when an Add/Update/Remove comes from sync.
  std::optional<syncer::ModelError> OnSyncAdd(
      const std::string& key,
      base::Value new_value,
      value_store::ValueStoreChangeList* changes);
  std::optional<syncer::ModelError> OnSyncUpdate(
      const std::string& key,
      base::Value old_value,
      base::Value new_value,
      value_store::ValueStoreChangeList* changes);
  std::optional<syncer::ModelError> OnSyncDelete(
      const std::string& key,
      base::Value old_value,
      value_store::ValueStoreChangeList* changes);

  // Observer to settings changes.
  SequenceBoundSettingsChangedCallback observer_;

  // Id of the extension these settings are for.
  ExtensionId const extension_id_;

  // Storage area to sync.
  const std::unique_ptr<value_store::ValueStore> delegate_;

  // Object which sends changes to sync.
  std::unique_ptr<SettingsSyncProcessor> sync_processor_;

  const syncer::DataType sync_type_;
  const syncer::SyncableService::StartSyncFlare flare_;
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_STORAGE_SYNCABLE_SETTINGS_STORAGE_H_