Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Unified Diff: ios/chrome/browser/ui/settings/utils/pref_backed_boolean.mm

Issue 2587023002: Upstream Chrome on iOS source code [8/11]. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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

Powered by Google App Engine
This is Rietveld 408576698