Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/api/prefs/pref_member.h" | 5 #include "chrome/browser/api/prefs/pref_member.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/value_conversions.h" | 9 #include "base/value_conversions.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/api/prefs/pref_service_base.h" |
| 11 #include "chrome/common/chrome_notification_types.h" | 11 #include "chrome/common/chrome_notification_types.h" |
| 12 | 12 |
| 13 using content::BrowserThread; | 13 using content::BrowserThread; |
| 14 | 14 |
| 15 namespace subtle { | 15 namespace subtle { |
| 16 | 16 |
| 17 PrefMemberBase::PrefMemberBase() | 17 PrefMemberBase::PrefMemberBase() |
| 18 : observer_(NULL), | 18 : observer_(NULL), |
| 19 prefs_(NULL), | 19 prefs_(NULL), |
| 20 setting_value_(false) { | 20 setting_value_(false) { |
| 21 } | 21 } |
| 22 | 22 |
| 23 PrefMemberBase::~PrefMemberBase() { | 23 PrefMemberBase::~PrefMemberBase() { |
| 24 Destroy(); | 24 Destroy(); |
| 25 } | 25 } |
| 26 | 26 |
| 27 | 27 |
| 28 void PrefMemberBase::Init(const char* pref_name, | 28 void PrefMemberBase::Init(const char* pref_name, |
| 29 PrefService* prefs, | 29 PrefServiceBase* prefs, |
| 30 content::NotificationObserver* observer) { | 30 content::NotificationObserver* observer) { |
| 31 DCHECK(pref_name); | 31 DCHECK(pref_name); |
| 32 DCHECK(prefs); | 32 DCHECK(prefs); |
| 33 DCHECK(pref_name_.empty()); // Check that Init is only called once. | 33 DCHECK(pref_name_.empty()); // Check that Init is only called once. |
| 34 observer_ = observer; | 34 observer_ = observer; |
| 35 prefs_ = prefs; | 35 prefs_ = prefs; |
| 36 pref_name_ = pref_name; | 36 pref_name_ = pref_name; |
| 37 // Check that the preference is registered. | 37 // Check that the preference is registered. |
| 38 DCHECK(prefs_->FindPreference(pref_name_.c_str())) | 38 DCHECK(prefs_->GetUserPrefValue(pref_name_.c_str())) |
|
Mattias Nissler (ping if slow)
2012/08/16 15:16:42
Wait, what's this? This is incorrect.
Jói
2012/08/16 15:34:18
See below.
| |
| 39 << pref_name << " not registered."; | 39 << pref_name << " not registered."; |
| 40 | 40 |
| 41 // Add ourselves as a pref observer so we can keep our local value in sync. | 41 // Add ourselves as a pref observer so we can keep our local value in sync. |
| 42 prefs_->AddPrefObserver(pref_name, this); | 42 prefs_->AddPrefObserver(pref_name, this); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void PrefMemberBase::Destroy() { | 45 void PrefMemberBase::Destroy() { |
| 46 if (prefs_ && !pref_name_.empty()) { | 46 if (prefs_ && !pref_name_.empty()) { |
| 47 prefs_->RemovePrefObserver(pref_name_.c_str(), this); | 47 prefs_->RemovePrefObserver(pref_name_.c_str(), this); |
| 48 prefs_ = NULL; | 48 prefs_ = NULL; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 62 const content::NotificationDetails& details) { | 62 const content::NotificationDetails& details) { |
| 63 VerifyValuePrefName(); | 63 VerifyValuePrefName(); |
| 64 DCHECK(chrome::NOTIFICATION_PREF_CHANGED == type); | 64 DCHECK(chrome::NOTIFICATION_PREF_CHANGED == type); |
| 65 UpdateValueFromPref(); | 65 UpdateValueFromPref(); |
| 66 if (!setting_value_ && observer_) | 66 if (!setting_value_ && observer_) |
| 67 observer_->Observe(type, source, details); | 67 observer_->Observe(type, source, details); |
| 68 } | 68 } |
| 69 | 69 |
| 70 void PrefMemberBase::UpdateValueFromPref() const { | 70 void PrefMemberBase::UpdateValueFromPref() const { |
| 71 VerifyValuePrefName(); | 71 VerifyValuePrefName(); |
| 72 const PrefService::Preference* pref = | |
| 73 prefs_->FindPreference(pref_name_.c_str()); | |
| 74 DCHECK(pref); | |
| 75 if (!internal()) | 72 if (!internal()) |
| 76 CreateInternal(); | 73 CreateInternal(); |
| 77 internal()->UpdateValue(pref->GetValue()->DeepCopy(), pref->IsManaged()); | 74 internal()->UpdateValue( |
| 75 prefs_->GetUserPrefValue(pref_name_.c_str())->DeepCopy(), | |
|
Mattias Nissler (ping if slow)
2012/08/16 15:16:42
also incorrect
Jói
2012/08/16 15:34:18
This, and the thing above, was me trying to get ri
Mattias Nissler (ping if slow)
2012/08/16 16:48:33
You could indeed make a GetValue() function on Pre
| |
| 76 prefs_->IsManagedPreference(pref_name_.c_str())); | |
| 78 } | 77 } |
| 79 | 78 |
| 80 void PrefMemberBase::VerifyPref() const { | 79 void PrefMemberBase::VerifyPref() const { |
| 81 VerifyValuePrefName(); | 80 VerifyValuePrefName(); |
| 82 if (!internal()) | 81 if (!internal()) |
| 83 UpdateValueFromPref(); | 82 UpdateValueFromPref(); |
| 84 } | 83 } |
| 85 | 84 |
| 86 PrefMemberBase::Internal::Internal() | 85 PrefMemberBase::Internal::Internal() |
| 87 : thread_id_(BrowserThread::UI), | 86 : thread_id_(BrowserThread::UI), |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 template <> | 162 template <> |
| 164 void PrefMember<FilePath>::UpdatePref(const FilePath& value) { | 163 void PrefMember<FilePath>::UpdatePref(const FilePath& value) { |
| 165 prefs()->SetFilePath(pref_name().c_str(), value); | 164 prefs()->SetFilePath(pref_name().c_str(), value); |
| 166 } | 165 } |
| 167 | 166 |
| 168 template <> | 167 template <> |
| 169 bool PrefMember<FilePath>::Internal::UpdateValueInternal(const Value& value) | 168 bool PrefMember<FilePath>::Internal::UpdateValueInternal(const Value& value) |
| 170 const { | 169 const { |
| 171 return base::GetValueAsFilePath(value, &value_); | 170 return base::GetValueAsFilePath(value, &value_); |
| 172 } | 171 } |
| OLD | NEW |