2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * InterfaceNamerControlPrefs.c
26 * - definitions for accessing InterfaceNamer control preferences
30 * Modification History
32 * January 12, 2017 Allan Nathanson (ajn@apple.com)
36 #include <TargetConditionals.h>
37 #include <SystemConfiguration/SystemConfiguration.h>
38 #include <SystemConfiguration/SCPrivate.h>
39 #include <SystemConfiguration/scprefs_observer.h>
40 #include "InterfaceNamerControlPrefs.h"
42 os_log_t
__log_InterfaceNamer();
45 * kInterfaceNamerControlPrefsID
46 * - identifies the InterfaceNamer preferences file that contains 'AllowNewInterfaces'
48 #define kInterfaceNamerControlPrefsIDStr "com.apple.InterfaceNamer.control.plist"
49 #define kInterfaceNamerControlPrefsID CFSTR(kInterfaceNamerControlPrefsIDStr)
53 * - indicates whether InterfaceNamer is allowed to create new interfaces
54 * while the screen is locked or not
56 #define kAllowNewInterfaces CFSTR("AllowNewInterfaces")
58 static SCPreferencesRef S_prefs
;
59 static InterfaceNamerControlPrefsCallBack S_callback
;
61 static SCPreferencesRef
62 InterfaceNamerControlPrefsGet(void)
64 if (S_prefs
== NULL
) {
65 InterfaceNamerControlPrefsInit(NULL
, NULL
);
72 prefs_changed(void * arg
)
75 os_activity_t activity
;
77 activity
= os_activity_create("processing InterfaceNamer preference change",
79 OS_ACTIVITY_FLAG_DEFAULT
);
80 os_activity_scope(activity
);
82 /* get the current value */
83 if (S_callback
!= NULL
) {
84 (*S_callback
)(S_prefs
);
94 * kInterfaceNamerControlManangedPrefsID
95 * - identifies the location of the managed preferences file
97 #define kManagedPrefsDirStr "/Library/Managed Preferences/mobile/"
98 #define kInterfaceNamerControlManagedPrefsID CFSTR(kManagedPrefsDirStr \
99 kInterfaceNamerControlPrefsIDStr)
100 static SCPreferencesRef S_managed_prefs
;
102 static SCPreferencesRef
103 InterfaceNamerControlManagedPrefsGet(void)
105 if (S_managed_prefs
== NULL
) {
106 CFMutableDictionaryRef options
;
108 options
= CFDictionaryCreateMutable(NULL
,
110 &kCFTypeDictionaryKeyCallBacks
,
111 &kCFTypeDictionaryValueCallBacks
);
112 CFDictionarySetValue(options
, kSCPreferencesOptionRemoveWhenEmpty
, kCFBooleanTrue
);
113 S_managed_prefs
= SCPreferencesCreateWithOptions(NULL
,
114 CFSTR("InterfaceNamerControlPrefs"),
115 kInterfaceNamerControlManagedPrefsID
,
120 return (S_managed_prefs
);
124 enable_prefs_observer(CFRunLoopRef runloop
)
126 CFRunLoopSourceContext context
;
127 dispatch_queue_t queue
;
128 CFRunLoopSourceRef source
;
130 bzero(&context
, sizeof(context
));
131 context
.perform
= prefs_changed
;
132 source
= CFRunLoopSourceCreate(kCFAllocatorDefault
, 0, &context
);
133 CFRunLoopAddSource(runloop
, source
, kCFRunLoopCommonModes
);
134 queue
= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0);
135 _scprefs_observer_watch(scprefs_observer_type_global
,
136 kInterfaceNamerControlPrefsIDStr
,
139 if (source
!= NULL
) {
140 CFRunLoopSourceSignal(source
);
141 if (runloop
!= NULL
) {
142 CFRunLoopWakeUp(runloop
);
149 #else /* TARGET_OS_IPHONE */
152 enable_prefs_observer(CFRunLoopRef runloop
)
154 #pragma unused(runloop)
158 #endif /* TARGET_OS_IPHONE */
161 InterfaceNamerControlPrefsChanged(SCPreferencesRef prefs
,
162 SCPreferencesNotification type
,
165 #pragma unused(prefs)
172 __private_extern__ SCPreferencesRef
173 InterfaceNamerControlPrefsInit(CFRunLoopRef runloop
,
174 InterfaceNamerControlPrefsCallBack callback
)
176 CFMutableDictionaryRef options
;
178 options
= CFDictionaryCreateMutable(NULL
,
180 &kCFTypeDictionaryKeyCallBacks
,
181 &kCFTypeDictionaryValueCallBacks
);
182 CFDictionarySetValue(options
, kSCPreferencesOptionRemoveWhenEmpty
, kCFBooleanTrue
);
183 S_prefs
= SCPreferencesCreateWithOptions(NULL
,
184 CFSTR("InterfaceNamerControlPrefs"),
185 kInterfaceNamerControlPrefsID
,
190 if ((runloop
!= NULL
) && (callback
!= NULL
)) {
191 S_callback
= callback
;
192 if (!SCPreferencesSetCallback(S_prefs
, InterfaceNamerControlPrefsChanged
, NULL
)) {
193 SC_log(LOG_NOTICE
, "SCPreferencesSetCallBack() failed: %s", SCErrorString(SCError()));
197 if (!SCPreferencesScheduleWithRunLoop(S_prefs
, runloop
, kCFRunLoopCommonModes
)) {
198 SC_log(LOG_NOTICE
, "SCPreferencesScheduleWithRunLoop() failed: %s", SCErrorString(SCError()));
199 (void) SCPreferencesSetCallback(S_prefs
, NULL
, NULL
);
202 enable_prefs_observer(runloop
);
210 InterfaceNamerControlPrefsSave(void)
212 Boolean saved
= FALSE
;
214 if (S_prefs
!= NULL
) {
215 saved
= SCPreferencesCommitChanges(S_prefs
);
216 SCPreferencesSynchronize(S_prefs
);
222 prefs_get_boolean(CFStringRef key
)
224 CFBooleanRef b
= NULL
;
225 SCPreferencesRef prefs
;
228 prefs
= InterfaceNamerControlManagedPrefsGet();
230 b
= SCPreferencesGetValue(prefs
, key
);
231 b
= isA_CFBoolean(b
);
236 #endif /* TARGET_OS_IPHONE */
238 prefs
= InterfaceNamerControlPrefsGet();
240 b
= SCPreferencesGetValue(prefs
, key
);
241 b
= isA_CFBoolean(b
);
247 prefs_set_boolean(CFStringRef key
, CFBooleanRef b
)
249 SCPreferencesRef prefs
;
251 prefs
= InterfaceNamerControlPrefsGet();
253 if (isA_CFBoolean(b
) == NULL
) {
254 SCPreferencesRemoveValue(prefs
, key
);
257 SCPreferencesSetValue(prefs
, key
, b
);
264 InterfaceNamerControlPrefsRefresh(void)
266 if (S_prefs
!= NULL
) {
267 SCPreferencesSynchronize(S_prefs
);
270 if (S_managed_prefs
!= NULL
) {
271 SCPreferencesSynchronize(S_managed_prefs
);
273 #endif /* TARGET_OS_IPHONE */
280 __private_extern__ Boolean
281 InterfaceNamerControlPrefsAllowNewInterfaces(void)
284 Boolean allow
= FALSE
;
286 b
= prefs_get_boolean(kAllowNewInterfaces
);
288 allow
= CFBooleanGetValue(b
);
290 /* flush the backing store */
291 InterfaceNamerControlPrefsRefresh();
298 __private_extern__ Boolean
299 InterfaceNamerControlPrefsSetAllowNewInterfaces(Boolean allow
)
302 prefs_set_boolean(kAllowNewInterfaces
, kCFBooleanTrue
);
304 prefs_set_boolean(kAllowNewInterfaces
, NULL
);
306 return (InterfaceNamerControlPrefsSave());