2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-09-27 03:21:57 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "PermissionRequestBase.h"
|
|
|
|
|
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Services.h"
|
2015-04-10 19:10:00 +03:00
|
|
|
#include "mozilla/dom/Element.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace indexedDB {
|
|
|
|
|
|
|
|
using namespace mozilla::services;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
#define IDB_PREFIX "indexedDB"
|
|
|
|
#define TOPIC_PREFIX IDB_PREFIX "-permissions-"
|
|
|
|
|
2019-02-22 01:54:28 +03:00
|
|
|
const nsLiteralCString kPermissionString = NS_LITERAL_CSTRING(IDB_PREFIX);
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
const char kPermissionPromptTopic[] = TOPIC_PREFIX "prompt";
|
2014-11-01 22:34:17 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-09-27 03:21:57 +04:00
|
|
|
const char kPermissionResponseTopic[] = TOPIC_PREFIX "response";
|
2014-11-01 22:34:17 +03:00
|
|
|
#endif
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
#undef TOPIC_PREFIX
|
|
|
|
#undef IDB_PREFIX
|
|
|
|
|
|
|
|
const uint32_t kPermissionDefault = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
|
|
|
|
void AssertSanity() {
|
2015-07-04 04:29:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-09-27 03:21:57 +04:00
|
|
|
|
2015-04-10 19:10:00 +03:00
|
|
|
PermissionRequestBase::PermissionRequestBase(Element* aOwnerElement,
|
2014-09-27 03:21:57 +04:00
|
|
|
nsIPrincipal* aPrincipal)
|
2015-04-10 19:10:00 +03:00
|
|
|
: mOwnerElement(aOwnerElement), mPrincipal(aPrincipal) {
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertSanity();
|
2015-04-10 19:10:00 +03:00
|
|
|
MOZ_ASSERT(aOwnerElement);
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(aPrincipal);
|
|
|
|
}
|
|
|
|
|
|
|
|
PermissionRequestBase::~PermissionRequestBase() { AssertSanity(); }
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult PermissionRequestBase::GetCurrentPermission(
|
|
|
|
nsIPrincipal* aPrincipal, PermissionValue* aCurrentValue) {
|
|
|
|
AssertSanity();
|
|
|
|
MOZ_ASSERT(aPrincipal);
|
|
|
|
MOZ_ASSERT(aCurrentValue);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPermissionManager> permMan = GetPermissionManager();
|
|
|
|
if (NS_WARN_IF(!permMan)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t intPermission;
|
|
|
|
nsresult rv = permMan->TestExactPermissionFromPrincipal(
|
|
|
|
aPrincipal, kPermissionString, &intPermission);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
PermissionValue permission = PermissionValueForIntPermission(intPermission);
|
|
|
|
|
|
|
|
MOZ_ASSERT(permission == kPermissionAllowed ||
|
|
|
|
permission == kPermissionDenied ||
|
|
|
|
permission == kPermissionPrompt);
|
|
|
|
|
|
|
|
*aCurrentValue = permission;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
auto PermissionRequestBase::PermissionValueForIntPermission(
|
|
|
|
uint32_t aIntPermission) -> PermissionValue {
|
|
|
|
AssertSanity();
|
|
|
|
|
|
|
|
switch (aIntPermission) {
|
|
|
|
case kPermissionDefault:
|
|
|
|
return kPermissionPrompt;
|
2014-11-28 11:44:12 +03:00
|
|
|
case kPermissionAllowed:
|
|
|
|
return kPermissionAllowed;
|
2014-09-27 03:21:57 +04:00
|
|
|
case kPermissionDenied:
|
|
|
|
return kPermissionDenied;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Bad permission!");
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("Should never get here!");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult PermissionRequestBase::PromptIfNeeded(PermissionValue* aCurrentValue) {
|
|
|
|
AssertSanity();
|
|
|
|
MOZ_ASSERT(aCurrentValue);
|
|
|
|
MOZ_ASSERT(mPrincipal);
|
|
|
|
|
|
|
|
// Tricky, we want to release the window and principal in all cases except
|
|
|
|
// when we successfully prompt.
|
2015-04-10 19:10:00 +03:00
|
|
|
nsCOMPtr<Element> element;
|
|
|
|
mOwnerElement.swap(element);
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
mPrincipal.swap(principal);
|
|
|
|
|
|
|
|
PermissionValue currentValue;
|
|
|
|
nsresult rv = GetCurrentPermission(principal, ¤tValue);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(currentValue != kPermissionDefault);
|
|
|
|
|
|
|
|
if (currentValue == kPermissionPrompt) {
|
|
|
|
nsCOMPtr<nsIObserverService> obsSvc = GetObserverService();
|
|
|
|
if (NS_WARN_IF(!obsSvc)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're about to prompt so swap the members back.
|
2015-04-10 19:10:00 +03:00
|
|
|
element.swap(mOwnerElement);
|
2014-09-27 03:21:57 +04:00
|
|
|
principal.swap(mPrincipal);
|
|
|
|
|
|
|
|
rv = obsSvc->NotifyObservers(static_cast<nsIObserver*>(this),
|
|
|
|
kPermissionPromptTopic, nullptr);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
// Finally release if we failed the prompt.
|
2015-04-10 19:10:00 +03:00
|
|
|
mOwnerElement = nullptr;
|
2014-09-27 03:21:57 +04:00
|
|
|
mPrincipal = nullptr;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aCurrentValue = currentValue;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PermissionRequestBase::SetExplicitPermission(nsIPrincipal* aPrincipal,
|
|
|
|
uint32_t aIntPermission) {
|
|
|
|
AssertSanity();
|
|
|
|
MOZ_ASSERT(aPrincipal);
|
|
|
|
MOZ_ASSERT(aIntPermission == kPermissionAllowed ||
|
|
|
|
aIntPermission == kPermissionDenied);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPermissionManager> permMan = GetPermissionManager();
|
|
|
|
if (NS_WARN_IF(!permMan)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-28 11:44:12 +03:00
|
|
|
nsresult rv =
|
|
|
|
permMan->AddFromPrincipal(aPrincipal, kPermissionString, aIntPermission,
|
|
|
|
nsIPermissionManager::EXPIRE_NEVER,
|
|
|
|
/* aExpireTime */ 0);
|
2014-09-27 03:21:57 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 05:58:50 +03:00
|
|
|
NS_IMPL_ISUPPORTS(PermissionRequestBase, nsIObserver, nsIIDBPermissionsRequest)
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-05-30 05:58:50 +03:00
|
|
|
PermissionRequestBase::GetBrowserElement(Element** aElement) {
|
2014-09-27 03:21:57 +04:00
|
|
|
AssertSanity();
|
2018-05-30 05:58:50 +03:00
|
|
|
*aElement = do_AddRef(mOwnerElement).take();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-09-27 03:21:57 +04:00
|
|
|
|
2018-05-30 05:58:50 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PermissionRequestBase::GetResponseObserver(nsIObserver** aObserver) {
|
|
|
|
AssertSanity();
|
|
|
|
*aObserver = do_AddRef(this).take();
|
|
|
|
return NS_OK;
|
2014-09-27 03:21:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PermissionRequestBase::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
|
|
|
AssertSanity();
|
|
|
|
MOZ_ASSERT(!strcmp(aTopic, kPermissionResponseTopic));
|
2015-04-10 19:10:00 +03:00
|
|
|
MOZ_ASSERT(mOwnerElement);
|
2014-09-27 03:21:57 +04:00
|
|
|
MOZ_ASSERT(mPrincipal);
|
|
|
|
|
2015-04-10 19:10:00 +03:00
|
|
|
nsCOMPtr<Element> element;
|
|
|
|
element.swap(mOwnerElement);
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
mPrincipal.swap(principal);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
uint32_t promptResult = nsDependentString(aData).ToInteger(&rv);
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(rv);
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
// The UI prompt code will only return one of these three values. We have to
|
|
|
|
// transform it to our values.
|
|
|
|
MOZ_ASSERT(promptResult == kPermissionDefault ||
|
|
|
|
promptResult == kPermissionAllowed ||
|
|
|
|
promptResult == kPermissionDenied);
|
|
|
|
|
|
|
|
if (promptResult != kPermissionDefault) {
|
|
|
|
// Save explicitly allowed or denied permissions now.
|
|
|
|
SetExplicitPermission(principal, promptResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
PermissionValue permission;
|
|
|
|
switch (promptResult) {
|
|
|
|
case kPermissionDefault:
|
|
|
|
permission = kPermissionPrompt;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kPermissionAllowed:
|
|
|
|
permission = kPermissionAllowed;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kPermissionDenied:
|
|
|
|
permission = kPermissionDenied;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Bad prompt result!");
|
|
|
|
}
|
|
|
|
|
|
|
|
OnPromptComplete(permission);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace indexedDB
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|