summaryrefslogtreecommitdiffstats
path: root/chrome/browser/prefs/pref_member.cc
blob: cc143846cf710e4604c324148a1a737e62a5eac6 (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
// Copyright (c) 2010 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/pref_member.h"

#include "base/logging.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/common/notification_type.h"

namespace subtle {

PrefMemberBase::PrefMemberBase()
    : observer_(NULL),
      prefs_(NULL),
      is_synced_(false),
      setting_value_(false) {
}

PrefMemberBase::~PrefMemberBase() {
  if (!pref_name_.empty())
    prefs_->RemovePrefObserver(pref_name_.c_str(), this);
}


void PrefMemberBase::Init(const char* pref_name, PrefService* prefs,
                          NotificationObserver* observer) {
  DCHECK(pref_name);
  DCHECK(prefs);
  DCHECK(pref_name_.empty());  // Check that Init is only called once.
  observer_ = observer;
  prefs_ = prefs;
  pref_name_ = pref_name;
  DCHECK(!pref_name_.empty());

  // Add ourself as a pref observer so we can keep our local value in sync.
  prefs_->AddPrefObserver(pref_name, this);
}

bool PrefMemberBase::IsManaged() const {
  DCHECK(!pref_name_.empty());
  const PrefService::Preference* pref =
      prefs_->FindPreference(pref_name_.c_str());
  return pref && pref->IsManaged();
}

void PrefMemberBase::Observe(NotificationType type,
                             const NotificationSource& source,
                             const NotificationDetails& details) {
  DCHECK(!pref_name_.empty());
  DCHECK(NotificationType::PREF_CHANGED == type);
  UpdateValueFromPref();
  is_synced_ = true;
  if (!setting_value_ && observer_)
    observer_->Observe(type, source, details);
}

void PrefMemberBase::VerifyValuePrefName() const {
  DCHECK(!pref_name_.empty());
}

}  // namespace subtle

BooleanPrefMember::BooleanPrefMember() : PrefMember<bool>() {
}

BooleanPrefMember::~BooleanPrefMember() {
}

void BooleanPrefMember::UpdateValueFromPref() const {
  value_ = prefs()->GetBoolean(pref_name().c_str());
}

void BooleanPrefMember::UpdatePref(const bool& value) {
  prefs()->SetBoolean(pref_name().c_str(), value);
}

IntegerPrefMember::IntegerPrefMember() : PrefMember<int>() {
}

IntegerPrefMember::~IntegerPrefMember() {
}

void IntegerPrefMember::UpdateValueFromPref() const {
  value_ = prefs()->GetInteger(pref_name().c_str());
}

void IntegerPrefMember::UpdatePref(const int& value) {
  prefs()->SetInteger(pref_name().c_str(), value);
}

RealPrefMember::RealPrefMember() : PrefMember<double>() {
}

RealPrefMember::~RealPrefMember() {
}

void RealPrefMember::UpdateValueFromPref() const {
  value_ = prefs()->GetReal(pref_name().c_str());
}

void RealPrefMember::UpdatePref(const double& value) {
  prefs()->SetReal(pref_name().c_str(), value);
}

StringPrefMember::StringPrefMember() : PrefMember<std::string>() {
}

StringPrefMember::~StringPrefMember() {
}

void StringPrefMember::UpdateValueFromPref() const {
  value_ = prefs()->GetString(pref_name().c_str());
}

void StringPrefMember::UpdatePref(const std::string& value) {
  prefs()->SetString(pref_name().c_str(), value);
}

FilePathPrefMember::FilePathPrefMember() : PrefMember<FilePath>() {
}

FilePathPrefMember::~FilePathPrefMember() {
}

void FilePathPrefMember::UpdateValueFromPref() const {
  value_ = prefs()->GetFilePath(pref_name().c_str());
}

void FilePathPrefMember::UpdatePref(const FilePath& value) {
  prefs()->SetFilePath(pref_name().c_str(), value);
}