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