| Index: ios/chrome/browser/ui/settings/utils/pref_backed_boolean.mm
|
| diff --git a/ios/chrome/browser/ui/settings/utils/pref_backed_boolean.mm b/ios/chrome/browser/ui/settings/utils/pref_backed_boolean.mm
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..4cf0491aa700a6917b93d7eec5013a8e1f2b2322
|
| --- /dev/null
|
| +++ b/ios/chrome/browser/ui/settings/utils/pref_backed_boolean.mm
|
| @@ -0,0 +1,49 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "components/prefs/pref_member.h"
|
| +#include "components/prefs/pref_service.h"
|
| +
|
| +namespace {
|
| +
|
| +// Adaptor function to allow invoking onChange as a base::Closure.
|
| +void OnChange(id<ObservableBoolean> object) {
|
| + [object.observer booleanDidChange:object];
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +@implementation PrefBackedBoolean {
|
| + BooleanPrefMember _pref;
|
| +}
|
| +
|
| +@synthesize observer = _observer;
|
| +
|
| +- (id)initWithPrefService:(PrefService*)prefs prefName:(const char*)prefName {
|
| + self = [super init];
|
| + if (self) {
|
| + // Create a base::Closure that calls onChange.
|
| + // Bind expects a refcounted object but allows us to pass a raw pointer
|
| + // with Unretained. Since the closure will be deleted when |_pref| is
|
| + // deleted, which happens when |self| is deleted, we know |self| will still
|
| + // be valid when the closure is invoked.
|
| + base::Closure onChangeClosure =
|
| + base::Bind(&OnChange, base::Unretained(self));
|
| + _pref.Init(prefName, prefs, onChangeClosure);
|
| + }
|
| + return self;
|
| +}
|
| +
|
| +- (BOOL)value {
|
| + return _pref.GetValue();
|
| +}
|
| +
|
| +- (void)setValue:(BOOL)value {
|
| + _pref.SetValue(value == YES);
|
| +}
|
| +
|
| +@end
|
|
|