15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const { BrowserWindow } = require('electron');
const fs = require('fs');
const LoadView = require('./loadView');
const Loader = require('../../loader');
 
class RendererJob {
 
  /**
    * constructor
    * @param  {String} name - job name
    * @param  {String} filepath - filepath to file
    * @param  {Object} options - options to create BrowserWindow
    */
  constructor(name, filepath, opt = {}) {
    // TODO Object.assign 只能单层对象结构,多层的对象会直接覆盖
    let options = Object.assign({
      show: false,
      webPreferences: {
        webSecurity: true,
        nodeIntegration: true,
        contextIsolation: false,
        //enableRemoteModule: true
      }
    }, opt);
 
    this.subWin = new BrowserWindow(options);
 
    this.jobReady = false;
    this.exec = Loader.getFullpath(filepath);;
    this.name = name;
    this.listeners = [];
    this.callbacks = [];
    this.fails = [];
    this.id = this.subWin.id;
    this.webSecurity = options.webPreferences.webSecurity;
 
    // this.callbacks.push(() => {
    //   MessageChannel.registry(name, this.id, this.subWin.webContents.getOSProcessId());
    // });
 
    // job state listener
    this.subWin.webContents.on('did-finish-load', this._didFinishLoad);
    this.subWin.webContents.on('did-fail-load', this._didFailLoad);
 
    // load job
    this._loadJob(this.exec);
  }
 
  /**
   * 显示开发者工具栏
   */
  openDevTools() {
    this.subWin.webContents.openDevTools({
      mode: 'undocked'
    });
  }
 
  /**
   * 窗口加载完成,即业务代码执行完毕
   */
  _didFinishLoad = () => {
    this.jobReady = true;
    this.callbacks.forEach(callback => {
      callback(this.id);
    });
  }
 
  /**
   * 窗口加载失败,即业务运行失败
   */
  _didFailLoad = (error) => {
    this.jobReady = false;
    this.fails.forEach(handle => {
      handle(error.toString());
    });
  }
 
 
  /**
   * 加载任务
   */
  _loadJob(filepath) {
    if (!this.webSecurity) {
      this._loadURLUnsafe(filepath);
    } else {
      this._loadURLSafe(filepath);
    }
  }
 
  /**
   * 安全的脚本注入
   */
  _loadURLSafe(filepath) {
    return new Promise((resolve, reject) => {
      fs.readFile(filepath, { encoding: 'utf-8' }, (err, buffer) => {
        if (err) {
          reject(err);
          this._didFailLoad(err);
          return console.error(err);
        }
 
        let param = {
          webSecurity: true,
          script: buffer.toString(),
          title: `${this.name} job`,
          base: filepath
        }
        const viewData = LoadView(param);
 
        this.subWin.loadURL(viewData)
        .then(resolve)
        .catch(err => {
          reject(err);
          this._didFailLoad(err);
          console.error(err);
        });
      })
    })
  }
 
  /**
   * 不安全的脚本注入
   */
  _loadURLUnsafe(filepath) {
    let param = {
      webSecurity: false,
      src: this.exec,
      title: `${this.name} job`,
      base: filepath
    }
    const viewData = LoadView(param);
 
    this.subWin.loadURL(viewData)
    .catch(err => {
      this._didFailLoad(err);
      console.error(err);
    });
  }
}
 
module.exports = RendererJob;