ariaNgNativeElectronService.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').factory('ariaNgNativeElectronService', ['$q', 'ariaNgLogService', 'ariaNgLocalizationService', function ($q, ariaNgLogService, ariaNgLocalizationService) {
  4. var electron = angular.isFunction(window.nodeRequire) ? nodeRequire('electron') : {};
  5. var ipcRenderer = electron.ipcRenderer || {};
  6. var onMainProcessEvent = function (channel, callback) {
  7. ipcRenderer.on && ipcRenderer.on(channel, callback);
  8. };
  9. var removeMainProcessEvent = function (channel, callback) {
  10. ipcRenderer.removeListener && ipcRenderer.removeListener(channel, callback);
  11. };
  12. var invokeMainProcessMethod = function (channel, ...args) {
  13. ipcRenderer.send && ipcRenderer.send(channel, ...args);
  14. };
  15. var invokeMainProcessMethodSync = function (channel, ...args) {
  16. if (!ipcRenderer.sendSync) {
  17. return null;
  18. }
  19. return ipcRenderer.sendSync(channel, ...args);
  20. };
  21. var invokeMainProcessMethodAsync = function (channel, ...args) {
  22. if (!ipcRenderer.invoke) {
  23. return null;
  24. }
  25. return ipcRenderer.invoke(channel, ...args);
  26. };
  27. onMainProcessEvent('on-main-log-debug', function (event, msg, obj) {
  28. ariaNgLogService.debug(msg, obj);
  29. });
  30. onMainProcessEvent('on-main-log-info', function (event, msg, obj) {
  31. ariaNgLogService.info(msg, obj);
  32. });
  33. onMainProcessEvent('on-main-log-warn', function (event, msg, obj) {
  34. ariaNgLogService.warn(msg, obj);
  35. });
  36. onMainProcessEvent('on-main-log-error', function (event, msg, obj) {
  37. ariaNgLogService.error(msg, obj);
  38. });
  39. invokeMainProcessMethod('on-render-electron-service-inited');
  40. return {
  41. getRuntimeEnvironment: function () {
  42. return invokeMainProcessMethodSync('render-sync-get-runtime-environment');
  43. },
  44. getVersion: function() {
  45. return invokeMainProcessMethodSync('render-sync-get-global-setting', 'version');
  46. },
  47. getAriaNgVersion: function() {
  48. return invokeMainProcessMethodSync('render-sync-get-global-setting', 'ariaNgVersion');
  49. },
  50. isDevMode: function () {
  51. return invokeMainProcessMethodSync('render-sync-get-global-setting', 'isDevMode');
  52. },
  53. useCustomAppTitle: function () {
  54. return invokeMainProcessMethodSync('render-sync-get-global-setting', 'useCustomAppTitle');
  55. },
  56. getWindowMaximizedAsync: function (callback) {
  57. return invokeMainProcessMethodAsync('render-get-native-window-maximized')
  58. .then(function onReceive(maximized) {
  59. if (callback) {
  60. callback(maximized);
  61. }
  62. });
  63. },
  64. minimizeWindow: function () {
  65. invokeMainProcessMethod('render-minimize-native-window');
  66. },
  67. maximizeOrRestoreWindow: function () {
  68. invokeMainProcessMethod('render-maximize-or-restore-native-window');
  69. },
  70. reload: function () {
  71. invokeMainProcessMethod('render-reload-native-window');
  72. },
  73. exitApp: function () {
  74. invokeMainProcessMethod('render-exit-native-app');
  75. },
  76. showTextboxContextMenu: function (context) {
  77. invokeMainProcessMethod('render-show-textbox-context-menu', context);
  78. },
  79. showSystemNotification: function (context) {
  80. invokeMainProcessMethod('render-show-system-notification', context);
  81. },
  82. setApplicationMenu: function () {
  83. invokeMainProcessMethod('render-update-app-menu-label', {
  84. AboutAriaNgNative: ariaNgLocalizationService.getLocalizedText('menu.AboutAriaNgNative'),
  85. Services: ariaNgLocalizationService.getLocalizedText('menu.Services'),
  86. HideAriaNgNative: ariaNgLocalizationService.getLocalizedText('menu.HideAriaNgNative'),
  87. HideOthers: ariaNgLocalizationService.getLocalizedText('menu.HideOthers'),
  88. ShowAll: ariaNgLocalizationService.getLocalizedText('menu.ShowAll'),
  89. QuitAriaNgNative: ariaNgLocalizationService.getLocalizedText('menu.QuitAriaNgNative'),
  90. Edit: ariaNgLocalizationService.getLocalizedText('menu.Edit'),
  91. Undo: ariaNgLocalizationService.getLocalizedText('menu.Undo'),
  92. Redo: ariaNgLocalizationService.getLocalizedText('menu.Redo'),
  93. Cut: ariaNgLocalizationService.getLocalizedText('menu.Cut'),
  94. Copy: ariaNgLocalizationService.getLocalizedText('menu.Copy'),
  95. Paste: ariaNgLocalizationService.getLocalizedText('menu.Paste'),
  96. Delete: ariaNgLocalizationService.getLocalizedText('menu.Delete'),
  97. SelectAll: ariaNgLocalizationService.getLocalizedText('menu.SelectAll'),
  98. Window: ariaNgLocalizationService.getLocalizedText('menu.Window'),
  99. Minimize: ariaNgLocalizationService.getLocalizedText('menu.Minimize'),
  100. Zoom: ariaNgLocalizationService.getLocalizedText('menu.Zoom'),
  101. BringAllToFront: ariaNgLocalizationService.getLocalizedText('menu.BringAllToFront')
  102. });
  103. },
  104. setTrayMenu: function () {
  105. invokeMainProcessMethod('render-update-tray-menu-label', {
  106. ShowAriaNgNative: ariaNgLocalizationService.getLocalizedText('tray.ShowAriaNgNative'),
  107. Exit: ariaNgLocalizationService.getLocalizedText('tray.Exit')
  108. });
  109. },
  110. setTrayToolTip: function (value) {
  111. invokeMainProcessMethod('render-update-tray-tip', value);
  112. },
  113. setMainWindowLanguage: function () {
  114. this.setApplicationMenu();
  115. this.setTrayMenu();
  116. },
  117. getNativeConfig: function () {
  118. var config = invokeMainProcessMethodSync('render-sync-get-native-config');
  119. var cfg = {};
  120. for (var key in config) {
  121. if (!config.hasOwnProperty(key)) {
  122. continue;
  123. }
  124. cfg[key] = angular.copy(config[key]);
  125. }
  126. return cfg;
  127. },
  128. setDefaultPosition: function (value) {
  129. invokeMainProcessMethod('render-set-native-config-default-position', value);
  130. },
  131. setMinimizedToTray: function (value) {
  132. invokeMainProcessMethod('render-set-native-config-minimized-to-tray', value);
  133. },
  134. setExecCommandOnStartup: function (value) {
  135. invokeMainProcessMethod('render-set-native-config-exec-command-on-startup', value);
  136. },
  137. setExecCommandArgumentsOnStartup: function (value) {
  138. invokeMainProcessMethod('render-set-native-config-exec-command-arguments-on-startup', value);
  139. },
  140. setExecDetachedCommandOnStartup: function (value) {
  141. invokeMainProcessMethod('render-set-native-config-exec-detached-command-on-startup', value);
  142. },
  143. getLastCheckUpdatesTimeAsync: function (callback) {
  144. return invokeMainProcessMethodAsync('render-get-native-config-last-check-updates-time')
  145. .then(function onReceive(lastCheckUpdatesTime) {
  146. if (callback) {
  147. callback(lastCheckUpdatesTime);
  148. }
  149. });
  150. },
  151. setLastCheckUpdatesTime: function (value) {
  152. invokeMainProcessMethod('render-set-native-config-last-check-updates-time', value);
  153. },
  154. getStartupCommandOutputAsync: function () {
  155. return invokeMainProcessMethodAsync('render-get-startup-command-process-output');
  156. },
  157. requestHttp: function (requestContext) {
  158. var deferred = $q.defer();
  159. invokeMainProcessMethodAsync('render-request-http', requestContext)
  160. .then(function onReceive(result) {
  161. if (result && result.success) {
  162. deferred.resolve(result.response);
  163. } else {
  164. deferred.reject(result.response);
  165. }
  166. }).catch(function onError() {
  167. deferred.reject({});
  168. });
  169. return deferred.promise;
  170. },
  171. createWebSocketClient: function (rpcUrl, options) {
  172. var WebSocketClient = function (rpcUrl, options) {
  173. var openCallback = null;
  174. var closeCallback = null;
  175. var messageCallback = null;
  176. Object.defineProperty(WebSocketClient.prototype, 'readyState', {
  177. get: function get() {
  178. return invokeMainProcessMethodSync('render-get-websocket-readystate');
  179. },
  180. set: function set() {
  181. throw new Error('The \"readyState\" property is readonly.');
  182. }
  183. });
  184. this.send = function (request) {
  185. invokeMainProcessMethod('render-send-websocket-message', {
  186. url: rpcUrl,
  187. data: request
  188. });
  189. };
  190. this.reconnect = function () {
  191. invokeMainProcessMethod('render-reconnect-websocket', rpcUrl, options);
  192. };
  193. this.onOpen = function (callback) {
  194. openCallback = callback;
  195. };
  196. this.onClose = function (callback) {
  197. closeCallback = callback;
  198. };
  199. this.onMessage = function (callback) {
  200. messageCallback = callback;
  201. };
  202. onMainProcessEvent('on-main-websocket-open', function (event, e) {
  203. if (e.url !== rpcUrl) {
  204. ariaNgLogService.debug('[ariaNgNativeElectronService.websocket.onOpen] event dropped, because rpc url not equals, excepted url: ' + rpcUrl + ", actual url: " + e.url);
  205. return;
  206. }
  207. if (angular.isFunction(openCallback)) {
  208. openCallback(e);
  209. }
  210. });
  211. onMainProcessEvent('on-main-websocket-close', function (event, e) {
  212. if (e.url !== rpcUrl) {
  213. ariaNgLogService.debug('[ariaNgNativeElectronService.websocket.onClose] event dropped, because rpc url not equals, excepted url: ' + rpcUrl + ", actual url: " + e.url);
  214. return;
  215. }
  216. if (angular.isFunction(closeCallback)) {
  217. closeCallback(e);
  218. }
  219. });
  220. onMainProcessEvent('on-main-websocket-message', function (event, message) {
  221. if (message.url !== rpcUrl) {
  222. ariaNgLogService.debug('[ariaNgNativeElectronService.websocket.onMessage] event dropped, because rpc url not equals, excepted url: ' + rpcUrl + ", actual url: " + message.url, message.data);
  223. return;
  224. }
  225. if (angular.isFunction(messageCallback)) {
  226. messageCallback(message);
  227. }
  228. });
  229. invokeMainProcessMethod('render-connect-websocket', rpcUrl, options);
  230. };
  231. return new WebSocketClient(rpcUrl, options);
  232. },
  233. openProjectLink: function () {
  234. invokeMainProcessMethod('render-open-external-url', 'https://github.com/mayswind/AriaNg-Native');
  235. },
  236. openProjectReleaseLink: function () {
  237. invokeMainProcessMethod('render-open-external-url', 'https://github.com/mayswind/AriaNg-Native/releases');
  238. },
  239. readPackageFile: function (path) {
  240. return invokeMainProcessMethodSync('render-sync-get-package-file-content', path);
  241. },
  242. getLocalFSFileBufferAsync: function (fullpath, callback) {
  243. return invokeMainProcessMethodAsync('render-get-localfs-file-buffer', fullpath)
  244. .then(function onReceive(buffer) {
  245. if (callback) {
  246. callback(buffer);
  247. }
  248. });
  249. },
  250. getLocalFSExistsAsync: function (fullpath, callback) {
  251. return invokeMainProcessMethodAsync('render-get-localfs-exists', fullpath)
  252. .then(function onReceive(exists) {
  253. if (callback) {
  254. callback(exists);
  255. }
  256. });
  257. },
  258. openFileInDirectory: function (dir, filename) {
  259. invokeMainProcessMethod('render-open-local-directory', dir, filename);
  260. },
  261. showOpenFileDialogAsync: function (filters, callback) {
  262. return invokeMainProcessMethodAsync('render-show-open-file-dialog', filters)
  263. .then(function onReceive(result) {
  264. if (callback) {
  265. callback({
  266. canceled: result.canceled,
  267. filePaths: result.filePaths
  268. });
  269. }
  270. });
  271. },
  272. parseBittorrentInfo: function (data) {
  273. var info = angular.copy(invokeMainProcessMethodSync('render-sync-parse-bittorrent-info', data));
  274. info.type = 'bittorrent';
  275. ariaNgLogService.debug('[ariaNgNativeElectronService.parseBittorrentInfo] bittorrent info', info);
  276. return info;
  277. },
  278. notifyMainProcessViewLoaded: function (locationPath) {
  279. invokeMainProcessMethod('on-render-view-content-loaded', locationPath);
  280. },
  281. notifyMainProcessorNewDropFile: function (message) {
  282. invokeMainProcessMethod('on-render-new-drop-file', message);
  283. },
  284. notifyMainProcessorNewDropText: function (message) {
  285. invokeMainProcessMethod('on-render-new-drop-text', message);
  286. },
  287. onMainWindowMaximize: function (callback) {
  288. onMainProcessEvent('on-main-window-maximized', callback);
  289. },
  290. onMainWindowUnmaximize: function (callback) {
  291. onMainProcessEvent('on-main-window-unmaximized', callback);
  292. },
  293. onMainProcessChangeDevMode: function (callback) {
  294. onMainProcessEvent('on-main-change-dev-mode', callback);
  295. },
  296. onMainProcessShowError: function (callback) {
  297. onMainProcessEvent('on-main-show-error', callback);
  298. },
  299. onMainProcessNavigateTo: function (callback) {
  300. onMainProcessEvent('on-main-navigate-to', callback);
  301. },
  302. onMainProcessNewTaskFromFile: function (callback) {
  303. onMainProcessEvent('on-main-new-task-from-file', callback);
  304. },
  305. onMainProcessNewTaskFromText: function (callback) {
  306. onMainProcessEvent('on-main-new-task-from-text', callback);
  307. },
  308. removeMainProcessNewTaskFromFileCallback: function (callback) {
  309. removeMainProcessEvent('on-main-new-task-from-file', callback);
  310. },
  311. removeMainProcessNewTaskFromTextCallback: function (callback) {
  312. removeMainProcessEvent('on-main-new-task-from-text', callback);
  313. }
  314. };
  315. }]);
  316. }());