michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsCache.h" michael@0: #include "nsDiskCache.h" michael@0: #include "nsDiskCacheEntry.h" michael@0: #include "nsDiskCacheBinding.h" michael@0: #include "nsCRT.h" michael@0: michael@0: #include "nsISerializable.h" michael@0: #include "nsSerializationHelper.h" michael@0: michael@0: /****************************************************************************** michael@0: * nsDiskCacheEntry michael@0: *****************************************************************************/ michael@0: michael@0: /** michael@0: * CreateCacheEntry() michael@0: * michael@0: * Creates an nsCacheEntry and sets all fields except for the binding. michael@0: */ michael@0: nsCacheEntry * michael@0: nsDiskCacheEntry::CreateCacheEntry(nsCacheDevice * device) michael@0: { michael@0: nsCacheEntry * entry = nullptr; michael@0: nsresult rv = nsCacheEntry::Create(Key(), michael@0: nsICache::STREAM_BASED, michael@0: nsICache::STORE_ON_DISK, michael@0: device, michael@0: &entry); michael@0: if (NS_FAILED(rv) || !entry) return nullptr; michael@0: michael@0: entry->SetFetchCount(mFetchCount); michael@0: entry->SetLastFetched(mLastFetched); michael@0: entry->SetLastModified(mLastModified); michael@0: entry->SetExpirationTime(mExpirationTime); michael@0: entry->SetCacheDevice(device); michael@0: // XXX why does nsCacheService have to fill out device in BindEntry()? michael@0: entry->SetDataSize(mDataSize); michael@0: michael@0: rv = entry->UnflattenMetaData(MetaData(), mMetaDataSize); michael@0: if (NS_FAILED(rv)) { michael@0: delete entry; michael@0: return nullptr; michael@0: } michael@0: michael@0: // Restore security info, if present michael@0: const char* info = entry->GetMetaDataElement("security-info"); michael@0: if (info) { michael@0: nsCOMPtr infoObj; michael@0: rv = NS_DeserializeObject(nsDependentCString(info), michael@0: getter_AddRefs(infoObj)); michael@0: if (NS_FAILED(rv)) { michael@0: delete entry; michael@0: return nullptr; michael@0: } michael@0: entry->SetSecurityInfo(infoObj); michael@0: } michael@0: michael@0: return entry; michael@0: } michael@0: michael@0: michael@0: /****************************************************************************** michael@0: * nsDiskCacheEntryInfo michael@0: *****************************************************************************/ michael@0: michael@0: NS_IMPL_ISUPPORTS(nsDiskCacheEntryInfo, nsICacheEntryInfo) michael@0: michael@0: NS_IMETHODIMP nsDiskCacheEntryInfo::GetClientID(char ** clientID) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(clientID); michael@0: return ClientIDFromCacheKey(nsDependentCString(mDiskEntry->Key()), clientID); michael@0: } michael@0: michael@0: extern const char DISK_CACHE_DEVICE_ID[]; michael@0: NS_IMETHODIMP nsDiskCacheEntryInfo::GetDeviceID(char ** deviceID) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(deviceID); michael@0: *deviceID = NS_strdup(mDeviceID); michael@0: return *deviceID ? NS_OK : NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP nsDiskCacheEntryInfo::GetKey(nsACString &clientKey) michael@0: { michael@0: return ClientKeyFromCacheKey(nsDependentCString(mDiskEntry->Key()), clientKey); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsDiskCacheEntryInfo::GetFetchCount(int32_t *aFetchCount) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aFetchCount); michael@0: *aFetchCount = mDiskEntry->mFetchCount; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsDiskCacheEntryInfo::GetLastFetched(uint32_t *aLastFetched) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aLastFetched); michael@0: *aLastFetched = mDiskEntry->mLastFetched; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsDiskCacheEntryInfo::GetLastModified(uint32_t *aLastModified) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aLastModified); michael@0: *aLastModified = mDiskEntry->mLastModified; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsDiskCacheEntryInfo::GetExpirationTime(uint32_t *aExpirationTime) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aExpirationTime); michael@0: *aExpirationTime = mDiskEntry->mExpirationTime; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsDiskCacheEntryInfo::IsStreamBased(bool *aStreamBased) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aStreamBased); michael@0: *aStreamBased = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsDiskCacheEntryInfo::GetDataSize(uint32_t *aDataSize) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aDataSize); michael@0: *aDataSize = mDiskEntry->mDataSize; michael@0: return NS_OK; michael@0: }