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
9OverlayPersistentPrefStore::OverlayPersistentPrefStore(
10    PersistentPrefStore* underlay)
11    : underlay_(underlay) {
12  underlay_->AddObserver(this);
13}
14OverlayPersistentPrefStore::~OverlayPersistentPrefStore() {
15  underlay_->RemoveObserver(this);
16}
17
18bool OverlayPersistentPrefStore::IsSetInOverlay(const std::string& key) const {
19  return overlay_.GetValue(key, NULL);
20}
21
22void OverlayPersistentPrefStore::AddObserver(PrefStore::Observer* observer) {
23  observers_.AddObserver(observer);
24}
25
26void OverlayPersistentPrefStore::RemoveObserver(PrefStore::Observer* observer) {
27  observers_.RemoveObserver(observer);
28}
29
30bool OverlayPersistentPrefStore::IsInitializationComplete() const {
31  return underlay_->IsInitializationComplete();
32}
33
34PrefStore::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
42PrefStore::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
61void 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
67void OverlayPersistentPrefStore::SetValueSilently(const std::string& key,
68                                                  Value* value) {
69  overlay_.SetValue(key, value);
70}
71
72void OverlayPersistentPrefStore::RemoveValue(const std::string& key) {
73  if (overlay_.RemoveValue(key))
74    FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
75}
76
77bool OverlayPersistentPrefStore::ReadOnly() const {
78  return false;
79}
80
81PersistentPrefStore::PrefReadError OverlayPersistentPrefStore::ReadPrefs() {
82  // We do not read intentionally.
83  return PersistentPrefStore::PREF_READ_ERROR_NONE;
84}
85
86bool OverlayPersistentPrefStore::WritePrefs() {
87  // We do not write intentionally.
88  return true;
89}
90
91void OverlayPersistentPrefStore::ScheduleWritePrefs() {
92  // We do not write intentionally.
93}
94
95void OverlayPersistentPrefStore::CommitPendingWrite() {
96  // We do not write intentionally.
97}
98
99void OverlayPersistentPrefStore::ReportValueChanged(const std::string& key) {
100  FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
101}
102
103void OverlayPersistentPrefStore::OnPrefValueChanged(const std::string& key) {
104  if (!overlay_.GetValue(key, NULL))
105    FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
106}
107
108void OverlayPersistentPrefStore::OnInitializationCompleted() {
109  FOR_EACH_OBSERVER(PrefStore::Observer, observers_,
110                    OnInitializationCompleted());
111}
112