d3ro-voice/apps/desktop/src/main/ipc/license-handlers.ts

118 lines
4.1 KiB
TypeScript

// src/main/ipc/license-handlers.ts
// Phase 11: 라이센스 IPC 핸들러
import { app, ipcMain, BrowserWindow } from 'electron'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors'
import { getLicenseService } from '../services/LicenseService'
import type {
ActivateLicenseParams,
UpgradePromptEvent,
LicenseInfo,
} from '@d3ro/core/types'
import { Feature } from '@d3ro/core/types'
import { billingUrl, PUBLIC_SITE_ORIGIN } from '@d3ro/core/web-urls'
import { openExternalSafe } from '../windows/web-contents-hardening'
/** 개발 빌드는 로컬 웹앱(`npm run dev --workspace=@d3ro/web`)의 결제 페이지를 연다. */
const DEV_WEB_ORIGIN = 'http://localhost:3000'
/** 업그레이드 유도 이벤트를 모든 렌더러에 broadcast */
function broadcastUpgradePrompt(event: UpgradePromptEvent): void {
for (const win of BrowserWindow.getAllWindows()) {
win.webContents.send(IPC_CHANNELS.LICENSE.UPGRADE_PROMPT, event)
}
}
/** 티어 변경을 모든 렌더러에 broadcast */
function broadcastTierChanged(info: LicenseInfo): void {
for (const win of BrowserWindow.getAllWindows()) {
win.webContents.send(IPC_CHANNELS.LICENSE.TIER_CHANGED, info)
}
}
export function registerLicenseHandlers(): void {
const license = getLicenseService()
// 이벤트 -> 렌더러 전파
license.on('upgrade-prompt', (event: UpgradePromptEvent) => {
broadcastUpgradePrompt(event)
})
license.on('tier-changed', (info: LicenseInfo) => {
broadcastTierChanged(info)
})
// ── handle 채널 ──
ipcMain.handle(IPC_CHANNELS.LICENSE.GET_INFO, async () => {
try {
return ipcSuccess(license.getInfo())
} catch (err) {
return ipcError(ErrorCode.UnknownError, `Failed to get license info: ${err}`)
}
})
ipcMain.handle(IPC_CHANNELS.LICENSE.ACTIVATE, async (_event, params: ActivateLicenseParams) => {
try {
const result = await license.activate(params.licenseKey)
return ipcSuccess(result)
} catch (err) {
return ipcError(ErrorCode.LicenseActivationFailed, `Activation failed: ${err}`)
}
})
ipcMain.handle(IPC_CHANNELS.LICENSE.DEACTIVATE, async () => {
try {
await license.deactivate()
return ipcSuccess(undefined)
} catch (err) {
return ipcError(ErrorCode.LicenseDeactivationFailed, `Deactivation failed: ${err}`)
}
})
ipcMain.handle(IPC_CHANNELS.LICENSE.CHECK_FEATURE, async (_event, params: { feature: Feature }) => {
try {
return ipcSuccess(license.canUse(params.feature))
} catch (err) {
return ipcError(ErrorCode.UnknownError, `Feature check failed: ${err}`)
}
})
ipcMain.handle(IPC_CHANNELS.LICENSE.GET_USAGE, async (_event, params: { feature: Feature }) => {
try {
return ipcSuccess(license.getUsage(params.feature))
} catch (err) {
return ipcError(ErrorCode.UnknownError, `Usage query failed: ${err}`)
}
})
ipcMain.handle(IPC_CHANNELS.LICENSE.GET_ALL_USAGE, async () => {
try {
return ipcSuccess(license.getAllUsage())
} catch (err) {
return ipcError(ErrorCode.UnknownError, `Usage query failed: ${err}`)
}
})
ipcMain.handle(IPC_CHANNELS.LICENSE.GET_TIER_COMPARISON, async () => {
try {
return ipcSuccess(license.getTierComparison())
} catch (err) {
return ipcError(ErrorCode.UnknownError, `Tier comparison failed: ${err}`)
}
})
ipcMain.handle(IPC_CHANNELS.LICENSE.OPEN_BILLING, async (_event, params: { tier: 'pro' | 'pro_plus' }) => {
try {
const url = billingUrl({ tier: params.tier })
const target = app.isPackaged ? url : url.replace(PUBLIC_SITE_ORIGIN, DEV_WEB_ORIGIN)
// 개발 빌드만 로컬 웹앱(http://localhost:3000) 오리진을 추가로 허용한다.
const opened = await openExternalSafe(target, app.isPackaged ? {} : { allowOrigins: [DEV_WEB_ORIGIN] })
if (!opened) return ipcError(ErrorCode.UnknownError, 'Failed to open billing: URL not allowed')
return ipcSuccess(undefined)
} catch (err) {
return ipcError(ErrorCode.UnknownError, `Failed to open billing: ${err}`)
}
})
}