Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(433)

Side by Side Diff: services/preferences/public/cpp/persistent_pref_store_client.cc

Issue 2743563003: Pref service: add persistent pref store frontend and backend. (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 #include "services/preferences/public/cpp/persistent_pref_store_client.h"
6
7 #include <utility>
8
9 #include "base/values.h"
10
11 namespace prefs {
12
13 PersistentPrefStoreClient::PersistentPrefStoreClient(
14 mojom::PersistentPrefStoreConnectorPtr connector)
15 : connector_(std::move(connector)) {}
16
17 void PersistentPrefStoreClient::SetValue(const std::string& key,
18 std::unique_ptr<base::Value> value,
19 uint32_t flags) {
20 base::Value* old_value = nullptr;
21 GetMutableValues().Get(key, &old_value);
22 if (!old_value || !value->Equals(old_value)) {
23 GetMutableValues().Set(key, std::move(value));
24 ReportValueChanged(key, flags);
25 }
26 }
27
28 void PersistentPrefStoreClient::RemoveValue(const std::string& key,
29 uint32_t flags) {
30 if (GetMutableValues().RemovePath(key, nullptr))
31 ReportValueChanged(key, flags);
32 }
33
34 bool PersistentPrefStoreClient::GetMutableValue(const std::string& key,
35 base::Value** result) {
36 return GetMutableValues().Get(key, result);
37 }
38
39 void PersistentPrefStoreClient::ReportValueChanged(const std::string& key,
40 uint32_t flags) {
41 DCHECK(pref_store_);
42 const base::Value* local_value = nullptr;
43 GetMutableValues().Get(key, &local_value);
44 pref_store_->SetValue(
45 key, local_value ? local_value->CreateDeepCopy() : nullptr, flags);
46 ReportPrefValueChanged(key);
47 }
48
49 void PersistentPrefStoreClient::SetValueSilently(
50 const std::string& key,
51 std::unique_ptr<base::Value> value,
52 uint32_t flags) {
53 DCHECK(pref_store_);
54 pref_store_->SetValue(key, value->CreateDeepCopy(), flags);
55 GetMutableValues().Set(key, std::move(value));
56 }
57
58 bool PersistentPrefStoreClient::ReadOnly() const {
59 return read_only_;
60 }
61
62 PersistentPrefStore::PrefReadError PersistentPrefStoreClient::GetReadError()
63 const {
64 return read_error_;
65 }
66
67 PersistentPrefStore::PrefReadError PersistentPrefStoreClient::ReadPrefs() {
68 PrefReadError read_error = PrefReadError::PREF_READ_ERROR_NONE;
69 bool read_only = false;
70 std::unique_ptr<base::DictionaryValue> local_prefs;
71 mojom::PersistentPrefStorePtr pref_store;
72 mojom::PrefStoreObserverRequest observer_request;
73 if (!connector_->Connect(&read_error, &read_only, &local_prefs, &pref_store,
74 &observer_request)) {
75 NOTREACHED();
76 }
77
78 OnCreateComplete(read_error, read_only, std::move(local_prefs),
79 std::move(pref_store), std::move(observer_request));
80 return read_error_;
81 }
82
83 void PersistentPrefStoreClient::ReadPrefsAsync(
84 ReadErrorDelegate* error_delegate) {
85 error_delegate_.reset(error_delegate);
86 connector_->Connect(base::Bind(&PersistentPrefStoreClient::OnCreateComplete,
87 base::Unretained(this)));
88 }
89
90 void PersistentPrefStoreClient::CommitPendingWrite() {
91 DCHECK(pref_store_);
92 pref_store_->CommitPendingWrite();
93 }
94
95 void PersistentPrefStoreClient::SchedulePendingLossyWrites() {
96 DCHECK(pref_store_);
97 return pref_store_->SchedulePendingLossyWrites();
98 }
99
100 void PersistentPrefStoreClient::ClearMutableValues() {
101 DCHECK(pref_store_);
102 return pref_store_->ClearMutableValues();
103 }
104
105 PersistentPrefStoreClient::~PersistentPrefStoreClient() {
106 if (!pref_store_)
107 return;
108
109 pref_store_->CommitPendingWrite();
110 }
111
112 void PersistentPrefStoreClient::OnCreateComplete(
113 PrefReadError read_error,
114 bool read_only,
115 std::unique_ptr<base::DictionaryValue> cached_prefs,
116 mojom::PersistentPrefStorePtr pref_store,
117 mojom::PrefStoreObserverRequest observer_request) {
118 connector_.reset();
119 read_error_ = read_error;
120 read_only_ = read_only;
121 pref_store_ = std::move(pref_store);
122 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE)
123 error_delegate_->OnError(read_error_);
124 error_delegate_.reset();
125
126 Init(std::move(cached_prefs), true, std::move(observer_request));
127 }
128
129 } // namespace prefs
OLDNEW
« no previous file with comments | « services/preferences/public/cpp/persistent_pref_store_client.h ('k') | services/preferences/public/cpp/pref_store_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698