summaryrefslogtreecommitdiffstats
path: root/chrome/browser/prefs/incognito_user_pref_store.cc
blob: 87bcb2c8870cbd6f2d2c94c5712b3a04e7dec84b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/prefs/incognito_user_pref_store.h"

#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chrome/common/pref_names.h"

IncognitoUserPrefStore::IncognitoUserPrefStore(
    PersistentPrefStore* underlay)
    : underlay_(underlay) {
  underlay_->AddObserver(this);
}

IncognitoUserPrefStore::~IncognitoUserPrefStore() {
  underlay_->RemoveObserver(this);
}

bool IncognitoUserPrefStore::IsSetInOverlay(const std::string& key) const {
  return overlay_.GetValue(key, NULL);
}

void IncognitoUserPrefStore::AddObserver(PrefStore::Observer* observer) {
  observers_.AddObserver(observer);
}

void IncognitoUserPrefStore::RemoveObserver(PrefStore::Observer* observer) {
  observers_.RemoveObserver(observer);
}

bool IncognitoUserPrefStore::IsInitializationComplete() const {
  return underlay_->IsInitializationComplete();
}

PrefStore::ReadResult IncognitoUserPrefStore::GetValue(
    const std::string& key,
    const Value** result) const {
  // If the |key| shall NOT be stored in the overlay store, there must not
  // be an entry.
  DCHECK(ShallBeStoredInOverlay(key) || !overlay_.GetValue(key, NULL));

  if (overlay_.GetValue(key, result))
    return READ_OK;
  return underlay_->GetValue(key, result);
}

PrefStore::ReadResult IncognitoUserPrefStore::GetMutableValue(
    const std::string& key,
    Value** result) {
  if (!ShallBeStoredInOverlay(key))
    return underlay_->GetMutableValue(key, result);

  if (overlay_.GetValue(key, result))
    return READ_OK;

  // Try to create copy of underlay if the overlay does not contain a value.
  Value* underlay_value = NULL;
  PrefStore::ReadResult read_result =
      underlay_->GetMutableValue(key, &underlay_value);
  if (read_result != READ_OK)
    return read_result;

  *result = underlay_value->DeepCopy();
  overlay_.SetValue(key, *result);
  return READ_OK;
}

void IncognitoUserPrefStore::SetValue(const std::string& key,
                                      Value* value) {
  if (!ShallBeStoredInOverlay(key)) {
    underlay_->SetValue(key, value);
    return;
  }

  if (overlay_.SetValue(key, value))
    ReportValueChanged(key);
}

void IncognitoUserPrefStore::SetValueSilently(const std::string& key,
                                              Value* value) {
  if (!ShallBeStoredInOverlay(key)) {
    underlay_->SetValueSilently(key, value);
    return;
  }

  overlay_.SetValue(key, value);
}

void IncognitoUserPrefStore::RemoveValue(const std::string& key) {
  if (!ShallBeStoredInOverlay(key)) {
    underlay_->RemoveValue(key);
    return;
  }

  if (overlay_.RemoveValue(key))
    ReportValueChanged(key);
}

bool IncognitoUserPrefStore::ReadOnly() const {
  return false;
}

PersistentPrefStore::PrefReadError IncognitoUserPrefStore::ReadPrefs() {
  // We do not read intentionally.
  OnInitializationCompleted(true);
  return PersistentPrefStore::PREF_READ_ERROR_NONE;
}

void IncognitoUserPrefStore::ReadPrefsAsync(
    ReadErrorDelegate* error_delegate_raw) {
  scoped_ptr<ReadErrorDelegate> error_delegate(error_delegate_raw);
  // We do not read intentionally.
  OnInitializationCompleted(true);
}

bool IncognitoUserPrefStore::WritePrefs() {
  // We do not write our content intentionally.
  return true;
}

void IncognitoUserPrefStore::ScheduleWritePrefs() {
  underlay_->ScheduleWritePrefs();
  // We do not write our content intentionally.
}

void IncognitoUserPrefStore::CommitPendingWrite() {
  underlay_->CommitPendingWrite();
  // We do not write our content intentionally.
}

void IncognitoUserPrefStore::ReportValueChanged(const std::string& key) {
  FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
}

void IncognitoUserPrefStore::OnPrefValueChanged(const std::string& key) {
  if (!overlay_.GetValue(key, NULL))
    ReportValueChanged(key);
}

void IncognitoUserPrefStore::OnInitializationCompleted(bool succeeded) {
  FOR_EACH_OBSERVER(PrefStore::Observer, observers_,
                    OnInitializationCompleted(succeeded));
}

bool IncognitoUserPrefStore::ShallBeStoredInOverlay(
    const std::string& key) const {
  // List of keys that cannot be changed in the user prefs file by the incognito
  // profile:
  return key == prefs::kBrowserWindowPlacement;
}