Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
6 #include "nsMacUtilsImpl.h"
8 #include <CoreFoundation/CoreFoundation.h>
10 NS_IMPL_ISUPPORTS(nsMacUtilsImpl, nsIMacUtils)
12 nsresult nsMacUtilsImpl::GetArchString(nsAString& archString)
13 {
14 if (!mBinaryArchs.IsEmpty()) {
15 archString.Assign(mBinaryArchs);
16 return NS_OK;
17 }
19 archString.Truncate();
21 bool foundPPC = false,
22 foundX86 = false,
23 foundPPC64 = false,
24 foundX86_64 = false;
26 CFBundleRef mainBundle = ::CFBundleGetMainBundle();
27 if (!mainBundle) {
28 return NS_ERROR_FAILURE;
29 }
31 CFArrayRef archList = ::CFBundleCopyExecutableArchitectures(mainBundle);
32 if (!archList) {
33 return NS_ERROR_FAILURE;
34 }
36 CFIndex archCount = ::CFArrayGetCount(archList);
37 for (CFIndex i = 0; i < archCount; i++) {
38 CFNumberRef arch = static_cast<CFNumberRef>(::CFArrayGetValueAtIndex(archList, i));
40 int archInt = 0;
41 if (!::CFNumberGetValue(arch, kCFNumberIntType, &archInt)) {
42 ::CFRelease(archList);
43 return NS_ERROR_FAILURE;
44 }
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 }
56 ::CFRelease(archList);
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 }
64 if (foundX86) {
65 if (!mBinaryArchs.IsEmpty()) {
66 mBinaryArchs.Append(NS_LITERAL_STRING("-"));
67 }
68 mBinaryArchs.Append(NS_LITERAL_STRING("i386"));
69 }
71 if (foundPPC64) {
72 if (!mBinaryArchs.IsEmpty()) {
73 mBinaryArchs.Append(NS_LITERAL_STRING("-"));
74 }
75 mBinaryArchs.Append(NS_LITERAL_STRING("ppc64"));
76 }
78 if (foundX86_64) {
79 if (!mBinaryArchs.IsEmpty()) {
80 mBinaryArchs.Append(NS_LITERAL_STRING("-"));
81 }
82 mBinaryArchs.Append(NS_LITERAL_STRING("x86_64"));
83 }
85 archString.Assign(mBinaryArchs);
87 return (archString.IsEmpty() ? NS_ERROR_FAILURE : NS_OK);
88 }
90 NS_IMETHODIMP nsMacUtilsImpl::GetIsUniversalBinary(bool *aIsUniversalBinary)
91 {
92 if (NS_WARN_IF(!aIsUniversalBinary))
93 return NS_ERROR_INVALID_ARG;
94 *aIsUniversalBinary = false;
96 nsAutoString archString;
97 nsresult rv = GetArchString(archString);
98 if (NS_FAILED(rv))
99 return rv;
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);
105 return NS_OK;
106 }
108 NS_IMETHODIMP nsMacUtilsImpl::GetArchitecturesInBinary(nsAString& archString)
109 {
110 return GetArchString(archString);
111 }
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;
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;
125 if (!sInitialized) {
126 size_t sz = sizeof(sIsNative);
127 sysctlbyname("sysctl.proc_native", &sIsNative, &sz, nullptr, 0);
128 sInitialized = true;
129 }
131 *aIsTranslated = !sIsNative;
132 #else
133 // Translation only exists for ppc code. Other architectures aren't
134 // translated.
135 *aIsTranslated = false;
136 #endif
138 return NS_OK;
139 }