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 // Caller must free result using delete[]. |
|
180 virtual TCHAR* ConvertStringToFileSystemString(TCHAR* Source, |
|
181 bool &release) = 0; |
|
182 |
|
183 // Caller must free result using delete[]. |
|
184 virtual TCHAR* ConvertFileSystemStringToString(TCHAR* Source, |
|
185 bool &release) = 0; |
|
186 |
|
187 // Returns: |
|
188 // Windows=C:\Users\<username>\AppData\Local |
|
189 // Linux=~/.local |
|
190 // Mac=~/Library/Application Support |
|
191 virtual TString GetAppDataDirectory() = 0; |
|
192 |
|
193 virtual TString GetPackageAppDirectory() = 0; |
|
194 virtual TString GetPackageLauncherDirectory() = 0; |
|
195 virtual TString GetPackageRuntimeBinDirectory() = 0; |
|
196 virtual TString GetAppName() = 0; |
|
197 |
|
198 virtual TString GetConfigFileName(); |
|
199 |
|
200 virtual TString GetBundledJavaLibraryFileName(TString RuntimePath) = 0; |
|
201 |
|
202 // Caller must free result. |
|
203 virtual ISectionalPropertyContainer* GetConfigFile(TString FileName) = 0; |
|
204 |
|
205 virtual TString GetModuleFileName() = 0; |
|
206 virtual TString GetPackageRootDirectory() = 0; |
|
207 |
|
208 virtual Module LoadLibrary(TString FileName) = 0; |
|
209 virtual void FreeLibrary(Module Module) = 0; |
|
210 virtual Procedure GetProcAddress(Module Module, std::string MethodName) = 0; |
|
211 |
|
212 // Caller must free result. |
|
213 virtual Process* CreateProcess() = 0; |
|
214 |
|
215 virtual bool IsMainThread() = 0; |
|
216 |
|
217 // Returns megabytes. |
|
218 virtual TPlatformNumber GetMemorySize() = 0; |
|
219 |
|
220 virtual std::map<TString, TString> GetKeys(); |
|
221 |
|
222 virtual void InitStreamLocale(wios *stream) = 0; |
|
223 virtual std::list<TString> LoadFromFile(TString FileName); |
|
224 virtual void SaveToFile(TString FileName, |
|
225 std::list<TString> Contents, bool ownerOnly); |
|
226 |
|
227 virtual TString GetTempDirectory() = 0; |
|
228 |
|
229 virtual void addPlatformDependencies(JavaLibrary *pJavaLibrary) = 0; |
|
230 |
|
231 public: |
|
232 // String helpers |
|
233 // Caller must free result using delete[]. |
|
234 static void CopyString(char *Destination, |
|
235 size_t NumberOfElements, const char *Source); |
|
236 |
|
237 // Caller must free result using delete[]. |
|
238 static void CopyString(wchar_t *Destination, |
|
239 size_t NumberOfElements, const wchar_t *Source); |
|
240 |
|
241 static WideString MultibyteStringToWideString(const char* value); |
|
242 static MultibyteString WideStringToMultibyteString(const wchar_t* value); |
|
243 }; |
|
244 |
|
245 class Exception: public std::exception { |
|
246 private: |
|
247 TString FMessage; |
|
248 |
|
249 protected: |
|
250 void SetMessage(const TString Message) { |
|
251 FMessage = Message; |
|
252 } |
|
253 |
|
254 public: |
|
255 explicit Exception() : exception() {} |
|
256 explicit Exception(const TString Message) : exception() { |
|
257 SetMessage(Message); |
|
258 } |
|
259 virtual ~Exception() throw() {} |
|
260 |
|
261 TString GetMessage() { return FMessage; } |
|
262 }; |
|
263 |
|
264 #endif // PLATFORM_H |
|