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_JVMOPTIONS _T("CONFIG_SECTION_JVMOPTIONS") 48 #define CONFIG_SECTION_APPCDSJVMOPTIONS _T("CONFIG_SECTION_APPCDSJVMOPTIONS") 49 #define CONFIG_SECTION_ARGOPTIONS _T("CONFIG_SECTION_ARGOPTIONS") 50 #define CONFIG_SECTION_APPCDSGENERATECACHEJVMOPTIONS \ 51 _T("CONFIG_SECTION_APPCDSGENERATECACHEJVMOPTIONS") 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_ID_KEY _T("CONFIG_APP_ID_KEY") 63 #define CONFIG_APP_MEMORY _T("CONFIG_APP_MEMORY") 64 #define CONFIG_APP_DEBUG _T("CONFIG_APP_DEBUG") 65 #define CONFIG_APPLICATION_INSTANCE _T("CONFIG_APPLICATION_INSTANCE") 66 67 #define JVM_RUNTIME_KEY _T("JVM_RUNTIME_KEY") 68 #define JPACKAGE_APP_DATA_DIR _T("CONFIG_APP_IDENTIFIER") 69 70 struct WideString { 71 size_t length; 72 wchar_t* data; 73 74 WideString() { length = 0; data = NULL; } 75 }; 76 77 struct MultibyteString { 78 size_t length; 79 char* data; 80 81 MultibyteString() { length = 0; data = NULL; } 82 }; 83 84 class Process { 85 protected: 86 std::list<TString> FOutput; 87 88 public: 89 Process() { 90 Output.SetInstance(this); 91 Input.SetInstance(this); 92 } 93 94 virtual ~Process() {} 95 96 virtual bool IsRunning() = 0; 97 virtual bool Terminate() = 0; 98 virtual bool Execute(const TString Application, 99 const std::vector<TString> Arguments, bool AWait = false) = 0; 100 virtual bool Wait() = 0; 101 virtual TProcessID GetProcessID() = 0; 102 103 virtual std::list<TString> GetOutput() { return FOutput; } 104 virtual void SetInput(TString Value) = 0; 105 106 ReadProperty<Process, std::list<TString>, &Process::GetOutput> Output; 107 WriteProperty<Process, TString, &Process::SetInput> Input; 108 }; 109 110 111 template <typename T> 112 class AutoFreePtr { 113 private: 114 T* FObject; 115 116 public: 117 AutoFreePtr() { 118 FObject = NULL; 119 } 120 121 AutoFreePtr(T* Value) { 122 FObject = Value; 123 } 124 125 ~AutoFreePtr() { 126 if (FObject != NULL) { 127 delete FObject; 128 } 129 } 130 131 operator T* () const { 132 return FObject; 133 } 134 135 T& operator* () const { 136 return *FObject; 137 } 138 139 T* operator->() const { 140 return FObject; 141 } 142 143 T** operator&() { 144 return &FObject; 145 } 146 147 T* operator=(const T * rhs) { 148 FObject = rhs; 149 return FObject; 150 } 151 }; 152 153 enum DebugState {dsNone, dsNative, dsJava}; 154 enum MessageResponse {mrOK, mrCancel}; 155 enum AppCDSState {cdsUninitialized, cdsDisabled, 156 cdsEnabled, cdsAuto, cdsGenCache}; 157 158 class Platform { 159 private: 160 AppCDSState FAppCDSState; 161 162 protected: 163 Platform(void): FAppCDSState(cdsUninitialized) { 164 } 165 166 public: 167 AppCDSState GetAppCDSState() { return FAppCDSState; } 168 void SetAppCDSState(AppCDSState Value) { FAppCDSState = Value; } 169 170 static Platform& GetInstance(); 171 172 virtual ~Platform(void) {} 173 174 public: 175 virtual void ShowMessage(TString title, TString description) = 0; 176 virtual void ShowMessage(TString description) = 0; 177 virtual MessageResponse ShowResponseMessage(TString title, 178 TString description) = 0; 179 180 virtual void SetCurrentDirectory(TString Value) = 0; 181 182 // Caller must free result using delete[]. 183 virtual TCHAR* ConvertStringToFileSystemString(TCHAR* Source, 184 bool &release) = 0; 185 186 // Caller must free result using delete[]. 187 virtual TCHAR* ConvertFileSystemStringToString(TCHAR* Source, 188 bool &release) = 0; 189 190 // Returns: 191 // Windows=C:\Users\<username>\AppData\Local 192 // Linux=~/.local 193 // Mac=~/Library/Application Support 194 virtual TString GetAppDataDirectory() = 0; 195 196 virtual TString GetPackageAppDirectory() = 0; 197 virtual TString GetPackageLauncherDirectory() = 0; 198 virtual TString GetPackageRuntimeBinDirectory() = 0; 199 virtual TString GetAppName() = 0; 200 201 virtual TString GetConfigFileName(); 202 203 virtual TString GetBundledJVMLibraryFileName(TString RuntimePath) = 0; 204 205 // Caller must free result. 206 virtual ISectionalPropertyContainer* GetConfigFile(TString FileName) = 0; 207 208 virtual TString GetModuleFileName() = 0; 209 virtual TString GetPackageRootDirectory() = 0; 210 211 virtual Module LoadLibrary(TString FileName) = 0; 212 virtual void FreeLibrary(Module Module) = 0; 213 virtual Procedure GetProcAddress(Module Module, std::string MethodName) = 0; 214 215 // Caller must free result. 216 virtual Process* CreateProcess() = 0; 217 218 virtual bool IsMainThread() = 0; 219 220 // Returns megabytes. 221 virtual TPlatformNumber GetMemorySize() = 0; 222 223 virtual std::map<TString, TString> GetKeys(); 224 225 virtual void InitStreamLocale(wios *stream) = 0; 226 virtual std::list<TString> LoadFromFile(TString FileName); 227 virtual void SaveToFile(TString FileName, 228 std::list<TString> Contents, bool ownerOnly); 229 230 virtual TString GetTempDirectory() = 0; 231 232 virtual void addPlatformDependencies(JavaLibrary *pJavaLibrary) = 0; 233 234 public: 235 // String helpers 236 // Caller must free result using delete[]. 237 static void CopyString(char *Destination, 238 size_t NumberOfElements, const char *Source); 239 240 // Caller must free result using delete[]. 241 static void CopyString(wchar_t *Destination, 242 size_t NumberOfElements, const wchar_t *Source); 243 244 static WideString MultibyteStringToWideString(const char* value); 245 static MultibyteString WideStringToMultibyteString(const wchar_t* value); 246 247 #ifdef DEBUG 248 virtual DebugState GetDebugState() = 0; 249 virtual int GetProcessID() = 0; 250 virtual bool IsNativeDebuggerPresent() = 0; 251 #endif //DEBUG 252 }; 253 254 class Exception: public std::exception { 255 private: 256 TString FMessage; 257 258 protected: 259 void SetMessage(const TString Message) { 260 FMessage = Message; 261 } 262 263 public: 264 explicit Exception() : exception() {} 265 explicit Exception(const TString Message) : exception() { 266 SetMessage(Message); 267 } 268 virtual ~Exception() throw() {} 269 270 TString GetMessage() { return FMessage; } 271 }; 272 273 #endif // PLATFORM_H