File size: 3,757 Bytes
8eeb77a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**

 * @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');
});