1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "MacStringHelpers.h"
6 #include "nsMacPreferencesReader.h"
10 #include "js/RootingAPI.h"
12 #include "mozilla/JSONStringWriteFuncs.h"
14 NS_IMPL_ISUPPORTS(nsMacPreferencesReader, nsIMacPreferencesReader)
16 using namespace mozilla;
18 static void EvaluateDict(JSONWriter* aWriter,
19 NSDictionary<NSString*, id>* aDict);
21 static void EvaluateArray(JSONWriter* aWriter, NSArray* aArray) {
22 for (id elem in aArray) {
23 if ([elem isKindOfClass:[NSString class]]) {
24 aWriter->StringElement(MakeStringSpan([elem UTF8String]));
25 } else if ([elem isKindOfClass:[NSNumber class]]) {
26 aWriter->IntElement([elem longLongValue]);
27 } else if ([elem isKindOfClass:[NSArray class]]) {
28 aWriter->StartArrayElement();
29 EvaluateArray(aWriter, elem);
31 } else if ([elem isKindOfClass:[NSDictionary class]]) {
32 aWriter->StartObjectElement();
33 EvaluateDict(aWriter, elem);
39 static void EvaluateDict(JSONWriter* aWriter,
40 NSDictionary<NSString*, id>* aDict) {
41 for (NSString* key in aDict) {
42 id value = aDict[key];
43 if ([value isKindOfClass:[NSString class]]) {
44 aWriter->StringProperty(MakeStringSpan([key UTF8String]),
45 MakeStringSpan([value UTF8String]));
46 } else if ([value isKindOfClass:[NSNumber class]]) {
47 aWriter->IntProperty(MakeStringSpan([key UTF8String]),
48 [value longLongValue]);
49 } else if ([value isKindOfClass:[NSArray class]]) {
50 aWriter->StartArrayProperty(MakeStringSpan([key UTF8String]));
51 EvaluateArray(aWriter, value);
53 } else if ([value isKindOfClass:[NSDictionary class]]) {
54 aWriter->StartObjectProperty(MakeStringSpan([key UTF8String]));
55 EvaluateDict(aWriter, value);
62 nsMacPreferencesReader::PoliciesEnabled(bool* aPoliciesEnabled) {
63 NSString* policiesEnabledStr =
64 [NSString stringWithUTF8String:ENTERPRISE_POLICIES_ENABLED_KEY];
65 *aPoliciesEnabled = [[NSUserDefaults standardUserDefaults]
66 boolForKey:policiesEnabledStr] == YES;
71 nsMacPreferencesReader::ReadPreferences(JSContext* aCx,
72 JS::MutableHandle<JS::Value> aResult) {
73 JSONStringWriteFunc<nsAutoCString> jsonStr;
74 JSONWriter w(jsonStr);
77 &w, [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
80 NS_ConvertUTF8toUTF16 jsonStr16(jsonStr.StringCRef());
82 JS::RootedValue val(aCx);
83 MOZ_ALWAYS_TRUE(JS_ParseJSON(aCx, jsonStr16.get(), jsonStr16.Length(), &val));