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