2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import { wrt } from '../browser/wrt'; // Load first for log
20 import { addonManager } from './addon_manager';
21 import { app } from 'electron';
22 import { WebApplication } from './web_application';
25 webApplication?: WebApplication = undefined;
27 inspectorEnabledByVconf = false;
30 app.on('before-quit', (event) => {
31 console.log('before-quit');
32 this.webApplication?.quit();
34 app.on('will-quit', (event) => {
35 console.log('will-quit');
36 addonManager.deactivateAll();
38 app.on('quit', (event) => {
40 if (this.webApplication) {
41 this.webApplication.finalize();
42 this.webApplication = undefined;
45 app.on('browser-window-created', () => {
46 console.log('browser-window-created');
47 if (!this.isLaunched) {
48 addonManager.activateAll();
49 this.isLaunched = true;
52 app.on('window-all-closed', () => {
53 console.log('window-all-closed');
56 app.on('web-contents-created', (event, webContents) => {
57 console.log('web-contents-created');
60 webContents.on('before-input-event', (event, input) => {
61 if (this.isLaunched && this.webApplication)
62 this.handleKeyEvents(input.key);
65 app.once('ready', (event) => {
67 let addonAvailable = addonManager.build();
68 console.log("addonBuild : " + addonAvailable);
70 const XWalkExtension = require('../common/wrt_xwalk_extension');
71 XWalkExtension.initialize();
72 XWalkExtension.preventCleanup();
75 wrt.tv.importCertificate('');
76 wrt.tv.clearDeadMount();
79 wrt.on('app-control', (event, appControl) => {
80 console.log('app-control');
81 let loadInfo = appControl.getLoadInfo();
82 let src = loadInfo.getSrc();
84 if (wrt.isElectronApp()) {
85 console.log('Electron App launch');
86 const Module = require('module');
87 Module.globalPaths.push(wrt.getAppPath());
88 let filePath = src[7] === '/' ? src.substr(8) : src.substr(7); // strip "file://"
89 let pkgJson = require(filePath);
90 let pos = filePath.lastIndexOf('/');
92 let mainJsPath = (pos !== -1 ? filePath.substr(0, pos + 1) : '') +
93 (pkgJson.main || 'index.js');
94 console.log('loading path:', mainJsPath);
95 Module._load(mainJsPath, Module, true);
98 console.log('Tizen Web App launch');
99 let launchMode = appControl.getData('http://samsung.com/appcontrol/data/launch_mode');
100 if (!this.webApplication) {
101 console.log('Creating WebApplication');
102 let options: RuntimeOption = {
103 isAddonAvailable: addonManager.isAddonAvailable(),
104 launchMode: launchMode
106 this.webApplication = new WebApplication(options);
108 this.inspectorEnabledByVconf = wrt.tv.needUseInspector();
109 if (this.inspectorEnabledByVconf && launchMode != 'backgroundExecution') {
110 this.webApplication.inspectorSrc = src;
113 let useDiskCache = appControl.getData('USE_DISKCACHE');
114 let halfWindowOption = appControl.getData('http://samsung.com/appcontrol/data/half_window_support');
115 wrt.tv.setDiskCache(useDiskCache);
116 wrt.tv.handleAppControlData(launchMode, halfWindowOption);
118 this.webApplication.mainWindow.loadURL(src);
119 this.webApplication.prelaunch(src);
121 console.log('Handling app-control event');
122 if (this.webApplication.preloadStatus == 'readyToShow') {
123 this.webApplication.show();
125 if (launchMode != 'backgroundAtStartup')
126 this.webApplication.preloadStatus = 'none';
129 let skipReload = appControl.getData('SkipReload');
130 if (skipReload == 'Yes') {
131 console.log('skipping reload');
132 // TODO : Need to care this situation and decide to pass the addon event emitter to resume()
133 this.webApplication.resume();
137 let reload = loadInfo.getReload() || this.webApplication.isAlwaysReload;
139 let originalUrl = this.webApplication.mainWindow.webContents.getURL();
141 console.log(`appcontrol src = ${src}, original url = ${originalUrl}`);
142 if (src && originalUrl) {
143 let appcontrolUrl = (new URL(src)).href;
144 let oldUrl = (new URL(originalUrl)).href;
145 console.log(`appcontrolUrl = ${appcontrolUrl}, oldUrl = ${oldUrl}`);
147 // Below case it must be distinguishable for known cases
148 // from 'file:///index.htmlx' to 'file:///index.html'
149 if (appcontrolUrl !== oldUrl.substr(0, appcontrolUrl.length))
154 } else if (src !== originalUrl) {
158 // handle http://tizen.org/appcontrol/operation/main operation specially.
159 // only menu-screen app can send launch request with main operation.
160 // in this case, web app should have to resume web app not reset.
161 if (reload && appControl.getOperation() == 'http://tizen.org/appcontrol/operation/main')
164 this.webApplication.handleAppControlReload(src);
166 this.webApplication.sendAppControlEvent();
169 this.setCookiePath();
170 this.launchInspector(appControl);
172 wrt.on('suspend', () => {
173 console.log('suspend');
174 this.webApplication?.suspend();
176 wrt.on('resume', () => {
177 console.log('resume');
178 this.webApplication?.resume();
180 wrt.on('low-memory', () => {
181 console.log('low-memory');
182 this.webApplication?.lowMemory();
184 wrt.on('message', (event, type, params) => {
185 console.log('message type(' + type + ') params : ' + params);
186 const app_id = params[0];
187 if (type === 'startService') {
188 require('../common/service_manager').startService(app_id, params[1]);
189 event.preventDefault();
190 } else if (type === 'stopService') {
191 require('../common/service_manager').stopService(app_id);
192 event.preventDefault();
195 wrt.on('ambient-tick', () => {
196 this.webApplication?.ambientTick();
198 wrt.on('ambient-changed', (event, ambient_mode) => {
199 console.log('ambient-changed , ambient_mode:' + ambient_mode);
200 this.webApplication?.ambientChanged(ambient_mode);
202 wrt.on('addon-installed', (event, path) => {
203 console.log('addon-installed at ' + path);
204 addonManager.checkAddon(path);
206 wrt.on('addon-uninstalled', (event, id) => {
207 console.log('addon-unistalled named ' + id);
209 wrt.on('wgt-checking-done', (event) => {
210 console.log('wgt-checking-done');
211 addonManager.updateDB();
214 /* FIXME: will uncheck after chromium-efl released */
215 if (wrt.getPlatformType() !== "product_tv")
216 wrt.getInstalledPkg();
219 private launchInspector(appControl: NativeWRTjs.AppControl) {
220 this.launchInspector = (param) => {}; // call once
221 console.log('launchInspector');
223 // AUL public key/Vconf - To support inspector
224 if (this.checkInspectorCondition(appControl)) {
225 let debugPort = wrt.startInspectorServer();
226 let data = { "port" : [ debugPort.toString() ] };
227 if (this.webApplication)
228 this.webApplication.debugPort = debugPort;
229 appControl.reply(data);
233 private checkInspectorCondition(appControl: NativeWRTjs.AppControl) {
234 let bundleDebug = (appControl.getData('__AUL_DEBUG__') === "1");
235 return (bundleDebug || this.inspectorEnabledByVconf);
238 private setCookiePath() {
239 this.setCookiePath = () => {}; // call once
240 console.log('setCookiePath');
242 // FIX ME : It must be supplemented to set a specific path
246 private handleKeyEvents(key: string) {
248 console.log(key + ' is pressed');
257 console.log('No handler for the key ' + key);
261 this.webApplication?.keyEvent(key);