OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/prefs/pref_member.h" | 5 #include "chrome/browser/prefs/pref_member.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/value_conversions.h" | 8 #include "base/value_conversions.h" |
9 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
10 #include "content/common/notification_source.h" | |
10 #include "content/common/notification_type.h" | 11 #include "content/common/notification_type.h" |
11 | 12 |
12 namespace subtle { | 13 namespace subtle { |
13 | 14 |
14 PrefMemberBase::PrefMemberBase() | 15 PrefMemberBase::PrefMemberBase() |
15 : observer_(NULL), | 16 : observer_(NULL), |
16 prefs_(NULL), | 17 prefs_(NULL), |
17 setting_value_(false) { | 18 setting_value_(false) { |
18 } | 19 } |
19 | 20 |
20 PrefMemberBase::~PrefMemberBase() { | 21 PrefMemberBase::~PrefMemberBase() { |
21 if (prefs_ && !pref_name_.empty()) | 22 Destroy(); |
22 prefs_->RemovePrefObserver(pref_name_.c_str(), this); | |
23 } | 23 } |
24 | 24 |
25 | 25 |
26 void PrefMemberBase::Init(const char* pref_name, PrefService* prefs, | 26 void PrefMemberBase::Init(const char* pref_name, PrefService* prefs, |
27 NotificationObserver* observer) { | 27 NotificationObserver* observer) { |
28 DCHECK(pref_name); | 28 DCHECK(pref_name); |
29 DCHECK(prefs); | 29 DCHECK(prefs); |
30 DCHECK(pref_name_.empty()); // Check that Init is only called once. | 30 DCHECK(pref_name_.empty()); // Check that Init is only called once. |
31 observer_ = observer; | 31 observer_ = observer; |
32 prefs_ = prefs; | 32 prefs_ = prefs; |
33 pref_name_ = pref_name; | 33 pref_name_ = pref_name; |
34 DCHECK(!pref_name_.empty()); | 34 DCHECK(!pref_name_.empty()); |
35 | 35 |
36 // Add ourselves as a pref observer so we can keep our local value in sync. | 36 // Add ourselves as a pref observer so we can keep our local value in sync. |
37 prefs_->AddPrefObserver(pref_name, this); | 37 prefs_->AddPrefObserver(pref_name, this); |
38 } | 38 } |
39 | 39 |
40 void PrefMemberBase::ObserveProfileDestruction(Profile* profile) { | |
41 DCHECK(registrar_.IsEmpty()); | |
42 registrar_.Add(this, | |
43 NotificationType::PROFILE_DESTROYED, | |
44 Source<Profile>(profile)); | |
45 } | |
46 | |
40 void PrefMemberBase::Destroy() { | 47 void PrefMemberBase::Destroy() { |
41 if (prefs_) { | 48 if (prefs_ && !pref_name_.empty()) { |
42 prefs_->RemovePrefObserver(pref_name_.c_str(), this); | 49 prefs_->RemovePrefObserver(pref_name_.c_str(), this); |
43 prefs_ = NULL; | 50 prefs_ = NULL; |
44 } | 51 } |
45 } | 52 } |
46 | 53 |
47 void PrefMemberBase::MoveToThread(BrowserThread::ID thread_id) { | 54 void PrefMemberBase::MoveToThread(BrowserThread::ID thread_id) { |
48 VerifyValuePrefName(); | 55 VerifyValuePrefName(); |
49 // Load the value from preferences if it hasn't been loaded so far. | 56 // Load the value from preferences if it hasn't been loaded so far. |
50 if (!internal()) | 57 if (!internal()) |
51 UpdateValueFromPref(); | 58 UpdateValueFromPref(); |
52 internal()->MoveToThread(thread_id); | 59 internal()->MoveToThread(thread_id); |
53 } | 60 } |
54 | 61 |
55 void PrefMemberBase::Observe(NotificationType type, | 62 void PrefMemberBase::Observe(NotificationType type, |
56 const NotificationSource& source, | 63 const NotificationSource& source, |
57 const NotificationDetails& details) { | 64 const NotificationDetails& details) { |
58 VerifyValuePrefName(); | 65 switch (type.value) { |
59 DCHECK(NotificationType::PREF_CHANGED == type); | 66 case NotificationType::PREF_CHANGED: { |
60 UpdateValueFromPref(); | 67 VerifyValuePrefName(); |
61 if (!setting_value_ && observer_) | 68 UpdateValueFromPref(); |
62 observer_->Observe(type, source, details); | 69 if (!setting_value_ && observer_) |
70 observer_->Observe(type, source, details); | |
71 break; | |
72 } | |
73 case NotificationType::PROFILE_DESTROYED: { | |
74 Destroy(); | |
75 registrar_.RemoveAll(); | |
76 break; | |
77 } | |
Mattias Nissler (ping if slow)
2011/04/05 08:48:27
No need for curlies (or make it consistent with th
Bernhard Bauer
2011/04/05 10:48:34
Done.
| |
78 default: | |
79 NOTREACHED(); | |
80 } | |
63 } | 81 } |
64 | 82 |
65 void PrefMemberBase::VerifyValuePrefName() const { | 83 void PrefMemberBase::VerifyValuePrefName() const { |
66 DCHECK(!pref_name_.empty()); | 84 DCHECK(!pref_name_.empty()); |
67 } | 85 } |
68 | 86 |
69 void PrefMemberBase::UpdateValueFromPref() const { | 87 void PrefMemberBase::UpdateValueFromPref() const { |
70 VerifyValuePrefName(); | 88 VerifyValuePrefName(); |
71 const PrefService::Preference* pref = | 89 const PrefService::Preference* pref = |
72 prefs_->FindPreference(pref_name_.c_str()); | 90 prefs_->FindPreference(pref_name_.c_str()); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 void PrefMember<FilePath>::UpdatePref(const FilePath& value) { | 173 void PrefMember<FilePath>::UpdatePref(const FilePath& value) { |
156 prefs()->SetFilePath(pref_name().c_str(), value); | 174 prefs()->SetFilePath(pref_name().c_str(), value); |
157 } | 175 } |
158 | 176 |
159 template <> | 177 template <> |
160 bool PrefMember<FilePath>::Internal::UpdateValueInternal(const Value& value) | 178 bool PrefMember<FilePath>::Internal::UpdateValueInternal(const Value& value) |
161 const { | 179 const { |
162 return base::GetValueAsFilePath(value, &value_); | 180 return base::GetValueAsFilePath(value, &value_); |
163 } | 181 } |
164 | 182 |
OLD | NEW |