1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef nsMacUtilsImpl_h___
8 #define nsMacUtilsImpl_h___
9 
10 #include "nsIMacUtils.h"
11 #include "nsString.h"
12 #include "mozilla/Atomics.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/StaticMutex.h"
15 #include "mozilla/StaticPtr.h"
16 
17 using mozilla::Atomic;
18 using mozilla::StaticAutoPtr;
19 using mozilla::StaticMutex;
20 
21 class nsMacUtilsImpl final : public nsIMacUtils {
22  public:
23   NS_DECL_ISUPPORTS
24   NS_DECL_NSIMACUTILS
25 
nsMacUtilsImpl()26   nsMacUtilsImpl() {}
27 
28   // Return the repo directory and the repo object directory respectively.
29   // These should only be used on Mac developer builds to determine the path
30   // to the repo or object directory.
31   static nsresult GetRepoDir(nsIFile** aRepoDir);
32   static nsresult GetObjDir(nsIFile** aObjDir);
33 
34 #if defined(MOZ_SANDBOX) || defined(__aarch64__)
35   static bool GetAppPath(nsCString& aAppPath);
36 #endif /* MOZ_SANDBOX || __aarch64__ */
37 
38 #if defined(MOZ_SANDBOX) && defined(DEBUG)
39   static nsresult GetBloatLogDir(nsCString& aDirectoryPath);
40   static nsresult GetDirectoryPath(const char* aPath,
41                                    nsCString& aDirectoryPath);
42 #endif /* MOZ_SANDBOX && DEBUG */
43 
44   static void EnableTCSMIfAvailable();
45   static bool IsTCSMAvailable();
46   static uint32_t GetPhysicalCPUCount();
47   static nsresult GetArchitecturesForBundle(uint32_t* aArchMask);
48   static nsresult GetArchitecturesForBinary(const char* aPath,
49                                             uint32_t* aArchMask);
50 
51 #if defined(__aarch64__)
52   // Pre-translate binaries to avoid translation delays when launching
53   // x64 child process instances for the first time. i.e. on first launch
54   // after installation or after an update. Translations are cached so
55   // repeated launches of the binaries do not encounter delays.
56   static int PreTranslateXUL();
57   static int PreTranslateBinary(nsCString aBinaryPath);
58 #endif
59 
60  private:
~nsMacUtilsImpl()61   ~nsMacUtilsImpl() {}
62 
63   nsresult GetArchString(nsAString& aArchString);
64 
65   // A string containing a "-" delimited list of architectures
66   // in our binary.
67   nsString mBinaryArchs;
68 
69 #if defined(MOZ_SANDBOX) || defined(__aarch64__)
70   // Cache the appDir returned from GetAppPath to avoid doing I/O
71   static StaticAutoPtr<nsCString> sCachedAppPath;
72   // For thread safe setting/checking of sCachedAppPath
73   static StaticMutex sCachedAppPathMutex;
74   // Utility method to call ClearOnShutdown() on the main thread
75   static nsresult ClearCachedAppPathOnShutdown();
76 #endif
77 
78   // The cached machine architectures of the .app bundle which can
79   // be multiple architectures for universal binaries.
80   static std::atomic<uint32_t> sBundleArchMaskAtomic;
81 
82 #if defined(__aarch64__)
83   // Limit XUL translation to one attempt
84   static std::atomic<bool> sIsXULTranslated;
85 #endif
86 
87   enum TCSMStatus { TCSM_Unknown = 0, TCSM_Available, TCSM_Unavailable };
88   static mozilla::Atomic<nsMacUtilsImpl::TCSMStatus> sTCSMStatus;
89 
90   static nsresult EnableTCSM();
91 #if defined(DEBUG)
92   static bool IsTCSMEnabled();
93 #endif
94 };
95 
96 // Global singleton service
97 // 697BD3FD-43E5-41CE-AD5E-C339175C0818
98 #define NS_MACUTILSIMPL_CID                          \
99   {                                                  \
100     0x697BD3FD, 0x43E5, 0x41CE, {                    \
101       0xAD, 0x5E, 0xC3, 0x39, 0x17, 0x5C, 0x08, 0x18 \
102     }                                                \
103   }
104 #define NS_MACUTILSIMPL_CONTRACTID "@mozilla.org/xpcom/mac-utils;1"
105 
106 #endif /* nsMacUtilsImpl_h___ */
107