Bug 1919083 - [ci] Enable os-integration variant for more suites, r=jmaher
[gecko.git] / xpcom / base / nsMacPreferencesReader.mm
blobbfec81cd85427c615046a2e8560ab796de8f41fa
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"
7 #include "nsString.h"
9 #include "js/JSON.h"
10 #include "js/RootingAPI.h"
11 #include "js/Value.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);
30       aWriter->EndArray();
31     } else if ([elem isKindOfClass:[NSDictionary class]]) {
32       aWriter->StartObjectElement();
33       EvaluateDict(aWriter, elem);
34       aWriter->EndObject();
35     }
36   }
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);
52       aWriter->EndArray();
53     } else if ([value isKindOfClass:[NSDictionary class]]) {
54       aWriter->StartObjectProperty(MakeStringSpan([key UTF8String]));
55       EvaluateDict(aWriter, value);
56       aWriter->EndObject();
57     }
58   }
61 NS_IMETHODIMP
62 nsMacPreferencesReader::PoliciesEnabled(bool* aPoliciesEnabled) {
63   NSString* policiesEnabledStr =
64       [NSString stringWithUTF8String:ENTERPRISE_POLICIES_ENABLED_KEY];
65   *aPoliciesEnabled = [[NSUserDefaults standardUserDefaults]
66                           boolForKey:policiesEnabledStr] == YES;
67   return NS_OK;
70 NS_IMETHODIMP
71 nsMacPreferencesReader::ReadPreferences(JSContext* aCx,
72                                         JS::MutableHandle<JS::Value> aResult) {
73   JSONStringWriteFunc<nsAutoCString> jsonStr;
74   JSONWriter w(jsonStr);
75   w.Start();
76   EvaluateDict(
77       &w, [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
78   w.End();
80   NS_ConvertUTF8toUTF16 jsonStr16(jsonStr.StringCRef());
82   JS::RootedValue val(aCx);
83   MOZ_ALWAYS_TRUE(JS_ParseJSON(aCx, jsonStr16.get(), jsonStr16.Length(), &val));
85   aResult.set(val);
86   return NS_OK;