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