1 /* 2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 34 #ifndef PLATFORM_H 35 #define PLATFORM_H 36 37 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <memory.h> 41 #include <string> 42 #include <map> 43 #include <list> 44 #include <vector> 45 46 47 #ifdef WIN32 48 #define WINDOWS 49 #endif //WIN32 50 //TODO Win64? 51 52 #ifdef __APPLE__ 53 #define MAC 54 #define POSIX 55 #endif //__APPLE__ 56 57 58 #ifdef __linux 59 #define LINUX 60 #endif //__linux 61 62 #ifdef LINUX 63 #define POSIX 64 #endif //LINUX 65 66 67 68 #ifdef WINDOWS 69 // Define Windows compatibility requirements XP or later 70 #define WINVER 0x0501 71 #define _WIN32_WINNT 0x0501 72 73 #include <Windows.h> 74 #include <tchar.h> 75 #include <shlobj.h> 76 #include <direct.h> 77 #include <process.h> 78 #include <malloc.h> 79 80 typedef std::wstring TString; 81 82 #define TRAILING_SLASH '\\' 83 #define PATH_SEPARATOR ';' 84 #define BAD_PATH_SEPARATOR ':' 85 86 typedef ULONGLONG TPlatformNumber; 87 88 #if defined _DEBUG && !defined DEBUG 89 #define DEBUG 90 #endif 91 92 #endif //WINDOWS 93 94 95 #ifdef POSIX 96 #include <errno.h> 97 #include <unistd.h> 98 #include <sys/stat.h> 99 #include <dlfcn.h> 100 #include <libgen.h> 101 102 #define _T(x) x 103 104 typedef char TCHAR; 105 typedef std::string TString; 106 107 #define TRAILING_SLASH '/' 108 #define PATH_SEPARATOR ':' 109 #define BAD_PATH_SEPARATOR ';' 110 #define MAX_PATH 1000 111 112 typedef long TPlatformNumber; 113 114 #define HMODULE void* 115 #endif //POSIX 116 117 118 #define CONFIG_MAINJAR_KEY _T("CONFIG_MAINJAR_KEY") 119 #define CONFIG_MAINCLASSNAME_KEY _T("CONFIG_MAINCLASSNAME_KEY") 120 #define CONFIG_CLASSPATH_KEY _T("CONFIG_CLASSPATH_KEY") 121 #define APP_NAME_KEY _T("APP_NAME_KEY") 122 #define CONFIG_SPLASH_KEY _T("CONFIG_SPLASH_KEY") 123 #define CONFIG_APP_ID_KEY _T("CONFIG_APP_ID_KEY") 124 #define CONFIG_APP_MEMORY _T("CONFIG_APP_MEMORY") 125 126 #if defined(WINDOWS) || defined(LINUX) 127 #define JVM_RUNTIME_KEY _T("JVM_RUNTIME_KEY") 128 #define PACKAGER_APP_DATA_DIR _T("CONFIG_APP_ID_KEY") 129 #endif //WINDOWS || LINUX 130 131 #ifdef MAC 132 #define JVM_RUNTIME_KEY _T("JVMRuntime") 133 #define PACKAGER_APP_DATA_DIR _T("CFBundleIdentifier") 134 #endif //MAC 135 136 137 typedef void* Module; 138 typedef void* Procedure; 139 140 struct TValueIndex { 141 TString value; 142 size_t index; 143 }; 144 145 typedef std::map<TString, TValueIndex> TOrderedMap; 146 147 148 class PropertyContainer { 149 public: 150 PropertyContainer(void) {} 151 virtual ~PropertyContainer(void) {} 152 153 virtual bool GetValue(const TString Key, TString& Value) = 0; 154 virtual size_t GetCount() = 0; 155 }; 156 157 enum DebugState {dsNone, dsNative, dsJava}; 158 159 class Platform { 160 protected: 161 Platform(void) {} 162 163 public: 164 static Platform& GetInstance(); 165 166 virtual ~Platform(void) {} 167 168 public: 169 virtual void ShowMessage(TString title, TString description) = 0; 170 virtual void ShowMessage(TString description) = 0; 171 172 virtual void SetCurrentDirectory(TString Value) = 0; 173 174 // Caller must free result using delete[]. 175 virtual TCHAR* ConvertStringToFileSystemString(TCHAR* Source, bool &release) = 0; 176 177 // Caller must free result using delete[]. 178 virtual TCHAR* ConvertFileSystemStringToString(TCHAR* Source, bool &release) = 0; 179 180 // Returns: 181 // Windows=C:\Users\<username>\AppData\Local\<app.preferences.id>\packager\jvmuserargs.cfg 182 // Linux=~/.local/<app.preferences.id>/packager/jvmuserargs.cfg 183 // Mac=~/Library/Application Support/<app.preferences.id>/packager/jvmuserargs.cfg 184 virtual TString GetAppDataDirectory() = 0; 185 186 virtual TString GetPackageAppDirectory() = 0; 187 virtual TString GetPackageLauncherDirectory() = 0; 188 virtual TString GetAppName() = 0; 189 190 virtual TString GetConfigFileName() = 0; 191 192 virtual TString GetBundledJVMLibraryFileName(TString RuntimePath) = 0; 193 virtual TString GetSystemJVMLibraryFileName() = 0; 194 virtual TString GetSystemJRE() = 0; 195 196 // Caller must free result. 197 virtual PropertyContainer* GetConfigFile(TString FileName) = 0; 198 199 virtual TString GetModuleFileName() = 0; 200 virtual TString GetPackageRootDirectory() = 0; 201 202 virtual Module LoadLibrary(TString FileName) = 0; 203 virtual void FreeLibrary(Module Module) = 0; 204 virtual Procedure GetProcAddress(Module Module, std::string MethodName) = 0; 205 virtual std::vector<TString> GetLibraryImports(const TString FileName) = 0; 206 virtual std::vector<TString> FilterOutRuntimeDependenciesForPlatform(std::vector<TString> Imports) = 0; 207 208 virtual bool IsMainThread() = 0; 209 210 // Returns megabytes. 211 virtual TPlatformNumber GetMemorySize() = 0; 212 213 virtual std::map<TString, TString> GetKeys() = 0; 214 215 virtual std::list<TString> LoadFromFile(TString FileName) = 0; 216 virtual void SaveToFile(TString FileName, std::list<TString> Contents, bool ownerOnly) = 0; 217 218 #ifdef DEBUG 219 virtual DebugState GetDebugState() = 0; 220 virtual int GetProcessID() = 0; 221 virtual bool IsNativeDebuggerPresent() = 0; 222 #endif //DEBUG 223 }; 224 225 226 class Library { 227 private: 228 std::vector<TString> *FDependentLibraryNames; 229 std::vector<Library*> *FDependenciesLibraries; 230 Module FModule; 231 232 void Initialize(); 233 void InitializeDependencies(); 234 void LoadDependencies(); 235 void UnloadDependencies(); 236 237 protected: 238 void* GetProcAddress(std::string MethodName); 239 240 public: 241 Library(); 242 Library(const TString &FileName); 243 ~Library(); 244 245 bool Load(const TString &FileName); 246 bool Unload(); 247 248 void AddDependency(const TString &FileName); 249 void AddDependencies(const std::vector<TString> &Dependencies); 250 }; 251 252 #endif //PLATFORM_H