chahuadev's picture
Upload 40 files
8eeb77a verified
/**
* @fileoverview Chahua Database Manager - Electron Preload Script
* @description สคริปต์ preload สำหรับการสื่อสารระหว่าง main process และ renderer process
* คุณสมบัติ: IPC Communication Bridge, File Operations, License Management
* @author ทีมพัฒนา Chahua
* @version 1.0.0
* @since 2024-01-01
* @calledBy main.js (Electron main process), renderer/js/*.js (UI components)
* @dependencies
* - electron: contextBridge, ipcRenderer modules
* - main.js: IPC handlers for file operations and license management
* @features
* - Secure IPC communication bridge
* - File system operations (read/write/dialog)
* - License status management
* - Menu action handling
* - System information exposure
* @security
* - Context isolation bridge
* - Limited API exposure to renderer
* - No direct Node.js access
* - IPC channel validation
* @example
* // In renderer process
* const version = await window.electronAPI.getAppVersion();
* const licenseStatus = await window.license.getStatus();
*/
const { contextBridge, ipcRenderer } = require('electron');
/**
* Electron API บริดจ์สำหรับ renderer process
* @namespace electronAPI
* @description เปิดเผย API ที่ปลอดภัยสำหรับการสื่อสารกับ main process
* @features
* - การจัดการไฟล์และไดอะล็อก
* - ข้อมูลแอปพลิเคชันและเซิร์ฟเวอร์
* - การจัดการเมนูและการอัปเดต
* - ข้อมูลระบบ
*/
// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
contextBridge.exposeInMainWorld('electronAPI', {
// App info
getAppVersion: () => ipcRenderer.invoke('get-app-version'),
// File operations
showSaveDialog: (options) => ipcRenderer.invoke('show-save-dialog', options),
showOpenDialog: (options) => ipcRenderer.invoke('show-open-dialog', options),
readFile: (filePath) => ipcRenderer.invoke('read-file', filePath),
writeFile: (filePath, content) => ipcRenderer.invoke('write-file', filePath, content),
// Server info
getServerPort: () => ipcRenderer.invoke('get-server-port'),
// Menu actions
onMenuAction: (callback) => ipcRenderer.on('menu-action', callback),
removeMenuActionListener: (callback) => ipcRenderer.removeListener('menu-action', callback),
// Updates
checkForUpdates: () => ipcRenderer.invoke('check-for-updates'),
// System info
platform: process.platform,
versions: process.versions
});
/**
* License API บริดจ์สำหรับการจัดการลิขสิทธิ์
* @namespace license
* @description API สำหรับตรวจสอบและจัดการสถานะลิขสิทธิ์
* @features
* - การตรวจสอบสถานะลิขสิทธิ์
* - การดึงข้อมูลลิขสิทธิ์
*/
// License API
contextBridge.exposeInMainWorld('license', {
getStatus: () => ipcRenderer.invoke('license:getStatus')
});
/**
* ทำความสะอาด event listeners เมื่อหน้าถูกปิด
* @description ลบ event listeners ทั้งหมดเพื่อป้องกัน memory leaks
*/
// Remove the event listener when the page is unloaded
window.addEventListener('beforeunload', () => {
ipcRenderer.removeAllListeners('menu-action');
});