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;
28 app.on('before-quit', (event: any) => {
29 console.log('before-quit');
30 this.webApplication?.beforeQuit();
33 app.on('will-quit', (event: any) => {
34 console.log('will-quit');
35 addonManager.deactivateAll();
38 app.on('quit', (event: any) => {
40 if (this.webApplication) {
41 this.webApplication.quit();
42 this.webApplication = undefined;
46 app.on('browser-window-created', () => {
47 console.log('browser-window-created');
50 app.on('window-all-closed', () => {
51 console.log('window-all-closed');
52 this.webApplication?.stopInspector();
56 app.on('web-contents-created', (event: any, webContents: any) => {
57 console.log('web-contents-created');
58 webContents.on('before-input-event', (event: any, input: any) => {
59 this.handleKeyEvents(input.key);
63 app.once('ready', (event: any) => {
66 let addonAvailable = addonManager.initialize();
67 console.log("addonBuild : " + addonAvailable);
69 const XWalkExtension = require('../common/wrt_xwalk_extension');
70 XWalkExtension.initialize();
71 XWalkExtension.preventCleanup();
76 wrt.on('app-control', (event: any, appControl: any) => {
77 console.log('app-control');
78 if (wrt.isElectronApp()) {
79 this.handleAppControlForElectronApp(appControl);
82 console.log('Tizen Web App launch');
83 if (!this.webApplication) {
84 this.createWebApplicationAndLoadUrl(appControl);
86 console.log('Handling app-control event');
87 this.webApplication.handleAppControlEvent(appControl);
91 wrt.on('suspend', () => {
92 console.log('suspend');
93 this.webApplication?.suspend();
96 wrt.on('resume', () => {
97 console.log('resume');
98 this.webApplication?.resume();
101 wrt.on('low-memory', () => {
102 console.log('low-memory');
103 this.webApplication?.clearCache();
106 wrt.on('message', (event: any, type: string, params: string[]) => {
107 console.log('message type(' + type + ') params : ' + params);
108 const app_id = params[0];
109 if (type === 'startService') {
110 require('../service/service_manager').startService(app_id, params[1]);
111 event.preventDefault();
112 } else if (type === 'stopService') {
113 require('../service/service_manager').stopService(app_id);
114 event.preventDefault();
115 } else if (type === 'hideSplashScreen') {
116 this.webApplication?.hideSplashScreen(params[0]);
117 } else if (type === 'selectionText') {
118 addonManager.emit('message', type, params[0]);
122 wrt.on('ambient-tick', () => {
123 this.webApplication?.ambientTick();
126 wrt.on('ambient-changed', (event: any, ambient_mode: boolean) => {
127 console.log('ambient-changed , ambient_mode:' + ambient_mode);
128 this.webApplication?.ambientChanged(ambient_mode);
132 private handleAppControlForElectronApp(appControl: any) {
133 console.log('Electron App launch');
134 let src = appControl.getLoadInfo().getSrc();
135 const Module = require('module');
136 Module.globalPaths.push(wrt.getAppPath());
137 let filePath = src[7] === '/' ? src.substr(8) : src.substr(7); // strip "file://"
138 const pkgJson: any = __non_webpack_require__(filePath);
139 let pos = filePath.lastIndexOf('/');
141 let mainJsPath = (pos !== -1 ? filePath.substr(0, pos + 1) : '') +
142 (pkgJson.main || 'index.js');
143 console.log('loading path:', mainJsPath);
144 Module._load(mainJsPath, Module, true);
148 private createWebApplicationAndLoadUrl(appControl: any) {
149 console.log('Creating WebApplication');
150 addonManager.activateAll();
151 let launchMode = appControl.getData('http://samsung.com/appcontrol/data/launch_mode');
152 let options: RuntimeOption = {
153 isAddonAvailable: addonManager.isAddonAvailable(),
154 launchMode: launchMode
156 this.webApplication = new WebApplication(options);
157 this.webApplication.loadUrl(appControl);
160 private handleKeyEvents(key: string) {
162 console.log(key + ' is pressed');
171 console.log('No handler for the key ' + key);
175 this.webApplication?.keyEvent(key);