Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Friends
nsDOMStorage Class Reference

#include <nsDOMStorage.h>

Inheritance diagram for nsDOMStorage:
Inheritance graph
[legend]
Collaboration diagram for nsDOMStorage:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsDOMStorage ()
 nsDOMStorage (nsIURI *aURI, const nsAString &aDomain, PRBool aUseDB)
virtual ~nsDOMStorage ()
NS_DECL_ISUPPORTS virtual
NS_DECL_NSIDOMSTORAGE void 
Init (nsIURI *aURI, const nsAString &aDomain, PRBool aUseDB)
virtual already_AddRefed
< nsIDOMStorage
Clone (nsIURI *aURI)
virtual nsTArray< nsString > * GetKeys ()
PRBool UseDB ()
PRBool CacheStoragePermissions ()
nsresult GetDBValue (const nsAString &aKey, nsAString &aValue, PRBool *aSecure, nsAString &aOwner)
nsresult SetDBValue (const nsAString &aKey, const nsAString &aValue, PRBool aSecure)
nsresult SetSecure (const nsAString &aKey, PRBool aSecure)
void ClearAll ()
DOMString key (in unsigned long index)
 Retrieve the name of the key at a particular index.
nsIDOMStorageItem getItem (in DOMString key)
 Retrieve an item with a given key.
void setItem (in DOMString key, in DOMString data)
 Assign a value with a key.
void removeItem (in DOMString key)
 Remove a key and its corresponding value.

Static Public Member Functions

static PRBool CanUseStorage (nsIURI *aURI, PRPackedBool *aSessionOnly)

Public Attributes

readonly attribute unsigned long length
 The number of keys stored.

Protected Member Functions

nsresult CacheKeysFromDB ()
void BroadcastChangeNotification ()

Static Protected Member Functions

static nsresult InitDB ()

Protected Attributes

PRPackedBool mUseDB
PRPackedBool mSessionOnly
PRPackedBool mItemsCached
nsCOMPtr< nsIURImURI
nsAutoString mDomain
nsTHashtable
< nsSessionStorageEntry
mItems

Friends

class nsDOMStorageManager

Detailed Description

Definition at line 107 of file nsDOMStorage.h.


Constructor & Destructor Documentation

Definition at line 259 of file nsDOMStorage.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

nsDOMStorage::nsDOMStorage ( nsIURI aURI,
const nsAString &  aDomain,
PRBool  aUseDB 
)

Definition at line 267 of file nsDOMStorage.cpp.

Here is the call graph for this function:

Definition at line 283 of file nsDOMStorage.cpp.

Here is the call graph for this function:


Member Function Documentation

Definition at line 821 of file nsDOMStorage.cpp.

{
  nsresult rv;
  nsCOMPtr<nsIObserverService> observerService =
    do_GetService("@mozilla.org/observer-service;1", &rv);
  if (NS_FAILED(rv)) {
    return;
  }

  // Fire off a notification that a storage object changed. If the
  // storage object is a session storage object, we don't pass a
  // domain, but if it's a global storage object we do.
  observerService->NotifyObservers((nsIDOMStorage *)this,
                                   "dom-storage-changed",
                                   UseDB() ? mDomain.get() : nsnull);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 621 of file nsDOMStorage.cpp.

{
#ifdef MOZ_STORAGE
  // cache all the keys in the hash. This is used by the Length and Key methods
  // use this cache for better performance. The disadvantage is that the
  // order may break if someone changes the keys in the database directly.
  if (!mItemsCached) {
    nsresult rv = InitDB();
    NS_ENSURE_SUCCESS(rv, rv);

    rv = gStorageDB->GetAllKeys(mDomain, this, &mItems);
    NS_ENSURE_SUCCESS(rv, rv);

    mItemsCached = PR_TRUE;
  }
#endif

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 135 of file nsDOMStorage.h.

Here is the call graph for this function:

PRBool nsDOMStorage::CanUseStorage ( nsIURI aURI,
PRPackedBool aSessionOnly 
) [static]

Definition at line 303 of file nsDOMStorage.cpp.

{
  // check if the domain can use storage. Downgrade to session only if only
  // session storage may be used.
  NS_ASSERTION(aURI && aSessionOnly, "null URI or session flag");

  if (!nsContentUtils::GetBoolPref(kStorageEnabled))
    return PR_FALSE;

  nsCOMPtr<nsIPermissionManager> permissionManager =
    do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
  if (!permissionManager)
    return PR_FALSE;

  *aSessionOnly = PR_FALSE;

  PRUint32 perm;
  permissionManager->TestPermission(aURI, kPermissionType, &perm);

  if (perm == nsIPermissionManager::DENY_ACTION)
    return PR_FALSE;

  if (perm == nsICookiePermission::ACCESS_SESSION) {
    *aSessionOnly = PR_TRUE;
  }
  else if (perm != nsIPermissionManager::ALLOW_ACTION) {
    PRUint32 cookieBehavior = nsContentUtils::GetIntPref(kCookiesBehavior);
    PRUint32 lifetimePolicy = nsContentUtils::GetIntPref(kCookiesLifetimePolicy);

    // treat ask as reject always
    if (cookieBehavior == BEHAVIOR_REJECT || lifetimePolicy == ASK_BEFORE_ACCEPT)
      return PR_FALSE;

    if (lifetimePolicy == ACCEPT_SESSION)
      *aSessionOnly = PR_TRUE;
  }

  return PR_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 749 of file nsDOMStorage.cpp.

Here is the call graph for this function:

Implements nsPIDOMStorage.

Definition at line 769 of file nsDOMStorage.cpp.

{
  if (UseDB()) {
    NS_ERROR("Uh, don't clone a global storage object.");

    return nsnull;
  }

  nsDOMStorage* storage = new nsDOMStorage(aURI, mDomain, PR_FALSE);
  if (!storage)
    return nsnull;

  mItems.EnumerateEntries(CopyStorageItems, storage);

  NS_ADDREF(storage);

  return storage;
}

Here is the call graph for this function:

nsresult nsDOMStorage::GetDBValue ( const nsAString &  aKey,
nsAString &  aValue,
PRBool aSecure,
nsAString &  aOwner 
)

Definition at line 642 of file nsDOMStorage.cpp.

{
  aValue.Truncate();

#ifdef MOZ_STORAGE
  if (!UseDB())
    return NS_OK;

  nsresult rv = InitDB();
  NS_ENSURE_SUCCESS(rv, rv);

  nsAutoString value;
  rv = gStorageDB->GetKeyValue(mDomain, aKey, value, aSecure, aOwner);
  if (NS_FAILED(rv))
    return rv;

  if (!IsCallerSecure() && *aSecure) {
    return NS_ERROR_DOM_SECURITY_ERR;
  }

  aValue.Assign(value);
#endif

  return NS_OK;
}

Here is the call graph for this function:

nsIDOMStorageItem nsIDOMStorage::getItem ( in DOMString  key) [inherited]

Retrieve an item with a given key.

Parameters:
keykey to retrieve
Returns:
found item or null if the key was not found

Implements nsPIDOMStorage.

Definition at line 806 of file nsDOMStorage.cpp.

{
  if (UseDB())
    CacheKeysFromDB();

  KeysArrayBuilderStruct keystruct;
  keystruct.callerIsSecure = IsCallerSecure();
  keystruct.keys = new nsTArray<nsString>();
  if (keystruct.keys)
    mItems.EnumerateEntries(KeysArrayBuilder, &keystruct);
 
  return keystruct.keys;
}

Here is the call graph for this function:

void nsDOMStorage::Init ( nsIURI aURI,
const nsAString &  aDomain,
PRBool  aUseDB 
) [virtual]

Implements nsPIDOMStorage.

Definition at line 290 of file nsDOMStorage.cpp.

{
  mURI = aURI;
  mDomain.Assign(aDomain);
#ifdef MOZ_STORAGE
  mUseDB = aUseDB;
#else
  mUseDB = PR_FALSE;
#endif
}
nsresult nsDOMStorage::InitDB ( ) [static, protected]

Definition at line 595 of file nsDOMStorage.cpp.

{
#ifdef MOZ_STORAGE
  if (!gStorageDB) {
    gStorageDB = new nsDOMStorageDB();
    if (!gStorageDB)
      return NS_ERROR_OUT_OF_MEMORY;

    nsresult rv = gStorageDB->Init();
    if (NS_FAILED(rv)) {
      // Failed to initialize the DB, delete it and null out the
      // pointer so we don't end up attempting to use an
      // un-initialized DB later on.

      delete gStorageDB;
      gStorageDB = nsnull;

      return rv;
    }
  }
#endif

  return NS_OK;
}

Here is the caller graph for this function:

DOMString nsIDOMStorage::key ( in unsigned long  index) [inherited]

Retrieve the name of the key at a particular index.

Parameters:
indexindex of the item to retrieve
Returns:
the key at index
Exceptions:
INDEX_SIZE_ERRif there is no key at that index
void nsIDOMStorage::removeItem ( in DOMString  key) [inherited]

Remove a key and its corresponding value.

Parameters:
keykey to remove
nsresult nsDOMStorage::SetDBValue ( const nsAString &  aKey,
const nsAString &  aValue,
PRBool  aSecure 
)

Definition at line 670 of file nsDOMStorage.cpp.

{
#ifdef MOZ_STORAGE
  if (!UseDB())
    return NS_OK;

  nsresult rv = InitDB();
  NS_ENSURE_SUCCESS(rv, rv);

  // Get the current domain for quota enforcement
  nsCOMPtr<nsIPrincipal> subjectPrincipal;
  nsContentUtils::GetSecurityManager()->
    GetSubjectPrincipal(getter_AddRefs(subjectPrincipal));

  nsAutoString currentDomain;

  if (subjectPrincipal) {
    nsCOMPtr<nsIURI> uri;
    rv = subjectPrincipal->GetURI(getter_AddRefs(uri));

    if (NS_SUCCEEDED(rv) && uri) {
      nsCAutoString currentDomainAscii;
      uri->GetAsciiHost(currentDomainAscii);
      currentDomain = NS_ConvertUTF8toUTF16(currentDomainAscii);
    }
    
    if (currentDomain.IsEmpty()) {
      return NS_ERROR_DOM_SECURITY_ERR;
    }
  } else {
    currentDomain = mDomain;
  }
  
  rv = gStorageDB->SetKey(mDomain, aKey, aValue, aSecure,
                          currentDomain, GetQuota(currentDomain));
  NS_ENSURE_SUCCESS(rv, rv);

  mItemsCached = PR_FALSE;

  BroadcastChangeNotification();
#endif

  return NS_OK;
}

Here is the call graph for this function:

void nsIDOMStorage::setItem ( in DOMString  key,
in DOMString  data 
) [inherited]

Assign a value with a key.

If the key does not exist already, a new key is added associated with that value. If the key already exists, then the existing value is replaced with a new value.

Parameters:
keykey to set
datadata to associate with the key
Returns:
found item or null if the key was not found
nsresult nsDOMStorage::SetSecure ( const nsAString &  aKey,
PRBool  aSecure 
)

Definition at line 718 of file nsDOMStorage.cpp.

{
#ifdef MOZ_STORAGE
  if (UseDB()) {
    nsresult rv = InitDB();
    NS_ENSURE_SUCCESS(rv, rv);

    return gStorageDB->SetSecure(mDomain, aKey, aSecure);
  }
#else
  return NS_ERROR_NOT_IMPLEMENTED;
#endif

  nsSessionStorageEntry *entry = mItems.GetEntry(aKey);
  NS_ASSERTION(entry, "Don't use SetSecure() with non-existing keys!");

  if (entry) {
    entry->mItem->SetSecureInternal(aSecure);
  }  

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 126 of file nsDOMStorage.h.

{ return mUseDB && !mSessionOnly; }

Here is the caller graph for this function:


Friends And Related Function Documentation

friend class nsDOMStorageManager [friend]

Definition at line 164 of file nsDOMStorage.h.


Member Data Documentation

readonly attribute unsigned long nsIDOMStorage::length [inherited]

The number of keys stored.

Definition at line 62 of file nsIDOMStorage.idl.

Definition at line 186 of file nsDOMStorage.h.

Definition at line 189 of file nsDOMStorage.h.

Definition at line 180 of file nsDOMStorage.h.

Definition at line 177 of file nsDOMStorage.h.

Definition at line 183 of file nsDOMStorage.h.

Definition at line 174 of file nsDOMStorage.h.


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