Skip to content

Commit

Permalink
Bug 1573236 - Part 2: Make the dom.storage_access.auto_grants.delayed…
Browse files Browse the repository at this point in the history
… work again with the new setup; r=baku

Differential Revision: https://phabricator.services.mozilla.com/D41622

--HG--
extra : moz-landing-system : lando
  • Loading branch information
ehsan committed Aug 14, 2019
1 parent 56e165b commit a2c6ed2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 50 deletions.
16 changes: 8 additions & 8 deletions dom/base/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15457,13 +15457,6 @@ already_AddRefed<mozilla::dom::Promise> Document::RequestStorageAccess(
Telemetry::LABELS_STORAGE_ACCESS_API_UI::Allow);
p->Resolve(AntiTrackingCommon::eAllow, __func__);
},
// Allow auto grant
[p] {
Telemetry::AccumulateCategorical(
Telemetry::LABELS_STORAGE_ACCESS_API_UI::
AllowAutomatically);
p->Resolve(AntiTrackingCommon::eAllowAutoGrant, __func__);
},
// Allow on any site
[p] {
Telemetry::AccumulateCategorical(
Expand Down Expand Up @@ -15531,7 +15524,14 @@ already_AddRefed<mozilla::dom::Promise> Document::RequestStorageAccess(
} else if (autoGrant) {
choice = AntiTrackingCommon::eAllowAutoGrant;
}
p->Resolve(choice, __func__);
if (!autoGrant) {
p->Resolve(choice, __func__);
} else {
sapr->MaybeDelayAutomaticGrants()->Then(
GetCurrentThreadSerialEventTarget(), __func__,
[p, choice] { p->Resolve(choice, __func__); },
[p] { p->Reject(false, __func__); });
}
return;
}
p->Reject(false, __func__);
Expand Down
68 changes: 38 additions & 30 deletions dom/base/StorageAccessPermissionRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(StorageAccessPermissionRequest,
StorageAccessPermissionRequest::StorageAccessPermissionRequest(
nsPIDOMWindowInner* aWindow, nsIPrincipal* aNodePrincipal,
AllowCallback&& aAllowCallback,
AllowAutoGrantCallback&& aAllowAutoGrantCallback,
AllowAnySiteCallback&& aAllowAnySiteCallback,
CancelCallback&& aCancelCallback)
: ContentPermissionRequestBase(aNodePrincipal, aWindow,
NS_LITERAL_CSTRING("dom.storage_access"),
NS_LITERAL_CSTRING("storage-access")),
mAllowCallback(std::move(aAllowCallback)),
mAllowAutoGrantCallback(std::move(aAllowAutoGrantCallback)),
mAllowAnySiteCallback(std::move(aAllowAnySiteCallback)),
mCancelCallback(std::move(aCancelCallback)),
mCallbackCalled(false) {
Expand All @@ -42,7 +40,6 @@ NS_IMETHODIMP
StorageAccessPermissionRequest::Cancel() {
if (!mCallbackCalled) {
mCallbackCalled = true;
mTimer = nullptr;
mCancelCallback();
}
return NS_OK;
Expand All @@ -56,38 +53,58 @@ StorageAccessPermissionRequest::Allow(JS::HandleValue aChoices) {
return rv;
}

// There is no support to allow grants automatically from the prompting code
// path.

if (!mCallbackCalled) {
mCallbackCalled = true;
if (choices.Length() == 1 &&
choices[0].choice().EqualsLiteral("allow-on-any-site")) {
mAllowAnySiteCallback();
} else if (choices.Length() == 1 &&
choices[0].choice().EqualsLiteral("allow-auto-grant")) {
unsigned simulatedDelay = CalculateSimulatedDelay();
if (simulatedDelay) {
MOZ_ASSERT(!mTimer);
RefPtr<StorageAccessPermissionRequest> self = this;
rv = NS_NewTimerWithFuncCallback(
getter_AddRefs(mTimer), CallAutoGrantCallback, this, simulatedDelay,
nsITimer::TYPE_ONE_SHOT, "DelayedAllowAutoGrantCallback");
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
NS_ADDREF(this);
} else {
mAllowAutoGrantCallback();
}
} else {
choices[0].choice().EqualsLiteral("allow")) {
mAllowCallback();
}
}
return NS_OK;
}

RefPtr<StorageAccessPermissionRequest::AutoGrantDelayPromise>
StorageAccessPermissionRequest::MaybeDelayAutomaticGrants() {
RefPtr<AutoGrantDelayPromise::Private> p =
new AutoGrantDelayPromise::Private(__func__);

unsigned simulatedDelay = CalculateSimulatedDelay();
if (simulatedDelay) {
nsCOMPtr<nsITimer> timer;
RefPtr<AutoGrantDelayPromise::Private> promise = p;
nsresult rv = NS_NewTimerWithFuncCallback(
getter_AddRefs(timer),
[](nsITimer* aTimer, void* aClosure) -> void {
auto* promise =
static_cast<AutoGrantDelayPromise::Private*>(aClosure);
promise->Resolve(true, __func__);
NS_RELEASE(aTimer);
NS_RELEASE(promise);
},
promise, simulatedDelay, nsITimer::TYPE_ONE_SHOT,
"DelayedAllowAutoGrantCallback");
if (NS_WARN_IF(NS_FAILED(rv))) {
p->Reject(false, __func__);
} else {
// Leak the references here! We'll release them inside the callback.
Unused << timer.forget();
Unused << promise.forget();
}
} else {
p->Resolve(false, __func__);
}
return p;
}

already_AddRefed<StorageAccessPermissionRequest>
StorageAccessPermissionRequest::Create(
nsPIDOMWindowInner* aWindow, AllowCallback&& aAllowCallback,
AllowAutoGrantCallback&& aAllowAutoGrantCallback,
AllowAnySiteCallback&& aAllowAnySiteCallback,
CancelCallback&& aCancelCallback) {
if (!aWindow) {
Expand All @@ -100,8 +117,7 @@ StorageAccessPermissionRequest::Create(
RefPtr<StorageAccessPermissionRequest> request =
new StorageAccessPermissionRequest(
aWindow, win->GetPrincipal(), std::move(aAllowCallback),
std::move(aAllowAutoGrantCallback), std::move(aAllowAnySiteCallback),
std::move(aCancelCallback));
std::move(aAllowAnySiteCallback), std::move(aCancelCallback));
return request.forget();
}

Expand All @@ -121,13 +137,5 @@ unsigned StorageAccessPermissionRequest::CalculateSimulatedDelay() {
return kMin + random % (kMax - kMin);
}

void StorageAccessPermissionRequest::CallAutoGrantCallback(nsITimer* aTimer,
void* aClosure) {
auto self = static_cast<StorageAccessPermissionRequest*>(aClosure);
self->mAllowAutoGrantCallback();
self->mTimer = nullptr;
NS_RELEASE(self);
}

} // namespace dom
} // namespace mozilla
21 changes: 9 additions & 12 deletions dom/base/StorageAccessPermissionRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define StorageAccessPermissionRequest_h_

#include "nsContentPermissionHelper.h"
#include "mozilla/MozPromise.h"

#include <functional>

Expand All @@ -28,34 +29,30 @@ class StorageAccessPermissionRequest final
NS_IMETHOD Allow(JS::HandleValue choices) override;

typedef std::function<void()> AllowCallback;
typedef std::function<void()> AllowAutoGrantCallback;
typedef std::function<void()> AllowAnySiteCallback;
typedef std::function<void()> CancelCallback;

static already_AddRefed<StorageAccessPermissionRequest> Create(
nsPIDOMWindowInner* aWindow, AllowCallback&& aAllowCallback,
AllowAutoGrantCallback&& aAllowAutoGrantCallback,
AllowAnySiteCallback&& aAllowAnySiteCallback,
CancelCallback&& aCancelCallback);

typedef MozPromise<bool, bool, true> AutoGrantDelayPromise;
RefPtr<AutoGrantDelayPromise> MaybeDelayAutomaticGrants();

private:
StorageAccessPermissionRequest(
nsPIDOMWindowInner* aWindow, nsIPrincipal* aNodePrincipal,
AllowCallback&& aAllowCallback,
AllowAutoGrantCallback&& aAllowAutoGrantCallback,
AllowAnySiteCallback&& aAllowAnySiteCallback,
CancelCallback&& aCancelCallback);
StorageAccessPermissionRequest(nsPIDOMWindowInner* aWindow,
nsIPrincipal* aNodePrincipal,
AllowCallback&& aAllowCallback,
AllowAnySiteCallback&& aAllowAnySiteCallback,
CancelCallback&& aCancelCallback);
~StorageAccessPermissionRequest();

unsigned CalculateSimulatedDelay();

static void CallAutoGrantCallback(nsITimer* aTimer, void* aClosure);

AllowCallback mAllowCallback;
AllowAutoGrantCallback mAllowAutoGrantCallback;
AllowAnySiteCallback mAllowAnySiteCallback;
CancelCallback mCancelCallback;
nsCOMPtr<nsITimer> mTimer;
nsTArray<PermissionRequest> mPermissionRequests;
bool mCallbackCalled;
};
Expand Down

0 comments on commit a2c6ed2

Please sign in to comment.