2010-09-09 15:15:40 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07: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/. */
|
2010-09-09 15:15:40 -07:00
|
|
|
|
|
|
|
#include "CheckPermissionsHelper.h"
|
|
|
|
|
|
|
|
#include "nsIDOMWindow.h"
|
2012-05-26 01:37:58 -07:00
|
|
|
#include "nsILoadContext.h"
|
|
|
|
#include "nsIWebNavigation.h"
|
2010-09-09 15:15:40 -07:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIPermissionManager.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDOMStorage.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "mozilla/Services.h"
|
2011-05-24 23:31:59 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2010-09-09 15:15:40 -07:00
|
|
|
|
2010-10-19 10:58:39 -07:00
|
|
|
#include "IndexedDatabaseManager.h"
|
|
|
|
|
2010-09-09 15:15:40 -07:00
|
|
|
#define PERMISSION_INDEXEDDB "indexedDB"
|
|
|
|
#define PREF_INDEXEDDB_ENABLED "dom.indexedDB.enabled"
|
|
|
|
#define TOPIC_PERMISSIONS_PROMPT "indexedDB-permissions-prompt"
|
|
|
|
#define TOPIC_PERMISSIONS_RESPONSE "indexedDB-permissions-response"
|
|
|
|
|
2012-07-05 14:11:48 -07:00
|
|
|
// This is a little confusing, but our default behavior (UNKNOWN_ACTION) is to
|
|
|
|
// allow access without a prompt. If the "indexedDB" permission is set to
|
|
|
|
// ALLOW_ACTION then we will issue a prompt before allowing access. Otherwise
|
|
|
|
// (DENY_ACTION) we deny access.
|
|
|
|
#define PERMISSION_ALLOWED nsIPermissionManager::UNKNOWN_ACTION
|
|
|
|
#define PERMISSION_DENIED nsIPermissionManager::DENY_ACTION
|
|
|
|
#define PERMISSION_PROMPT nsIPermissionManager::ALLOW_ACTION
|
|
|
|
|
2011-05-24 23:31:59 -07:00
|
|
|
using namespace mozilla;
|
2010-09-09 15:15:40 -07:00
|
|
|
USING_INDEXEDDB_NAMESPACE
|
|
|
|
using namespace mozilla::services;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2012-07-15 18:35:47 -07:00
|
|
|
GetIndexedDBPermissions(nsIDOMWindow* aWindow)
|
2010-09-09 15:15:40 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2011-05-24 23:31:59 -07:00
|
|
|
if (!Preferences::GetBool(PREF_INDEXEDDB_ENABLED)) {
|
2012-07-05 14:11:48 -07:00
|
|
|
return PERMISSION_DENIED;
|
2010-09-09 15:15:40 -07:00
|
|
|
}
|
|
|
|
|
2012-07-05 14:11:48 -07:00
|
|
|
// No window here means chrome access.
|
2012-01-03 07:27:39 -08:00
|
|
|
if (!aWindow) {
|
2012-07-05 14:11:48 -07:00
|
|
|
return PERMISSION_ALLOWED;
|
2012-01-03 07:27:39 -08:00
|
|
|
}
|
|
|
|
|
2010-09-09 15:15:40 -07:00
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop(do_QueryInterface(aWindow));
|
|
|
|
NS_ENSURE_TRUE(sop, nsIPermissionManager::DENY_ACTION);
|
|
|
|
|
|
|
|
if (nsContentUtils::IsSystemPrincipal(sop->GetPrincipal())) {
|
2012-07-05 14:11:48 -07:00
|
|
|
return PERMISSION_ALLOWED;
|
2010-09-09 15:15:40 -07:00
|
|
|
}
|
|
|
|
|
2012-05-26 01:37:58 -07:00
|
|
|
nsCOMPtr<nsIWebNavigation> webNav = do_GetInterface(aWindow);
|
|
|
|
nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(webNav);
|
|
|
|
if (loadContext && loadContext->UsePrivateBrowsing()) {
|
2010-09-09 15:15:40 -07:00
|
|
|
// TODO Support private browsing indexedDB?
|
2012-07-05 14:11:48 -07:00
|
|
|
NS_WARNING("IndexedDB may not be used while in private browsing mode!");
|
|
|
|
return PERMISSION_DENIED;
|
2010-09-09 15:15:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPermissionManager> permissionManager =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
2012-07-05 14:11:48 -07:00
|
|
|
NS_ENSURE_TRUE(permissionManager, PERMISSION_DENIED);
|
2010-09-09 15:15:40 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t permission;
|
2012-07-15 18:35:47 -07:00
|
|
|
nsresult rv =
|
|
|
|
permissionManager->TestPermissionFromPrincipal(sop->GetPrincipal(),
|
|
|
|
PERMISSION_INDEXEDDB,
|
|
|
|
&permission);
|
2012-07-05 14:11:48 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, PERMISSION_DENIED);
|
2010-09-09 15:15:40 -07:00
|
|
|
|
|
|
|
return permission;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS3(CheckPermissionsHelper, nsIRunnable,
|
|
|
|
nsIInterfaceRequestor,
|
|
|
|
nsIObserver)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CheckPermissionsHelper::Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t permission = mHasPrompted ?
|
2010-09-09 15:15:40 -07:00
|
|
|
mPromptResult :
|
2012-07-15 18:35:47 -07:00
|
|
|
GetIndexedDBPermissions(mWindow);
|
2010-09-09 15:15:40 -07:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
if (mHasPrompted) {
|
2011-04-29 16:46:20 -07:00
|
|
|
// Add permissions to the database, but only if we are in the parent
|
|
|
|
// process (if we are in the child process, we have already
|
|
|
|
// set the permission when the prompt was shown in the parent, as
|
|
|
|
// we cannot set the permission from the child).
|
2012-07-05 14:11:48 -07:00
|
|
|
if (permission != PERMISSION_PROMPT &&
|
|
|
|
IndexedDatabaseManager::IsMainProcess()) {
|
2010-09-09 15:15:40 -07:00
|
|
|
nsCOMPtr<nsIPermissionManager> permissionManager =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
|
|
|
NS_ENSURE_STATE(permissionManager);
|
2012-07-05 14:11:48 -07:00
|
|
|
|
2012-07-15 18:35:47 -07:00
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(mWindow);
|
|
|
|
NS_ENSURE_TRUE(sop, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
rv = permissionManager->AddFromPrincipal(sop->GetPrincipal(),
|
|
|
|
PERMISSION_INDEXEDDB, permission,
|
|
|
|
nsIPermissionManager::EXPIRE_NEVER,
|
|
|
|
0);
|
2010-09-09 15:15:40 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
2012-07-05 14:11:48 -07:00
|
|
|
else if (permission == PERMISSION_PROMPT && mPromptAllowed) {
|
2010-09-09 15:15:40 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obs = GetObserverService();
|
|
|
|
rv = obs->NotifyObservers(static_cast<nsIRunnable*>(this),
|
2012-07-30 07:20:58 -07:00
|
|
|
TOPIC_PERMISSIONS_PROMPT, nullptr);
|
2010-09-09 15:15:40 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-20 09:10:56 -07:00
|
|
|
nsRefPtr<OpenDatabaseHelper> helper;
|
2010-09-09 15:15:40 -07:00
|
|
|
helper.swap(mHelper);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindow> window;
|
|
|
|
window.swap(mWindow);
|
|
|
|
|
2012-07-05 14:11:48 -07:00
|
|
|
if (permission == PERMISSION_ALLOWED) {
|
2010-10-19 10:58:42 -07:00
|
|
|
IndexedDatabaseManager* mgr = IndexedDatabaseManager::Get();
|
|
|
|
NS_ASSERTION(mgr, "This should never be null!");
|
|
|
|
|
|
|
|
return helper->Dispatch(mgr->IOThread());
|
2010-09-09 15:15:40 -07:00
|
|
|
}
|
|
|
|
|
2012-07-05 14:11:48 -07:00
|
|
|
NS_ASSERTION(permission == PERMISSION_PROMPT ||
|
|
|
|
permission == PERMISSION_DENIED,
|
2010-09-09 15:15:40 -07:00
|
|
|
"Unknown permission!");
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
helper->SetError(NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR);
|
2011-10-20 09:10:56 -07:00
|
|
|
|
|
|
|
return helper->RunImmediately();
|
2010-09-09 15:15:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CheckPermissionsHelper::GetInterface(const nsIID& aIID,
|
|
|
|
void** aResult)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIObserver))) {
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIDOMWindow))) {
|
|
|
|
return mWindow->QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
*aResult = nullptr;
|
2010-09-09 15:15:40 -07:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CheckPermissionsHelper::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!strcmp(aTopic, TOPIC_PERMISSIONS_RESPONSE), "Bad topic!");
|
2011-11-07 16:15:45 -08:00
|
|
|
NS_ASSERTION(mPromptAllowed, "How did we get here?");
|
2010-09-09 15:15:40 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mHasPrompted = true;
|
2010-09-09 15:15:40 -07:00
|
|
|
|
|
|
|
nsresult rv;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t promptResult = nsDependentString(aData).ToInteger(&rv);
|
2010-09-09 15:15:40 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-05 14:11:48 -07:00
|
|
|
// Have to convert the permission we got from the user to our weird reversed
|
|
|
|
// permission type.
|
|
|
|
switch (promptResult) {
|
|
|
|
case nsIPermissionManager::ALLOW_ACTION:
|
|
|
|
mPromptResult = PERMISSION_ALLOWED;
|
|
|
|
break;
|
|
|
|
case nsIPermissionManager::DENY_ACTION:
|
|
|
|
mPromptResult = PERMISSION_DENIED;
|
|
|
|
break;
|
|
|
|
case nsIPermissionManager::UNKNOWN_ACTION:
|
|
|
|
mPromptResult = PERMISSION_PROMPT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown permission type!");
|
|
|
|
mPromptResult = PERMISSION_DENIED;
|
|
|
|
}
|
|
|
|
|
2011-11-02 05:53:12 -07:00
|
|
|
rv = NS_DispatchToCurrentThread(this);
|
2010-10-19 10:58:39 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
2010-09-09 15:15:40 -07:00
|
|
|
}
|