0bdaa493cbb0feda34513aac2141e25130ccb6fc
[platform/framework/web/crosswalk-tizen.git] / wrt / src / runtime.js
1 'use strict';
2 const {app, ipcMain, pwrt} = require('electron');
3 const IPC_MESSAGE = require('./ipc_message');
4 const WAS_EVENT = require('./was_event');
5 const WebApplication = require('./web_application');
6 const events = require('./events');
7 const runtime_debug = require('debug')('RUNTIME');
8 const try_debug = require('debug')('TRY');
9
10 class Runtime {
11     constructor(options) {
12         this.webApplication = null;
13         this.quitting = false;
14         this.handleWasEvents();
15         this.handleIpcMessages();
16         this.isLaunched = false;
17
18         var _this = this;
19         app.on('before-quit', function(event) {
20             runtime_debug('before-quit');
21             _this.quitting = true;
22         });
23         app.on('will-quit', function(event) {
24             runtime_debug('will-quit');
25         });
26         app.on('quit', function(event) {
27             return runtime_debug('quit');
28         });
29         app.on('browser-window-blur', function() {
30             return runtime_debug('browser-window-blur');
31         });
32         app.on('browser-window-focus', function() {
33             return runtime_debug('browser-window-focus');
34         });
35         app.on('browser-window-created', function() {
36             return runtime_debug('browser-window-created');
37         });
38         app.on('gpu-process-crashed', function() {
39             return runtime_debug('gpu-process-crashed');
40         });
41         app.on('window-all-closed', function(event) {
42             return runtime_debug('window-all-closed');
43         });
44         app.on('will-finish-launching', function(event) {
45             runtime_debug('will-finish-launching');
46             if (pwrt.isElectronLaunch()) {
47                 console.log("Electron App launch");
48                 let filePath = pwrt.getPath();
49                 let pkgJson = require(filePath.substr(7, filePath.length - 12));
50                 let mainJsPath = filePath.substr(7, filePath.length - 19) +
51                                                 (pkgJson.main || 'index.js');
52
53                 const Module = require('module');
54                 Module._load(mainJsPath, Module, true);
55              }
56         });
57         app.on('ready', function(event) {
58             runtime_debug('ready');
59             if (pwrt.isElectronLaunch()) {
60                 return;
61             }
62             this.webApplication = new WebApplication(options);
63         });
64     }
65     onPause(web_window_id) {
66         runtime_debug('onPause : ' + web_window_id);
67         try {
68             return this.webApplication.suspend(web_window_id);
69         } catch(e) {
70             try_debug('catch e : ', e.message);
71         }
72     }
73     onResume(web_window_id) {
74         runtime_debug('onResume : ' + web_window_id);
75         try {
76             return this.webApplication.resume(web_window_id);
77         } catch(e) {
78             try_debug('catch e : ', e.message);
79         }
80     }
81     onAppControl() {}
82     onLanguageChanged() {}
83     onLowMemory() {}
84     handleWasEvents() {
85         var _this = this;
86         events.on(WAS_EVENT.WEBAPPLICATION.RESUME, (sender, id) => {
87             runtime_debug('handleWasMessages: focus ' + id);
88             return _this.onResume(id);
89         });
90         events.on(WAS_EVENT.WEBAPPLICATION.SUSPEND, (sender, id) => {
91             return _this.onPause(id);
92         });
93     }
94     handleIpcMessages() {
95         var _this = this;
96         ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DID_FINISH_LOAD, (sender, id) => {
97             runtime_debug('handleWasMessages: did-finish-load ' + id);
98             return app.emit(IPC_MESSAGE.WEBCONTENTS.DID_FINISH_LOAD, id);
99         });
100         ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DID_FAIL_LOAD, function(sender, id, params) {
101             runtime_debug('handleWasMessages: did-fail-load ' + id);
102             runtime_debug('  params:' + JSON.stringify(params));
103             return app.emit(IPC_MESSAGE.WEBCONTENTS.DID_FAIL_LOAD, id, params);
104         });
105         ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DID_FRAME_FINISH_LOAD, function(sender, id, params) {
106             runtime_debug('handleWasMessages: did-frame-finish-load ' + id);
107             runtime_debug('  params:' + JSON.stringify(params));
108             return app.emit(IPC_MESSAGE.WEBCONTENTS.DID_FRAME_FINISH_LOAD, id, params);
109         });
110         ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DID_START_LOADING, (sender, id) => {
111             runtime_debug('handleWasMessages: did-start-loading ' + id);
112             return app.emit(IPC_MESSAGE.WEBCONTENTS.DID_START_LOADING, id);
113         });
114         ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DID_STOP_LOADING, (sender, id) => {
115             runtime_debug('handleWasMessages: did-stop-loading ' + id);
116             return app.emit(IPC_MESSAGE.WEBCONTENTS.DID_STOP_LOADING, id);
117         });
118         ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DID_GET_RESPONSE_DETAILS, function(sender, id, params) {
119             runtime_debug('handleWasMessages: did-get-response-details ' + id);
120             runtime_debug('  params:' + JSON.stringify(params));
121             return app.emit(IPC_MESSAGE.WEBCONTENTS.DID_GET_RESPONSE_DETAILS, id, params);
122         });
123         ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DID_GET_REDIRECT_REQUEST, function(sender, id, params) {
124             runtime_debug('handleWasMessages: did-get-redirect-request ' + id);
125             runtime_debug('  params:' + JSON.stringify(params));
126             return app.emit(IPC_MESSAGE.WEBCONTENTS.DID_GET_REDIRECT_REQUEST, id, params);
127         });
128         ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DOM_READY, (sender, id) => {
129             runtime_debug('handleWasMessages: dom-ready ' + id);
130             return app.emit(IPC_MESSAGE.WEBCONTENTS.DOM_READY, id);
131         });
132         ipcMain.on(IPC_MESSAGE.WEBCONTENTS.PAGE_FAVICON_UPDATED, function(sender, id, params) {
133             runtime_debug('handleWasMessages: page-favicon-updated ' + id);
134             runtime_debug('  params:' + JSON.stringify(params));
135             return app.emit(IPC_MESSAGE.WEBCONTENTS.PAGE_FAVICON_UPDATED, id, params);
136         });
137         ipcMain.on(IPC_MESSAGE.WEBCONTENTS.NEW_WINDOW, function(sender, id, params) {
138             runtime_debug('handleWasMessages: new-window ' + id);
139             runtime_debug('  params:' + JSON.stringify(params));
140             return app.emit(IPC_MESSAGE.WEBCONTENTS.NEW_WINDOW, id, params);
141         });
142         ipcMain.on(IPC_MESSAGE.WEBCONTENTS.CRASHED, (sender, id) => {
143             runtime_debug('handleWasMessages: crashed ' + id);
144             return app.emit(IPC_MESSAGE.WEBCONTENTS.CRASHED, id);
145         });
146         ipcMain.on(IPC_MESSAGE.WEBCONTENTS.PLUGIN_CRASHED, function(sender, id, params) {
147             runtime_debug('handleWasMessages: plugin-crashed ' + id);
148             runtime_debug('  params:' + JSON.stringify(params));
149             return app.emit(IPC_MESSAGE.WEBCONTENTS.PLUGIN_CRASHED, id, params);
150         });
151         ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DESTROYED, (sender, id) => {
152             runtime_debug('handleWasMessages: destroyed ' + id);
153             return app.emit(IPC_MESSAGE.WEBCONTENTS.DESTROYED, id);
154         });
155     }
156 }
157 module.exports = Runtime;