Closed Bug 2062077 Opened 3 months ago Closed 2 months ago

Reduce duplicated promise boilerplate in the Storage Access API entry points in Document.cpp

Categories

(Core :: Privacy: Anti-Tracking, task)

task

Tracking

()

RESOLVED FIXED
157 Branch
Tracking Status
firefox157 --- fixed

People

(Reporter: dmehic, Assigned: momar)

References

(Blocks 1 open bug)

Details

(Keywords: good-first-bug, Whiteboard: [lang=c++])

Attachments

(1 external file)

Filing as a good first bug to learn workflows.

Document::RequestStorageAccess() and Document::RequestStorageAccessForOrigin() in dom/base/Document.cpp already share their algorithm — that consolidation happened in bug 1765309 and bug 1765307, which moved the real logic into StorageAccessAPIHelper. What is left over is promise boilerplate repeated many times in both functions. This bug is about collapsing that boilerplate into two small static helpers and dropping a pair of dead local variables. It is a mechanical change with no intended behavior change.

(This is the residue of bug 1733330, which was closed as a duplicate of bug 1765307.)

Link to the code:

There are three parts.

1. A helper for the "settle from Maybe<bool>" blocks

This exact shape appears 6 times — 4 in RequestStorageAccess, 2 in RequestStorageAccessForOrigin:

if (result.isSome()) {
  if (result.value()) {
    promise->MaybeResolveWithUndefined();
  } else {
    ConsumeTransientUserGestureActivation();
    promise->MaybeRejectWithNotAllowedError("requestStorageAccess not allowed"_ns);
  }
  return promise.forget();
}

Add one small helper covering it and use it at all 6 sites. A signature along the lines of a bool "did this settle the promise, should the caller return early" works well, but the exact shape is up to you.

2. A helper for the reject idiom

ConsumeTransientUserGestureActivation() immediately followed by

promise->MaybeRejectWithNotAllowedError("requestStorageAccess not allowed"_ns);

appears 14 times across the two functions. Add a helper for it (part 1's helper can then call it too).

Two things to be careful about — this is the only part of the bug that is not blind search-and-replace:

  • MaybeRejectWithNotAllowedError actually appears 15 times. The 15th one, in the final ->Then() rejection handler of RequestStorageAccessForOrigin (around line 20512), deliberately does not consume user activation. Leave that one as it is.
  • There is also a standalone ConsumeTransientUserGestureActivation() around line 20433, under the comment "Consume user activation before entering the async part of this method". It is not part of a rejection and must stay where it is.

3. Remove two dead local variables

Both functions contain this, at roughly lines 20276 and 20411:

RefPtr<nsGlobalWindowOuter> outer =
    nsGlobalWindowOuter::Cast(inner->GetOuterWindow());
if (!outer) {
  // ... reject ...
}

outer is never used after its own null check — the identifier appears exactly twice in each function, the declaration and the guard. Since nsGlobalWindowOuter::Cast is a plain static cast, each of these collapses to:

if (!inner->GetOuterWindow()) {
  // ... reject ...
}

Explicitly out of scope

Document::HasStorageAccess() (around line 19948) repeats the same GetScopeObject() / Promise::Create / aRv.Failed() preamble, but it passes Promise::ePropagateUserInteraction where the other two do not. Unifying that preamble is deliberately left out of this bug — please do not include it.

To verify the fix:

./mach build binaries
./mach test toolkit/components/antitracking/test/browser/browser_storageAccessPrivilegeAPI.js
./mach test toolkit/components/antitracking/test/browser/
./mach format

browser_storageAccessPrivilegeAPI.js is the test that specifically covers requestStorageAccessForOrigin; the rest of that directory covers requestStorageAccess and hasStorageAccess. Because this is a pure refactor, every test that passed before your patch must still pass after it.

Why this is worth doing: these two functions are 166 and 190 lines respectively, and a large fraction of that is the same four or five lines written out over and over. Collapsing it removes roughly 100 lines and makes the parts that genuinely differ between the two entry points — which principal is used, which context check applies, how user activation is handled — much easier to see. That matters here because the Storage Access API is security-sensitive code where those differences are load-bearing.

Tutorial to contribute:
https://firefox-source-docs.mozilla.org/contributing/contribution_quickref.html
https://firefox-source-docs.mozilla.org/contributing/stack_quickref.html

Please don't ask for the bug to be assigned. It will be automatically assigned to the first patch.

Assignee: nobody → momar

RequestStorageAccess and RequestStorageAccessForOrigin repeated the same
resolve-or-reject-with-NotAllowedError logic at every step. Factor it into
two small helpers and drop two locals that were
only ever used for a null check.

Status: NEW → ASSIGNED

There is an r+ patch which didn't land and no activity in this bug for 1 week.
:momar, could you have a look please?
If you still have some work to do, you can add an action "Plan Changes" in Phabricator.
For more information, please visit BugBot documentation.

Flags: needinfo?(tihuang)
Flags: needinfo?(momar)
Flags: needinfo?(tihuang)
Flags: needinfo?(momar)
Status: ASSIGNED → RESOLVED
Closed: 2 months ago
Resolution: --- → FIXED
Target Milestone: --- → 157 Branch
QA Whiteboard: [qa-triage-done-c158/b157]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: