[Service] Upgrade device home as v1.0.4
[platform/framework/web/wrtjs.git] / device_home / service / app_proxy.js
1 const express = require('express');
2 const url = require('url');
3 const AppRouter = require('./app_router');
4 var appRouters = [];
5 var path = null;
6 var currentD2DAppId = null;
7
8 function runApp(appId, port, callback) {
9     function onRunningAppsContext(contexts) {
10         var isRunning = false;
11         for (var i = 0; i < contexts.length; i++) {
12             if (appId === contexts[i].appId) {
13                 isRunning = true;
14                 break;
15             }
16         }
17
18         if (isRunning && currentD2DAppId === appId) {
19             callback();
20         } else {
21             const urlParam = require('./service').getUrlParam();
22             const urlObj = url.parse(urlParam, true).query;
23             const appControl = new tizen.ApplicationControl(
24                 "http://tizen.org/appcontrol/operation/default", null, null, null,
25                 [new tizen.ApplicationControlData(
26                     "http://tizen.org/appcontrol/data/launch_port", [port]
27                 ),
28                 new tizen.ApplicationControlData(
29                     "http://tizen.org/appcontrol/data/url_parameter", [JSON.stringify(urlObj)]
30                 )]
31             );
32
33             currentD2DAppId = appId;
34             tizen.application.launchAppControl(appControl, appId, callback);
35         }
36     }
37     tizen.application.getAppsContext(onRunningAppsContext);
38 }
39
40 function verifyD2DApp(appID) {
41     var metaDataArray = tizen.application.getAppMetaData(appID),
42         metaDataArray = metaDataArray.filter(function (metaData) {
43             return metaData.key === "d2dservice" && metaData.value === "enable";
44         });
45     return metaDataArray.length;
46 }
47
48 module.exports = function (app, port) {
49     var appProxy = express.Router();
50
51     appProxy.use('/app', express.json());
52     appProxy.post('/', (req, res) => {
53         var action = req.body.action;
54         path = req.body.appPkgID ? req.body.appPkgID : path;
55         var appId = req.body.appAppID;
56         var pkgId = req.body.appPkgID;
57         var name = appId.split(".")[1];
58         var appRouter = appRouters.filter(function (router) {
59             return router.path === path;
60         })[0];
61
62         if (verifyD2DApp(appId)) {
63             if (!appRouter) {
64                 appRouters.push({
65                     path: path,
66                     name: name,
67                     router: new AppRouter(app, path)
68                 });
69             }
70             console.log('[GlobalWebServer] appProxy.post ', path, action);
71             // run app
72             runApp(appId, port, function () {
73                 res.send({ port: port });
74             });
75         }
76     });
77
78     appProxy.get('/', (req, res) => {
79         var baseRoute = 'app';
80         var myIndex = -1;
81         for (var i = 0; i < appRouters.length; i++) {
82             if (appRouters[i].path == path) {
83                 myIndex = i;
84                 break;
85             }
86         }
87         var myApp = '/' + appRouters[myIndex].name;
88         var myRoute = baseRoute.concat(myApp);
89         res.redirect(myRoute);
90         appProxy.use(myApp, appRouters[myIndex].router);
91     });
92
93     return appProxy;
94 }