Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Member Functions | Protected Attributes
nsDOMStorageDB Class Reference

#include <nsDOMStorageDB.h>

Collaboration diagram for nsDOMStorageDB:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsDOMStorageDB ()
 ~nsDOMStorageDB ()
nsresult Init ()
nsresult GetAllKeys (const nsAString &aDomain, nsDOMStorage *aStorage, nsTHashtable< nsSessionStorageEntry > *aKeys)
 Retrieve a list of all the keys associated with a particular domain.
nsresult GetKeyValue (const nsAString &aDomain, const nsAString &aKey, nsAString &aValue, PRBool *aSecure, nsAString &aOwner)
 Retrieve a value and secure flag for a key from storage.
nsresult SetKey (const nsAString &aDomain, const nsAString &aKey, const nsAString &aValue, PRBool aSecure, const nsAString &aOwner, PRInt32 aQuota)
 Set the value and secure flag for a key in storage.
nsresult SetSecure (const nsAString &aDomain, const nsAString &aKey, const PRBool aSecure)
 Set the secure flag for a key in storage.
nsresult RemoveKey (const nsAString &aDomain, const nsAString &aKey, const nsAString &aOwner, PRInt32 aKeyUsage)
 Removes a key from storage.
nsresult RemoveAll ()
 Removes all keys from storage.

Protected Member Functions

nsresult GetUsage (const nsAString &aOwner, PRInt32 *aUsage)

Protected Attributes

nsCOMPtr< mozIStorageConnectionmConnection
nsCOMPtr< mozIStorageStatementmGetAllKeysStatement
nsCOMPtr< mozIStorageStatementmGetKeyValueStatement
nsCOMPtr< mozIStorageStatementmInsertKeyStatement
nsCOMPtr< mozIStorageStatementmUpdateKeyStatement
nsCOMPtr< mozIStorageStatementmSetSecureStatement
nsCOMPtr< mozIStorageStatementmRemoveKeyStatement
nsCOMPtr< mozIStorageStatementmRemoveAllStatement
nsCOMPtr< mozIStorageStatementmGetUsageStatement
nsAutoString mCachedOwner
PRInt32 mCachedUsage

Detailed Description

Definition at line 48 of file nsDOMStorageDB.h.


Constructor & Destructor Documentation

Definition at line 51 of file nsDOMStorageDB.h.

{};

Definition at line 52 of file nsDOMStorageDB.h.

{};

Member Function Documentation

nsresult nsDOMStorageDB::GetAllKeys ( const nsAString &  aDomain,
nsDOMStorage aStorage,
nsTHashtable< nsSessionStorageEntry > *  aKeys 
)

Retrieve a list of all the keys associated with a particular domain.

Definition at line 189 of file nsDOMStorageDB.cpp.

{
  mozStorageStatementScoper scope(mGetAllKeysStatement);

  nsresult rv = mGetAllKeysStatement->BindStringParameter(0, aDomain);
  NS_ENSURE_SUCCESS(rv, rv);

  PRBool exists;
  while (NS_SUCCEEDED(rv = mGetAllKeysStatement->ExecuteStep(&exists)) &&
         exists) {

    nsAutoString key;
    rv = mGetAllKeysStatement->GetString(0, key);
    NS_ENSURE_SUCCESS(rv, rv);

    PRInt32 secureInt = 0;
    rv = mGetAllKeysStatement->GetInt32(1, &secureInt);
    NS_ENSURE_SUCCESS(rv, rv);

    nsSessionStorageEntry* entry = aKeys->PutEntry(key);
    NS_ENSURE_TRUE(entry, NS_ERROR_OUT_OF_MEMORY);
 
    entry->mItem = new nsDOMStorageItem(aStorage, key, EmptyString(), secureInt);
    if (!entry->mItem) {
      aKeys->RawRemoveEntry(entry);
      return NS_ERROR_OUT_OF_MEMORY;
    }
  }

  return NS_OK;
}

Here is the call graph for this function:

nsresult nsDOMStorageDB::GetKeyValue ( const nsAString &  aDomain,
const nsAString &  aKey,
nsAString &  aValue,
PRBool aSecure,
nsAString &  aOwner 
)

Retrieve a value and secure flag for a key from storage.

Exceptions:
NS_ERROR_DOM_NOT_FOUND_ERRif key not found

Definition at line 224 of file nsDOMStorageDB.cpp.

{
  mozStorageStatementScoper scope(mGetKeyValueStatement);

  nsresult rv = mGetKeyValueStatement->BindStringParameter(0, aDomain);
  NS_ENSURE_SUCCESS(rv, rv);
  rv = mGetKeyValueStatement->BindStringParameter(1, aKey);
  NS_ENSURE_SUCCESS(rv, rv);

  PRBool exists;
  rv = mGetKeyValueStatement->ExecuteStep(&exists);
  NS_ENSURE_SUCCESS(rv, rv);

  PRInt32 secureInt = 0;
  if (exists) {
    rv = mGetKeyValueStatement->GetString(0, aValue);
    NS_ENSURE_SUCCESS(rv, rv);

    rv = mGetKeyValueStatement->GetInt32(1, &secureInt);
    NS_ENSURE_SUCCESS(rv, rv);

    rv = mGetKeyValueStatement->GetString(2, aOwner);
    NS_ENSURE_SUCCESS(rv, rv);
  }
  else {
    rv = NS_ERROR_DOM_NOT_FOUND_ERR;
  }

  *aSecure = (PRBool)secureInt;

  return rv;
}
nsresult nsDOMStorageDB::GetUsage ( const nsAString &  aOwner,
PRInt32 aUsage 
) [protected]

Definition at line 427 of file nsDOMStorageDB.cpp.

{
  mozStorageStatementScoper scope(mGetUsageStatement);

  nsresult rv = mGetUsageStatement->BindStringParameter(0, aOwner);
  NS_ENSURE_SUCCESS(rv, rv);
  
  PRBool exists;
  rv = mGetUsageStatement->ExecuteStep(&exists);
  NS_ENSURE_SUCCESS(rv, rv);

  if (!exists) {
    *aUsage = 0;
    return NS_OK;
  }
  
  return mGetUsageStatement->GetInt32(0, aUsage);
}

Here is the caller graph for this function:

Definition at line 51 of file nsDOMStorageDB.cpp.

{
  nsresult rv;

  nsCOMPtr<nsIFile> storageFile;
  rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
                              getter_AddRefs(storageFile));
  NS_ENSURE_SUCCESS(rv, rv);
  rv = storageFile->Append(NS_LITERAL_STRING("webappsstore.sqlite"));
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<mozIStorageService> service;

  service = do_GetService(MOZ_STORAGE_SERVICE_CONTRACTID, &rv);
  NS_ENSURE_SUCCESS(rv, rv);
  rv = service->OpenDatabase(storageFile, getter_AddRefs(mConnection));
  if (rv == NS_ERROR_FILE_CORRUPTED) {
    // delete the db and try opening again
    rv = storageFile->Remove(PR_FALSE);
    NS_ENSURE_SUCCESS(rv, rv);
    rv = service->OpenDatabase(storageFile, getter_AddRefs(mConnection));
  }
  NS_ENSURE_SUCCESS(rv, rv);

  PRBool exists;
  rv = mConnection->TableExists(NS_LITERAL_CSTRING("webappsstore"), &exists);
  NS_ENSURE_SUCCESS(rv, rv);
  if (! exists) {
    rv = mConnection->ExecuteSimpleSQL(
           NS_LITERAL_CSTRING("CREATE TABLE webappsstore ("
                              "domain TEXT, "
                              "key TEXT, "
                              "value TEXT, "
                              "secure INTEGER, "
                              "owner TEXT)"));
    NS_ENSURE_SUCCESS(rv, rv);
  }
  
  rv = mConnection->TableExists(NS_LITERAL_CSTRING("moz_webappsstore"),
                                &exists);
  NS_ENSURE_SUCCESS(rv, rv);

  if (exists) {
      // upgrade an old store
      
      // create a temporary index to handle dup checking
       rv = mConnection->ExecuteSimpleSQL(
              NS_LITERAL_CSTRING("CREATE UNIQUE INDEX webappsstore_tmp "
                                 " ON webappsstore(domain, key)"));

      // if the index can't be created, there are dup domain/key combos
      // in moz_webappstore2, which indicates a bug elsewhere.  Fail to upgrade
      // in this case
      if (NS_SUCCEEDED(rv)) {
          rv = mConnection->ExecuteSimpleSQL(
                 NS_LITERAL_CSTRING("INSERT OR IGNORE INTO "
                                    "webappsstore(domain, key, value, secure, owner) "
                                    "SELECT domain, key, value, secure, domain "
                                    "FROM moz_webappsstore"));

          // try to drop the index even in case of an error
          mConnection->ExecuteSimpleSQL(
            NS_LITERAL_CSTRING("DROP INDEX webappsstore_tmp"));

          NS_ENSURE_SUCCESS(rv, rv);

          rv = mConnection->ExecuteSimpleSQL(
                 NS_LITERAL_CSTRING("DROP TABLE moz_webappsstore"));
          NS_ENSURE_SUCCESS(rv, rv);
      }
  }
  
  // retrieve all keys associated with a domain
  rv = mConnection->CreateStatement(
         NS_LITERAL_CSTRING("SELECT key, secure FROM webappsstore "
                            "WHERE domain = ?1"),
         getter_AddRefs(mGetAllKeysStatement));
  NS_ENSURE_SUCCESS(rv, rv);

  // retrieve a value given a domain and a key
  rv = mConnection->CreateStatement(
         NS_LITERAL_CSTRING("SELECT value, secure, owner FROM webappsstore "
                            "WHERE domain = ?1 "
                            "AND key = ?2"),
         getter_AddRefs(mGetKeyValueStatement));
  NS_ENSURE_SUCCESS(rv, rv);

  // insert a new key
  rv = mConnection->CreateStatement(
    NS_LITERAL_CSTRING("INSERT INTO "
                       "webappsstore(domain, key, value, secure, owner) "
                       "VALUES (?1, ?2, ?3, ?4, ?5)"),
         getter_AddRefs(mInsertKeyStatement));
  NS_ENSURE_SUCCESS(rv, rv);

  // update an existing key
  rv = mConnection->CreateStatement(
         NS_LITERAL_CSTRING("UPDATE webappsstore "
                            "SET value = ?1, secure = ?2, owner = ?3"
                            "WHERE domain = ?4 "
                            "AND key = ?5 "),
         getter_AddRefs(mUpdateKeyStatement));
  NS_ENSURE_SUCCESS(rv, rv);

  // update the secure status of an existing key
  rv = mConnection->CreateStatement(
         NS_LITERAL_CSTRING("UPDATE webappsstore "
                            "SET secure = ?1 "
                            "WHERE domain = ?2 "
                            "AND key = ?3 "),
         getter_AddRefs(mSetSecureStatement));
  NS_ENSURE_SUCCESS(rv, rv);

  // remove a key
  rv = mConnection->CreateStatement(
         NS_LITERAL_CSTRING("DELETE FROM webappsstore "
                            "WHERE domain = ?1 "
                            "AND key = ?2"),
         getter_AddRefs(mRemoveKeyStatement));
  NS_ENSURE_SUCCESS(rv, rv);

  // remove all keys
  rv = mConnection->CreateStatement(
         NS_LITERAL_CSTRING("DELETE FROM webappsstore"),
         getter_AddRefs(mRemoveAllStatement));
  NS_ENSURE_SUCCESS(rv, rv);
  
  // check the usage for a given owner
  rv = mConnection->CreateStatement(
         NS_LITERAL_CSTRING("SELECT SUM(LENGTH(key) + LENGTH(value)) "
                            "FROM webappsstore "
                            "WHERE owner = ?1"),
         getter_AddRefs(mGetUsageStatement));

  return rv;
}

Here is the call graph for this function:

Removes all keys from storage.

Used when clearing storage.

Definition at line 420 of file nsDOMStorageDB.cpp.

nsresult nsDOMStorageDB::RemoveKey ( const nsAString &  aDomain,
const nsAString &  aKey,
const nsAString &  aOwner,
PRInt32  aKeyUsage 
)

Removes a key from storage.

Definition at line 400 of file nsDOMStorageDB.cpp.

{
  mozStorageStatementScoper scope(mRemoveKeyStatement);

  if (aOwner == mCachedOwner) {
    mCachedUsage -= aKeyUsage;
  }

  nsresult rv = mRemoveKeyStatement->BindStringParameter(0, aDomain);
  NS_ENSURE_SUCCESS(rv, rv);
  rv = mRemoveKeyStatement->BindStringParameter(1, aKey);
  NS_ENSURE_SUCCESS(rv, rv);

  return mRemoveKeyStatement->Execute();
}
nsresult nsDOMStorageDB::SetKey ( const nsAString &  aDomain,
const nsAString &  aKey,
const nsAString &  aValue,
PRBool  aSecure,
const nsAString &  aOwner,
PRInt32  aQuota 
)

Set the value and secure flag for a key in storage.

Definition at line 262 of file nsDOMStorageDB.cpp.

{
  mozStorageStatementScoper scope(mGetKeyValueStatement);
 
  PRInt32 usage = 0;
  nsresult rv;
  if (!aOwner.IsEmpty()) {
    if (aOwner == mCachedOwner) {
      usage = mCachedUsage;
    } else {
      rv = GetUsage(aOwner, &usage);
      NS_ENSURE_SUCCESS(rv, rv);
    }
  }

  usage += aKey.Length() + aValue.Length();

  rv = mGetKeyValueStatement->BindStringParameter(0, aDomain);
  NS_ENSURE_SUCCESS(rv, rv);
  rv = mGetKeyValueStatement->BindStringParameter(1, aKey);
  NS_ENSURE_SUCCESS(rv, rv);

  PRBool exists;
  rv = mGetKeyValueStatement->ExecuteStep(&exists);
  NS_ENSURE_SUCCESS(rv, rv);

  if (exists) {
    if (!aSecure) {
      PRInt32 secureInt = 0;
      rv = mGetKeyValueStatement->GetInt32(1, &secureInt);
      NS_ENSURE_SUCCESS(rv, rv);
      if (secureInt)
        return NS_ERROR_DOM_SECURITY_ERR;
    }

    nsAutoString previousOwner;
    rv = mGetKeyValueStatement->GetString(2, previousOwner);
    NS_ENSURE_SUCCESS(rv, rv);
    
    if (previousOwner == aOwner) {
      nsAutoString previousValue;
      rv = mGetKeyValueStatement->GetString(0, previousValue);
      NS_ENSURE_SUCCESS(rv, rv);
      usage -= aKey.Length() + previousValue.Length();
    }

    mGetKeyValueStatement->Reset();

    if (usage > aQuota) {
      return NS_ERROR_DOM_QUOTA_REACHED;
    }

    mozStorageStatementScoper scopeupdate(mUpdateKeyStatement);

    rv = mUpdateKeyStatement->BindStringParameter(0, aValue);
    NS_ENSURE_SUCCESS(rv, rv);
    rv = mUpdateKeyStatement->BindInt32Parameter(1, aSecure);
    NS_ENSURE_SUCCESS(rv, rv);
    rv = mUpdateKeyStatement->BindStringParameter(2, aOwner);
    NS_ENSURE_SUCCESS(rv, rv);
    rv = mUpdateKeyStatement->BindStringParameter(3, aDomain);
    NS_ENSURE_SUCCESS(rv, rv);
    rv = mUpdateKeyStatement->BindStringParameter(4, aKey);
    NS_ENSURE_SUCCESS(rv, rv);

    rv = mUpdateKeyStatement->Execute();
    NS_ENSURE_SUCCESS(rv, rv);
  }
  else {
    if (usage > aQuota) {
      return NS_ERROR_DOM_QUOTA_REACHED;
    }
    
    mozStorageStatementScoper scopeinsert(mInsertKeyStatement);
    
    rv = mInsertKeyStatement->BindStringParameter(0, aDomain);
    NS_ENSURE_SUCCESS(rv, rv);
    rv = mInsertKeyStatement->BindStringParameter(1, aKey);
    NS_ENSURE_SUCCESS(rv, rv);
    rv = mInsertKeyStatement->BindStringParameter(2, aValue);
    NS_ENSURE_SUCCESS(rv, rv);
    rv = mInsertKeyStatement->BindInt32Parameter(3, aSecure);
    NS_ENSURE_SUCCESS(rv, rv);
    rv = mInsertKeyStatement->BindStringParameter(4, aOwner);
    NS_ENSURE_SUCCESS(rv, rv);
    
    rv = mInsertKeyStatement->Execute();
    NS_ENSURE_SUCCESS(rv, rv);
  }

  if (!aOwner.IsEmpty()) {
    mCachedOwner = aOwner;
    mCachedUsage = usage;
  }

  return NS_OK;
}

Here is the call graph for this function:

nsresult nsDOMStorageDB::SetSecure ( const nsAString &  aDomain,
const nsAString &  aKey,
const PRBool  aSecure 
)

Set the secure flag for a key in storage.

Does nothing if the key was not found.

Definition at line 366 of file nsDOMStorageDB.cpp.

{
  mozStorageStatementScoper scope(mGetKeyValueStatement);

  nsresult rv = mGetKeyValueStatement->BindStringParameter(0, aDomain);
  NS_ENSURE_SUCCESS(rv, rv);
  rv = mGetKeyValueStatement->BindStringParameter(1, aKey);
  NS_ENSURE_SUCCESS(rv, rv);

  PRBool exists;
  rv = mGetKeyValueStatement->ExecuteStep(&exists);
  NS_ENSURE_SUCCESS(rv, rv);

  if (exists) {
    mGetKeyValueStatement->Reset();
    
    mozStorageStatementScoper scopeupdate(mUpdateKeyStatement);

    rv = mSetSecureStatement->BindInt32Parameter(0, aSecure ? 1 : 0);
    NS_ENSURE_SUCCESS(rv, rv);
    rv = mSetSecureStatement->BindStringParameter(1, aDomain);
    NS_ENSURE_SUCCESS(rv, rv);
    rv = mSetSecureStatement->BindStringParameter(2, aKey);
    NS_ENSURE_SUCCESS(rv, rv);

    return mSetSecureStatement->Execute();
  }

  return NS_OK;
}

Member Data Documentation

Definition at line 127 of file nsDOMStorageDB.h.

Definition at line 128 of file nsDOMStorageDB.h.

Definition at line 116 of file nsDOMStorageDB.h.

Definition at line 118 of file nsDOMStorageDB.h.

Definition at line 119 of file nsDOMStorageDB.h.

Definition at line 125 of file nsDOMStorageDB.h.

Definition at line 120 of file nsDOMStorageDB.h.

Definition at line 124 of file nsDOMStorageDB.h.

Definition at line 123 of file nsDOMStorageDB.h.

Definition at line 122 of file nsDOMStorageDB.h.

Definition at line 121 of file nsDOMStorageDB.h.


The documentation for this class was generated from the following files: