From 2788e8d9d2247a46aee51ec8eebdd0cb69b3955c Mon Sep 17 00:00:00 2001 From: "dcamp@mozilla.com" Date: Wed, 3 Oct 2007 23:05:32 -0700 Subject: [PATCH] let chrome check globalStorage usage for a domain. b=396249, r=enndeakin, sr=jst, a=jst --- dom/public/idl/storage/Makefile.in | 1 + .../idl/storage/nsIDOMStorageManager.idl | 52 +++++++++++++++++++ dom/src/storage/nsDOMStorage.cpp | 24 ++++++++- dom/src/storage/nsDOMStorage.h | 8 ++- dom/src/storage/nsDOMStorageDB.cpp | 23 +++++--- dom/src/storage/nsDOMStorageDB.h | 4 +- layout/build/nsLayoutCID.h | 4 ++ layout/build/nsLayoutModule.cpp | 7 +++ 8 files changed, 112 insertions(+), 11 deletions(-) create mode 100644 dom/public/idl/storage/nsIDOMStorageManager.idl diff --git a/dom/public/idl/storage/Makefile.in b/dom/public/idl/storage/Makefile.in index c46f21d6edb..7485927ee45 100644 --- a/dom/public/idl/storage/Makefile.in +++ b/dom/public/idl/storage/Makefile.in @@ -52,6 +52,7 @@ EXPORTS = \ XPIDLSRCS = \ nsIDOMToString.idl \ + nsIDOMStorageManager.idl \ $(NULL) SDK_XPIDLSRCS = \ diff --git a/dom/public/idl/storage/nsIDOMStorageManager.idl b/dom/public/idl/storage/nsIDOMStorageManager.idl new file mode 100644 index 00000000000..c1069284681 --- /dev/null +++ b/dom/public/idl/storage/nsIDOMStorageManager.idl @@ -0,0 +1,52 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Mozilla Corporation. + * Portions created by the Initial Developer are Copyright (C) 2007 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsISupports.idl" + +[scriptable, uuid(74dc93d1-8cdf-43b1-a52c-776dcb873475)] +interface nsIDOMStorageManager : nsISupports +{ + /** + * Return the amount of disk space used by a domain. Usage is checked + * against the domain of the page that set the key (the owner domain), not + * the domain of the storage object. + * + * @param aOwnerDomain The domain to check. + * @returns the space usage of the domain, in bytes. + */ + long getUsage(in AString aOwnerDomain); +}; diff --git a/dom/src/storage/nsDOMStorage.cpp b/dom/src/storage/nsDOMStorage.cpp index f542cf353c1..15c1f47a882 100644 --- a/dom/src/storage/nsDOMStorage.cpp +++ b/dom/src/storage/nsDOMStorage.cpp @@ -131,7 +131,9 @@ nsSessionStorageEntry::~nsSessionStorageEntry() nsDOMStorageManager* nsDOMStorageManager::gStorageManager; -NS_IMPL_ISUPPORTS1(nsDOMStorageManager, nsIObserver) +NS_IMPL_ISUPPORTS2(nsDOMStorageManager, + nsIDOMStorageManager, + nsIObserver) //static nsresult @@ -156,6 +158,16 @@ nsDOMStorageManager::Initialize() return NS_OK; } +//static +nsDOMStorageManager* +nsDOMStorageManager::GetInstance() +{ + NS_ASSERTION(gStorageManager, + "nsDOMStorageManager::GetInstance() called before Initialize()"); + NS_IF_ADDREF(gStorageManager); + return gStorageManager; +} + //static void nsDOMStorageManager::Shutdown() @@ -194,6 +206,16 @@ nsDOMStorageManager::Observe(nsISupports *aSubject, return NS_OK; } +NS_IMETHODIMP +nsDOMStorageManager::GetUsage(const nsAString& aDomain, + PRInt32 *aUsage) +{ + nsresult rv = nsDOMStorage::InitDB(); + NS_ENSURE_SUCCESS(rv, rv); + + return nsDOMStorage::gStorageDB->GetUsage(aDomain, aUsage); +} + void nsDOMStorageManager::AddToStoragesHash(nsDOMStorage* aStorage) { diff --git a/dom/src/storage/nsDOMStorage.h b/dom/src/storage/nsDOMStorage.h index 81d63a875a9..558b3159732 100644 --- a/dom/src/storage/nsDOMStorage.h +++ b/dom/src/storage/nsDOMStorage.h @@ -51,6 +51,7 @@ #include "nsIDOMToString.h" #include "nsDOMEvent.h" #include "nsIDOMStorageEvent.h" +#include "nsIDOMStorageManager.h" #ifdef MOZ_STORAGE #include "nsDOMStorageDB.h" @@ -80,12 +81,16 @@ public: nsRefPtr mItem; }; -class nsDOMStorageManager : public nsIObserver +class nsDOMStorageManager : public nsIDOMStorageManager + , public nsIObserver { public: // nsISupports NS_DECL_ISUPPORTS + // nsIDOMStorageManager + NS_DECL_NSIDOMSTORAGEMANAGER + // nsIObserver NS_DECL_NSIOBSERVER @@ -95,6 +100,7 @@ public: nsresult ClearAllStorages(); static nsresult Initialize(); + static nsDOMStorageManager* GetInstance(); static void Shutdown(); static nsDOMStorageManager* gStorageManager; diff --git a/dom/src/storage/nsDOMStorageDB.cpp b/dom/src/storage/nsDOMStorageDB.cpp index ef25a420c98..89dc5b15435 100644 --- a/dom/src/storage/nsDOMStorageDB.cpp +++ b/dom/src/storage/nsDOMStorageDB.cpp @@ -271,12 +271,8 @@ nsDOMStorageDB::SetKey(const nsAString& aDomain, PRInt32 usage = 0; nsresult rv; if (!aOwner.IsEmpty()) { - if (aOwner == mCachedOwner) { - usage = mCachedUsage; - } else { - rv = GetUsage(aOwner, &usage); - NS_ENSURE_SUCCESS(rv, rv); - } + rv = GetUsage(aOwner, &usage); + NS_ENSURE_SUCCESS(rv, rv); } usage += aKey.Length() + aValue.Length(); @@ -426,6 +422,11 @@ nsDOMStorageDB::RemoveAll() nsresult nsDOMStorageDB::GetUsage(const nsAString &aOwner, PRInt32 *aUsage) { + if (aOwner == mCachedOwner) { + *aUsage = mCachedUsage; + return NS_OK; + } + mozStorageStatementScoper scope(mGetUsageStatement); nsresult rv = mGetUsageStatement->BindStringParameter(0, aOwner); @@ -440,5 +441,13 @@ nsDOMStorageDB::GetUsage(const nsAString &aOwner, PRInt32 *aUsage) return NS_OK; } - return mGetUsageStatement->GetInt32(0, aUsage); + rv = mGetUsageStatement->GetInt32(0, aUsage); + NS_ENSURE_SUCCESS(rv, rv); + + if (!aOwner.IsEmpty()) { + mCachedOwner = aOwner; + mCachedUsage = *aUsage; + } + + return NS_OK; } diff --git a/dom/src/storage/nsDOMStorageDB.h b/dom/src/storage/nsDOMStorageDB.h index 8a10b1db189..feeb8401e2c 100644 --- a/dom/src/storage/nsDOMStorageDB.h +++ b/dom/src/storage/nsDOMStorageDB.h @@ -111,10 +111,10 @@ public: nsresult RemoveAll(); -protected: - nsresult GetUsage(const nsAString &aOwner, PRInt32 *aUsage); +protected: + nsCOMPtr mConnection; nsCOMPtr mGetAllKeysStatement; diff --git a/layout/build/nsLayoutCID.h b/layout/build/nsLayoutCID.h index 4c7f58bb1f5..7e616786442 100644 --- a/layout/build/nsLayoutCID.h +++ b/layout/build/nsLayoutCID.h @@ -214,4 +214,8 @@ #define NS_DOMSTORAGE_CID \ { 0x8b449142, 0x1eab, 0x4bfa, { 0x98, 0x30, 0xfa, 0xb6, 0xeb, 0xb0, 0x97, 0x74 } } +// {b88a4712-eb52-4c10-9b85-bf5894b510f0} +#define NS_DOMSTORAGEMANAGER_CID \ +{ 0xb88a4712, 0xeb52, 0x4c10, { 0x9b, 0x85, 0xbf, 0x58, 0x94, 0xb5, 0x10, 0xf0 } } + #endif /* nsLayoutCID_h__ */ diff --git a/layout/build/nsLayoutModule.cpp b/layout/build/nsLayoutModule.cpp index f31a1668c9c..58107c84dd4 100644 --- a/layout/build/nsLayoutModule.cpp +++ b/layout/build/nsLayoutModule.cpp @@ -283,6 +283,8 @@ NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(txNodeSetAdaptor, Init) NS_GENERIC_FACTORY_CONSTRUCTOR(nsDOMSerializer) NS_GENERIC_FACTORY_CONSTRUCTOR(nsXMLHttpRequest) NS_GENERIC_FACTORY_CONSTRUCTOR(nsDOMParser) +NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsDOMStorageManager, + nsDOMStorageManager::GetInstance) //----------------------------------------------------------------------------- @@ -1307,6 +1309,11 @@ static const nsModuleComponentInfo gComponents[] = { "@mozilla.org/dom/storage;1", NS_NewDOMStorage }, + { "DOM Storage Manager", + NS_DOMSTORAGEMANAGER_CID, + "@mozilla.org/dom/storagemanager;1", + nsDOMStorageManagerConstructor }, + { "Text Editor", NS_TEXTEDITOR_CID, "@mozilla.org/editor/texteditor;1",