1 /* 2 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #ifndef PLATFORM_H 27 #define PLATFORM_H 28 29 #include "PlatformDefs.h" 30 #include "Properties.h" 31 #include "OrderedMap.h" 32 #include "Library.h" 33 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <memory.h> 37 #include <string> 38 #include <map> 39 #include <list> 40 #include <vector> 41 #include <fstream> 42 43 using namespace std; 44 45 // Config file sections 46 #define CONFIG_SECTION_APPLICATION _T("CONFIG_SECTION_APPLICATION") 47 #define CONFIG_SECTION_JAVAOPTIONS _T("CONFIG_SECTION_JAVAOPTIONS") 48 #define CONFIG_SECTION_APPCDSJAVAOPTIONS _T("CONFIG_SECTION_APPCDSJAVAOPTIONS") 49 #define CONFIG_SECTION_ARGOPTIONS _T("CONFIG_SECTION_ARGOPTIONS") 50 #define CONFIG_SECTION_APPCDSGENERATECACHEJAVAOPTIONS \ 51 _T("CONFIG_SECTION_APPCDSGENERATECACHEJAVAOPTIONS") 52 53 // Config file keys. 54 #define CONFIG_VERSION _T("CONFIG_VERSION") 55 #define CONFIG_MAINJAR_KEY _T("CONFIG_MAINJAR_KEY") 56 #define CONFIG_MAINMODULE_KEY _T("CONFIG_MAINMODULE_KEY") 57 #define CONFIG_MAINCLASSNAME_KEY _T("CONFIG_MAINCLASSNAME_KEY") 58 #define CONFIG_CLASSPATH_KEY _T("CONFIG_CLASSPATH_KEY") 59 #define CONFIG_MODULEPATH_KEY _T("CONFIG_MODULEPATH_KEY") 60 #define APP_NAME_KEY _T("APP_NAME_KEY") 61 #define CONFIG_SPLASH_KEY _T("CONFIG_SPLASH_KEY") 62 #define CONFIG_APP_MEMORY _T("CONFIG_APP_MEMORY") 63 #define CONFIG_APP_DEBUG _T("CONFIG_APP_DEBUG") 64 #define CONFIG_APPLICATION_INSTANCE _T("CONFIG_APPLICATION_INSTANCE") 65 66 #define JAVA_RUNTIME_KEY _T("JAVA_RUNTIME_KEY") 67 #define JPACKAGE_APP_DATA_DIR _T("CONFIG_APP_IDENTIFIER") 68 69 struct WideString { 70 size_t length; 71 wchar_t* data; 72 73 WideString() { length = 0; data = NULL; } 74 }; 75 76 struct MultibyteString { 77 size_t length; 78 char* data; 79 80 MultibyteString() { length = 0; data = NULL; } 81 }; 82 83 class Process { 84 protected: 85 std::list<TString> FOutput; 86 87 public: 88 Process() { 89 Output.SetInstance(this); 90 Input.SetInstance(this); 91 } 92 93 virtual ~Process() {} 94 95 virtual bool IsRunning() = 0; 96 virtual bool Terminate() = 0; 97 virtual bool Execute(const TString Application, 98 const std::vector<TString> Arguments, bool AWait = false) = 0; 99 virtual bool Wait() = 0; 100 virtual TProcessID GetProcessID() = 0; 101 102 virtual std::list<TString> GetOutput() { return FOutput; } 103 virtual void SetInput(TString Value) = 0; 104 105 ReadProperty<Process, std::list<TString>, &Process::GetOutput> Output; 106 WriteProperty<Process, TString, &Process::SetInput> Input; 107 }; 108 109 110 template <typename T> 111 class AutoFreePtr { 112 private: 113 T* FObject; 114 115 public: 116 AutoFreePtr() { 117 FObject = NULL; 118 } 119 120 AutoFreePtr(T* Value) { 121 FObject = Value; 122 } 123 124 ~AutoFreePtr() { 125 if (FObject != NULL) { 126 delete FObject; 127 } 128 } 129 130 operator T* () const { 131 return FObject; 132 } 133 134 T& operator* () const { 135 return *FObject; 136 } 137 138 T* operator->() const { 139 return FObject; 140 } 141 142 T** operator&() { 143 return &FObject; 144 } 145 146 T* operator=(const T * rhs) { 147 FObject = rhs; 148 return FObject; 149 } 150 }; 151 152 enum DebugState {dsNone, dsNative, dsJava}; 153 enum MessageResponse {mrOK, mrCancel}; 154 enum AppCDSState {cdsUninitialized, cdsDisabled, 155 cdsEnabled, cdsAuto, cdsGenCache}; 156 157 class Platform { 158 private: 159 AppCDSState FAppCDSState; 160 161 protected: 162 Platform(void): FAppCDSState(cdsUninitialized) { 163 } 164 165 public: 166 AppCDSState GetAppCDSState() { return FAppCDSState; } 167 void SetAppCDSState(AppCDSState Value) { FAppCDSState = Value; } 168 169 static Platform& GetInstance(); 170 171 virtual ~Platform(void) {} 172 173 public: 174 virtual void ShowMessage(TString title, TString description) = 0; 175 virtual void ShowMessage(TString description) = 0; 176 virtual MessageResponse ShowResponseMessage(TString title, 177 TString description) = 0; 178 179 virtual void SetCurrentDirectory(TString Value) = 0; 180 181 // Caller must free result using delete[]. 182 virtual TCHAR* ConvertStringToFileSystemString(TCHAR* Source, 183 bool &release) = 0; 184 185 // Caller must free result using delete[]. 186 virtual TCHAR* ConvertFileSystemStringToString(TCHAR* Source, 187 bool &release) = 0; 188 189 // Returns: 190 // Windows=C:\Users\<username>\AppData\Local 191 // Linux=~/.local 192 // Mac=~/Library/Application Support 193 virtual TString GetAppDataDirectory() = 0; 194 195 virtual TString GetPackageAppDirectory() = 0; 196 virtual TString GetPackageLauncherDirectory() = 0; 197 virtual TString GetPackageRuntimeBinDirectory() = 0; 198 virtual TString GetAppName() = 0; 199 200 virtual TString GetConfigFileName(); 201 202 virtual TString GetBundledJavaLibraryFileName(TString RuntimePath) = 0; 203 204 // Caller must free result. 205 virtual ISectionalPropertyContainer* GetConfigFile(TString FileName) = 0; 206 207 virtual TString GetModuleFileName() = 0; 208 virtual TString GetPackageRootDirectory() = 0; 209 210 virtual Module LoadLibrary(TString FileName) = 0; 211 virtual void FreeLibrary(Module Module) = 0; 212 virtual Procedure GetProcAddress(Module Module, std::string MethodName) = 0; 213 214 // Caller must free result. 215 virtual Process* CreateProcess() = 0; 216 217 virtual bool IsMainThread() = 0; 218 219 // Returns megabytes. 220 virtual TPlatformNumber GetMemorySize() = 0; 221 222 virtual std::map<TString, TString> GetKeys(); 223 224 virtual void InitStreamLocale(wios *stream) = 0; 225 virtual std::list<TString> LoadFromFile(TString FileName); 226 virtual void SaveToFile(TString FileName, 227 std::list<TString> Contents, bool ownerOnly); 228 229 virtual TString GetTempDirectory() = 0; 230 231 virtual void addPlatformDependencies(JavaLibrary *pJavaLibrary) = 0; 232 233 public: 234 // String helpers 235 // Caller must free result using delete[]. 236 static void CopyString(char *Destination, 237 size_t NumberOfElements, const char *Source); 238 239 // Caller must free result using delete[]. 240 static void CopyString(wchar_t *Destination, 241 size_t NumberOfElements, const wchar_t *Source); 242 243 static WideString MultibyteStringToWideString(const char* value); 244 static MultibyteString WideStringToMultibyteString(const wchar_t* value); 245 }; 246 247 class Exception: public std::exception { 248 private: 249 TString FMessage; 250 251 protected: 252 void SetMessage(const TString Message) { 253 FMessage = Message; 254 } 255 256 public: 257 explicit Exception() : exception() {} 258 explicit Exception(const TString Message) : exception() { 259 SetMessage(Message); 260 } 261 virtual ~Exception() throw() {} 262 263 TString GetMessage() { return FMessage; } 264 }; 265 266 #endif // PLATFORM_H