This commit is contained in:
2025-09-25 15:31:11 +08:00
parent d18a3d5279
commit 972e76e655
11 changed files with 766 additions and 453 deletions

View File

@@ -1,4 +1,4 @@
import { reactive, ref, nextTick } from 'vue'
import { reactive } from 'vue'
import type { ResourceService } from './ResourceService'
import type { EventCommunicationService } from './EventCommunicationService'
import { v4 as uuidv4 } from 'uuid'
@@ -73,6 +73,7 @@ export interface SandboxInstance {
cpuUsage: number
networkRequests: number
errors: string[]
messageHandler?: (event: MessageEvent) => void
}
/**
@@ -320,6 +321,12 @@ export class ApplicationSandboxEngine {
try {
this.updateSandboxState(sandbox, SandboxState.DESTROYED)
// 移除消息事件监听器
if (sandbox.messageHandler) {
window.removeEventListener('message', sandbox.messageHandler);
sandbox.messageHandler = undefined;
}
// 清理DOM元素
if (sandbox.iframe) {
sandbox.iframe.remove()
@@ -621,11 +628,14 @@ export class ApplicationSandboxEngine {
// 这些头部主要用于服务器端设置,在客户端我们通过其他方式实现
// 监听iframe的消息事件
window.addEventListener('message', (event) => {
const messageHandler = (event: MessageEvent) => {
if (event.source === sandbox.iframe!.contentWindow) {
this.handleSandboxMessage(sandbox, event.data)
}
})
};
window.addEventListener('message', messageHandler);
// 存储事件监听器引用,以便在销毁时移除
sandbox.messageHandler = messageHandler;
// 简化安全限制主要依赖iframe的sandbox属性
sandbox.iframe.addEventListener('load', () => {
@@ -855,316 +865,316 @@ export class ApplicationSandboxEngine {
return this.wrapResponse(this.sendToSystem('window.getSize'));
}
}
// 存储SDK实现
class StorageSDKImpl extends SDKBase {
constructor(appId) {
super();
this._appId = appId;
}
async set(key, value) {
return this.wrapResponse(this.sendToSystem('storage.set', { key, value }));
}
async get(key) {
return this.wrapResponse(this.sendToSystem('storage.get', { key }));
}
async remove(key) {
return this.wrapResponse(this.sendToSystem('storage.remove', { key }));
}
async clear() {
return this.wrapResponse(this.sendToSystem('storage.clear'));
}
async keys() {
return this.wrapResponse(this.sendToSystem('storage.keys'));
}
async has(key) {
return this.wrapResponse(this.sendToSystem('storage.has', { key }));
}
async getStats() {
return this.wrapResponse(this.sendToSystem('storage.getStats'));
}
// 存储SDK实现
class StorageSDKImpl extends SDKBase {
constructor(appId) {
super();
this._appId = appId;
}
// 网络SDK实现
class NetworkSDKImpl extends SDKBase {
constructor(appId) {
super();
this._appId = appId;
}
async request(url, config) {
return this.wrapResponse(this.sendToSystem('network.request', { url, config }));
}
async get(url, config) {
return this.request(url, { ...config, method: 'GET' });
}
async post(url, data, config) {
return this.request(url, { ...config, method: 'POST', body: data });
}
async put(url, data, config) {
return this.request(url, { ...config, method: 'PUT', body: data });
}
async delete(url, config) {
return this.request(url, { ...config, method: 'DELETE' });
}
async set(key, value) {
return this.wrapResponse(this.sendToSystem('storage.set', { key, value }));
}
// 事件SDK实现
class EventSDKImpl extends SDKBase {
constructor(appId) {
super();
this._appId = appId;
}
async get(key) {
return this.wrapResponse(this.sendToSystem('storage.get', { key }));
}
async remove(key) {
return this.wrapResponse(this.sendToSystem('storage.remove', { key }));
}
async clear() {
return this.wrapResponse(this.sendToSystem('storage.clear'));
}
async keys() {
return this.wrapResponse(this.sendToSystem('storage.keys'));
}
async has(key) {
return this.wrapResponse(this.sendToSystem('storage.has', { key }));
}
async getStats() {
return this.wrapResponse(this.sendToSystem('storage.getStats'));
}
}
// 网络SDK实现
class NetworkSDKImpl extends SDKBase {
constructor(appId) {
super();
this._appId = appId;
}
async request(url, config) {
return this.wrapResponse(this.sendToSystem('network.request', { url, config }));
}
async get(url, config) {
return this.request(url, { ...config, method: 'GET' });
}
async post(url, data, config) {
return this.request(url, { ...config, method: 'POST', body: data });
}
async put(url, data, config) {
return this.request(url, { ...config, method: 'PUT', body: data });
}
async delete(url, config) {
return this.request(url, { ...config, method: 'DELETE' });
}
}
// 事件SDK实现
class EventSDKImpl extends SDKBase {
constructor(appId) {
super();
this._appId = appId;
}
async emit(channel, data) {
return this.wrapResponse(this.sendToSystem('events.emit', { channel, data }));
}
async on(channel, callback, config) {
const result = await this.wrapResponse(this.sendToSystem('events.on', { channel, config }));
async emit(channel, data) {
return this.wrapResponse(this.sendToSystem('events.emit', { channel, data }));
}
async on(channel, callback, config) {
const result = await this.wrapResponse(this.sendToSystem('events.on', { channel, config }));
if (result.success && result.data) {
// 注册事件监听器
window.addEventListener('message', (event) => {
if (event.data?.type === 'system:event' && event.data?.subscriptionId === result.data) {
try {
callback(event.data.message);
} catch (error) {
console.error('事件回调处理错误:', error);
}
if (result.success && result.data) {
// 注册事件监听器
window.addEventListener('message', (event) => {
if (event.data?.type === 'system:event' && event.data?.subscriptionId === result.data) {
try {
callback(event.data.message);
} catch (error) {
console.error('事件回调处理错误:', error);
}
});
}
});
}
return result;
}
async off(subscriptionId) {
return this.wrapResponse(this.sendToSystem('events.off', { subscriptionId }));
}
async broadcast(channel, data) {
return this.wrapResponse(this.sendToSystem('events.broadcast', { channel, data }));
}
async sendTo(targetAppId, data) {
return this.wrapResponse(this.sendToSystem('events.sendTo', { targetAppId, data }));
}
}
// UI SDK实现
class UISDKImpl extends SDKBase {
constructor(appId) {
super();
this._appId = appId;
}
async showDialog(options) {
return this.wrapResponse(this.sendToSystem('ui.showDialog', options));
}
async showNotification(options) {
return this.wrapResponse(this.sendToSystem('ui.showNotification', options));
}
async showToast(message, type, duration) {
return this.wrapResponse(this.sendToSystem('ui.showToast', { message, type, duration }));
}
}
// 系统SDK实现
class SystemSDKImpl extends SDKBase {
constructor(appId) {
super();
this._appId = appId;
}
async getSystemInfo() {
return this.wrapResponse(this.sendToSystem('system.getSystemInfo'));
}
async getAppInfo() {
return this.wrapResponse(this.sendToSystem('system.getAppInfo'));
}
async getClipboard() {
return this.wrapResponse(this.sendToSystem('system.getClipboard'));
}
async setClipboard(text) {
return this.wrapResponse(this.sendToSystem('system.setClipboard', { text }));
}
async getCurrentTime() {
const result = await this.wrapResponse(this.sendToSystem('system.getCurrentTime'));
if (result.success && result.data) {
result.data = new Date(result.data);
}
return result;
}
async generateUUID() {
return this.wrapResponse(this.sendToSystem('system.generateUUID'));
}
}
// 主SDK实现类
class SystemDesktopSDKImpl {
constructor() {
this.version = '1.0.0';
this._appId = '';
this._initialized = false;
// 初始化各个子模块为null
this._window = null;
this._storage = null;
this._network = null;
this._events = null;
this._ui = null;
this._system = null;
}
get appId() {
return this._appId;
}
get initialized() {
return this._initialized;
}
get window() {
if (!this._initialized) {
console.warn('[SystemSDK] window模块未初始化');
throw new Error('SDK未初始化');
}
return this._window;
}
get storage() {
if (!this._initialized) {
console.warn('[SystemSDK] storage模块未初始化');
throw new Error('SDK未初始化');
}
return this._storage;
}
get network() {
if (!this._initialized) {
console.warn('[SystemSDK] network模块未初始化');
throw new Error('SDK未初始化');
}
return this._network;
}
get events() {
if (!this._initialized) {
console.warn('[SystemSDK] events模块未初始化');
throw new Error('SDK未初始化');
}
return this._events;
}
get ui() {
if (!this._initialized) {
console.warn('[SystemSDK] ui模块未初始化');
throw new Error('SDK未初始化');
}
return this._ui;
}
get system() {
if (!this._initialized) {
console.warn('[SystemSDK] system模块未初始化');
throw new Error('SDK未初始化');
}
return this._system;
}
async init(config) {
try {
console.log('[SystemSDK] 开始初始化SDK配置:', config);
if (this._initialized) {
console.warn('[SystemSDK] SDK已初始化');
return { success: false, error: 'SDK已初始化' };
}
return result;
}
async off(subscriptionId) {
return this.wrapResponse(this.sendToSystem('events.off', { subscriptionId }));
}
async broadcast(channel, data) {
return this.wrapResponse(this.sendToSystem('events.broadcast', { channel, data }));
}
async sendTo(targetAppId, data) {
return this.wrapResponse(this.sendToSystem('events.sendTo', { targetAppId, data }));
this._appId = config.appId;
// 初始化各个子模块
this._window = new WindowSDKImpl(this._appId);
this._storage = new StorageSDKImpl(this._appId);
this._network = new NetworkSDKImpl(this._appId);
this._events = new EventSDKImpl(this._appId);
this._ui = new UISDKImpl(this._appId);
this._system = new SystemSDKImpl(this._appId);
this._initialized = true;
console.log('[SystemSDK] SDK初始化完成应用ID:', this._appId);
// 通知父窗口SDK已初始化
window.parent.postMessage({
type: 'sdk:initialized',
appId: this._appId
}, '*');
return { success: true, data: true };
} catch (error) {
console.error('[SystemSDK] 初始化失败:', error);
return {
success: false,
error: error instanceof Error ? error.message : '初始化失败',
};
}
}
// UI SDK实现
class UISDKImpl extends SDKBase {
constructor(appId) {
super();
this._appId = appId;
}
async showDialog(options) {
return this.wrapResponse(this.sendToSystem('ui.showDialog', options));
}
async showNotification(options) {
return this.wrapResponse(this.sendToSystem('ui.showNotification', options));
}
async showToast(message, type, duration) {
return this.wrapResponse(this.sendToSystem('ui.showToast', { message, type, duration }));
}
}
// 系统SDK实现
class SystemSDKImpl extends SDKBase {
constructor(appId) {
super();
this._appId = appId;
}
async getSystemInfo() {
return this.wrapResponse(this.sendToSystem('system.getSystemInfo'));
}
async getAppInfo() {
return this.wrapResponse(this.sendToSystem('system.getAppInfo'));
}
async getClipboard() {
return this.wrapResponse(this.sendToSystem('system.getClipboard'));
}
async setClipboard(text) {
return this.wrapResponse(this.sendToSystem('system.setClipboard', { text }));
}
async getCurrentTime() {
const result = await this.wrapResponse(this.sendToSystem('system.getCurrentTime'));
if (result.success && result.data) {
result.data = new Date(result.data);
async destroy() {
try {
if (!this._initialized) {
return { success: false, error: 'SDK未初始化' };
}
return result;
}
async generateUUID() {
return this.wrapResponse(this.sendToSystem('system.generateUUID'));
}
}
// 主SDK实现类
class SystemDesktopSDKImpl {
constructor() {
this.version = '1.0.0';
this._appId = '';
this._initialized = false;
this._appId = '';
console.log('[SystemSDK] SDK已销毁');
// 初始化各个子模块为null
this._window = null;
this._storage = null;
this._network = null;
this._events = null;
this._ui = null;
this._system = null;
}
get appId() {
return this._appId;
}
get initialized() {
return this._initialized;
}
get window() {
if (!this._initialized) {
console.warn('[SystemSDK] window模块未初始化');
throw new Error('SDK未初始化');
}
return this._window;
}
get storage() {
if (!this._initialized) {
console.warn('[SystemSDK] storage模块未初始化');
throw new Error('SDK未初始化');
}
return this._storage;
}
get network() {
if (!this._initialized) {
console.warn('[SystemSDK] network模块未初始化');
throw new Error('SDK未初始化');
}
return this._network;
}
get events() {
if (!this._initialized) {
console.warn('[SystemSDK] events模块未初始化');
throw new Error('SDK未初始化');
}
return this._events;
}
get ui() {
if (!this._initialized) {
console.warn('[SystemSDK] ui模块未初始化');
throw new Error('SDK未初始化');
}
return this._ui;
}
get system() {
if (!this._initialized) {
console.warn('[SystemSDK] system模块未初始化');
throw new Error('SDK未初始化');
}
return this._system;
}
async init(config) {
try {
console.log('[SystemSDK] 开始初始化SDK配置:', config);
if (this._initialized) {
console.warn('[SystemSDK] SDK已初始化');
return { success: false, error: 'SDK已初始化' };
}
this._appId = config.appId;
// 初始化各个子模块
this._window = new WindowSDKImpl(this._appId);
this._storage = new StorageSDKImpl(this._appId);
this._network = new NetworkSDKImpl(this._appId);
this._events = new EventSDKImpl(this._appId);
this._ui = new UISDKImpl(this._appId);
this._system = new SystemSDKImpl(this._appId);
this._initialized = true;
console.log('[SystemSDK] SDK初始化完成应用ID:', this._appId);
// 通知父窗口SDK已初始化
window.parent.postMessage({
type: 'sdk:initialized',
appId: this._appId
}, '*');
return { success: true, data: true };
} catch (error) {
console.error('[SystemSDK] 初始化失败:', error);
return {
success: false,
error: error instanceof Error ? error.message : '初始化失败',
};
}
}
async destroy() {
try {
if (!this._initialized) {
return { success: false, error: 'SDK未初始化' };
}
this._initialized = false;
this._appId = '';
console.log('[SystemSDK] SDK已销毁');
return { success: true, data: true };
} catch (error) {
console.error('[SystemSDK] 销毁失败:', error);
return {
success: false,
error: error instanceof Error ? error.message : '销毁失败',
};
}
return { success: true, data: true };
} catch (error) {
console.error('[SystemSDK] 销毁失败:', error);
return {
success: false,
error: error instanceof Error ? error.message : '销毁失败',
};
}
}
// 创建全局SDK实例
const SystemSDK = new SystemDesktopSDKImpl();
// 在window对象上挂载SDK
window.SystemSDK = SystemSDK;
console.log('[SystemSDK] SDK已在iframe中注入并挂载到window对象');
// 通知系统应用已准备就绪
window.parent.postMessage({ type: 'app:ready' }, '*');
})();
`;
iframeDoc.head.appendChild(script)
}
}
// 创建全局SDK实例
const SystemSDK = new SystemDesktopSDKImpl();
// 在window对象上挂载SDK
window.SystemSDK = SystemSDK;
console.log('[SystemSDK] SDK已在iframe中注入并挂载到window对象');
// 通知系统应用已准备就绪
window.parent.postMessage({ type: 'app:ready' }, '*');
})();
`;
iframeDoc.head.appendChild(script)
}
/**
* 设置性能监控