OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "components/prefs/pref_member.h" |
| 9 #include "components/prefs/pref_service.h" |
| 10 |
| 11 namespace { |
| 12 |
| 13 // Adaptor function to allow invoking onChange as a base::Closure. |
| 14 void OnChange(id<ObservableBoolean> object) { |
| 15 [object.observer booleanDidChange:object]; |
| 16 } |
| 17 |
| 18 } // namespace |
| 19 |
| 20 @implementation PrefBackedBoolean { |
| 21 BooleanPrefMember _pref; |
| 22 } |
| 23 |
| 24 @synthesize observer = _observer; |
| 25 |
| 26 - (id)initWithPrefService:(PrefService*)prefs prefName:(const char*)prefName { |
| 27 self = [super init]; |
| 28 if (self) { |
| 29 // Create a base::Closure that calls onChange. |
| 30 // Bind expects a refcounted object but allows us to pass a raw pointer |
| 31 // with Unretained. Since the closure will be deleted when |_pref| is |
| 32 // deleted, which happens when |self| is deleted, we know |self| will still |
| 33 // be valid when the closure is invoked. |
| 34 base::Closure onChangeClosure = |
| 35 base::Bind(&OnChange, base::Unretained(self)); |
| 36 _pref.Init(prefName, prefs, onChangeClosure); |
| 37 } |
| 38 return self; |
| 39 } |
| 40 |
| 41 - (BOOL)value { |
| 42 return _pref.GetValue(); |
| 43 } |
| 44 |
| 45 - (void)setValue:(BOOL)value { |
| 46 _pref.SetValue(value == YES); |
| 47 } |
| 48 |
| 49 @end |
OLD | NEW |