123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
"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 = {};
},
start: function(appName, params) {
var session = new cls.VMSession(this._identifier++);
this._eventListener.emit(this.sessionAdded, session);
var subAppInfo = context.bootstrapInfo.subAppInfo;
if (subAppInfo) {
session._baseInfos = (window.opener && window.opener.gbc &&
window.opener.gbc.SessionService.getCurrent() &&
window.opener.gbc.SessionService.getCurrent()._baseInfos);
session.startTask(subAppInfo);
} else {
session.start(appName, params);
}
this._sessions.push(session);
context.HostService.displaySession();
return session;
},
startDirect: function(wrapper, headers) {
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, headers);
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._identifier === 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);
});