|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "nsMacUtilsImpl.h" |
|
7 |
|
8 #include <CoreFoundation/CoreFoundation.h> |
|
9 |
|
10 NS_IMPL_ISUPPORTS(nsMacUtilsImpl, nsIMacUtils) |
|
11 |
|
12 nsresult nsMacUtilsImpl::GetArchString(nsAString& archString) |
|
13 { |
|
14 if (!mBinaryArchs.IsEmpty()) { |
|
15 archString.Assign(mBinaryArchs); |
|
16 return NS_OK; |
|
17 } |
|
18 |
|
19 archString.Truncate(); |
|
20 |
|
21 bool foundPPC = false, |
|
22 foundX86 = false, |
|
23 foundPPC64 = false, |
|
24 foundX86_64 = false; |
|
25 |
|
26 CFBundleRef mainBundle = ::CFBundleGetMainBundle(); |
|
27 if (!mainBundle) { |
|
28 return NS_ERROR_FAILURE; |
|
29 } |
|
30 |
|
31 CFArrayRef archList = ::CFBundleCopyExecutableArchitectures(mainBundle); |
|
32 if (!archList) { |
|
33 return NS_ERROR_FAILURE; |
|
34 } |
|
35 |
|
36 CFIndex archCount = ::CFArrayGetCount(archList); |
|
37 for (CFIndex i = 0; i < archCount; i++) { |
|
38 CFNumberRef arch = static_cast<CFNumberRef>(::CFArrayGetValueAtIndex(archList, i)); |
|
39 |
|
40 int archInt = 0; |
|
41 if (!::CFNumberGetValue(arch, kCFNumberIntType, &archInt)) { |
|
42 ::CFRelease(archList); |
|
43 return NS_ERROR_FAILURE; |
|
44 } |
|
45 |
|
46 if (archInt == kCFBundleExecutableArchitecturePPC) |
|
47 foundPPC = true; |
|
48 else if (archInt == kCFBundleExecutableArchitectureI386) |
|
49 foundX86 = true; |
|
50 else if (archInt == kCFBundleExecutableArchitecturePPC64) |
|
51 foundPPC64 = true; |
|
52 else if (archInt == kCFBundleExecutableArchitectureX86_64) |
|
53 foundX86_64 = true; |
|
54 } |
|
55 |
|
56 ::CFRelease(archList); |
|
57 |
|
58 // The order in the string must always be the same so |
|
59 // don't do this in the loop. |
|
60 if (foundPPC) { |
|
61 mBinaryArchs.Append(NS_LITERAL_STRING("ppc")); |
|
62 } |
|
63 |
|
64 if (foundX86) { |
|
65 if (!mBinaryArchs.IsEmpty()) { |
|
66 mBinaryArchs.Append(NS_LITERAL_STRING("-")); |
|
67 } |
|
68 mBinaryArchs.Append(NS_LITERAL_STRING("i386")); |
|
69 } |
|
70 |
|
71 if (foundPPC64) { |
|
72 if (!mBinaryArchs.IsEmpty()) { |
|
73 mBinaryArchs.Append(NS_LITERAL_STRING("-")); |
|
74 } |
|
75 mBinaryArchs.Append(NS_LITERAL_STRING("ppc64")); |
|
76 } |
|
77 |
|
78 if (foundX86_64) { |
|
79 if (!mBinaryArchs.IsEmpty()) { |
|
80 mBinaryArchs.Append(NS_LITERAL_STRING("-")); |
|
81 } |
|
82 mBinaryArchs.Append(NS_LITERAL_STRING("x86_64")); |
|
83 } |
|
84 |
|
85 archString.Assign(mBinaryArchs); |
|
86 |
|
87 return (archString.IsEmpty() ? NS_ERROR_FAILURE : NS_OK); |
|
88 } |
|
89 |
|
90 NS_IMETHODIMP nsMacUtilsImpl::GetIsUniversalBinary(bool *aIsUniversalBinary) |
|
91 { |
|
92 if (NS_WARN_IF(!aIsUniversalBinary)) |
|
93 return NS_ERROR_INVALID_ARG; |
|
94 *aIsUniversalBinary = false; |
|
95 |
|
96 nsAutoString archString; |
|
97 nsresult rv = GetArchString(archString); |
|
98 if (NS_FAILED(rv)) |
|
99 return rv; |
|
100 |
|
101 // The delimiter char in the arch string is '-', so if that character |
|
102 // is in the string we know we have multiple architectures. |
|
103 *aIsUniversalBinary = (archString.Find("-") > -1); |
|
104 |
|
105 return NS_OK; |
|
106 } |
|
107 |
|
108 NS_IMETHODIMP nsMacUtilsImpl::GetArchitecturesInBinary(nsAString& archString) |
|
109 { |
|
110 return GetArchString(archString); |
|
111 } |
|
112 |
|
113 /* readonly attribute boolean isTranslated; */ |
|
114 // True when running under binary translation (Rosetta). |
|
115 NS_IMETHODIMP nsMacUtilsImpl::GetIsTranslated(bool *aIsTranslated) |
|
116 { |
|
117 #ifdef __ppc__ |
|
118 static bool sInitialized = false; |
|
119 |
|
120 // Initialize sIsNative to 1. If the sysctl fails because it doesn't |
|
121 // exist, then translation is not possible, so the process must not be |
|
122 // running translated. |
|
123 static int32_t sIsNative = 1; |
|
124 |
|
125 if (!sInitialized) { |
|
126 size_t sz = sizeof(sIsNative); |
|
127 sysctlbyname("sysctl.proc_native", &sIsNative, &sz, nullptr, 0); |
|
128 sInitialized = true; |
|
129 } |
|
130 |
|
131 *aIsTranslated = !sIsNative; |
|
132 #else |
|
133 // Translation only exists for ppc code. Other architectures aren't |
|
134 // translated. |
|
135 *aIsTranslated = false; |
|
136 #endif |
|
137 |
|
138 return NS_OK; |
|
139 } |