1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is XPCOM utility functions for Mac OS X
17 * The Initial Developer of the Original Code is Google Inc.
18 * Portions created by the Initial Developer are Copyright (C) 2006
19 * the Initial Developer. All Rights Reserved.
22 * Mark Mentovai <mark@moxienet.com> (Original Author)
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsMacUtilsImpl.h"
40 #include <CoreFoundation/CoreFoundation.h>
41 #include <sys/sysctl.h>
43 NS_IMPL_ISUPPORTS1(nsMacUtilsImpl
, nsIMacUtils
)
45 nsresult
nsMacUtilsImpl::GetArchString(nsAString
& archString
)
47 if (!mBinaryArchs
.IsEmpty()) {
48 archString
.Assign(mBinaryArchs
);
52 archString
.Truncate();
54 PRBool foundPPC
= PR_FALSE
,
56 foundPPC64
= PR_FALSE
,
57 foundX86_64
= PR_FALSE
;
59 CFBundleRef mainBundle
= ::CFBundleGetMainBundle();
61 return NS_ERROR_FAILURE
;
64 CFArrayRef archList
= ::CFBundleCopyExecutableArchitectures(mainBundle
);
66 return NS_ERROR_FAILURE
;
69 CFIndex archCount
= ::CFArrayGetCount(archList
);
70 for (CFIndex i
= 0; i
< archCount
; i
++) {
71 CFNumberRef arch
= static_cast<CFNumberRef
>(::CFArrayGetValueAtIndex(archList
, i
));
74 if (!::CFNumberGetValue(arch
, kCFNumberIntType
, &archInt
)) {
75 ::CFRelease(archList
);
76 return NS_ERROR_FAILURE
;
79 if (archInt
== kCFBundleExecutableArchitecturePPC
)
81 else if (archInt
== kCFBundleExecutableArchitectureI386
)
83 else if (archInt
== kCFBundleExecutableArchitecturePPC64
)
85 else if (archInt
== kCFBundleExecutableArchitectureX86_64
)
86 foundX86_64
= PR_TRUE
;
89 ::CFRelease(archList
);
91 // The order in the string must always be the same so
92 // don't do this in the loop.
94 mBinaryArchs
.Append(NS_LITERAL_STRING("ppc"));
98 if (!mBinaryArchs
.IsEmpty()) {
99 mBinaryArchs
.Append(NS_LITERAL_STRING("-"));
101 mBinaryArchs
.Append(NS_LITERAL_STRING("i386"));
105 if (!mBinaryArchs
.IsEmpty()) {
106 mBinaryArchs
.Append(NS_LITERAL_STRING("-"));
108 mBinaryArchs
.Append(NS_LITERAL_STRING("ppc64"));
112 if (!mBinaryArchs
.IsEmpty()) {
113 mBinaryArchs
.Append(NS_LITERAL_STRING("-"));
115 mBinaryArchs
.Append(NS_LITERAL_STRING("x86_64"));
118 archString
.Assign(mBinaryArchs
);
120 return (archString
.IsEmpty() ? NS_ERROR_FAILURE
: NS_OK
);
123 NS_IMETHODIMP
nsMacUtilsImpl::GetIsUniversalBinary(PRBool
*aIsUniversalBinary
)
125 NS_ENSURE_ARG_POINTER(aIsUniversalBinary
);
126 *aIsUniversalBinary
= PR_FALSE
;
128 nsAutoString archString
;
129 nsresult rv
= GetArchString(archString
);
133 // The delimiter char in the arch string is '-', so if that character
134 // is in the string we know we have multiple architectures.
135 *aIsUniversalBinary
= (archString
.Find("-") > -1);
140 NS_IMETHODIMP
nsMacUtilsImpl::GetArchitecturesInBinary(nsAString
& archString
)
142 return GetArchString(archString
);
145 /* readonly attribute boolean isTranslated; */
146 // True when running under binary translation (Rosetta).
147 NS_IMETHODIMP
nsMacUtilsImpl::GetIsTranslated(PRBool
*aIsTranslated
)
150 static PRBool sInitialized
= PR_FALSE
;
152 // Initialize sIsNative to 1. If the sysctl fails because it doesn't
153 // exist, then translation is not possible, so the process must not be
154 // running translated.
155 static PRInt32 sIsNative
= 1;
158 size_t sz
= sizeof(sIsNative
);
159 sysctlbyname("sysctl.proc_native", &sIsNative
, &sz
, NULL
, 0);
160 sInitialized
= PR_TRUE
;
163 *aIsTranslated
= !sIsNative
;
165 // Translation only exists for ppc code. Other architectures aren't
167 *aIsTranslated
= PR_FALSE
;