Bug 575870 - Enable the firefox button on xp themed, classic, and aero basic. r=dao...
[mozilla-central.git] / xpcom / base / nsMacUtilsImpl.cpp
blob5c6ec3e4b5a51948d2089d0faf8e641185820aca
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
13 * License.
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.
21 * Contributor(s):
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);
49 return NS_OK;
52 archString.Truncate();
54 PRBool foundPPC = PR_FALSE,
55 foundX86 = PR_FALSE,
56 foundPPC64 = PR_FALSE,
57 foundX86_64 = PR_FALSE;
59 CFBundleRef mainBundle = ::CFBundleGetMainBundle();
60 if (!mainBundle) {
61 return NS_ERROR_FAILURE;
64 CFArrayRef archList = ::CFBundleCopyExecutableArchitectures(mainBundle);
65 if (!archList) {
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));
73 int archInt = 0;
74 if (!::CFNumberGetValue(arch, kCFNumberIntType, &archInt)) {
75 ::CFRelease(archList);
76 return NS_ERROR_FAILURE;
79 if (archInt == kCFBundleExecutableArchitecturePPC)
80 foundPPC = PR_TRUE;
81 else if (archInt == kCFBundleExecutableArchitectureI386)
82 foundX86 = PR_TRUE;
83 else if (archInt == kCFBundleExecutableArchitecturePPC64)
84 foundPPC64 = PR_TRUE;
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.
93 if (foundPPC) {
94 mBinaryArchs.Append(NS_LITERAL_STRING("ppc"));
97 if (foundX86) {
98 if (!mBinaryArchs.IsEmpty()) {
99 mBinaryArchs.Append(NS_LITERAL_STRING("-"));
101 mBinaryArchs.Append(NS_LITERAL_STRING("i386"));
104 if (foundPPC64) {
105 if (!mBinaryArchs.IsEmpty()) {
106 mBinaryArchs.Append(NS_LITERAL_STRING("-"));
108 mBinaryArchs.Append(NS_LITERAL_STRING("ppc64"));
111 if (foundX86_64) {
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);
130 if (NS_FAILED(rv))
131 return rv;
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);
137 return NS_OK;
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)
149 #ifdef __ppc__
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;
157 if (!sInitialized) {
158 size_t sz = sizeof(sIsNative);
159 sysctlbyname("sysctl.proc_native", &sIsNative, &sz, NULL, 0);
160 sInitialized = PR_TRUE;
163 *aIsTranslated = !sIsNative;
164 #else
165 // Translation only exists for ppc code. Other architectures aren't
166 // translated.
167 *aIsTranslated = PR_FALSE;
168 #endif
170 return NS_OK;