123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
"use strict";
modulum('SessionService', ['InitService'],
function(context, cls) {
context.SessionService = context.oo.StaticClass( {
__name: "SessionService",
sessionAdded: "sessionAdded",
sessionRemoved: "sessionRemoved",
_identifier: 0,
_sessions: null,
_bySessionId: null,
_eventListener: null,
init: function() {
this._eventListener = new cls.EventListener();
this._sessions = [];
this._bySessionId = {};
},
startApplication: function(appName, params, procId) {
var session = new cls.VMSession(this._identifier++);
this._eventListener.emit(this.sessionAdded, session);
var subAppInfo = context.bootstrapInfo.subAppInfo;
if (subAppInfo) {
let parentSession = context.BrowserWindowsService.getParentSession();
session._baseInfos = parentSession && parentSession._baseInfos;
session.startSubTask(subAppInfo, (app) => {
if (procId) {
app.info().connectionInfo = app.info().connectionInfo || {};
app.info().procId = procId;
app.info().connectionInfo.procId = procId;
}
});
} else {
let app = session.start(appName, params);
if (procId) {
app.info().connectionInfo = app.info().connectionInfo || {};
app.info().procId = procId;
app.info().connectionInfo.procId = procId;
}
}
this._sessions.push(session);
context.HostService.displaySession();
return session;
},
startDirect: function(wrapper, readyData) {
var session = null;
if (this._sessions.length) {
session = this._sessions[0];
} else {
session = new cls.VMSession(this._identifier++);
this._sessions.push(session);
this._eventListener.emit(this.sessionAdded, session);
context.HostService.displaySession();
}
session.startDirect(wrapper, readyData);
return session;
},
fromSessionId: function(id) {
return this._bySessionId[id];
},
updateSessionId: function(session, id) {
this._bySessionId[id] = session;
},
remove: function(session, restarting) {
this._bySessionId[session.getSessionId()] = null;
this._sessions.remove(session);
this._eventListener.emit(this.sessionRemoved, session);
if (!this._sessions.length && !restarting) {
var baseURI = document.baseURI;
if (!baseURI) {
var base = document.getElementsByTagName("base");
if (base.length) {
baseURI = base[0].href || "";
}
}
context.UrlService.setCurrentUrl(baseURI.replace(/\/$/, "/index.html"));
context.HostService.displayNoSession();
}
},
getSessions: function() {
return this._sessions;
},
getSession: function(identifier) {
return this._sessions.filter(function(item) {
return item.getIdentifier() === identifier;
})[0];
},
getCurrent: function() {
if (this._sessions) {
return this._sessions[this._sessions.length - 1];
} else {
return null;
}
},
onSessionAdded: function(hook) {
return this._eventListener.when(this.sessionAdded, hook);
},
onSessionRemoved: function(hook) {
return this._eventListener.when(this.sessionRemoved, hook);
}
});
context.InitService.register(context.SessionService);
});