2001-02-23 16:18:01 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04: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/. */
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2001-03-01 05:25:07 +03:00
|
|
|
#include "nsICache.h"
|
2001-03-21 01:42:03 +03:00
|
|
|
#include "nsCache.h"
|
2001-02-26 17:45:45 +03:00
|
|
|
#include "nsCacheService.h"
|
2001-02-23 16:18:01 +03:00
|
|
|
#include "nsCacheEntryDescriptor.h"
|
|
|
|
#include "nsCacheEntry.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2001-03-01 11:29:43 +03:00
|
|
|
#include "nsIOutputStream.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
2012-09-18 01:31:46 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2011-12-17 14:30:29 +04:00
|
|
|
#define kMinDecompressReadBufLen 1024
|
|
|
|
#define kMinCompressWriteBufLen 1024
|
|
|
|
|
2012-09-18 01:31:46 +04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsAsyncDoomEvent
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
class nsAsyncDoomEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
nsAsyncDoomEvent(nsCacheEntryDescriptor *descriptor,
|
|
|
|
nsICacheListener *listener)
|
|
|
|
{
|
|
|
|
mDescriptor = descriptor;
|
|
|
|
mListener = listener;
|
|
|
|
mThread = do_GetCurrentThread();
|
|
|
|
// We addref the listener here and release it in nsNotifyDoomListener
|
|
|
|
// on the callers thread. If posting of nsNotifyDoomListener event fails
|
|
|
|
// we leak the listener which is better than releasing it on a wrong
|
|
|
|
// thread.
|
|
|
|
NS_IF_ADDREF(mListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
nsresult status = NS_OK;
|
|
|
|
|
|
|
|
{
|
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSASYNCDOOMEVENT_RUN));
|
|
|
|
|
|
|
|
if (mDescriptor->mCacheEntry) {
|
|
|
|
status = nsCacheService::gService->DoomEntry_Internal(
|
|
|
|
mDescriptor->mCacheEntry, true);
|
|
|
|
} else if (!mDescriptor->mDoomedOnClose) {
|
|
|
|
status = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mListener) {
|
|
|
|
mThread->Dispatch(new nsNotifyDoomListener(mListener, status),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
// posted event will release the reference on the correct thread
|
|
|
|
mListener = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-03-18 04:23:03 +04:00
|
|
|
nsRefPtr<nsCacheEntryDescriptor> mDescriptor;
|
2012-09-18 01:31:46 +04:00
|
|
|
nsICacheListener *mListener;
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsCacheEntryDescriptor,
|
|
|
|
nsICacheEntryDescriptor,
|
|
|
|
nsICacheEntryInfo)
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
nsCacheEntryDescriptor::nsCacheEntryDescriptor(nsCacheEntry * entry,
|
2001-02-24 04:36:11 +03:00
|
|
|
nsCacheAccessMode accessGranted)
|
2001-03-07 12:27:36 +03:00
|
|
|
: mCacheEntry(entry),
|
2010-12-29 04:25:14 +03:00
|
|
|
mAccessGranted(accessGranted),
|
2012-12-14 21:03:01 +04:00
|
|
|
mOutputWrapper(nullptr),
|
2012-09-18 01:31:46 +04:00
|
|
|
mLock("nsCacheEntryDescriptor.mLock"),
|
|
|
|
mAsyncDoomPending(false),
|
2012-12-14 21:03:01 +04:00
|
|
|
mDoomedOnClose(false),
|
|
|
|
mClosingDescriptor(false)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2004-11-01 00:58:51 +03:00
|
|
|
PR_INIT_CLIST(this);
|
|
|
|
NS_ADDREF(nsCacheService::GlobalInstance()); // ensure it lives for the lifetime of the descriptor
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
nsCacheEntryDescriptor::~nsCacheEntryDescriptor()
|
|
|
|
{
|
2005-03-18 11:46:54 +03:00
|
|
|
// No need to close if the cache entry has already been severed. This
|
|
|
|
// helps avoid a shutdown assertion (bug 285519) that is caused when
|
|
|
|
// consumers end up holding onto these objects past xpcom-shutdown. It's
|
|
|
|
// okay for them to do that because the cache service calls our Close
|
|
|
|
// method during xpcom-shutdown, so we don't need to complain about it.
|
|
|
|
if (mCacheEntry)
|
|
|
|
Close();
|
|
|
|
|
2015-01-19 20:50:16 +03:00
|
|
|
NS_ASSERTION(mInputWrappers.IsEmpty(),
|
2012-12-14 21:03:01 +04:00
|
|
|
"We have still some input wrapper!");
|
|
|
|
NS_ASSERTION(!mOutputWrapper, "We have still an output wrapper!");
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
nsCacheService * service = nsCacheService::GlobalInstance();
|
|
|
|
NS_RELEASE(service);
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-14 04:11:14 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCacheEntryDescriptor::GetClientID(char ** result)
|
|
|
|
{
|
2001-03-21 01:42:03 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2006-06-28 03:13:48 +04:00
|
|
|
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETCLIENTID));
|
2001-03-21 01:42:03 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
return ClientIDFromCacheKey(*(mCacheEntry->Key()), result);
|
2001-03-14 04:11:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-30 05:18:34 +04:00
|
|
|
NS_IMETHODIMP
|
2010-02-21 17:53:13 +03:00
|
|
|
nsCacheEntryDescriptor::GetDeviceID(char ** aDeviceID)
|
2001-04-30 05:18:34 +04:00
|
|
|
{
|
2010-02-21 17:53:13 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aDeviceID);
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETDEVICEID));
|
2001-04-30 05:18:34 +04:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2010-02-21 17:53:13 +03:00
|
|
|
const char* deviceID = mCacheEntry->GetDeviceID();
|
|
|
|
if (!deviceID) {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aDeviceID = nullptr;
|
2010-02-21 17:53:13 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aDeviceID = NS_strdup(deviceID);
|
|
|
|
return *aDeviceID ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
2001-04-30 05:18:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
NS_IMETHODIMP
|
2005-01-13 06:25:28 +03:00
|
|
|
nsCacheEntryDescriptor::GetKey(nsACString &result)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETKEY));
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2001-03-21 01:42:03 +03:00
|
|
|
return ClientKeyFromCacheKey(*(mCacheEntry->Key()), result);
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheEntryDescriptor::GetFetchCount(int32_t *result)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2001-03-01 05:25:07 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETFETCHCOUNT));
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2001-03-01 05:25:07 +03:00
|
|
|
*result = mCacheEntry->FetchCount();
|
2001-02-23 16:18:01 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheEntryDescriptor::GetLastFetched(uint32_t *result)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2001-03-01 05:25:07 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETLASTFETCHED));
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2001-03-14 06:54:05 +03:00
|
|
|
*result = mCacheEntry->LastFetched();
|
2001-02-23 16:18:01 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-03-08 08:37:00 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheEntryDescriptor::GetLastModified(uint32_t *result)
|
2001-03-08 08:37:00 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETLASTMODIFIED));
|
2001-03-08 08:37:00 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2001-03-14 06:54:05 +03:00
|
|
|
*result = mCacheEntry->LastModified();
|
2001-03-08 08:37:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheEntryDescriptor::GetExpirationTime(uint32_t *result)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2001-03-01 05:25:07 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETEXPIRATIONTIME));
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2001-03-14 06:54:05 +03:00
|
|
|
*result = mCacheEntry->ExpirationTime();
|
2001-02-23 16:18:01 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheEntryDescriptor::SetExpirationTime(uint32_t expirationTime)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_SETEXPIRATIONTIME));
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2001-03-14 06:54:05 +03:00
|
|
|
mCacheEntry->SetExpirationTime(expirationTime);
|
2001-03-29 09:54:58 +04:00
|
|
|
mCacheEntry->MarkEntryDirty();
|
2001-02-23 16:18:01 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsCacheEntryDescriptor::IsStreamBased(bool *result)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2001-03-01 05:25:07 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_ISSTREAMBASED));
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
*result = mCacheEntry->IsStreamData();
|
2001-02-23 16:18:01 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsCacheEntryDescriptor::GetPredictedDataSize(int64_t *result)
|
2010-09-08 02:39:28 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETPREDICTEDDATASIZE));
|
2010-09-08 02:39:28 +04:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
*result = mCacheEntry->PredictedDataSize();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsCacheEntryDescriptor::SetPredictedDataSize(int64_t
|
2010-09-08 02:39:28 +04:00
|
|
|
predictedSize)
|
|
|
|
{
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_SETPREDICTEDDATASIZE));
|
2010-09-08 02:39:28 +04:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
mCacheEntry->SetPredictedDataSize(predictedSize);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-02-26 17:45:45 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsCacheEntryDescriptor::GetDataSize(uint32_t *result)
|
2001-02-26 17:45:45 +03:00
|
|
|
{
|
2001-03-01 05:25:07 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETDATASIZE));
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2011-12-17 14:30:29 +04:00
|
|
|
const char* val = mCacheEntry->GetMetaDataElement("uncompressed-len");
|
|
|
|
if (!val) {
|
|
|
|
*result = mCacheEntry->DataSize();
|
|
|
|
} else {
|
|
|
|
*result = atol(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsCacheEntryDescriptor::GetStorageDataSize(uint32_t *result)
|
2011-12-17 14:30:29 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETSTORAGEDATASIZE));
|
2011-12-17 14:30:29 +04:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2001-03-01 05:25:07 +03:00
|
|
|
*result = mCacheEntry->DataSize();
|
2011-12-17 14:30:29 +04:00
|
|
|
|
2001-02-28 03:06:13 +03:00
|
|
|
return NS_OK;
|
2001-02-26 17:45:45 +03:00
|
|
|
}
|
2001-02-28 07:10:43 +03:00
|
|
|
|
|
|
|
|
2001-03-01 22:52:06 +03:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheEntryDescriptor::RequestDataSizeChange(int32_t deltaSize)
|
2001-03-01 22:52:06 +03:00
|
|
|
{
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_REQUESTDATASIZECHANGE));
|
2001-09-20 05:09:01 +04:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2001-03-01 22:52:06 +03:00
|
|
|
nsresult rv;
|
2002-08-07 05:13:29 +04:00
|
|
|
rv = nsCacheService::OnDataSizeChange(mCacheEntry, deltaSize);
|
2001-03-01 22:52:06 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2001-03-04 03:11:30 +03:00
|
|
|
// XXX review for signed/unsigned math errors
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t newDataSize = mCacheEntry->DataSize() + deltaSize;
|
2001-03-01 22:52:06 +03:00
|
|
|
mCacheEntry->SetDataSize(newDataSize);
|
2001-03-29 09:54:58 +04:00
|
|
|
mCacheEntry->TouchData();
|
2001-03-01 22:52:06 +03:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheEntryDescriptor::SetDataSize(uint32_t dataSize)
|
2001-02-26 17:45:45 +03:00
|
|
|
{
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_SETDATASIZE));
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2001-03-04 03:11:30 +03:00
|
|
|
// XXX review for signed/unsigned math errors
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t deltaSize = dataSize - mCacheEntry->DataSize();
|
2001-03-01 08:01:43 +03:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
nsresult rv;
|
|
|
|
rv = nsCacheService::OnDataSizeChange(mCacheEntry, deltaSize);
|
|
|
|
// this had better be NS_OK, this call instance is advisory for memory cache objects
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// XXX review for signed/unsigned math errors
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t newDataSize = mCacheEntry->DataSize() + deltaSize;
|
2002-08-07 05:13:29 +04:00
|
|
|
mCacheEntry->SetDataSize(newDataSize);
|
|
|
|
mCacheEntry->TouchData();
|
|
|
|
} else {
|
|
|
|
NS_WARNING("failed SetDataSize() on memory cache object!");
|
|
|
|
}
|
|
|
|
|
2001-03-01 22:52:06 +03:00
|
|
|
return rv;
|
2001-02-26 17:45:45 +03:00
|
|
|
}
|
|
|
|
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-03-01 22:52:06 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheEntryDescriptor::OpenInputStream(uint32_t offset, nsIInputStream ** result)
|
2001-02-26 17:45:45 +03:00
|
|
|
{
|
2001-03-01 05:25:07 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2001-02-28 03:06:13 +03:00
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
nsInputStreamWrapper* cacheInput = nullptr;
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_OPENINPUTSTREAM));
|
2003-01-18 05:15:14 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
if (!mCacheEntry->IsStreamData()) return NS_ERROR_CACHE_DATA_IS_NOT_STREAM;
|
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
// Don't open any new stream when closing descriptor or clearing entries
|
|
|
|
if (mClosingDescriptor || nsCacheService::GetClearingEntries())
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
// ensure valid permissions
|
|
|
|
if (!(mAccessGranted & nsICache::ACCESS_READ))
|
|
|
|
return NS_ERROR_CACHE_READ_ACCESS_DENIED;
|
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
const char *val;
|
|
|
|
val = mCacheEntry->GetMetaDataElement("uncompressed-len");
|
|
|
|
if (val) {
|
|
|
|
cacheInput = new nsDecompressInputStreamWrapper(this, offset);
|
|
|
|
} else {
|
|
|
|
cacheInput = new nsInputStreamWrapper(this, offset);
|
|
|
|
}
|
|
|
|
if (!cacheInput) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
mInputWrappers.AppendElement(cacheInput);
|
2011-12-17 14:30:29 +04:00
|
|
|
}
|
2003-01-18 05:15:14 +03:00
|
|
|
|
|
|
|
NS_ADDREF(*result = cacheInput);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheEntryDescriptor::OpenOutputStream(uint32_t offset, nsIOutputStream ** result)
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
nsOutputStreamWrapper* cacheOutput = nullptr;
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_OPENOUTPUTSTREAM));
|
2003-01-18 05:15:14 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
if (!mCacheEntry->IsStreamData()) return NS_ERROR_CACHE_DATA_IS_NOT_STREAM;
|
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
// Don't open any new stream when closing descriptor or clearing entries
|
|
|
|
if (mClosingDescriptor || nsCacheService::GetClearingEntries())
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
// ensure valid permissions
|
|
|
|
if (!(mAccessGranted & nsICache::ACCESS_WRITE))
|
|
|
|
return NS_ERROR_CACHE_WRITE_ACCESS_DENIED;
|
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
int32_t compressionLevel = nsCacheService::CacheCompressionLevel();
|
|
|
|
const char *val;
|
|
|
|
val = mCacheEntry->GetMetaDataElement("uncompressed-len");
|
|
|
|
if ((compressionLevel > 0) && val) {
|
|
|
|
cacheOutput = new nsCompressOutputStreamWrapper(this, offset);
|
|
|
|
} else {
|
|
|
|
// clear compression flag when compression disabled - see bug 715198
|
|
|
|
if (val) {
|
|
|
|
mCacheEntry->SetMetaDataElement("uncompressed-len", nullptr);
|
|
|
|
}
|
|
|
|
cacheOutput = new nsOutputStreamWrapper(this, offset);
|
2012-01-17 15:51:17 +04:00
|
|
|
}
|
2012-12-14 21:03:01 +04:00
|
|
|
if (!cacheOutput) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
mOutputWrapper = cacheOutput;
|
2011-12-17 14:30:29 +04:00
|
|
|
}
|
2003-01-18 05:15:14 +03:00
|
|
|
|
|
|
|
NS_ADDREF(*result = cacheOutput);
|
2001-03-01 05:25:07 +03:00
|
|
|
return NS_OK;
|
2001-02-26 17:45:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
NS_IMETHODIMP
|
2001-03-01 05:25:07 +03:00
|
|
|
nsCacheEntryDescriptor::GetCacheElement(nsISupports ** result)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2001-03-01 05:25:07 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETCACHEELEMENT));
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
if (mCacheEntry->IsStreamData()) return NS_ERROR_CACHE_DATA_IS_STREAM;
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_IF_ADDREF(*result = mCacheEntry->Data());
|
|
|
|
return NS_OK;
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCacheEntryDescriptor::SetCacheElement(nsISupports * cacheElement)
|
|
|
|
{
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_SETCACHEELEMENT));
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
if (mCacheEntry->IsStreamData()) return NS_ERROR_CACHE_DATA_IS_STREAM;
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
return nsCacheService::SetCacheElement(mCacheEntry, cacheElement);
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
NS_IMETHODIMP
|
2001-03-01 05:25:07 +03:00
|
|
|
nsCacheEntryDescriptor::GetAccessGranted(nsCacheAccessMode *result)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2001-03-01 05:25:07 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
|
|
|
*result = mAccessGranted;
|
2001-02-23 16:18:01 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
NS_IMETHODIMP
|
2001-03-01 05:25:07 +03:00
|
|
|
nsCacheEntryDescriptor::GetStoragePolicy(nsCacheStoragePolicy *result)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2001-03-01 05:25:07 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETSTORAGEPOLICY));
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2007-12-08 01:07:03 +03:00
|
|
|
*result = mCacheEntry->StoragePolicy();
|
|
|
|
return NS_OK;
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
NS_IMETHODIMP
|
2001-02-24 04:02:37 +03:00
|
|
|
nsCacheEntryDescriptor::SetStoragePolicy(nsCacheStoragePolicy policy)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_SETSTORAGEPOLICY));
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
2001-03-21 01:42:03 +03:00
|
|
|
// XXX validate policy against session?
|
2002-02-19 08:29:49 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool storageEnabled = false;
|
2002-08-07 05:13:29 +04:00
|
|
|
storageEnabled = nsCacheService::IsStorageEnabledForPolicy_Locked(policy);
|
2002-02-19 08:29:49 +03:00
|
|
|
if (!storageEnabled) return NS_ERROR_FAILURE;
|
2007-12-08 01:07:03 +03:00
|
|
|
|
|
|
|
// Don't change the storage policy of entries we can't write
|
2007-12-08 01:18:13 +03:00
|
|
|
if (!(mAccessGranted & nsICache::ACCESS_WRITE))
|
2007-12-08 01:07:03 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2002-02-19 08:29:49 +03:00
|
|
|
|
2007-12-08 01:07:03 +03:00
|
|
|
// Don't allow a cache entry to move from memory-only to anything else
|
|
|
|
if (mCacheEntry->StoragePolicy() == nsICache::STORE_IN_MEMORY &&
|
|
|
|
policy != nsICache::STORE_IN_MEMORY)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2001-03-21 01:42:03 +03:00
|
|
|
mCacheEntry->SetStoragePolicy(policy);
|
2001-03-29 09:54:58 +04:00
|
|
|
mCacheEntry->MarkEntryDirty();
|
2001-03-21 01:42:03 +03:00
|
|
|
return NS_OK;
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2001-03-07 12:27:36 +03:00
|
|
|
|
2001-03-07 04:40:41 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCacheEntryDescriptor::GetFile(nsIFile ** result)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETFILE));
|
2001-03-07 04:40:41 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
2002-08-07 05:13:29 +04:00
|
|
|
|
|
|
|
return nsCacheService::GetFileForEntry(mCacheEntry, result);
|
2001-03-07 04:40:41 +03:00
|
|
|
}
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-03-07 12:27:36 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCacheEntryDescriptor::GetSecurityInfo(nsISupports ** result)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETSECURITYINFO));
|
2001-03-07 12:27:36 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2007-11-30 21:05:54 +03:00
|
|
|
*result = mCacheEntry->SecurityInfo();
|
|
|
|
NS_IF_ADDREF(*result);
|
|
|
|
return NS_OK;
|
2001-03-07 12:27:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCacheEntryDescriptor::SetSecurityInfo(nsISupports * securityInfo)
|
|
|
|
{
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_SETSECURITYINFO));
|
2001-03-07 12:27:36 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
mCacheEntry->SetSecurityInfo(securityInfo);
|
2001-03-29 09:54:58 +04:00
|
|
|
mCacheEntry->MarkEntryDirty();
|
2001-03-07 12:27:36 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCacheEntryDescriptor::Doom()
|
|
|
|
{
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_DOOM));
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
return nsCacheService::DoomEntry(mCacheEntry);
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCacheEntryDescriptor::DoomAndFailPendingRequests(nsresult status)
|
|
|
|
{
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_DOOMANDFAILPENDINGREQUESTS));
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-18 01:31:46 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCacheEntryDescriptor::AsyncDoom(nsICacheListener *listener)
|
|
|
|
{
|
|
|
|
bool asyncDoomPending;
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
asyncDoomPending = mAsyncDoomPending;
|
|
|
|
mAsyncDoomPending = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asyncDoomPending) {
|
|
|
|
// AsyncDoom was already called. Notify listener if it is non-null,
|
|
|
|
// otherwise just return success.
|
|
|
|
if (listener) {
|
|
|
|
nsresult rv = NS_DispatchToCurrentThread(
|
|
|
|
new nsNotifyDoomListener(listener, NS_ERROR_NOT_AVAILABLE));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_IF_ADDREF(listener);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsIRunnable> event = new nsAsyncDoomEvent(this, listener);
|
|
|
|
return nsCacheService::DispatchToCacheIOThread(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCacheEntryDescriptor::MarkValid()
|
|
|
|
{
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_MARKVALID));
|
2001-03-06 07:01:00 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
nsresult rv = nsCacheService::ValidateEntry(mCacheEntry);
|
2001-03-06 07:01:00 +03:00
|
|
|
return rv;
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-02-27 08:35:53 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCacheEntryDescriptor::Close()
|
|
|
|
{
|
2012-12-14 21:03:01 +04:00
|
|
|
nsRefPtr<nsOutputStreamWrapper> outputWrapper;
|
|
|
|
nsTArray<nsRefPtr<nsInputStreamWrapper> > inputWrappers;
|
|
|
|
|
|
|
|
{
|
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_CLOSE));
|
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
// Make sure no other stream can be opened
|
|
|
|
mClosingDescriptor = true;
|
|
|
|
outputWrapper = mOutputWrapper;
|
2015-01-19 20:50:16 +03:00
|
|
|
for (size_t i = 0; i < mInputWrappers.Length(); i++)
|
|
|
|
inputWrappers.AppendElement(mInputWrappers[i]);
|
2012-12-14 21:03:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call Close() on the streams outside the lock since it might need to call
|
|
|
|
// methods that grab the cache service lock, e.g. compressed output stream
|
|
|
|
// when it finalizes the entry
|
|
|
|
if (outputWrapper) {
|
|
|
|
if (NS_FAILED(outputWrapper->Close())) {
|
|
|
|
NS_WARNING("Dooming entry because Close() failed!!!");
|
|
|
|
Doom();
|
|
|
|
}
|
|
|
|
outputWrapper = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0 ; i < inputWrappers.Length() ; i++)
|
|
|
|
inputWrappers[i]->Close();
|
|
|
|
|
|
|
|
inputWrappers.Clear();
|
|
|
|
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_CLOSE));
|
2013-01-03 19:39:50 +04:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
2001-02-28 03:06:13 +03:00
|
|
|
|
2005-03-18 11:46:54 +03:00
|
|
|
// XXX perhaps closing descriptors should clear/sever transports
|
2002-08-07 05:13:29 +04:00
|
|
|
|
2001-02-28 03:06:13 +03:00
|
|
|
// tell nsCacheService we're going away
|
2002-08-07 05:13:29 +04:00
|
|
|
nsCacheService::CloseDescriptor(this);
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ASSERTION(mCacheEntry == nullptr, "mCacheEntry not null");
|
2001-02-28 03:06:13 +03:00
|
|
|
|
2001-02-27 08:35:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
NS_IMETHODIMP
|
2006-03-09 23:02:54 +03:00
|
|
|
nsCacheEntryDescriptor::GetMetaDataElement(const char *key, char **result)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2006-03-09 23:02:54 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(key);
|
2012-07-30 18:20:58 +04:00
|
|
|
*result = nullptr;
|
2003-03-25 18:20:10 +03:00
|
|
|
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_GETMETADATAELEMENT));
|
2006-03-09 23:02:54 +03:00
|
|
|
NS_ENSURE_TRUE(mCacheEntry, NS_ERROR_NOT_AVAILABLE);
|
2001-02-28 03:06:13 +03:00
|
|
|
|
2003-03-25 18:20:10 +03:00
|
|
|
const char *value;
|
2001-03-21 01:42:03 +03:00
|
|
|
|
2003-03-25 18:20:10 +03:00
|
|
|
value = mCacheEntry->GetMetaDataElement(key);
|
2001-03-21 01:42:03 +03:00
|
|
|
if (!value) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
*result = NS_strdup(value);
|
2001-03-21 01:42:03 +03:00
|
|
|
if (!*result) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return NS_OK;
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-03-02 04:51:41 +03:00
|
|
|
nsCacheEntryDescriptor::SetMetaDataElement(const char *key, const char *value)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2006-03-09 23:02:54 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(key);
|
2001-02-28 03:06:13 +03:00
|
|
|
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_SETMETADATAELEMENT));
|
2006-03-09 23:02:54 +03:00
|
|
|
NS_ENSURE_TRUE(mCacheEntry, NS_ERROR_NOT_AVAILABLE);
|
2003-03-25 18:20:10 +03:00
|
|
|
|
2001-03-04 03:11:30 +03:00
|
|
|
// XXX allow null value, for clearing key?
|
2003-03-25 18:20:10 +03:00
|
|
|
|
|
|
|
nsresult rv = mCacheEntry->SetMetaDataElement(key, value);
|
2001-03-29 09:54:58 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
mCacheEntry->TouchMetaData();
|
2001-02-23 16:18:01 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-04-04 07:30:45 +04:00
|
|
|
nsCacheEntryDescriptor::VisitMetaData(nsICacheMetaDataVisitor * visitor)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2012-06-30 03:20:34 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHEENTRYDESCRIPTOR_VISITMETADATA));
|
|
|
|
// XXX check callers, we're calling out of module
|
2001-04-04 07:30:45 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(visitor);
|
2001-02-28 03:06:13 +03:00
|
|
|
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2001-04-04 07:30:45 +04:00
|
|
|
return mCacheEntry->VisitMetaDataElements(visitor);
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-03-07 12:27:36 +03:00
|
|
|
/******************************************************************************
|
2011-12-17 14:30:29 +04:00
|
|
|
* nsCacheInputStream - a wrapper for nsIInputStream keeps the cache entry
|
2003-01-18 05:15:14 +03:00
|
|
|
* open while referenced.
|
2001-03-07 12:27:36 +03:00
|
|
|
******************************************************************************/
|
2001-02-28 03:06:13 +03:00
|
|
|
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_IMPL_ADDREF(nsCacheEntryDescriptor::nsInputStreamWrapper)
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2013-01-03 23:24:27 +04:00
|
|
|
nsCacheEntryDescriptor::nsInputStreamWrapper::Release()
|
|
|
|
{
|
|
|
|
// Holding a reference to descriptor ensures that cache service won't go
|
|
|
|
// away. Do not grab cache service lock if there is no descriptor.
|
|
|
|
nsRefPtr<nsCacheEntryDescriptor> desc;
|
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
desc = mDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
nsCacheService::Lock(LOCK_TELEM(NSINPUTSTREAMWRAPPER_RELEASE));
|
|
|
|
|
|
|
|
nsrefcnt count;
|
|
|
|
NS_PRECONDITION(0 != mRefCnt, "dup release");
|
2013-07-19 06:24:13 +04:00
|
|
|
count = --mRefCnt;
|
2013-01-03 23:24:27 +04:00
|
|
|
NS_LOG_RELEASE(this, count, "nsCacheEntryDescriptor::nsInputStreamWrapper");
|
|
|
|
|
|
|
|
if (0 == count) {
|
|
|
|
// don't use desc here since mDescriptor might be already nulled out
|
|
|
|
if (mDescriptor) {
|
2015-01-19 20:50:16 +03:00
|
|
|
NS_ASSERTION(mDescriptor->mInputWrappers.Contains(this),
|
2013-01-03 23:24:27 +04:00
|
|
|
"Wrapper not found in array!");
|
|
|
|
mDescriptor->mInputWrappers.RemoveElement(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
nsCacheService::Unlock();
|
|
|
|
|
|
|
|
mRefCnt = 1;
|
|
|
|
delete (this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
nsCacheService::Unlock();
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsCacheEntryDescriptor::nsInputStreamWrapper)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END_THREADSAFE
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult nsCacheEntryDescriptor::
|
|
|
|
nsInputStreamWrapper::LazyInit()
|
|
|
|
{
|
2013-01-03 23:24:27 +04:00
|
|
|
// Check if we have the descriptor. If not we can't even grab the cache
|
|
|
|
// lock since it is not ensured that the cache service still exists.
|
2012-12-14 21:03:01 +04:00
|
|
|
if (!mDescriptor)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2013-01-03 23:24:27 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSINPUTSTREAMWRAPPER_LAZYINIT));
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCacheAccessMode mode;
|
|
|
|
nsresult rv = mDescriptor->GetAccessGranted(&mode);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-02-28 03:06:13 +03:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
NS_ENSURE_TRUE(mode & nsICache::ACCESS_READ, NS_ERROR_UNEXPECTED);
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCacheEntry* cacheEntry = mDescriptor->CacheEntry();
|
|
|
|
if (!cacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
rv = nsCacheService::OpenInputStreamForEntry(cacheEntry, mode,
|
|
|
|
mStartOffset,
|
|
|
|
getter_AddRefs(mInput));
|
2012-06-01 02:20:02 +04:00
|
|
|
|
|
|
|
CACHE_LOG_DEBUG(("nsInputStreamWrapper::LazyInit "
|
2012-06-01 02:20:03 +04:00
|
|
|
"[entry=%p, wrapper=%p, mInput=%p, rv=%d]",
|
2012-08-09 11:09:40 +04:00
|
|
|
mDescriptor, this, mInput.get(), int(rv)));
|
2012-06-01 02:20:02 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitialized = true;
|
2003-01-18 05:15:14 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2013-01-03 23:24:27 +04:00
|
|
|
nsresult nsCacheEntryDescriptor::
|
|
|
|
nsInputStreamWrapper::EnsureInit()
|
|
|
|
{
|
|
|
|
if (mInitialized) {
|
|
|
|
NS_ASSERTION(mDescriptor, "Bad state");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LazyInit();
|
|
|
|
}
|
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
void nsCacheEntryDescriptor::
|
|
|
|
nsInputStreamWrapper::CloseInternal()
|
|
|
|
{
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
2013-01-03 23:24:27 +04:00
|
|
|
if (!mDescriptor) {
|
|
|
|
NS_ASSERTION(!mInitialized, "Bad state");
|
|
|
|
NS_ASSERTION(!mInput, "Bad state");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSINPUTSTREAMWRAPPER_CLOSEINTERNAL));
|
|
|
|
|
|
|
|
if (mDescriptor) {
|
|
|
|
mDescriptor->mInputWrappers.RemoveElement(this);
|
|
|
|
nsCacheService::ReleaseObject_Locked(mDescriptor);
|
|
|
|
mDescriptor = nullptr;
|
|
|
|
}
|
|
|
|
mInitialized = false;
|
|
|
|
mInput = nullptr;
|
|
|
|
}
|
|
|
|
|
2001-03-07 12:27:36 +03:00
|
|
|
nsresult nsCacheEntryDescriptor::
|
2003-01-18 05:15:14 +03:00
|
|
|
nsInputStreamWrapper::Close()
|
2012-12-14 21:03:01 +04:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
return Close_Locked();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsCacheEntryDescriptor::
|
|
|
|
nsInputStreamWrapper::Close_Locked()
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult rv = EnsureInit();
|
2012-12-14 21:03:01 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = mInput->Close();
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(!mInput,
|
|
|
|
"Shouldn't have mInput when EnsureInit() failed");
|
|
|
|
}
|
2001-03-07 12:27:36 +03:00
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
// Call CloseInternal() even when EnsureInit() failed, e.g. in case we are
|
|
|
|
// closing streams with nsCacheService::CloseAllStream()
|
|
|
|
CloseInternal();
|
|
|
|
return rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
}
|
2001-03-07 12:27:36 +03:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult nsCacheEntryDescriptor::
|
2012-08-22 19:56:38 +04:00
|
|
|
nsInputStreamWrapper::Available(uint64_t *avail)
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
2012-12-14 21:03:01 +04:00
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult rv = EnsureInit();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-06-25 10:22:44 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
return mInput->Available(avail);
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult nsCacheEntryDescriptor::
|
2012-08-22 19:56:38 +04:00
|
|
|
nsInputStreamWrapper::Read(char *buf, uint32_t count, uint32_t *countRead)
|
2012-12-14 21:03:01 +04:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
return Read_Locked(buf, count, countRead);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsCacheEntryDescriptor::
|
|
|
|
nsInputStreamWrapper::Read_Locked(char *buf, uint32_t count, uint32_t *countRead)
|
2001-03-01 22:24:10 +03:00
|
|
|
{
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult rv = EnsureInit();
|
2012-06-01 02:20:03 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = mInput->Read(buf, count, countRead);
|
2001-03-07 12:27:36 +03:00
|
|
|
|
2012-06-01 02:20:03 +04:00
|
|
|
CACHE_LOG_DEBUG(("nsInputStreamWrapper::Read "
|
|
|
|
"[entry=%p, wrapper=%p, mInput=%p, rv=%d]",
|
|
|
|
mDescriptor, this, mInput.get(), rv));
|
|
|
|
|
|
|
|
return rv;
|
2001-03-01 11:29:43 +03:00
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult nsCacheEntryDescriptor::
|
|
|
|
nsInputStreamWrapper::ReadSegments(nsWriteSegmentFun writer, void *closure,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count, uint32_t *countRead)
|
2001-03-01 11:29:43 +03:00
|
|
|
{
|
2005-02-18 21:39:53 +03:00
|
|
|
// cache stream not buffered
|
2001-03-07 12:27:36 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2001-03-01 11:29:43 +03:00
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult nsCacheEntryDescriptor::
|
2011-09-29 10:19:26 +04:00
|
|
|
nsInputStreamWrapper::IsNonBlocking(bool *result)
|
2001-03-01 11:29:43 +03:00
|
|
|
{
|
2003-01-18 05:15:14 +03:00
|
|
|
// cache streams will never return NS_BASE_STREAM_WOULD_BLOCK
|
2011-10-17 18:59:28 +04:00
|
|
|
*result = false;
|
2001-06-25 10:22:44 +04:00
|
|
|
return NS_OK;
|
2001-03-01 11:29:43 +03:00
|
|
|
}
|
|
|
|
|
2001-03-07 12:27:36 +03:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
/******************************************************************************
|
2011-12-17 14:30:29 +04:00
|
|
|
* nsDecompressInputStreamWrapper - an input stream wrapper that decompresses
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_IMPL_ADDREF(nsCacheEntryDescriptor::nsDecompressInputStreamWrapper)
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2013-01-03 23:24:27 +04:00
|
|
|
nsCacheEntryDescriptor::nsDecompressInputStreamWrapper::Release()
|
|
|
|
{
|
|
|
|
// Holding a reference to descriptor ensures that cache service won't go
|
|
|
|
// away. Do not grab cache service lock if there is no descriptor.
|
|
|
|
nsRefPtr<nsCacheEntryDescriptor> desc;
|
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
desc = mDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
nsCacheService::Lock(LOCK_TELEM(
|
|
|
|
NSDECOMPRESSINPUTSTREAMWRAPPER_RELEASE));
|
|
|
|
|
|
|
|
nsrefcnt count;
|
|
|
|
NS_PRECONDITION(0 != mRefCnt, "dup release");
|
2013-07-19 06:24:13 +04:00
|
|
|
count = --mRefCnt;
|
2013-01-03 23:24:27 +04:00
|
|
|
NS_LOG_RELEASE(this, count,
|
|
|
|
"nsCacheEntryDescriptor::nsDecompressInputStreamWrapper");
|
|
|
|
|
|
|
|
if (0 == count) {
|
|
|
|
// don't use desc here since mDescriptor might be already nulled out
|
|
|
|
if (mDescriptor) {
|
2015-01-19 20:50:16 +03:00
|
|
|
NS_ASSERTION(mDescriptor->mInputWrappers.Contains(this),
|
2013-01-03 23:24:27 +04:00
|
|
|
"Wrapper not found in array!");
|
|
|
|
mDescriptor->mInputWrappers.RemoveElement(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
nsCacheService::Unlock();
|
|
|
|
|
|
|
|
mRefCnt = 1;
|
|
|
|
delete (this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
nsCacheService::Unlock();
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsCacheEntryDescriptor::nsDecompressInputStreamWrapper)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END_THREADSAFE
|
2011-12-17 14:30:29 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsCacheEntryDescriptor::
|
|
|
|
nsDecompressInputStreamWrapper::Read(char * buf,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count,
|
|
|
|
uint32_t *countRead)
|
2011-12-17 14:30:29 +04:00
|
|
|
{
|
2012-12-14 21:03:01 +04:00
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
2011-12-17 14:30:29 +04:00
|
|
|
int zerr = Z_OK;
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (!mStreamInitialized) {
|
|
|
|
rv = InitZstream();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mZstream.next_out = (Bytef*)buf;
|
|
|
|
mZstream.avail_out = count;
|
|
|
|
|
|
|
|
if (mReadBufferLen < count) {
|
|
|
|
// Allocate a buffer for reading from the input stream. This will
|
|
|
|
// determine the max number of compressed bytes read from the
|
|
|
|
// input stream at one time. Making the buffer size proportional
|
|
|
|
// to the request size is not necessary, but helps minimize the
|
|
|
|
// number of read requests to the input stream.
|
2013-01-15 16:22:03 +04:00
|
|
|
uint32_t newBufLen = std::max(count, (uint32_t)kMinDecompressReadBufLen);
|
2011-12-17 14:30:29 +04:00
|
|
|
unsigned char* newBuf;
|
|
|
|
newBuf = (unsigned char*)nsMemory::Realloc(mReadBuffer,
|
|
|
|
newBufLen);
|
|
|
|
if (newBuf) {
|
|
|
|
mReadBuffer = newBuf;
|
|
|
|
mReadBufferLen = newBufLen;
|
|
|
|
}
|
|
|
|
if (!mReadBuffer) {
|
|
|
|
mReadBufferLen = 0;
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// read and inflate data until the output buffer is full, or
|
|
|
|
// there is no more data to read
|
|
|
|
while (NS_SUCCEEDED(rv) &&
|
|
|
|
zerr == Z_OK &&
|
|
|
|
mZstream.avail_out > 0 &&
|
|
|
|
count > 0) {
|
|
|
|
if (mZstream.avail_in == 0) {
|
2012-12-14 21:03:01 +04:00
|
|
|
rv = nsInputStreamWrapper::Read_Locked((char*)mReadBuffer,
|
|
|
|
mReadBufferLen,
|
|
|
|
&mZstream.avail_in);
|
2011-12-17 14:30:29 +04:00
|
|
|
if (NS_FAILED(rv) || !mZstream.avail_in) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mZstream.next_in = mReadBuffer;
|
|
|
|
}
|
|
|
|
zerr = inflate(&mZstream, Z_NO_FLUSH);
|
|
|
|
if (zerr == Z_STREAM_END) {
|
|
|
|
// The compressed data may have been stored in multiple
|
|
|
|
// chunks/streams. To allow for this case, re-initialize
|
|
|
|
// the inflate stream and continue decompressing from
|
|
|
|
// the next byte.
|
|
|
|
Bytef * saveNextIn = mZstream.next_in;
|
|
|
|
unsigned int saveAvailIn = mZstream.avail_in;
|
|
|
|
Bytef * saveNextOut = mZstream.next_out;
|
|
|
|
unsigned int saveAvailOut = mZstream.avail_out;
|
|
|
|
inflateReset(&mZstream);
|
|
|
|
mZstream.next_in = saveNextIn;
|
|
|
|
mZstream.avail_in = saveAvailIn;
|
|
|
|
mZstream.next_out = saveNextOut;
|
|
|
|
mZstream.avail_out = saveAvailOut;
|
|
|
|
zerr = Z_OK;
|
|
|
|
} else if (zerr != Z_OK) {
|
|
|
|
rv = NS_ERROR_INVALID_CONTENT_ENCODING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*countRead = count - mZstream.avail_out;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsCacheEntryDescriptor::
|
|
|
|
nsDecompressInputStreamWrapper::Close()
|
|
|
|
{
|
2012-12-14 21:03:01 +04:00
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (!mDescriptor)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2011-12-17 14:30:29 +04:00
|
|
|
EndZstream();
|
|
|
|
if (mReadBuffer) {
|
|
|
|
nsMemory::Free(mReadBuffer);
|
|
|
|
mReadBuffer = 0;
|
|
|
|
mReadBufferLen = 0;
|
|
|
|
}
|
2012-12-14 21:03:01 +04:00
|
|
|
return nsInputStreamWrapper::Close_Locked();
|
2011-12-17 14:30:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsCacheEntryDescriptor::
|
|
|
|
nsDecompressInputStreamWrapper::InitZstream()
|
|
|
|
{
|
2012-12-14 21:03:01 +04:00
|
|
|
if (!mDescriptor)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
if (mStreamEnded)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-12-17 14:30:29 +04:00
|
|
|
// Initialize zlib inflate stream
|
|
|
|
mZstream.zalloc = Z_NULL;
|
|
|
|
mZstream.zfree = Z_NULL;
|
|
|
|
mZstream.opaque = Z_NULL;
|
|
|
|
mZstream.next_out = Z_NULL;
|
|
|
|
mZstream.avail_out = 0;
|
|
|
|
mZstream.next_in = Z_NULL;
|
|
|
|
mZstream.avail_in = 0;
|
|
|
|
if (inflateInit(&mZstream) != Z_OK) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-04-04 13:15:10 +04:00
|
|
|
mStreamInitialized = true;
|
2011-12-17 14:30:29 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsCacheEntryDescriptor::
|
|
|
|
nsDecompressInputStreamWrapper::EndZstream()
|
|
|
|
{
|
|
|
|
if (mStreamInitialized && !mStreamEnded) {
|
|
|
|
inflateEnd(&mZstream);
|
2012-12-14 21:03:01 +04:00
|
|
|
mStreamInitialized = false;
|
2012-04-04 13:15:10 +04:00
|
|
|
mStreamEnded = true;
|
2011-12-17 14:30:29 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsOutputStreamWrapper - a wrapper for nsIOutputstream to track the amount of
|
|
|
|
* data written to a cache entry.
|
|
|
|
* - also keeps the cache entry open while referenced.
|
2003-01-18 05:15:14 +03:00
|
|
|
******************************************************************************/
|
2001-03-01 11:29:43 +03:00
|
|
|
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_IMPL_ADDREF(nsCacheEntryDescriptor::nsOutputStreamWrapper)
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2013-01-03 23:24:27 +04:00
|
|
|
nsCacheEntryDescriptor::nsOutputStreamWrapper::Release()
|
|
|
|
{
|
|
|
|
// Holding a reference to descriptor ensures that cache service won't go
|
|
|
|
// away. Do not grab cache service lock if there is no descriptor.
|
|
|
|
nsRefPtr<nsCacheEntryDescriptor> desc;
|
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
desc = mDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
nsCacheService::Lock(LOCK_TELEM(NSOUTPUTSTREAMWRAPPER_RELEASE));
|
|
|
|
|
|
|
|
nsrefcnt count;
|
|
|
|
NS_PRECONDITION(0 != mRefCnt, "dup release");
|
2013-07-19 06:24:13 +04:00
|
|
|
count = --mRefCnt;
|
2013-01-03 23:24:27 +04:00
|
|
|
NS_LOG_RELEASE(this, count,
|
|
|
|
"nsCacheEntryDescriptor::nsOutputStreamWrapper");
|
|
|
|
|
|
|
|
if (0 == count) {
|
|
|
|
// don't use desc here since mDescriptor might be already nulled out
|
|
|
|
if (mDescriptor)
|
|
|
|
mDescriptor->mOutputWrapper = nullptr;
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
nsCacheService::Unlock();
|
|
|
|
|
|
|
|
mRefCnt = 1;
|
|
|
|
delete (this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
nsCacheService::Unlock();
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsCacheEntryDescriptor::nsOutputStreamWrapper)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIOutputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END_THREADSAFE
|
2001-03-01 22:52:06 +03:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult nsCacheEntryDescriptor::
|
|
|
|
nsOutputStreamWrapper::LazyInit()
|
2001-03-01 22:24:10 +03:00
|
|
|
{
|
2013-01-03 23:24:27 +04:00
|
|
|
// Check if we have the descriptor. If not we can't even grab the cache
|
|
|
|
// lock since it is not ensured that the cache service still exists.
|
2012-12-14 21:03:01 +04:00
|
|
|
if (!mDescriptor)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2013-01-03 23:24:27 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSOUTPUTSTREAMWRAPPER_LAZYINIT));
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCacheAccessMode mode;
|
|
|
|
nsresult rv = mDescriptor->GetAccessGranted(&mode);
|
2001-03-01 22:24:10 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
NS_ENSURE_TRUE(mode & nsICache::ACCESS_WRITE, NS_ERROR_UNEXPECTED);
|
2001-03-01 22:52:06 +03:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCacheEntry* cacheEntry = mDescriptor->CacheEntry();
|
|
|
|
if (!cacheEntry) return NS_ERROR_NOT_AVAILABLE;
|
2001-03-01 11:29:43 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ASSERTION(mOutput == nullptr, "mOutput set in LazyInit");
|
2001-03-01 11:29:43 +03:00
|
|
|
|
2011-06-22 12:49:35 +04:00
|
|
|
nsCOMPtr<nsIOutputStream> stream;
|
|
|
|
rv = nsCacheService::OpenOutputStreamForEntry(cacheEntry, mode, mStartOffset,
|
|
|
|
getter_AddRefs(stream));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2010-12-29 04:25:14 +03:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCacheDevice* device = cacheEntry->CacheDevice();
|
2011-06-22 12:49:35 +04:00
|
|
|
if (device) {
|
|
|
|
// the entry has been truncated to mStartOffset bytes, inform device
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t size = cacheEntry->DataSize();
|
2011-06-22 12:49:35 +04:00
|
|
|
rv = device->OnDataSizeChange(cacheEntry, mStartOffset - size);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
cacheEntry->SetDataSize(mStartOffset);
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2001-03-07 12:27:36 +03:00
|
|
|
|
2011-06-22 12:49:35 +04:00
|
|
|
// If anything above failed, clean up internal state and get out of here
|
|
|
|
// (see bug #654926)...
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-03-15 23:00:15 +04:00
|
|
|
nsCacheService::ReleaseObject_Locked(stream.forget().take());
|
2012-12-14 21:03:01 +04:00
|
|
|
mDescriptor->mOutputWrapper = nullptr;
|
|
|
|
nsCacheService::ReleaseObject_Locked(mDescriptor);
|
|
|
|
mDescriptor = nullptr;
|
|
|
|
mInitialized = false;
|
2011-06-22 12:49:35 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2001-03-07 12:27:36 +03:00
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
mOutput = stream;
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitialized = true;
|
2003-01-18 05:15:14 +03:00
|
|
|
return NS_OK;
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2013-01-03 23:24:27 +04:00
|
|
|
nsresult nsCacheEntryDescriptor::
|
|
|
|
nsOutputStreamWrapper::EnsureInit()
|
|
|
|
{
|
|
|
|
if (mInitialized) {
|
|
|
|
NS_ASSERTION(mDescriptor, "Bad state");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LazyInit();
|
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult nsCacheEntryDescriptor::
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOutputStreamWrapper::OnWrite(uint32_t count)
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
2012-09-28 10:57:33 +04:00
|
|
|
if (count > INT32_MAX) return NS_ERROR_UNEXPECTED;
|
2012-08-22 19:56:38 +04:00
|
|
|
return mDescriptor->RequestDataSizeChange((int32_t)count);
|
2003-01-18 05:15:14 +03:00
|
|
|
}
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
void nsCacheEntryDescriptor::
|
|
|
|
nsOutputStreamWrapper::CloseInternal()
|
|
|
|
{
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
2013-01-03 23:24:27 +04:00
|
|
|
if (!mDescriptor) {
|
|
|
|
NS_ASSERTION(!mInitialized, "Bad state");
|
|
|
|
NS_ASSERTION(!mOutput, "Bad state");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
nsCacheServiceAutoLock lock(LOCK_TELEM(NSOUTPUTSTREAMWRAPPER_CLOSEINTERNAL));
|
|
|
|
|
|
|
|
if (mDescriptor) {
|
|
|
|
mDescriptor->mOutputWrapper = nullptr;
|
|
|
|
nsCacheService::ReleaseObject_Locked(mDescriptor);
|
|
|
|
mDescriptor = nullptr;
|
|
|
|
}
|
|
|
|
mInitialized = false;
|
|
|
|
mOutput = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-07 12:27:36 +03:00
|
|
|
NS_IMETHODIMP nsCacheEntryDescriptor::
|
2003-01-18 05:15:14 +03:00
|
|
|
nsOutputStreamWrapper::Close()
|
2012-12-14 21:03:01 +04:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
return Close_Locked();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsCacheEntryDescriptor::
|
|
|
|
nsOutputStreamWrapper::Close_Locked()
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
|
|
|
nsresult rv = EnsureInit();
|
2012-12-14 21:03:01 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = mOutput->Close();
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(!mOutput,
|
|
|
|
"Shouldn't have mOutput when EnsureInit() failed");
|
|
|
|
}
|
2001-03-07 12:27:36 +03:00
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
// Call CloseInternal() even when EnsureInit() failed, e.g. in case we are
|
|
|
|
// closing streams with nsCacheService::CloseAllStream()
|
|
|
|
CloseInternal();
|
|
|
|
return rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
}
|
2001-03-07 12:27:36 +03:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
NS_IMETHODIMP nsCacheEntryDescriptor::
|
|
|
|
nsOutputStreamWrapper::Flush()
|
2001-03-07 12:27:36 +03:00
|
|
|
{
|
2012-12-14 21:03:01 +04:00
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult rv = EnsureInit();
|
2001-03-07 12:27:36 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
return mOutput->Flush();
|
2001-03-07 12:27:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsCacheEntryDescriptor::
|
|
|
|
nsOutputStreamWrapper::Write(const char * buf,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count,
|
|
|
|
uint32_t * result)
|
2012-12-14 21:03:01 +04:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
return Write_Locked(buf, count, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsCacheEntryDescriptor::
|
|
|
|
nsOutputStreamWrapper::Write_Locked(const char * buf,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t * result)
|
2001-03-07 12:27:36 +03:00
|
|
|
{
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult rv = EnsureInit();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = OnWrite(count);
|
2001-03-07 12:27:36 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
|
2001-03-07 12:27:36 +03:00
|
|
|
return mOutput->Write(buf, count, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsCacheEntryDescriptor::
|
|
|
|
nsOutputStreamWrapper::WriteFrom(nsIInputStream * inStr,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count,
|
|
|
|
uint32_t * result)
|
2001-03-07 12:27:36 +03:00
|
|
|
{
|
2003-01-18 05:15:14 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2001-03-07 12:27:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsCacheEntryDescriptor::
|
|
|
|
nsOutputStreamWrapper::WriteSegments(nsReadSegmentFun reader,
|
2003-01-18 05:15:14 +03:00
|
|
|
void * closure,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count,
|
|
|
|
uint32_t * result)
|
2001-03-07 12:27:36 +03:00
|
|
|
{
|
2003-01-18 05:15:14 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2001-03-07 12:27:36 +03:00
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
NS_IMETHODIMP nsCacheEntryDescriptor::
|
2011-09-29 10:19:26 +04:00
|
|
|
nsOutputStreamWrapper::IsNonBlocking(bool *result)
|
2001-03-07 12:27:36 +03:00
|
|
|
{
|
2003-01-18 05:15:14 +03:00
|
|
|
// cache streams will never return NS_BASE_STREAM_WOULD_BLOCK
|
2011-10-17 18:59:28 +04:00
|
|
|
*result = false;
|
2003-01-18 05:15:14 +03:00
|
|
|
return NS_OK;
|
2001-03-07 12:27:36 +03:00
|
|
|
}
|
2011-12-17 14:30:29 +04:00
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsCompressOutputStreamWrapper - an output stream wrapper that compresses
|
|
|
|
* data before it is written
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_IMPL_ADDREF(nsCacheEntryDescriptor::nsCompressOutputStreamWrapper)
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2013-01-03 23:24:27 +04:00
|
|
|
nsCacheEntryDescriptor::nsCompressOutputStreamWrapper::Release()
|
|
|
|
{
|
|
|
|
// Holding a reference to descriptor ensures that cache service won't go
|
|
|
|
// away. Do not grab cache service lock if there is no descriptor.
|
|
|
|
nsRefPtr<nsCacheEntryDescriptor> desc;
|
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
desc = mDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
nsCacheService::Lock(LOCK_TELEM(NSCOMPRESSOUTPUTSTREAMWRAPPER_RELEASE));
|
|
|
|
|
|
|
|
nsrefcnt count;
|
|
|
|
NS_PRECONDITION(0 != mRefCnt, "dup release");
|
2013-07-19 06:24:13 +04:00
|
|
|
count = --mRefCnt;
|
2013-01-03 23:24:27 +04:00
|
|
|
NS_LOG_RELEASE(this, count,
|
|
|
|
"nsCacheEntryDescriptor::nsCompressOutputStreamWrapper");
|
|
|
|
|
|
|
|
if (0 == count) {
|
|
|
|
// don't use desc here since mDescriptor might be already nulled out
|
|
|
|
if (mDescriptor)
|
|
|
|
mDescriptor->mOutputWrapper = nullptr;
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
nsCacheService::Unlock();
|
|
|
|
|
|
|
|
mRefCnt = 1;
|
|
|
|
delete (this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc)
|
|
|
|
nsCacheService::Unlock();
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsCacheEntryDescriptor::nsCompressOutputStreamWrapper)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIOutputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END_THREADSAFE
|
2011-12-17 14:30:29 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsCacheEntryDescriptor::
|
|
|
|
nsCompressOutputStreamWrapper::Write(const char * buf,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count,
|
|
|
|
uint32_t * result)
|
2011-12-17 14:30:29 +04:00
|
|
|
{
|
2012-12-14 21:03:01 +04:00
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
2011-12-17 14:30:29 +04:00
|
|
|
int zerr = Z_OK;
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (!mStreamInitialized) {
|
|
|
|
rv = InitZstream();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mWriteBuffer) {
|
|
|
|
// Once allocated, this buffer is referenced by the zlib stream and
|
|
|
|
// cannot be grown. We use 2x(initial write request) to approximate
|
|
|
|
// a stream buffer size proportional to request buffers.
|
2013-01-15 16:22:03 +04:00
|
|
|
mWriteBufferLen = std::max(count*2, (uint32_t)kMinCompressWriteBufLen);
|
2011-12-17 14:30:29 +04:00
|
|
|
mWriteBuffer = (unsigned char*)nsMemory::Alloc(mWriteBufferLen);
|
|
|
|
if (!mWriteBuffer) {
|
|
|
|
mWriteBufferLen = 0;
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mZstream.next_out = mWriteBuffer;
|
|
|
|
mZstream.avail_out = mWriteBufferLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compress (deflate) the requested buffer. Keep going
|
|
|
|
// until the entire buffer has been deflated.
|
|
|
|
mZstream.avail_in = count;
|
|
|
|
mZstream.next_in = (Bytef*)buf;
|
|
|
|
while (mZstream.avail_in > 0) {
|
|
|
|
zerr = deflate(&mZstream, Z_NO_FLUSH);
|
|
|
|
if (zerr == Z_STREAM_ERROR) {
|
2012-01-25 17:52:29 +04:00
|
|
|
deflateEnd(&mZstream);
|
2012-12-14 21:03:01 +04:00
|
|
|
mStreamEnded = true;
|
2012-04-04 13:15:10 +04:00
|
|
|
mStreamInitialized = false;
|
2011-12-17 14:30:29 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
// Note: Z_BUF_ERROR is non-fatal and sometimes expected here.
|
|
|
|
|
|
|
|
// If the compression stream output buffer is filled, write
|
|
|
|
// it out to the underlying stream wrapper.
|
|
|
|
if (mZstream.avail_out == 0) {
|
|
|
|
rv = WriteBuffer();
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-01-25 17:52:29 +04:00
|
|
|
deflateEnd(&mZstream);
|
2012-12-14 21:03:01 +04:00
|
|
|
mStreamEnded = true;
|
2012-04-04 13:15:10 +04:00
|
|
|
mStreamInitialized = false;
|
2011-12-17 14:30:29 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*result = count;
|
|
|
|
mUncompressedCount += *result;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsCacheEntryDescriptor::
|
|
|
|
nsCompressOutputStreamWrapper::Close()
|
|
|
|
{
|
2012-12-14 21:03:01 +04:00
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (!mDescriptor)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
nsresult retval = NS_OK;
|
|
|
|
nsresult rv;
|
2011-12-17 14:30:29 +04:00
|
|
|
int zerr = 0;
|
|
|
|
|
|
|
|
if (mStreamInitialized) {
|
|
|
|
// complete compression of any data remaining in the zlib stream
|
|
|
|
do {
|
|
|
|
zerr = deflate(&mZstream, Z_FINISH);
|
|
|
|
rv = WriteBuffer();
|
2012-12-14 21:03:01 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
retval = rv;
|
2011-12-17 14:30:29 +04:00
|
|
|
} while (zerr == Z_OK && rv == NS_OK);
|
|
|
|
deflateEnd(&mZstream);
|
2012-12-14 21:03:01 +04:00
|
|
|
mStreamInitialized = false;
|
2011-12-17 14:30:29 +04:00
|
|
|
}
|
2012-12-14 21:03:01 +04:00
|
|
|
// Do not allow to initialize stream after calling Close().
|
|
|
|
mStreamEnded = true;
|
2011-12-17 14:30:29 +04:00
|
|
|
|
|
|
|
if (mDescriptor->CacheEntry()) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString uncompressedLenStr;
|
2011-12-17 14:30:29 +04:00
|
|
|
rv = mDescriptor->GetMetaDataElement("uncompressed-len",
|
|
|
|
getter_Copies(uncompressedLenStr));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t oldCount = uncompressedLenStr.ToInteger(&rv);
|
2011-12-17 14:30:29 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mUncompressedCount += oldCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uncompressedLenStr.Adopt(0);
|
|
|
|
uncompressedLenStr.AppendInt(mUncompressedCount);
|
|
|
|
rv = mDescriptor->SetMetaDataElement("uncompressed-len",
|
|
|
|
uncompressedLenStr.get());
|
2012-12-14 21:03:01 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
retval = rv;
|
2011-12-17 14:30:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mWriteBuffer) {
|
|
|
|
nsMemory::Free(mWriteBuffer);
|
|
|
|
mWriteBuffer = 0;
|
|
|
|
mWriteBufferLen = 0;
|
|
|
|
}
|
|
|
|
|
2012-12-14 21:03:01 +04:00
|
|
|
rv = nsOutputStreamWrapper::Close_Locked();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
retval = rv;
|
|
|
|
|
|
|
|
return retval;
|
2011-12-17 14:30:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsCacheEntryDescriptor::
|
|
|
|
nsCompressOutputStreamWrapper::InitZstream()
|
|
|
|
{
|
2012-12-14 21:03:01 +04:00
|
|
|
if (!mDescriptor)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
if (mStreamEnded)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-12-17 14:30:29 +04:00
|
|
|
// Determine compression level: Aggressive compression
|
|
|
|
// may impact performance on mobile devices, while a
|
|
|
|
// lower compression level still provides substantial
|
|
|
|
// space savings for many text streams.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t compressionLevel = nsCacheService::CacheCompressionLevel();
|
2011-12-17 14:30:29 +04:00
|
|
|
|
|
|
|
// Initialize zlib deflate stream
|
|
|
|
mZstream.zalloc = Z_NULL;
|
|
|
|
mZstream.zfree = Z_NULL;
|
|
|
|
mZstream.opaque = Z_NULL;
|
|
|
|
if (deflateInit2(&mZstream, compressionLevel, Z_DEFLATED,
|
|
|
|
MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mZstream.next_in = Z_NULL;
|
|
|
|
mZstream.avail_in = 0;
|
|
|
|
|
2012-04-04 13:15:10 +04:00
|
|
|
mStreamInitialized = true;
|
2011-12-17 14:30:29 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsCacheEntryDescriptor::
|
|
|
|
nsCompressOutputStreamWrapper::WriteBuffer()
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesToWrite = mWriteBufferLen - mZstream.avail_out;
|
|
|
|
uint32_t result = 0;
|
2012-12-14 21:03:01 +04:00
|
|
|
nsresult rv = nsCacheEntryDescriptor::nsOutputStreamWrapper::Write_Locked(
|
2011-12-17 14:30:29 +04:00
|
|
|
(const char *)mWriteBuffer, bytesToWrite, &result);
|
|
|
|
mZstream.next_out = mWriteBuffer;
|
|
|
|
mZstream.avail_out = mWriteBufferLen;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|