From d4e2dcaff1bb957f41384e9e6a0dbb830dffe09c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roy=20Wellington=20=E2=85=A3?= Date: Sun, 14 Oct 2018 17:23:47 -0700 Subject: [PATCH 1/3] Detect if access to localStorage is forbidden by the user's browser If the user's cookie/persistent storage setting forbid access to localStorage, catch the exception and abort the access. Currently, attempting to use the expand/contract links at the top of the page for structs/consts/etc. fails due to an unhandled error while accessing localStorage, if such access is forbidden, as the exception from the failed access propagates all the way out, interrupting the expand/contract. Instead, I would like to degrade gracefully; the access won't happen (the collapse/expand state won't get persisted) but the actual expanding/contracting of the item will go on to succeed. Fixes #55079 --- src/librustdoc/html/static/storage.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js index 4ef8349fa9ce9..9dc78f7beb610 100644 --- a/src/librustdoc/html/static/storage.js +++ b/src/librustdoc/html/static/storage.js @@ -28,6 +28,12 @@ function onEach(arr, func) { function updateLocalStorage(name, value) { if (typeof(Storage) !== "undefined") { + try { + window.localStorage; + } catch(err) { + // Storage is supported, but browser preferences deny access to it. + return; + } localStorage[name] = value; } else { // No Web Storage support so we do nothing From 8362aa2178959186c09899ca173ffb296fb3445e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roy=20Wellington=20=E2=85=A3?= Date: Mon, 15 Oct 2018 20:38:51 -0700 Subject: [PATCH 2/3] Extract localStorage tests out into a helper method; use in getCurrentValue() 1. Extract the tests for whether or not we have workable localStorage out into a helper method, so it can be more easily reused 2. Use it in getCurrentValue() too, for the same reasons, as suggested in code review --- src/librustdoc/html/static/storage.js | 28 +++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js index 9dc78f7beb610..206b3128eb359 100644 --- a/src/librustdoc/html/static/storage.js +++ b/src/librustdoc/html/static/storage.js @@ -27,13 +27,7 @@ function onEach(arr, func) { } function updateLocalStorage(name, value) { - if (typeof(Storage) !== "undefined") { - try { - window.localStorage; - } catch(err) { - // Storage is supported, but browser preferences deny access to it. - return; - } + if (usableLocalStorage()) { localStorage[name] = value; } else { // No Web Storage support so we do nothing @@ -41,12 +35,30 @@ function updateLocalStorage(name, value) { } function getCurrentValue(name) { - if (typeof(Storage) !== "undefined" && localStorage[name] !== undefined) { + if (usableLocalStorage() && localStorage[name] !== undefined) { return localStorage[name]; } return null; } +function usableLocalStorage() { + // Check if the browser supports localStorage at all: + if (typeof(Storage) === "undefined") { + return false; + } + // Check if we can access it; this access will fail if the browser + // preferences deny access to localStorage, e.g., to prevent storage of + // "cookies" (or cookie-likes, as is the case here). + try { + window.localStorage; + } catch(err) { + // Storage is supported, but browser preferences deny access to it. + return false; + } + + return true; +} + function switchTheme(styleElem, mainStyleElem, newTheme) { var fullBasicCss = "rustdoc" + resourcesSuffix + ".css"; var fullNewTheme = newTheme + resourcesSuffix + ".css"; From cbe98ec803c023deb6d200b6761acd7c7d73254c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roy=20Wellington=20=E2=85=A3?= Date: Tue, 16 Oct 2018 22:15:27 -0700 Subject: [PATCH 3/3] Move usableLocalStorage() above functions that make use of it --- src/librustdoc/html/static/storage.js | 30 +++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js index 206b3128eb359..e10e330402f5e 100644 --- a/src/librustdoc/html/static/storage.js +++ b/src/librustdoc/html/static/storage.js @@ -26,21 +26,6 @@ function onEach(arr, func) { return false; } -function updateLocalStorage(name, value) { - if (usableLocalStorage()) { - localStorage[name] = value; - } else { - // No Web Storage support so we do nothing - } -} - -function getCurrentValue(name) { - if (usableLocalStorage() && localStorage[name] !== undefined) { - return localStorage[name]; - } - return null; -} - function usableLocalStorage() { // Check if the browser supports localStorage at all: if (typeof(Storage) === "undefined") { @@ -59,6 +44,21 @@ function usableLocalStorage() { return true; } +function updateLocalStorage(name, value) { + if (usableLocalStorage()) { + localStorage[name] = value; + } else { + // No Web Storage support so we do nothing + } +} + +function getCurrentValue(name) { + if (usableLocalStorage() && localStorage[name] !== undefined) { + return localStorage[name]; + } + return null; +} + function switchTheme(styleElem, mainStyleElem, newTheme) { var fullBasicCss = "rustdoc" + resourcesSuffix + ".css"; var fullNewTheme = newTheme + resourcesSuffix + ".css";