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"
|
|
|
|
|
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
|
|
|
|
PRUint32
|
|
|
|
GetIndexedDBPermissions(const nsACString& aASCIIOrigin,
|
|
|
|
nsIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2011-05-24 23:31:59 -07:00
|
|
|
if (!Preferences::GetBool(PREF_INDEXEDDB_ENABLED)) {
|
2012-07-01 21:49:38 -07:00
|
|
|
return nsIPermissionManager::DENY_ACTION;
|
2010-09-09 15:15:40 -07:00
|
|
|
}
|
|
|
|
|
2012-07-01 21:49:38 -07:00
|
|
|
// No window here means chrome access
|
2012-01-03 07:27:39 -08:00
|
|
|
if (!aWindow) {
|
2012-07-01 21:49:38 -07:00
|
|
|
return nsIPermissionManager::ALLOW_ACTION;
|
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-01 21:49:38 -07:00
|
|
|
return nsIPermissionManager::ALLOW_ACTION;
|
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-01 21:49:38 -07:00
|
|
|
return nsIPermissionManager::DENY_ACTION;
|
2010-09-09 15:15:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), aASCIIOrigin);
|
2012-07-01 21:49:38 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nsIPermissionManager::DENY_ACTION);
|
2010-09-09 15:15:40 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPermissionManager> permissionManager =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
2012-07-01 21:49:38 -07:00
|
|
|
NS_ENSURE_TRUE(permissionManager, nsIPermissionManager::DENY_ACTION);
|
2010-09-09 15:15:40 -07:00
|
|
|
|
|
|
|
PRUint32 permission;
|
|
|
|
rv = permissionManager->TestPermission(uri, PERMISSION_INDEXEDDB,
|
|
|
|
&permission);
|
2012-07-01 21:49:38 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nsIPermissionManager::DENY_ACTION);
|
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!");
|
|
|
|
|
|
|
|
PRUint32 permission = mHasPrompted ?
|
|
|
|
mPromptResult :
|
|
|
|
GetIndexedDBPermissions(mASCIIOrigin, mWindow);
|
|
|
|
|
|
|
|
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-01 21:49:38 -07:00
|
|
|
if (permission != nsIPermissionManager::UNKNOWN_ACTION &&
|
|
|
|
XRE_GetProcessType() == GeckoProcessType_Default) {
|
2010-09-09 15:15:40 -07:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), mASCIIOrigin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-07-01 21:49:38 -07:00
|
|
|
|
2010-09-09 15:15:40 -07:00
|
|
|
nsCOMPtr<nsIPermissionManager> permissionManager =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
|
|
|
NS_ENSURE_STATE(permissionManager);
|
2012-07-01 21:49:38 -07:00
|
|
|
|
2010-09-09 15:15:40 -07:00
|
|
|
rv = permissionManager->Add(uri, PERMISSION_INDEXEDDB, permission,
|
|
|
|
nsIPermissionManager::EXPIRE_NEVER, 0);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
2012-07-01 21:49:38 -07:00
|
|
|
else if (permission == nsIPermissionManager::UNKNOWN_ACTION &&
|
|
|
|
mPromptAllowed) {
|
2010-09-09 15:15:40 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obs = GetObserverService();
|
|
|
|
rv = obs->NotifyObservers(static_cast<nsIRunnable*>(this),
|
|
|
|
TOPIC_PERMISSIONS_PROMPT, nsnull);
|
|
|
|
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-01 21:49:38 -07:00
|
|
|
if (permission == nsIPermissionManager::ALLOW_ACTION) {
|
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-01 21:49:38 -07:00
|
|
|
NS_ASSERTION(permission == nsIPermissionManager::UNKNOWN_ACTION ||
|
|
|
|
permission == nsIPermissionManager::DENY_ACTION,
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = nsnull;
|
|
|
|
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-07-01 21:49:38 -07:00
|
|
|
mPromptResult = nsDependentString(aData).ToInteger(&rv);
|
2010-09-09 15:15:40 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
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
|
|
|
}
|