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     virtual TString GetSystemJVMLibraryFileName();
  70     virtual TString GetSystemJRE();
  71 
  72     virtual ISectionalPropertyContainer* GetConfigFile(TString FileName);
  73 
  74     virtual TString GetModuleFileName();
  75     virtual Module LoadLibrary(TString FileName);
  76     virtual void FreeLibrary(Module AModule);
  77     virtual Procedure GetProcAddress(Module AModule, std::string MethodName);
  78     virtual std::vector<TString> GetLibraryImports(const TString FileName);
  79     virtual std::vector<TString> FilterOutRuntimeDependenciesForPlatform(std::vector<TString> Imports);
  80 
  81     virtual Process* CreateProcess();
  82 
  83     virtual bool IsMainThread();
  84     virtual TPlatformNumber GetMemorySize();
  85 
  86 #ifdef DEBUG
  87     virtual bool IsNativeDebuggerPresent();
  88     virtual int GetProcessID();
  89 #endif //DEBUG
  90 };
  91 #pragma warning( pop ) // C4250
  92 
  93 
  94 class WindowsJavaUserPreferences : public JavaUserPreferences {
  95 public:
  96     WindowsJavaUserPreferences(void);
  97     ~WindowsJavaUserPreferences(void);
  98 
  99     virtual bool Load(TString Appid);
 100 };
 101 
 102 
 103 class FileHandle {
 104 private:
 105     HANDLE FHandle;
 106 
 107 public:
 108     FileHandle(std::wstring FileName);
 109     ~FileHandle();
 110 
 111     bool IsValid();
 112     HANDLE GetHandle();
 113 };
 114 
 115 
 116 class FileMappingHandle {
 117 private:
 118     HANDLE FHandle;
 119 
 120 public:
 121     FileMappingHandle(HANDLE FileHandle);
 122     ~FileMappingHandle();
 123 
 124     bool IsValid();
 125     HANDLE GetHandle();
 126 };
 127 
 128 
 129 class FileData {
 130 private:
 131     LPVOID FBaseAddress;
 132 
 133 public:
 134     FileData(HANDLE Handle);
 135     ~FileData();
 136 
 137     bool IsValid();
 138     LPVOID GetBaseAddress();
 139 };
 140 
 141 
 142 class WindowsLibrary {
 143 private:
 144     TString FFileName;
 145 
 146     // Given an RVA, look up the section header that encloses it and return a
 147     // pointer to its IMAGE_SECTION_HEADER
 148     static PIMAGE_SECTION_HEADER GetEnclosingSectionHeader(DWORD rva, PIMAGE_NT_HEADERS pNTHeader);
 149     static LPVOID GetPtrFromRVA(DWORD rva, PIMAGE_NT_HEADERS pNTHeader, DWORD imageBase);
 150     static std::vector<TString> GetImportsSection(DWORD base, PIMAGE_NT_HEADERS pNTHeader);
 151     static std::vector<TString> DumpPEFile(PIMAGE_DOS_HEADER dosHeader);
 152 
 153 public:
 154     WindowsLibrary(const TString FileName);
 155 
 156     std::vector<TString> GetImports();
 157 };
 158 
 159 
 160 class WindowsJob {
 161 private:
 162     HANDLE FHandle;
 163 
 164 public:
 165     WindowsJob();
 166     ~WindowsJob();
 167 
 168     HANDLE GetHandle();
 169 };
 170 
 171 
 172 class WindowsProcess : public Process {
 173 private:
 174     bool FRunning;
 175 
 176     PROCESS_INFORMATION FProcessInfo;
 177     static WindowsJob FJob;
 178 
 179     void Cleanup();
 180     bool ReadOutput();
 181 
 182 public:
 183     WindowsProcess();
 184     virtual ~WindowsProcess();
 185 
 186     virtual bool IsRunning();
 187     virtual bool Terminate();
 188     virtual bool Execute(const TString Application, const std::vector<TString> Arguments,
 189         bool AWait = false);
 190     virtual bool Wait();
 191     virtual TProcessID GetProcessID();
 192     virtual void SetInput(TString Value);
 193     virtual std::list<TString> GetOutput();
 194 };
 195 
 196 
 197 
 198 
 199 #endif //WINDOWSPLATFORM_H
 200 
 201 #endif // WINDOWS