(targetSession: Electron.Session)
| 112 | * 供内置插件使用(外部插件不需要访问应用图标) |
| 113 | */ |
| 114 | export function registerIconProtocolForSession(targetSession: Electron.Session): void { |
| 115 | if (targetSession.protocol.isProtocolHandled('ztools-icon')) { |
| 116 | return |
| 117 | } |
| 118 | |
| 119 | targetSession.protocol.handle('ztools-icon', async (request) => { |
| 120 | try { |
| 121 | const urlPath = request.url.replace('ztools-icon://', '') |
| 122 | const iconPath = decodeURIComponent(urlPath) |
| 123 | |
| 124 | // 命中内存缓存:刷新 LRU 并返回 |
| 125 | const cached = iconMemoryCache.get(iconPath) |
| 126 | if (cached) { |
| 127 | setIconCache(iconPath, cached) |
| 128 | return createIconResponse(cached) |
| 129 | } |
| 130 | |
| 131 | // 未命中:通过串行队列提取图标 |
| 132 | const buffer = await extractIconQueued(iconPath) |
| 133 | |
| 134 | // 写入内存缓存 |
| 135 | setIconCache(iconPath, buffer) |
| 136 | |
| 137 | return createIconResponse(buffer) |
| 138 | } catch (error) { |
| 139 | console.error('[Main] 图标提取失败:', error) |
| 140 | return new Response('Icon Error', { status: 404 }) |
| 141 | } |
| 142 | }) |
| 143 | } |
no test coverage detected