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

Side by Side Diff: chrome/browser/prefs/tracked/protected_pref_store.cc

Issue 205813002: Separate storage for protected preferences into Protected Preferences file. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@pp4_profile_pref_store
Patch Set: Pull out some changes into other CLs. Created 6 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 (c) 2014 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 "chrome/browser/prefs/tracked/protected_pref_store.h"
6
7 #include "base/memory/ref_counted.h"
8 #include "base/values.h"
9 #include "chrome/browser/prefs/pref_hash_store.h"
10 #include "chrome/browser/prefs/pref_hash_store_transaction.h"
11
12 ProtectedPrefStore::TeeObserver::TeeObserver(ProtectedPrefStore* outer)
13 : outer_(outer),
14 failed_sub_initializations_(0),
15 successful_sub_initializations_(0) {}
16
17 void ProtectedPrefStore::TeeObserver::OnPrefValueChanged(
18 const std::string& key) {
19 // There is no need to tell clients about changes if they have not yet been
20 // told about initialization.
21 if (failed_sub_initializations_ + successful_sub_initializations_ == 2) {
Bernhard Bauer 2014/03/26 15:05:50 You could negate the condition and early-return. T
erikwright (departed) 2014/03/26 21:08:12 Done.
22 FOR_EACH_OBSERVER(
23 PrefStore::Observer, outer_->observers_, OnPrefValueChanged(key));
24 }
25 }
26
27 void ProtectedPrefStore::TeeObserver::OnInitializationCompleted(
28 bool succeeded) {
29 if (succeeded)
30 ++successful_sub_initializations_;
31 else
32 ++failed_sub_initializations_;
33
34 DCHECK_LE(failed_sub_initializations_ + successful_sub_initializations_, 2);
35
36 if (failed_sub_initializations_ + successful_sub_initializations_ == 2) {
37
38 if (!outer_->on_initialization_.is_null())
39 outer_->on_initialization_.Run();
40
41 if (successful_sub_initializations_ == 2 && outer_->read_error_delegate_) {
42 PersistentPrefStore::PrefReadError read_error = outer_->GetReadError();
43 if (read_error != PersistentPrefStore::PREF_READ_ERROR_NONE)
44 outer_->read_error_delegate_->OnError(read_error);
45 }
46
47 FOR_EACH_OBSERVER(
48 PrefStore::Observer,
49 outer_->observers_,
50 OnInitializationCompleted(successful_sub_initializations_ == 2));
51 }
52 }
53
54 ProtectedPrefStore::ProtectedPrefStore(
55 const scoped_refptr<PersistentPrefStore>& unprotected_pref_store,
56 const scoped_refptr<PersistentPrefStore>& protected_pref_store,
57 const std::set<std::string>& protected_pref_names,
58 const base::Closure& on_initialization)
59 : unprotected_pref_store_(unprotected_pref_store),
60 protected_pref_store_(protected_pref_store),
61 protected_preference_names_(protected_pref_names),
62 on_initialization_(on_initialization),
63 tee_observer_(this) {
64
65 unprotected_pref_store_->AddObserver(&tee_observer_);
66 protected_pref_store_->AddObserver(&tee_observer_);
67 }
68
69 void ProtectedPrefStore::AddObserver(Observer* observer) {
70 observers_.AddObserver(observer);
71 }
72
73 void ProtectedPrefStore::RemoveObserver(Observer* observer) {
74 observers_.RemoveObserver(observer);
75 }
76
77 bool ProtectedPrefStore::HasObservers() const {
78 return observers_.might_have_observers();
79 }
80
81 bool ProtectedPrefStore::IsInitializationComplete() const {
82 return unprotected_pref_store_->IsInitializationComplete() &&
83 protected_pref_store_->IsInitializationComplete();
84 }
85
86 bool ProtectedPrefStore::GetValue(const std::string& key,
87 const base::Value** result) const {
88 return StoreForKey(key)->GetValue(key, result);
89 }
90
91 void ProtectedPrefStore::SetValue(const std::string& key, base::Value* value) {
92 StoreForKey(key)->SetValue(key, value);
93 }
94
95 void ProtectedPrefStore::RemoveValue(const std::string& key) {
96 StoreForKey(key)->RemoveValue(key);
97 }
98
99 bool ProtectedPrefStore::GetMutableValue(const std::string& key,
100 base::Value** result) {
101 return StoreForKey(key)->GetMutableValue(key, result);
102 }
103
104 void ProtectedPrefStore::ReportValueChanged(const std::string& key) {
105 StoreForKey(key)->ReportValueChanged(key);
106 }
107
108 void ProtectedPrefStore::SetValueSilently(const std::string& key,
109 base::Value* value) {
110 StoreForKey(key)->SetValueSilently(key, value);
111 }
112
113 bool ProtectedPrefStore::ReadOnly() const {
114 return protected_pref_store_->ReadOnly() ||
115 unprotected_pref_store_->ReadOnly();
116 }
117
118 PersistentPrefStore::PrefReadError ProtectedPrefStore::GetReadError() const {
119 PersistentPrefStore::PrefReadError read_error =
120 unprotected_pref_store_->GetReadError();
121 return read_error != PersistentPrefStore::PREF_READ_ERROR_NONE
122 ? read_error
123 : protected_pref_store_->GetReadError();
124 }
125
126 PersistentPrefStore::PrefReadError ProtectedPrefStore::ReadPrefs() {
127 PersistentPrefStore::PrefReadError unprotected_read_error =
128 unprotected_pref_store_->ReadPrefs();
129 PersistentPrefStore::PrefReadError protected_read_error =
130 protected_pref_store_->ReadPrefs();
131
132 return unprotected_read_error != PersistentPrefStore::PREF_READ_ERROR_NONE
133 ? unprotected_read_error
134 : protected_read_error;
135 }
136
137 void ProtectedPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) {
138 read_error_delegate_.reset(error_delegate);
139 unprotected_pref_store_->ReadPrefsAsync(NULL);
140 protected_pref_store_->ReadPrefsAsync(NULL);
141 }
142
143 void ProtectedPrefStore::CommitPendingWrite() {
144 unprotected_pref_store_->CommitPendingWrite();
145 protected_pref_store_->CommitPendingWrite();
146 }
147
148 ProtectedPrefStore::~ProtectedPrefStore() {
149 unprotected_pref_store_->RemoveObserver(&tee_observer_);
150 protected_pref_store_->RemoveObserver(&tee_observer_);
151 }
152
153 const PersistentPrefStore*
154 ProtectedPrefStore::StoreForKey(const std::string& key) const {
155 if (ContainsKey(protected_preference_names_, key) ||
156 protected_pref_store_->GetValue(key, NULL)) {
157 return protected_pref_store_.get();
158 }
159 return unprotected_pref_store_.get();
160 }
161
162 PersistentPrefStore* ProtectedPrefStore::StoreForKey(const std::string& key) {
163 if (ContainsKey(protected_preference_names_, key))
164 return protected_pref_store_.get();
165
166 // Check if this unprotected value was previously protected. If so, migrate it
167 // back to the unprotected store.
168 // It's hard to do this in a single pass at startup because PrefStore does not
169 // permit us to enumerate its contents.
170 const base::Value* value = NULL;
171 if (protected_pref_store_->GetValue(key, &value)) {
172 scoped_ptr<base::Value> migrated_value(value->DeepCopy());
173 value = NULL;
174 unprotected_pref_store_->SetValue(key, migrated_value.release());
175 unprotected_pref_store_->CommitPendingWrite();
176 protected_pref_store_->RemoveValue(key);
177 protected_pref_store_->CommitPendingWrite();
178 }
179
180 return unprotected_pref_store_.get();
181 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698