# IndexedDB in Containers - contextualIdentities

**URL:** <https://discourse.mozilla.org/t/indexeddb-in-containers-contextualidentities/30537>\
**Category:** Development\
**Created:** [July 29, 2018, 9:40am UTC](https://discourse.mozilla.org/t/indexeddb-in-containers-contextualidentities/30537 "2018-07-29T09:40:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [July 29, 2018, 9:40am UTC](https://discourse.mozilla.org/t/indexeddb-in-containers-contextualidentities/30537/1 "2018-07-29T09:40:05Z")

</div>

Hello,

I’m now accessing `IndexedDB` from my extension page and I just found out, that if I open my extension page in different container, it won’t have access to the same database!

**Can I detect that the tab is opened in a container without `contextualIdentities` permission?** (because if I know it’s container tab, I can ask my background script for data from database)

Also [Optional permission for `contextualIdentities` is not yet implemented](https://bugzilla.mozilla.org/show_bug.cgi?id=1475834).

---

<div class="post-metadata">

**Author:** ![NilkasG](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@NilkasG](https://discourse.mozilla.org/u/NilkasG)\
**Post date:** [July 29, 2018, 11:31am UTC](https://discourse.mozilla.org/t/indexeddb-in-containers-contextualidentities/30537/2 "2018-07-29T11:31:25Z")

</div>

I think you have access to the background page exactly iff your page is executed in the same storage context, i.e.:

```auto
async function iAmInNormalContext() {
    return (await browser.runtime.getBackgroundPage()) != null;
}

```

Note1: In Firefox, the synchronous version on `extension` works as well.  
Note 2: In Chrome, this would load the “event” background page if suspended.

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [July 29, 2018, 12:54pm UTC](https://discourse.mozilla.org/t/indexeddb-in-containers-contextualidentities/30537/3 "2018-07-29T12:54:27Z")

</div>

**Nice workaround!** Thank you!  
This will also identify Private Browsing tab (where IndexedDB is also inaccessible).

I just hope this will be working also in the future… (at least for a year)

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [July 3, 2019, 1:28pm UTC](https://discourse.mozilla.org/t/indexeddb-in-containers-contextualidentities/30537/4 "2019-07-03T13:28:48Z")

</div>

There is actually a race condition bug… if the code is called right after browser starts (if add-on page is set as homepage), then `await browser.runtime.getBackgroundPage()` will return `null`, even though it’s not a container.

And if you are “smart” like me and store the result in constant:

```auto
const IS_CONTAINER_OR_PRIVATE_TAB_PROMISE = browser.runtime.getBackgroundPage().then(bc => bc === null);

```

then it will keep report wrong value until you refresh the tab ☹.  
It took me two hours to track this bug down! Crazy!
