LCOV - app.info - xpcom/build/BinaryPath.h

LCOV - code coverage report
| Current view: | directory - xpcom/build - BinaryPath.h (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 22 | 4 | 18.2 %
| Date: | 2012-04-07 | Functions: | 1 | 1 | 100.0 %

           1                 : /* ***** BEGIN LICENSE BLOCK *****
           2                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
           3                 :  *
           4                 :  * The contents of this file are subject to the Mozilla Public License Version
           5                 :  * 1.1 (the "License"); you may not use this file except in compliance with
           6                 :  * the License. You may obtain a copy of the License at
           7                 :  * http://www.mozilla.org/MPL/
           8                 :  *
           9                 :  * Software distributed under the License is distributed on an "AS IS" basis,
          10                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
          11                 :  * for the specific language governing rights and limitations under the
          12                 :  * License.
          13                 :  *
          14                 :  * The Original Code is mozilla.org code
          15                 :  *
          16                 :  * The Initial Developer of the Original Code is
          17                 :  * Mozilla Foundation.
          18                 :  * Portions created by the Initial Developer are Copyright (C) 2011
          19                 :  * the Initial Developer. All Rights Reserved.
          20                 :  *
          21                 :  * Contributor(s):
          22                 :  *   Mike Hommey <mh@glandium.org>
          23                 :  *
          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.
          35                 :  *
          36                 :  * ***** END LICENSE BLOCK ***** */
          37                 :
          38                 : #ifndef mozilla_BinaryPath_h
          39                 : #define mozilla_BinaryPath_h
          40                 :
          41                 : #include "nsXPCOMPrivate.h" // for MAXPATHLEN
          42                 : #include "prtypes.h"
          43                 : #ifdef XP_WIN
          44                 : #include <windows.h>
          45                 : #elif defined(XP_MACOSX)
          46                 : #include <CoreFoundation/CoreFoundation.h>
          47                 : #elif defined(XP_UNIX)
          48                 : #include <sys/stat.h>
          49                 : #include <string.h>
          50                 : #endif
          51                 :
          52                 : namespace mozilla {
          53                 :
          54                 : class BinaryPath {
          55                 : public:
          56                 : #ifdef XP_WIN
          57                 :   static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
          58                 :   {
          59                 :     PRUnichar wide_path[MAXPATHLEN];
          60                 :     nsresult rv = GetW(argv0, wide_path);
          61                 :     if (NS_FAILED(rv))
          62                 :       return rv;
          63                 :     WideCharToMultiByte(CP_UTF8, 0, wide_path, -1,
          64                 :                         aResult, MAXPATHLEN, NULL, NULL);
          65                 :     return NS_OK;
          66                 :   }
          67                 :
          68                 : private:
          69                 :   static nsresult GetW(const char *argv0, PRUnichar aResult[MAXPATHLEN])
          70                 :   {
          71                 :     if (::GetModuleFileNameW(0, aResult, MAXPATHLEN))
          72                 :       return NS_OK;
          73                 :     return NS_ERROR_FAILURE;
          74                 :   }
          75                 :
          76                 : #elif defined(XP_MACOSX)
          77                 :   static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
          78                 :   {
          79                 :     // Works even if we're not bundled.
          80                 :     CFBundleRef appBundle = CFBundleGetMainBundle();
          81                 :     if (!appBundle)
          82                 :       return NS_ERROR_FAILURE;
          83                 :
          84                 :     CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
          85                 :     if (!executableURL)
          86                 :       return NS_ERROR_FAILURE;
          87                 :
          88                 :     nsresult rv;
          89                 :     if (CFURLGetFileSystemRepresentation(executableURL, false, (UInt8 *)aResult, MAXPATHLEN))
          90                 :       rv = NS_OK;
          91                 :     else
          92                 :       rv = NS_ERROR_FAILURE;
          93                 :     CFRelease(executableURL);
          94                 :     return rv;
          95                 :   }
          96                 :
          97                 : #elif defined(ANDROID)
          98                 :   static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
          99                 :   {
         100                 :     // On Android, we use the GRE_HOME variable that is set by the Java
         101                 :     // bootstrap code.
         102                 :     const char *greHome = getenv("GRE_HOME");
         103                 : #if defined(MOZ_WIDGET_GONK)
         104                 :     if (!greHome)
         105                 :       greHome = "/system/b2g";
         106                 : #endif
         107                 :
         108                 :     if (!greHome)
         109                 :       return NS_ERROR_FAILURE;
         110                 :
         111                 :     snprintf(aResult, MAXPATHLEN, "%s/%s", greHome, "dummy");
         112                 :     aResult[MAXPATHLEN] = '\0';
         113                 :     return NS_OK;
         114                 :   }
         115                 :
         116                 : #elif defined(XP_UNIX)
         117               2 :   static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
         118                 :   {
         119                 :     struct stat fileStat;
         120                 :     // on unix, there is no official way to get the path of the current binary.
         121                 :     // instead of using the MOZILLA_FIVE_HOME hack, which doesn't scale to
         122                 :     // multiple applications, we will try a series of techniques:
         123                 :     //
         124                 :     // 1) use realpath() on argv[0], which works unless we're loaded from the
         125                 :     //    PATH. Only do so if argv[0] looks like a path (contains a /).
         126                 :     // 2) manually walk through the PATH and look for ourself
         127                 :     // 3) give up
         128               4 :     if (strchr(argv0, '/') && realpath(argv0, aResult) &&
         129               2 :         stat(aResult, &fileStat) == 0)
         130               2 :       return NS_OK;
         131                 :
         132               0 :     const char *path = getenv("PATH");
         133               0 :     if (!path)
         134               0 :       return NS_ERROR_FAILURE;
         135                 :
         136               0 :     char *pathdup = strdup(path);
         137               0 :     if (!pathdup)
         138               0 :       return NS_ERROR_OUT_OF_MEMORY;
         139                 :
         140               0 :     bool found = false;
         141               0 :     char *token = strtok(pathdup, ":");
         142               0 :     while (token) {
         143                 :       char tmpPath[MAXPATHLEN];
         144               0 :       sprintf(tmpPath, "%s/%s", token, argv0);
         145               0 :       if (realpath(tmpPath, aResult) && stat(aResult, &fileStat) == 0) {
         146               0 :         found = true;
         147               0 :         break;
         148                 :       }
         149               0 :       token = strtok(NULL, ":");
         150                 :     }
         151               0 :     free(pathdup);
         152               0 :     if (found)
         153               0 :       return NS_OK;
         154               0 :     return NS_ERROR_FAILURE;
         155                 :   }
         156                 :
         157                 : #elif defined(XP_OS2)
         158                 :   static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
         159                 :   {
         160                 :     PPIB ppib;
         161                 :     PTIB ptib;
         162                 :     DosGetInfoBlocks( &ptib, &ppib);
         163                 :     DosQueryModuleName(ppib->pib_hmte, MAXPATHLEN, aResult);
         164                 :   }
         165                 :
         166                 : #else
         167                 : #error Oops, you need platform-specific code here
         168                 : #endif
         169                 :
         170                 : public:
         171                 :   static nsresult GetFile(const char *argv0, nsILocalFile* *aResult)
         172                 :   {
         173                 :     nsCOMPtr<nsILocalFile> lf;
         174                 : #ifdef XP_WIN
         175                 :     PRUnichar exePath[MAXPATHLEN];
         176                 :     nsresult rv = GetW(argv0, exePath);
         177                 : #else
         178                 :     char exePath[MAXPATHLEN];
         179                 :     nsresult rv = Get(argv0, exePath);
         180                 : #endif
         181                 :     if (NS_FAILED(rv))
         182                 :       return rv;
         183                 : #ifdef XP_WIN
         184                 :     rv = NS_NewLocalFile(nsDependentString(exePath), true,
         185                 :                          getter_AddRefs(lf));
         186                 : #else
         187                 :     rv = NS_NewNativeLocalFile(nsDependentCString(exePath), true,
         188                 :                                getter_AddRefs(lf));
         189                 : #endif
         190                 :     if (NS_FAILED(rv))
         191                 :       return rv;
         192                 :     NS_ADDREF(*aResult = lf);
         193                 :     return NS_OK;
         194                 :   }
         195                 : };
         196                 :
         197                 : }
         198                 :
         199                 : #endif /* mozilla_BinaryPath_h */

Generated by: LCOV version 1.7