File: wrap_with_prefix_pref_store.cc

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 (174 lines) | stat: -rw-r--r-- 6,193 bytes parent folder | download | duplicates (9)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// 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.

#include "components/prefs/wrap_with_prefix_pref_store.h"

#include <string>
#include <string_view>

#include "base/strings/strcat.h"
#include "base/strings/string_util.h"

WrapWithPrefixPrefStore::WrapWithPrefixPrefStore(
    scoped_refptr<PersistentPrefStore> target_pref_store,
    std::string_view path_prefix)
    : target_pref_store_(std::move(target_pref_store)),
      dotted_prefix_(base::StrCat({path_prefix, "."})) {
  target_pref_store_->AddObserver(this);
}

WrapWithPrefixPrefStore::~WrapWithPrefixPrefStore() {
  target_pref_store_->RemoveObserver(this);
}

bool WrapWithPrefixPrefStore::GetValue(std::string_view key,
                                       const base::Value** value) const {
  return target_pref_store_->GetValue(AddDottedPrefix(key), value);
}

base::Value::Dict WrapWithPrefixPrefStore::GetValues() const {
  base::Value::Dict values = target_pref_store_->GetValues();
  std::string_view prefix(dotted_prefix_.c_str(), dotted_prefix_.size() - 1);
  if (base::Value::Dict* values_with_prefix =
          values.FindDictByDottedPath(prefix)) {
    return std::move(*values_with_prefix);
  }
  return {};
}

bool WrapWithPrefixPrefStore::GetMutableValue(std::string_view key,
                                              base::Value** value) {
  return target_pref_store_->GetMutableValue(AddDottedPrefix(key), value);
}

void WrapWithPrefixPrefStore::AddObserver(PrefStore::Observer* observer) {
  observers_.AddObserver(observer);
}

void WrapWithPrefixPrefStore::RemoveObserver(PrefStore::Observer* observer) {
  observers_.RemoveObserver(observer);
}

bool WrapWithPrefixPrefStore::HasObservers() const {
  return !observers_.empty();
}

bool WrapWithPrefixPrefStore::IsInitializationComplete() const {
  return target_pref_store_->IsInitializationComplete();
}

void WrapWithPrefixPrefStore::SetValue(std::string_view key,
                                       base::Value value,
                                       uint32_t flags) {
  target_pref_store_->SetValue(AddDottedPrefix(key), std::move(value), flags);
}

void WrapWithPrefixPrefStore::SetValueSilently(std::string_view key,
                                               base::Value value,
                                               uint32_t flags) {
  target_pref_store_->SetValueSilently(AddDottedPrefix(key), std::move(value),
                                       flags);
}

void WrapWithPrefixPrefStore::RemoveValue(std::string_view key,
                                          uint32_t flags) {
  target_pref_store_->RemoveValue(AddDottedPrefix(key), flags);
}

void WrapWithPrefixPrefStore::RemoveValuesByPrefixSilently(
    std::string_view prefix) {
  target_pref_store_->RemoveValuesByPrefixSilently(AddDottedPrefix(prefix));
}

bool WrapWithPrefixPrefStore::ReadOnly() const {
  return target_pref_store_->ReadOnly();
}

PersistentPrefStore::PrefReadError WrapWithPrefixPrefStore::GetReadError()
    const {
  return target_pref_store_->GetReadError();
}

PersistentPrefStore::PrefReadError WrapWithPrefixPrefStore::ReadPrefs() {
  // The target pref store should have been initialized prior to calling
  // ReadPrefs() on this store.
  CHECK(target_pref_store_->IsInitializationComplete() ||
        // To catch case where target pref store initialization failed.
        target_pref_store_->GetReadError() !=
            PersistentPrefStore::PREF_READ_ERROR_NONE);
  return target_pref_store_->GetReadError();
}

void WrapWithPrefixPrefStore::ReadPrefsAsync(
    ReadErrorDelegate* error_delegate) {
  // The target pref store should either have been initialized or should have an
  // ongoing read.
  CHECK(IsInitializationComplete() ||
        // To catch case where target pref store initialization failed.
        GetReadError() != PersistentPrefStore::PREF_READ_ERROR_NONE ||
        // ReadPrefsAsync() was called but it's still ongoing.
        target_pref_store_->HasReadErrorDelegate());
  read_error_delegate_.emplace(error_delegate);
  if (PersistentPrefStore::PrefReadError read_error = GetReadError();
      read_error != PersistentPrefStore::PREF_READ_ERROR_NONE &&
      error_delegate) {
    error_delegate->OnError(read_error);
  }
}

void WrapWithPrefixPrefStore::SchedulePendingLossyWrites() {
  // This store is only a wrapper and relies on the target pref store being
  // independently notified of this.
}

void WrapWithPrefixPrefStore::OnStoreDeletionFromDisk() {
  // This store is only a wrapper and relies on the target pref store being
  // independently notified of this.
}

void WrapWithPrefixPrefStore::ReportValueChanged(std::string_view key,
                                                 uint32_t flags) {
  return target_pref_store_->ReportValueChanged(AddDottedPrefix(key), flags);
}

void WrapWithPrefixPrefStore::OnPrefValueChanged(std::string_view key) {
  if (!HasDottedPrefix(key)) {
    return;
  }
  std::string_view original_key(RemoveDottedPrefix(key));
  for (PrefStore::Observer& observer : observers_) {
    observer.OnPrefValueChanged(original_key);
  }
}

void WrapWithPrefixPrefStore::OnInitializationCompleted(bool succeeded) {
  if (PersistentPrefStore::PrefReadError read_error = GetReadError();
      read_error != PersistentPrefStore::PREF_READ_ERROR_NONE &&
      read_error_delegate_.has_value() && read_error_delegate_.value()) {
    read_error_delegate_.value()->OnError(read_error);
  }
  for (PrefStore::Observer& observer : observers_) {
    observer.OnInitializationCompleted(succeeded);
  }
}

std::string WrapWithPrefixPrefStore::AddDottedPrefix(
    std::string_view path) const {
  return base::StrCat({dotted_prefix_, path});
}

std::string_view WrapWithPrefixPrefStore::RemoveDottedPrefix(
    std::string_view path) const {
  CHECK(HasDottedPrefix(path));
  path.remove_prefix(dotted_prefix_.size());
  return path;
}

bool WrapWithPrefixPrefStore::HasDottedPrefix(std::string_view path) const {
  return base::StartsWith(path, dotted_prefix_);
}

bool WrapWithPrefixPrefStore::HasReadErrorDelegate() const {
  return read_error_delegate_.has_value();
}