xpcom/build/BinaryPath.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #ifndef mozilla_BinaryPath_h
michael@0 6 #define mozilla_BinaryPath_h
michael@0 7
michael@0 8 #include "nsXPCOMPrivate.h" // for MAXPATHLEN
michael@0 9 #ifdef XP_WIN
michael@0 10 #include <windows.h>
michael@0 11 #elif defined(XP_MACOSX)
michael@0 12 #include <CoreFoundation/CoreFoundation.h>
michael@0 13 #elif defined(XP_UNIX)
michael@0 14 #include <sys/stat.h>
michael@0 15 #include <string.h>
michael@0 16 #endif
michael@0 17
michael@0 18 namespace mozilla {
michael@0 19
michael@0 20 class BinaryPath {
michael@0 21 public:
michael@0 22 #ifdef XP_WIN
michael@0 23 static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
michael@0 24 {
michael@0 25 wchar_t wide_path[MAXPATHLEN];
michael@0 26 nsresult rv = GetW(argv0, wide_path);
michael@0 27 if (NS_FAILED(rv))
michael@0 28 return rv;
michael@0 29 WideCharToMultiByte(CP_UTF8, 0, wide_path, -1,
michael@0 30 aResult, MAXPATHLEN, nullptr, nullptr);
michael@0 31 return NS_OK;
michael@0 32 }
michael@0 33
michael@0 34 private:
michael@0 35 static nsresult GetW(const char *argv0, wchar_t aResult[MAXPATHLEN])
michael@0 36 {
michael@0 37 if (::GetModuleFileNameW(0, aResult, MAXPATHLEN))
michael@0 38 return NS_OK;
michael@0 39 return NS_ERROR_FAILURE;
michael@0 40 }
michael@0 41
michael@0 42 #elif defined(XP_MACOSX)
michael@0 43 static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
michael@0 44 {
michael@0 45 // Works even if we're not bundled.
michael@0 46 CFBundleRef appBundle = CFBundleGetMainBundle();
michael@0 47 if (!appBundle)
michael@0 48 return NS_ERROR_FAILURE;
michael@0 49
michael@0 50 CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
michael@0 51 if (!executableURL)
michael@0 52 return NS_ERROR_FAILURE;
michael@0 53
michael@0 54 nsresult rv;
michael@0 55 if (CFURLGetFileSystemRepresentation(executableURL, false, (UInt8 *)aResult, MAXPATHLEN))
michael@0 56 rv = NS_OK;
michael@0 57 else
michael@0 58 rv = NS_ERROR_FAILURE;
michael@0 59 CFRelease(executableURL);
michael@0 60 return rv;
michael@0 61 }
michael@0 62
michael@0 63 #elif defined(ANDROID)
michael@0 64 static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
michael@0 65 {
michael@0 66 // On Android, we use the GRE_HOME variable that is set by the Java
michael@0 67 // bootstrap code.
michael@0 68 const char *greHome = getenv("GRE_HOME");
michael@0 69 #if defined(MOZ_WIDGET_GONK)
michael@0 70 if (!greHome)
michael@0 71 greHome = "/system/b2g";
michael@0 72 #endif
michael@0 73
michael@0 74 if (!greHome)
michael@0 75 return NS_ERROR_FAILURE;
michael@0 76
michael@0 77 snprintf(aResult, MAXPATHLEN, "%s/%s", greHome, "dummy");
michael@0 78 aResult[MAXPATHLEN-1] = '\0';
michael@0 79 return NS_OK;
michael@0 80 }
michael@0 81
michael@0 82 #elif defined(XP_UNIX)
michael@0 83 static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
michael@0 84 {
michael@0 85 struct stat fileStat;
michael@0 86 // on unix, there is no official way to get the path of the current binary.
michael@0 87 // instead of using the MOZILLA_FIVE_HOME hack, which doesn't scale to
michael@0 88 // multiple applications, we will try a series of techniques:
michael@0 89 //
michael@0 90 // 1) use realpath() on argv[0], which works unless we're loaded from the
michael@0 91 // PATH. Only do so if argv[0] looks like a path (contains a /).
michael@0 92 // 2) manually walk through the PATH and look for ourself
michael@0 93 // 3) give up
michael@0 94 if (strchr(argv0, '/') && realpath(argv0, aResult) &&
michael@0 95 stat(aResult, &fileStat) == 0)
michael@0 96 return NS_OK;
michael@0 97
michael@0 98 const char *path = getenv("PATH");
michael@0 99 if (!path)
michael@0 100 return NS_ERROR_FAILURE;
michael@0 101
michael@0 102 char *pathdup = strdup(path);
michael@0 103 if (!pathdup)
michael@0 104 return NS_ERROR_OUT_OF_MEMORY;
michael@0 105
michael@0 106 bool found = false;
michael@0 107 char *token = strtok(pathdup, ":");
michael@0 108 while (token) {
michael@0 109 char tmpPath[MAXPATHLEN];
michael@0 110 sprintf(tmpPath, "%s/%s", token, argv0);
michael@0 111 if (realpath(tmpPath, aResult) && stat(aResult, &fileStat) == 0) {
michael@0 112 found = true;
michael@0 113 break;
michael@0 114 }
michael@0 115 token = strtok(nullptr, ":");
michael@0 116 }
michael@0 117 free(pathdup);
michael@0 118 if (found)
michael@0 119 return NS_OK;
michael@0 120 return NS_ERROR_FAILURE;
michael@0 121 }
michael@0 122
michael@0 123 #else
michael@0 124 #error Oops, you need platform-specific code here
michael@0 125 #endif
michael@0 126
michael@0 127 public:
michael@0 128 static nsresult GetFile(const char *argv0, nsIFile* *aResult)
michael@0 129 {
michael@0 130 nsCOMPtr<nsIFile> lf;
michael@0 131 #ifdef XP_WIN
michael@0 132 wchar_t exePath[MAXPATHLEN];
michael@0 133 nsresult rv = GetW(argv0, exePath);
michael@0 134 #else
michael@0 135 char exePath[MAXPATHLEN];
michael@0 136 nsresult rv = Get(argv0, exePath);
michael@0 137 #endif
michael@0 138 if (NS_FAILED(rv))
michael@0 139 return rv;
michael@0 140 #ifdef XP_WIN
michael@0 141 rv = NS_NewLocalFile(nsDependentString(exePath), true,
michael@0 142 getter_AddRefs(lf));
michael@0 143 #else
michael@0 144 rv = NS_NewNativeLocalFile(nsDependentCString(exePath), true,
michael@0 145 getter_AddRefs(lf));
michael@0 146 #endif
michael@0 147 if (NS_FAILED(rv))
michael@0 148 return rv;
michael@0 149 NS_ADDREF(*aResult = lf);
michael@0 150 return NS_OK;
michael@0 151 }
michael@0 152 };
michael@0 153
michael@0 154 }
michael@0 155
michael@0 156 #endif /* mozilla_BinaryPath_h */

mercurial