From 08d370d32e648c2823e1bc31fcd9f7e1f96d155a Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 3 Mar 2020 11:31:14 -0500 Subject: [PATCH] Catch possible exceptions when accessing window.localStorage Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/899 This is a code review of previous commits related to the above issue. --- platform/chromium/vapi-common.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/platform/chromium/vapi-common.js b/platform/chromium/vapi-common.js index efeb4c82f..34b1d6d64 100644 --- a/platform/chromium/vapi-common.js +++ b/platform/chromium/vapi-common.js @@ -246,23 +246,22 @@ vAPI.localStorage = { this.cache = new Promise(resolve => { browser.storage.local.get('localStorage', bin => { this.cache = undefined; - if ( - bin instanceof Object === false || - bin.localStorage instanceof Object === false - ) { - this.cache = {}; - const ls = self.localStorage; - for ( let i = 0; i < ls.length; i++ ) { - const key = ls.key(i); - this.cache[key] = ls.getItem(key); - } - //ls.clear(); - browser.storage.local.set({ localStorage: this.cache }); - } else { - try { + try { + if ( + bin instanceof Object === false || + bin.localStorage instanceof Object === false + ) { + this.cache = {}; + const ls = self.localStorage; + for ( let i = 0; i < ls.length; i++ ) { + const key = ls.key(i); + this.cache[key] = ls.getItem(key); + } + browser.storage.local.set({ localStorage: this.cache }); + } else { this.cache = bin.localStorage; - } catch(ex) { } + } catch(ex) { } if ( this.cache instanceof Object === false ) { this.cache = {};