| 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_type.h" | 10 #include "content/common/notification_type.h" |
| 11 | 11 |
| 12 namespace subtle { | 12 namespace subtle { |
| 13 | 13 |
| 14 PrefMemberBase::PrefMemberBase() | 14 PrefMemberBase::PrefMemberBase() |
| 15 : observer_(NULL), | 15 : observer_(NULL), |
| 16 prefs_(NULL), | 16 prefs_(NULL), |
| 17 setting_value_(false) { | 17 setting_value_(false) { |
| 18 } | 18 } |
| 19 | 19 |
| 20 PrefMemberBase::~PrefMemberBase() { | 20 PrefMemberBase::~PrefMemberBase() { |
| 21 if (prefs_ && !pref_name_.empty()) | 21 Destroy(); |
| 22 prefs_->RemovePrefObserver(pref_name_.c_str(), this); | |
| 23 } | 22 } |
| 24 | 23 |
| 25 | 24 |
| 26 void PrefMemberBase::Init(const char* pref_name, PrefService* prefs, | 25 void PrefMemberBase::Init(const char* pref_name, PrefService* prefs, |
| 27 NotificationObserver* observer) { | 26 NotificationObserver* observer) { |
| 28 DCHECK(pref_name); | 27 DCHECK(pref_name); |
| 29 DCHECK(prefs); | 28 DCHECK(prefs); |
| 30 DCHECK(pref_name_.empty()); // Check that Init is only called once. | 29 DCHECK(pref_name_.empty()); // Check that Init is only called once. |
| 31 observer_ = observer; | 30 observer_ = observer; |
| 32 prefs_ = prefs; | 31 prefs_ = prefs; |
| 33 pref_name_ = pref_name; | 32 pref_name_ = pref_name; |
| 34 DCHECK(!pref_name_.empty()); | 33 DCHECK(!pref_name_.empty()); |
| 35 | 34 |
| 36 // Add ourselves as a pref observer so we can keep our local value in sync. | 35 // Add ourselves as a pref observer so we can keep our local value in sync. |
| 37 prefs_->AddPrefObserver(pref_name, this); | 36 prefs_->AddPrefObserver(pref_name, this); |
| 38 } | 37 } |
| 39 | 38 |
| 40 void PrefMemberBase::Destroy() { | 39 void PrefMemberBase::Destroy() { |
| 41 if (prefs_) { | 40 if (prefs_ && !pref_name_.empty()) { |
| 42 prefs_->RemovePrefObserver(pref_name_.c_str(), this); | 41 prefs_->RemovePrefObserver(pref_name_.c_str(), this); |
| 43 prefs_ = NULL; | 42 prefs_ = NULL; |
| 44 } | 43 } |
| 45 } | 44 } |
| 46 | 45 |
| 47 void PrefMemberBase::MoveToThread(BrowserThread::ID thread_id) { | 46 void PrefMemberBase::MoveToThread(BrowserThread::ID thread_id) { |
| 48 VerifyValuePrefName(); | 47 VerifyValuePrefName(); |
| 49 // Load the value from preferences if it hasn't been loaded so far. | 48 // Load the value from preferences if it hasn't been loaded so far. |
| 50 if (!internal()) | 49 if (!internal()) |
| 51 UpdateValueFromPref(); | 50 UpdateValueFromPref(); |
| 52 internal()->MoveToThread(thread_id); | 51 internal()->MoveToThread(thread_id); |
| 53 } | 52 } |
| 54 | 53 |
| 55 void PrefMemberBase::Observe(NotificationType type, | 54 void PrefMemberBase::Observe(NotificationType type, |
| 56 const NotificationSource& source, | 55 const NotificationSource& source, |
| 57 const NotificationDetails& details) { | 56 const NotificationDetails& details) { |
| 58 VerifyValuePrefName(); | 57 VerifyValuePrefName(); |
| 59 DCHECK(NotificationType::PREF_CHANGED == type); | 58 DCHECK(NotificationType::PREF_CHANGED == type); |
| 60 UpdateValueFromPref(); | 59 UpdateValueFromPref(); |
| 61 if (!setting_value_ && observer_) | 60 if (!setting_value_ && observer_) |
| 62 observer_->Observe(type, source, details); | 61 observer_->Observe(type, source, details); |
| 63 } | 62 } |
| 64 | 63 |
| 65 void PrefMemberBase::VerifyValuePrefName() const { | |
| 66 DCHECK(!pref_name_.empty()); | |
| 67 } | |
| 68 | |
| 69 void PrefMemberBase::UpdateValueFromPref() const { | 64 void PrefMemberBase::UpdateValueFromPref() const { |
| 70 VerifyValuePrefName(); | 65 VerifyValuePrefName(); |
| 71 const PrefService::Preference* pref = | 66 const PrefService::Preference* pref = |
| 72 prefs_->FindPreference(pref_name_.c_str()); | 67 prefs_->FindPreference(pref_name_.c_str()); |
| 73 DCHECK(pref); | 68 DCHECK(pref); |
| 74 if (!internal()) | 69 if (!internal()) |
| 75 CreateInternal(); | 70 CreateInternal(); |
| 76 internal()->UpdateValue(pref->GetValue()->DeepCopy(), pref->IsManaged()); | 71 internal()->UpdateValue(pref->GetValue()->DeepCopy(), pref->IsManaged()); |
| 77 } | 72 } |
| 78 | 73 |
| 74 void PrefMemberBase::VerifyPref() const { |
| 75 VerifyValuePrefName(); |
| 76 if (!internal()) |
| 77 UpdateValueFromPref(); |
| 78 } |
| 79 |
| 79 PrefMemberBase::Internal::Internal() : thread_id_(BrowserThread::UI) { } | 80 PrefMemberBase::Internal::Internal() : thread_id_(BrowserThread::UI) { } |
| 80 PrefMemberBase::Internal::~Internal() { } | 81 PrefMemberBase::Internal::~Internal() { } |
| 81 | 82 |
| 82 bool PrefMemberBase::Internal::IsOnCorrectThread() const { | 83 bool PrefMemberBase::Internal::IsOnCorrectThread() const { |
| 83 // In unit tests, there may not be a UI thread. | 84 // In unit tests, there may not be a UI thread. |
| 84 return (BrowserThread::CurrentlyOn(thread_id_) || | 85 return (BrowserThread::CurrentlyOn(thread_id_) || |
| 85 (thread_id_ == BrowserThread::UI && | 86 (thread_id_ == BrowserThread::UI && |
| 86 !BrowserThread::IsMessageLoopValid(BrowserThread::UI))); | 87 !BrowserThread::IsMessageLoopValid(BrowserThread::UI))); |
| 87 } | 88 } |
| 88 | 89 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 void PrefMember<FilePath>::UpdatePref(const FilePath& value) { | 156 void PrefMember<FilePath>::UpdatePref(const FilePath& value) { |
| 156 prefs()->SetFilePath(pref_name().c_str(), value); | 157 prefs()->SetFilePath(pref_name().c_str(), value); |
| 157 } | 158 } |
| 158 | 159 |
| 159 template <> | 160 template <> |
| 160 bool PrefMember<FilePath>::Internal::UpdateValueInternal(const Value& value) | 161 bool PrefMember<FilePath>::Internal::UpdateValueInternal(const Value& value) |
| 161 const { | 162 const { |
| 162 return base::GetValueAsFilePath(value, &value_); | 163 return base::GetValueAsFilePath(value, &value_); |
| 163 } | 164 } |
| 164 | 165 |
| OLD | NEW |