3acfa301f3ff59c7d555cb7463a381db4cc52134
[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 module.exports = function(app, port) {
41     var appProxy = express.Router();
42     
43     appProxy.use('/app', express.json());
44     appProxy.post('/', (req, res) => {
45         var action = req.body.action;
46         path = req.body.appPkgID ? req.body.appPkgID : path;
47         var appId = req.body.appAppID;
48         var pkgId = req.body.appPkgID;
49         var name = appId.split(".")[1];
50         var appRouter = appRouters.filter(function (router) {
51             return router.path === path;
52         })[0];
53
54         if (!appRouter) {
55             appRouters.push({
56                 path: path,
57                 name: name,
58                 router: new AppRouter(app, path)
59             });
60         }
61
62         console.log('[GlobalWebServer] appProxy.post ', path, action);
63
64         // run app
65         runApp(appId, port, function() {
66             res.send({port:port});
67         });
68     });
69
70     appProxy.get('/', (req, res) => {
71         var baseRoute = 'app';
72         var myIndex = -1;
73         for (var i = 0; i < appRouters.length; i++) {
74             if (appRouters[i].path == path) {
75                 myIndex = i;
76                 break;
77             }
78         }
79         var myApp = '/' + appRouters[myIndex].name;
80         var myRoute = baseRoute.concat(myApp);
81         res.redirect(myRoute);
82         appProxy.use(myApp, appRouters[myIndex].router);
83     });
84
85     return appProxy;
86 }