Skip to content

Commit 6c4028e

Browse files
natebiggsCommit Queue
authored andcommitted
[ddc] Update ddc_module_loader.js to safely check for localStorage in page.
When running in an iframe without specific flags, DDC may not have access to `localStorage`. In this context, even trying to access `window.localStorage` can cause an exception in the iframe. Wrapping the check in a try/catch allows us to safely check for access before using it. Change-Id: I0c5d3d0ac34a550444c12b52f6519a9446ebfe9e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405608 Commit-Queue: Nate Biggs <[email protected]> Reviewed-by: Mark Zhou <[email protected]>
1 parent ba9ef09 commit 6c4028e

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

pkg/dev_compiler/lib/js/ddc/ddc_module_loader.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,19 @@ if (!self.dart_library) {
340340
}
341341
self.dart_library.library = library;
342342

343+
// Local storage may be blocked by a browser policy in which case even
344+
// trying to access it will throw.
345+
function isLocalStorageAvailable() {
346+
try {
347+
!!self.localStorage;
348+
return true;
349+
} catch (e) {
350+
return false;
351+
}
352+
}
353+
343354
// Store executed modules upon reload.
344-
if (!!self.addEventListener && !!self.localStorage) {
355+
if (!!self.addEventListener && isLocalStorageAvailable()) {
345356
self.addEventListener('beforeunload', function (event) {
346357
_nameToApp.forEach(function (_, appName) {
347358
if (!_executedLibraries.get(appName)) {

0 commit comments

Comments
 (0)