OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/prefs/pref_service.h" | 8 #include "chrome/browser/prefs/pref_service.h" |
9 #include "chrome/common/notification_type.h" | 9 #include "chrome/common/notification_type.h" |
10 | 10 |
11 namespace subtle { | 11 namespace subtle { |
12 | 12 |
13 PrefMemberBase::PrefMemberBase() | 13 PrefMemberBase::PrefMemberBase() |
14 : observer_(NULL), | 14 : observer_(NULL), |
15 prefs_(NULL), | 15 prefs_(NULL), |
16 is_synced_(false), | 16 is_synced_(false), |
17 setting_value_(false) { | 17 setting_value_(false) { |
18 } | 18 } |
19 | 19 |
20 PrefMemberBase::~PrefMemberBase() { | 20 PrefMemberBase::~PrefMemberBase() { |
21 if (!pref_name_.empty()) | 21 if (prefs_ && !pref_name_.empty()) |
22 prefs_->RemovePrefObserver(pref_name_.c_str(), this); | 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 ourself as a pref observer so we can keep our local value in sync. | 36 // Add ourself 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::Destroy() { | |
41 if (prefs_) { | |
42 prefs_->RemovePrefObserver(pref_name_.c_str(), this); | |
43 prefs_ = NULL; | |
44 } | |
Dan Beam
2016/06/17 21:56:15
out of curiosity, could pref_name_.clear() be call
battre
2016/06/20 07:53:46
I did not find anything subtle that would break.
Dan Beam
2016/06/21 22:50:41
I think making Destroy() private and encouraging f
| |
45 } | |
46 | |
40 bool PrefMemberBase::IsManaged() const { | 47 bool PrefMemberBase::IsManaged() const { |
41 DCHECK(!pref_name_.empty()); | 48 DCHECK(!pref_name_.empty()); |
42 const PrefService::Preference* pref = | 49 const PrefService::Preference* pref = |
43 prefs_->FindPreference(pref_name_.c_str()); | 50 prefs_->FindPreference(pref_name_.c_str()); |
44 return pref && pref->IsManaged(); | 51 return pref && pref->IsManaged(); |
45 } | 52 } |
46 | 53 |
47 void PrefMemberBase::Observe(NotificationType type, | 54 void PrefMemberBase::Observe(NotificationType type, |
48 const NotificationSource& source, | 55 const NotificationSource& source, |
49 const NotificationDetails& details) { | 56 const NotificationDetails& details) { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 FilePathPrefMember::~FilePathPrefMember() { | 130 FilePathPrefMember::~FilePathPrefMember() { |
124 } | 131 } |
125 | 132 |
126 void FilePathPrefMember::UpdateValueFromPref() const { | 133 void FilePathPrefMember::UpdateValueFromPref() const { |
127 value_ = prefs()->GetFilePath(pref_name().c_str()); | 134 value_ = prefs()->GetFilePath(pref_name().c_str()); |
128 } | 135 } |
129 | 136 |
130 void FilePathPrefMember::UpdatePref(const FilePath& value) { | 137 void FilePathPrefMember::UpdatePref(const FilePath& value) { |
131 prefs()->SetFilePath(pref_name().c_str(), value); | 138 prefs()->SetFilePath(pref_name().c_str(), value); |
132 } | 139 } |
OLD | NEW |