737d4a387466cac3f8c8fce38692545b0aea7b28
[platform/framework/web/wrtjs.git] / wrt_app / src / runtime.ts
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 'use strict';
18
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';
23
24 class Runtime {
25   webApplication?: WebApplication = undefined;
26
27   constructor() {
28     app.on('before-quit', (event: any) => {
29       console.log('before-quit');
30       this.webApplication?.beforeQuit();
31     });
32
33     app.on('will-quit', (event: any) => {
34       console.log('will-quit');
35       addonManager.deactivateAll();
36     });
37
38     app.on('quit', (event: any) => {
39       console.log('quit');
40       if (this.webApplication) {
41         this.webApplication.quit();
42         this.webApplication = undefined;
43       }
44     });
45
46     app.on('browser-window-created', () => {
47       console.log('browser-window-created');
48     });
49
50     app.on('window-all-closed', () => {
51       console.log('window-all-closed');
52       this.webApplication?.stopInspector();
53       app.quit();
54     });
55
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);
60       });
61     });
62
63     app.once('ready', (event: any) => {
64       console.log('ready');
65       if (!wrt.tv) {
66         let addonAvailable = addonManager.initialize();
67         console.log("addonBuild : " + addonAvailable);
68         if (addonAvailable) {
69           const XWalkExtension = require('../common/wrt_xwalk_extension');
70           XWalkExtension.initialize();
71           XWalkExtension.preventCleanup();
72         }
73       }
74     });
75
76     wrt.on('app-control', (event: any, appControl: any) => {
77       console.log('app-control');
78       if (wrt.isElectronApp()) {
79         this.handleAppControlForElectronApp(appControl);
80         return;
81       }
82       console.log('Tizen Web App launch');
83       if (!this.webApplication) {
84         this.createWebApplicationAndLoadUrl(appControl);
85       } else {
86         console.log('Handling app-control event');
87         this.webApplication.handleAppControlEvent(appControl);
88       }
89     });
90
91     wrt.on('suspend', () => {
92       console.log('suspend');
93       this.webApplication?.suspend();
94     });
95
96     wrt.on('resume', () => {
97       console.log('resume');
98       this.webApplication?.resume();
99     });
100
101     wrt.on('low-memory', () => {
102       console.log('low-memory');
103       this.webApplication?.clearCache();
104     });
105
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]);
119       }
120     });
121
122     wrt.on('ambient-tick', () => {
123       this.webApplication?.ambientTick();
124     });
125
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);
129     });
130   }
131
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('/');
140
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);
145     app.emit('ready');
146   }
147
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
155     }
156     this.webApplication = new WebApplication(options);
157     this.webApplication.loadUrl(appControl);
158   }
159
160   private handleKeyEvents(key: string) {
161     let valid = false;
162     console.log(key + ' is pressed');
163     switch(key) {
164       case "ArrowUp":
165       case "Up":
166       case "ArrowDown":
167       case "Down":
168         valid = true;
169         break;
170       default:
171         console.log('No handler for the key ' + key);
172         break;
173     }
174     if (valid)
175       this.webApplication?.keyEvent(key);
176   }
177 }
178
179 new Runtime();