9c552be4fb79494161cb2a50c20ddc07879c4587
[platform/framework/web/crosswalk-tizen.git] / wrt / src / web_window.js
1 'use strict';
2 const {BrowserWindow, app} = require('electron');
3 const IPC_MESSAGE = require('./ipc_message');
4 const WAS_EVENT = require('./was_event');
5 const events = require('./events');
6 const target_debug = require('debug')('TARGET');
7 const webwindow_debug = require('debug')('WEBWINDOW');
8
9 class WebWindow {
10     constructor(webApplication, options) {
11         webwindow_debug('WebWindow : ' + options.path);
12         this.webApplication = webApplication;
13         webApplication.addWebWindow(this);
14         let winopt = this.getBrowserWindowOption();
15         webwindow_debug('winopt: ' + JSON.stringify(winopt));
16         this.mainWindow = new BrowserWindow(winopt);
17         if (process.env.DEBUG || options.devMode) {
18             this.mainWindow.webContents.openDevTools({
19                 detached: true
20             });
21         }
22         webwindow_debug('mainWindow id : ' + this.mainWindow.id);
23         this.handleEvents(winopt);
24         this.appID = options.appID;
25         webwindow_debug('this.appID ' + this.appID);
26         this.setUrl(options.path);
27     }
28     getBrowserWindowOption() {
29         return {
30             fullscreen: false,
31             show: false,
32             'web-preferences': {
33                 'direct-write': true,
34                 'subpixel-font-scaling': false,
35                 'web-security': false
36             }
37         };
38     }
39     handleEvents(options) {
40         var self = this;
41         this.mainWindow.on('focus', function() {
42             webwindow_debug('WebWindow : focus');
43             events.emit(WAS_EVENT.RUNTIME.FOCUS, 'web_window', self.mainWindow.id);
44         });
45         this.mainWindow.on('blur', function() {
46             webwindow_debug('WebWindow : blur');
47             events.emit(WAS_EVENT.RUNTIME.UNFOCUS, 'web_window', self.mainWindow.id);
48         });
49         this.mainWindow.on('maximize', function(event, command, args) {
50             webwindow_debug('WebWindow : maximize');
51             webwindow_debug('WebWindow : maximize event  = ' + (JSON.stringify(command)));
52             webwindow_debug('WebWindow : maximize event  = ' + (JSON.stringify(args)));
53         });
54         this.mainWindow.on('unmaximize', function() {
55             webwindow_debug('WebWindow : unmaximize');
56         });
57         this.mainWindow.on('minimize', function() {
58             webwindow_debug('WebWindow : minimize');
59         });
60         this.mainWindow.on('restore', function() {
61             webwindow_debug('WebWindow : restore');
62         });
63         this.mainWindow.on('resize', function() {
64             webwindow_debug('WebWindow : resize');
65         });
66         this.mainWindow.on('move', function() {
67             webwindow_debug('WebWindow : move');
68         });
69         this.mainWindow.on('enter-full-screen', function() {
70             webwindow_debug('WebWindow : enter-full-screen');
71         });
72         this.mainWindow.on('leave-full-screen', function() {
73             webwindow_debug('WebWindow : leave-full-screen');
74         });
75         this.mainWindow.on('enter-html-full-screen', function() {
76             webwindow_debug('WebWindow : enter-html-full-screen');
77         });
78         this.mainWindow.on('leave-html-full-screen', function() {
79             webwindow_debug('WebWindow : leave-html-full-screen');
80         });
81         this.mainWindow.on('devtools-opened', function() {
82             webwindow_debug('WebWindow : devtools-opened');
83         });
84         this.mainWindow.on('devtools-closed', function() {
85             webwindow_debug('WebWindow : devtools-closed');
86         });
87         this.mainWindow.on('devtools-focused', function() {
88             webwindow_debug('WebWindow : devtools-focused');
89         });
90         this.mainWindow.on('app-command', function() {
91             webwindow_debug('WebWindow : app-command');
92         });
93         this.mainWindow.on('page-title-updated', function() {
94             webwindow_debug('WebWindow : page-title-updated');
95         });
96         this.mainWindow.on('closed', function() {
97             webwindow_debug('mainWindow closed!');
98             //TODO: should remove the reference to the window
99             self.webApplication.removeWebWindow(self);
100         });
101         this.mainWindow.on('responsive', function() {
102             webwindow_debug('WebWindow : responsive');
103         });
104         this.mainWindow.on('unresponsive', function() {
105             webwindow_debug('WebWindow : unresponsive');
106         });
107         this.mainWindow.on('language-changed', function(event, locale) {
108             webwindow_debug('WebWindow : language-changed event  = ' + (JSON.stringify(locale)));
109         });
110         this.mainWindow.on('app-on-suspend', function() {
111             webwindow_debug('WebWindow : app-on-suspend');
112             events.emit(WAS_EVENT.WEBAPPLICATION.SUSPEND, 'web_window', self.mainWindow.id);
113         });
114         this.mainWindow.on('app-on-resume', function() {
115             webwindow_debug('WebWindow : app-on-resume');
116             events.emit(WAS_EVENT.WEBAPPLICATION.RESUME, 'web_window', self.mainWindow.id);
117         });
118         this.mainWindow.webContents.on('crashed', function() {
119             webwindow_debug('WebWindow : webContents crashed');
120             self.webApplication.exit(100);
121         });
122         this.mainWindow.webContents.on('did-finish-load', function() {
123             webwindow_debug('WebWindow : webContents did-finish-load');
124             if(!options.show){
125                 webwindow_debug('WebWindow : browserWindow show options is ',options.show);
126                 self.show();
127             }
128             app.emit(IPC_MESSAGE.WEBCONTENTS.DID_FINISH_LOAD, self.mainWindow.id);
129         });
130         this.mainWindow.webContents.on('did-fail-load', function(event, errorCode, errorDescription, validatedUrl) {
131             webwindow_debug('WebWindow : webContents did-fail-load');
132             var params = {};
133             params.errorCode = errorCode;
134             params.errorDescription = errorDescription;
135             params.validatedUrl = validatedUrl;
136             app.emit(IPC_MESSAGE.WEBCONTENTS.DID_FAIL_LOAD, self.mainWindow.id, params);
137         });
138         this.mainWindow.webContents.on('did-frame-finish-load', function(event, isMainFrame) {
139             webwindow_debug('WebWindow : webContents did-frame-finish-load:');
140             var params = {};
141             params.isMainFrame = isMainFrame;
142             app.emit(IPC_MESSAGE.WEBCONTENTS.DID_FRAME_FINISH_LOAD, self.mainWindow.id, params);
143         });
144         this.mainWindow.webContents.on('did-start-loading', function() {
145             webwindow_debug('WebWindow : webContents did-start-loading');
146             app.emit(IPC_MESSAGE.WEBCONTENTS.DID_START_LOADING, self.mainWindow.id);
147         });
148         this.mainWindow.webContents.on('did-stop-loading', function() {
149             webwindow_debug('WebWindow : webContents did-stop-loading');
150             app.emit(IPC_MESSAGE.WEBCONTENTS.DID_STOP_LOADING, self.mainWindow.id);
151         });
152         this.mainWindow.webContents.on('did-get-response-details', function(event, status, newUrl, originalUrl, httpResponseCode, requestMethod, referrer, headers) {
153             webwindow_debug('WebWindow : webContents did-get-response-details');
154             var params = {};
155             params.status = status;
156             params.newUrl = newUrl;
157             params.originalUrl = originalUrl;
158             params.httpResponseCode = httpResponseCode;
159             params.requestMethod = requestMethod;
160             params.referrer = referrer;
161             params.headers = headers;
162             app.emit(IPC_MESSAGE.WEBCONTENTS.DID_GET_RESPONSE_DETAILS, self.mainWindow.id, params);
163         });
164         this.mainWindow.webContents.on('did-get-redirect-request', function(event, oldUrl, newUrl, isMainFrame, httpResponseCode, requestMethod, referrer, headers) {
165             webwindow_debug('WebWindow : webContents did-get-redirect-request');
166             var params = {};
167             params.oldUrl = oldUrl;
168             params.newUrl = newUrl;
169             params.isMainFrame = isMainFrame;
170             params.httpResponseCode = httpResponseCode;
171             params.requestMethod = requestMethod;
172             params.referrer = referrer;
173             params.headers = headers;
174             app.emit(IPC_MESSAGE.WEBCONTENTS.DID_GET_REDIRECT_REQUEST, self.mainWindow.id, params);
175         });
176         this.mainWindow.webContents.on('dom-ready', function(event) {
177             webwindow_debug('WebWindow : webContents dom-ready');
178             app.emit(IPC_MESSAGE.WEBCONTENTS.DOM_READY, self.mainWindow.id);
179         });
180         this.mainWindow.webContents.on('page-favicon-updated', function(event, favicons) {
181             webwindow_debug('WebWindow : webContents page-favicon-updated');
182             var params = {};
183             params.favicons = favicons;
184             app.emit(IPC_MESSAGE.WEBCONTENTS.PAGE_FAVICON_UPDATED, self.mainWindow.id, params);
185         });
186         this.mainWindow.webContents.on('new-window', function(event, url, frameName, disposition, options) {
187             webwindow_debug('WebWindow : webContents new-window');
188             var params = {};
189             params.url = url;
190             params.frameName = frameName;
191             params.disposition = disposition;
192             params.options = options;
193             app.emit(IPC_MESSAGE.WEBCONTENTS.NEW_WINDOW, self.mainWindow.id, params);
194         });
195         this.mainWindow.webContents.on('will-navigate', function(event, url) {
196             webwindow_debug('WebWindow : webContents will-navigate');
197         });
198         this.mainWindow.webContents.on('crashed', function() {
199             webwindow_debug('WebWindow : webContents crashed');
200             app.emit(IPC_MESSAGE.WEBCONTENTS.CRASHED, self.mainWindow.id);
201         });
202         this.mainWindow.webContents.on('plugin-crashed', function(event, name, version) {
203             webwindow_debug('WebWindow : webContents plugin-crashed');
204             var params = {};
205             params.name = name;
206             params.version = version;
207             app.emit(IPC_MESSAGE.WEBCONTENTS.PLUGIN_CRASHED, self.mainWindow.id, params);
208         });
209         this.mainWindow.webContents.on('destroyed', function() {
210             webwindow_debug('WebWindow : webContents destroyed');
211             app.emit(IPC_MESSAGE.WEBCONTENTS.DESTROYED, self.mainWindow.id);
212         });
213     }
214     setUrl(path) {
215         this.mainWindow.loadURL(path);
216     }
217     show() {
218         this.mainWindow.show();
219     }
220     close() {
221         this.mainWindow.close();
222     }
223     focus() {
224         this.mainWindow.focus();
225     }
226     minimize() {
227         this.mainWindow.minimize();
228     }
229     maximize() {
230         this.mainWindow.maximize();
231     }
232     restore() {
233         this.mainWindow.restore();
234     }
235     isFocused() {
236         this.mainWindow.isFocused();
237     }
238     isMinimized() {
239         this.mainWindow.isMinimized();
240     }
241     isWebWindowFocused() {
242         this.mainWindow.isWebWindowFocused();
243     }
244     reload() {
245         this.mainWindow.restart();
246     }
247     toggleDevTools() {
248         this.mainWindow.toggleDevTools();
249     }
250     send(event, data) {
251         this.mainWindow.webContents.send(event, data);
252     }
253     getLocale() {
254         return this.mainWindow.getLocale();
255     }
256 }
257 module.exports = WebWindow;