diff --git a/index.js b/index.js index 7c01829..634405a 100644 --- a/index.js +++ b/index.js @@ -81,20 +81,9 @@ const ContainerService = { // Map of identities. ContextualIdentityService.getIdentities().forEach(identity => { - this._identitiesState[identity.userContextId] = { - hiddenTabUrls: [], - openTabs: 0 - }; + this._remapTabsIfMissing(identity.userContextId); }); - // It can happen that this jsm is loaded after the opening a container tab. - for (let tab of tabs) { // eslint-disable-line prefer-const - const userContextId = this._getUserContextIdFromTab(tab); - if (userContextId) { - ++this._identitiesState[userContextId].openTabs; - } - } - tabs.on("open", tab => { const userContextId = this._getUserContextIdFromTab(tab); if (userContextId) { @@ -216,6 +205,25 @@ const ContainerService = { } }, + _createIdentityState() { + return { + hiddenTabUrls: [], + openTabs: 0 + }; + }, + + _remapTabsIfMissing(userContextId) { + // We already know this userContextId. + if (userContextId in this._identitiesState) { + return; + } + + this._identitiesState[userContextId] = this._createIdentityState(); + this._containerTabIterator(userContextId, () => { + ++this._identitiesState[userContextId].openTabs; + }); + }, + // Tabs management hideTabs(args) { @@ -225,6 +233,14 @@ const ContainerService = { return; } + this._remapTabsIfMissing(args.userContextId); + + // We should check if this userContextId exists. + if ((args.userContextId in this._identitiesState)) { + resolve(null); + return; + } + this._containerTabIterator(args.userContextId, tab => { const object = this._createTabObject(tab); @@ -251,6 +267,14 @@ const ContainerService = { return Promise.reject("showTabs must be called with userContextId argument."); } + this._remapTabsIfMissing(args.userContextId); + + // We should check if this userContextId exists. + if (!(args.userContextId in this._identitiesState)) { + resolve(null); + return; + } + const promises = []; for (let object of this._identitiesState[args.userContextId].hiddenTabUrls) { // eslint-disable-line prefer-const @@ -319,6 +343,14 @@ const ContainerService = { return; } + this._remapTabsIfMissing(args.userContextId); + + // We should check if this userContextId exists. + if (!(args.userContextId in this._identitiesState)) { + resolve([]); + return; + } + const list = []; this._containerTabIterator(args.userContextId, tab => { list.push(this._createTabObject(tab)); @@ -433,6 +465,7 @@ const ContainerService = { const identities = []; ContextualIdentityService.getIdentities().forEach(identity => { + this._remapTabsIfMissing(identity.userContextId); const convertedIdentity = this._convert(identity); identities.push(convertedIdentity); }); @@ -462,10 +495,7 @@ const ContainerService = { const identity = ContextualIdentityService.create(args.name, icon, color); - this._identitiesState[identity.userContextId] = { - hiddenTabUrls: [], - openTabs: 0 - }; + this._identitiesState[identity.userContextId] = this._createIdentityState(); this._refreshNeeded().then(() => { return this._convert(identity);