1 /*
   2  * Copyright (c) 2014, 2016, 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 #include "Platform.h"
  35 
  36 #ifdef WINDOWS
  37 
  38 #ifndef WINDOWSPLATFORM_H
  39 #define WINDOWSPLATFORM_H
  40 
  41 #include "GenericPlatform.h"
  42 #include "JavaUserPreferences.h"
  43 
  44 #include <Windows.h>
  45 
  46 
  47 #pragma warning( push )
  48 #pragma warning( disable : 4250 ) // C4250 - 'class1' : inherits 'class2::member'
  49 class WindowsPlatform : virtual public Platform, GenericPlatform {
  50 private:
  51     DWORD FMainThread;
  52 
  53 public:
  54     WindowsPlatform(void);
  55     virtual ~WindowsPlatform(void);
  56 
  57     virtual TCHAR* ConvertStringToFileSystemString(TCHAR* Source, bool &release);
  58     virtual TCHAR* ConvertFileSystemStringToString(TCHAR* Source, bool &release);
  59 
  60     virtual void ShowMessage(TString title, TString description);
  61     virtual void ShowMessage(TString description);
  62     virtual MessageResponse ShowResponseMessage(TString title, TString description);
  63     //virtual MessageResponse ShowResponseMessage(TString description);
  64 
  65     virtual void SetCurrentDirectory(TString Value);
  66     virtual TString GetPackageRootDirectory();
  67     virtual TString GetAppDataDirectory();
  68     virtual TString GetBundledJVMLibraryFileName(TString RuntimePath);
  69 
  70     virtual ISectionalPropertyContainer* GetConfigFile(TString FileName);
  71 
  72     virtual TString GetModuleFileName();
  73     virtual Module LoadLibrary(TString FileName);
  74     virtual void FreeLibrary(Module AModule);
  75     virtual Procedure GetProcAddress(Module AModule, std::string MethodName);
  76     virtual std::vector<TString> GetLibraryImports(const TString FileName);
  77     virtual std::vector<TString> FilterOutRuntimeDependenciesForPlatform(std::vector<TString> Imports);
  78 
  79     virtual Process* CreateProcess();
  80 
  81     virtual bool IsMainThread();
  82     virtual TPlatformNumber GetMemorySize();
  83 
  84 #ifdef DEBUG
  85     virtual bool IsNativeDebuggerPresent();
  86     virtual int GetProcessID();
  87 #endif //DEBUG
  88 };
  89 #pragma warning( pop ) // C4250
  90 
  91 
  92 class WindowsJavaUserPreferences : public JavaUserPreferences {
  93 public:
  94     WindowsJavaUserPreferences(void);
  95     ~WindowsJavaUserPreferences(void);
  96 
  97     virtual bool Load(TString Appid);
  98 };
  99 
 100 
 101 class FileHandle {
 102 private:
 103     HANDLE FHandle;
 104 
 105 public:
 106     FileHandle(std::wstring FileName);
 107     ~FileHandle();
 108 
 109     bool IsValid();
 110     HANDLE GetHandle();
 111 };
 112 
 113 
 114 class FileMappingHandle {
 115 private:
 116     HANDLE FHandle;
 117 
 118 public:
 119     FileMappingHandle(HANDLE FileHandle);
 120     ~FileMappingHandle();
 121 
 122     bool IsValid();
 123     HANDLE GetHandle();
 124 };
 125 
 126 
 127 class FileData {
 128 private:
 129     LPVOID FBaseAddress;
 130 
 131 public:
 132     FileData(HANDLE Handle);
 133     ~FileData();
 134 
 135     bool IsValid();
 136     LPVOID GetBaseAddress();
 137 };
 138 
 139 
 140 class WindowsLibrary {
 141 private:
 142     TString FFileName;
 143 
 144     // Given an RVA, look up the section header that encloses it and return a
 145     // pointer to its IMAGE_SECTION_HEADER
 146     static PIMAGE_SECTION_HEADER GetEnclosingSectionHeader(DWORD rva, PIMAGE_NT_HEADERS pNTHeader);
 147     static LPVOID GetPtrFromRVA(DWORD rva, PIMAGE_NT_HEADERS pNTHeader, DWORD imageBase);
 148     static std::vector<TString> GetImportsSection(DWORD base, PIMAGE_NT_HEADERS pNTHeader);
 149     static std::vector<TString> DumpPEFile(PIMAGE_DOS_HEADER dosHeader);
 150 
 151 public:
 152     WindowsLibrary(const TString FileName);
 153 
 154     std::vector<TString> GetImports();
 155 };
 156 
 157 
 158 class WindowsJob {
 159 private:
 160     HANDLE FHandle;
 161 
 162 public:
 163     WindowsJob();
 164     ~WindowsJob();
 165 
 166     HANDLE GetHandle();
 167 };
 168 
 169 
 170 class WindowsProcess : public Process {
 171 private:
 172     bool FRunning;
 173 
 174     PROCESS_INFORMATION FProcessInfo;
 175     static WindowsJob FJob;
 176 
 177     void Cleanup();
 178     bool ReadOutput();
 179 
 180 public:
 181     WindowsProcess();
 182     virtual ~WindowsProcess();
 183 
 184     virtual bool IsRunning();
 185     virtual bool Terminate();
 186     virtual bool Execute(const TString Application, const std::vector<TString> Arguments,
 187         bool AWait = false);
 188     virtual bool Wait();
 189     virtual TProcessID GetProcessID();
 190     virtual void SetInput(TString Value);
 191     virtual std::list<TString> GetOutput();
 192 };
 193 
 194 
 195 
 196 
 197 #endif //WINDOWSPLATFORM_H
 198 
 199 #endif // WINDOWS