1 // Copyright (c) 2011 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/overlay_persistent_pref_store.h"
6
7 #include "base/values.h"
8
OverlayPersistentPrefStore(PersistentPrefStore * underlay)9 OverlayPersistentPrefStore::OverlayPersistentPrefStore(
10 PersistentPrefStore* underlay)
11 : underlay_(underlay) {
12 underlay_->AddObserver(this);
13 }
~OverlayPersistentPrefStore()14 OverlayPersistentPrefStore::~OverlayPersistentPrefStore() {
15 underlay_->RemoveObserver(this);
16 }
17
IsSetInOverlay(const std::string & key) const18 bool OverlayPersistentPrefStore::IsSetInOverlay(const std::string& key) const {
19 return overlay_.GetValue(key, NULL);
20 }
21
AddObserver(PrefStore::Observer * observer)22 void OverlayPersistentPrefStore::AddObserver(PrefStore::Observer* observer) {
23 observers_.AddObserver(observer);
24 }
25
RemoveObserver(PrefStore::Observer * observer)26 void OverlayPersistentPrefStore::RemoveObserver(PrefStore::Observer* observer) {
27 observers_.RemoveObserver(observer);
28 }
29
IsInitializationComplete() const30 bool OverlayPersistentPrefStore::IsInitializationComplete() const {
31 return underlay_->IsInitializationComplete();
32 }
33
GetValue(const std::string & key,const Value ** result) const34 PrefStore::ReadResult OverlayPersistentPrefStore::GetValue(
35 const std::string& key,
36 const Value** result) const {
37 if (overlay_.GetValue(key, result))
38 return READ_OK;
39 return underlay_->GetValue(key, result);
40 }
41
GetMutableValue(const std::string & key,Value ** result)42 PrefStore::ReadResult OverlayPersistentPrefStore::GetMutableValue(
43 const std::string& key,
44 Value** result) {
45 if (overlay_.GetValue(key, result))
46 return READ_OK;
47
48 // Try to create copy of underlay if the overlay does not contain a value.
49 Value* underlay_value = NULL;
50 PrefStore::ReadResult read_result =
51 underlay_->GetMutableValue(key, &underlay_value);
52 if (read_result == READ_OK) {
53 *result = underlay_value->DeepCopy();
54 overlay_.SetValue(key, *result);
55 return READ_OK;
56 }
57 // Return read failure if underlay stores no value for |key|.
58 return read_result;
59 }
60
SetValue(const std::string & key,Value * value)61 void OverlayPersistentPrefStore::SetValue(const std::string& key,
62 Value* value) {
63 if (overlay_.SetValue(key, value))
64 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
65 }
66
SetValueSilently(const std::string & key,Value * value)67 void OverlayPersistentPrefStore::SetValueSilently(const std::string& key,
68 Value* value) {
69 overlay_.SetValue(key, value);
70 }
71
RemoveValue(const std::string & key)72 void OverlayPersistentPrefStore::RemoveValue(const std::string& key) {
73 if (overlay_.RemoveValue(key))
74 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
75 }
76
ReadOnly() const77 bool OverlayPersistentPrefStore::ReadOnly() const {
78 return false;
79 }
80
ReadPrefs()81 PersistentPrefStore::PrefReadError OverlayPersistentPrefStore::ReadPrefs() {
82 // We do not read intentionally.
83 return PersistentPrefStore::PREF_READ_ERROR_NONE;
84 }
85
WritePrefs()86 bool OverlayPersistentPrefStore::WritePrefs() {
87 // We do not write intentionally.
88 return true;
89 }
90
ScheduleWritePrefs()91 void OverlayPersistentPrefStore::ScheduleWritePrefs() {
92 // We do not write intentionally.
93 }
94
CommitPendingWrite()95 void OverlayPersistentPrefStore::CommitPendingWrite() {
96 // We do not write intentionally.
97 }
98
ReportValueChanged(const std::string & key)99 void OverlayPersistentPrefStore::ReportValueChanged(const std::string& key) {
100 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
101 }
102
OnPrefValueChanged(const std::string & key)103 void OverlayPersistentPrefStore::OnPrefValueChanged(const std::string& key) {
104 if (!overlay_.GetValue(key, NULL))
105 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
106 }
107
OnInitializationCompleted()108 void OverlayPersistentPrefStore::OnInitializationCompleted() {
109 FOR_EACH_OBSERVER(PrefStore::Observer, observers_,
110 OnInitializationCompleted());
111 }
112