1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is nsDiskCacheEntry.cpp, released
17 : * May 10, 2001.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Netscape Communications Corporation.
21 : * Portions created by the Initial Developer are Copyright (C) 2001
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Gordon Sheridan <gordon@netscape.com>
26 : * Patrick C. Beard <beard@netscape.com>
27 : *
28 : * Alternatively, the contents of this file may be used under the terms of
29 : * either the GNU General Public License Version 2 or later (the "GPL"), or
30 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 : * in which case the provisions of the GPL or the LGPL are applicable instead
32 : * of those above. If you wish to allow use of your version of this file only
33 : * under the terms of either the GPL or the LGPL, and not to allow others to
34 : * use your version of this file under the terms of the MPL, indicate your
35 : * decision by deleting the provisions above and replace them with the notice
36 : * and other provisions required by the GPL or the LGPL. If you do not delete
37 : * the provisions above, a recipient may use your version of this file under
38 : * the terms of any one of the MPL, the GPL or the LGPL.
39 : *
40 : * ***** END LICENSE BLOCK ***** */
41 :
42 : #include "nsDiskCache.h"
43 : #include "nsDiskCacheEntry.h"
44 : #include "nsDiskCacheBinding.h"
45 : #include "nsCRT.h"
46 :
47 : #include "nsCache.h"
48 :
49 : #include "nsISerializable.h"
50 : #include "nsSerializationHelper.h"
51 :
52 : /******************************************************************************
53 : * nsDiskCacheEntry
54 : *****************************************************************************/
55 :
56 : /**
57 : * CreateCacheEntry()
58 : *
59 : * Creates an nsCacheEntry and sets all fields except for the binding.
60 : */
61 : nsCacheEntry *
62 279 : nsDiskCacheEntry::CreateCacheEntry(nsCacheDevice * device)
63 : {
64 279 : nsCacheEntry * entry = nsnull;
65 279 : nsresult rv = nsCacheEntry::Create(Key(),
66 : nsICache::STREAM_BASED,
67 : nsICache::STORE_ON_DISK,
68 : device,
69 279 : &entry);
70 279 : if (NS_FAILED(rv) || !entry) return nsnull;
71 :
72 279 : entry->SetFetchCount(mFetchCount);
73 279 : entry->SetLastFetched(mLastFetched);
74 279 : entry->SetLastModified(mLastModified);
75 279 : entry->SetExpirationTime(mExpirationTime);
76 279 : entry->SetCacheDevice(device);
77 : // XXX why does nsCacheService have to fill out device in BindEntry()?
78 279 : entry->SetDataSize(mDataSize);
79 :
80 279 : rv = entry->UnflattenMetaData(MetaData(), mMetaDataSize);
81 279 : if (NS_FAILED(rv)) {
82 0 : delete entry;
83 0 : return nsnull;
84 : }
85 :
86 : // Restore security info, if present
87 279 : const char* info = entry->GetMetaDataElement("security-info");
88 279 : if (info) {
89 0 : nsCOMPtr<nsISupports> infoObj;
90 0 : NS_DeserializeObject(nsDependentCString(info), getter_AddRefs(infoObj));
91 0 : entry->SetSecurityInfo(infoObj);
92 : }
93 :
94 279 : return entry;
95 : }
96 :
97 :
98 : /******************************************************************************
99 : * nsDiskCacheEntryInfo
100 : *****************************************************************************/
101 :
102 0 : NS_IMPL_ISUPPORTS1(nsDiskCacheEntryInfo, nsICacheEntryInfo)
103 :
104 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetClientID(char ** clientID)
105 : {
106 0 : NS_ENSURE_ARG_POINTER(clientID);
107 0 : return ClientIDFromCacheKey(nsDependentCString(mDiskEntry->Key()), clientID);
108 : }
109 :
110 : extern const char DISK_CACHE_DEVICE_ID[];
111 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetDeviceID(char ** deviceID)
112 : {
113 0 : NS_ENSURE_ARG_POINTER(deviceID);
114 0 : *deviceID = NS_strdup(mDeviceID);
115 0 : return *deviceID ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
116 : }
117 :
118 :
119 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetKey(nsACString &clientKey)
120 : {
121 0 : return ClientKeyFromCacheKey(nsDependentCString(mDiskEntry->Key()), clientKey);
122 : }
123 :
124 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetFetchCount(PRInt32 *aFetchCount)
125 : {
126 0 : NS_ENSURE_ARG_POINTER(aFetchCount);
127 0 : *aFetchCount = mDiskEntry->mFetchCount;
128 0 : return NS_OK;
129 : }
130 :
131 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetLastFetched(PRUint32 *aLastFetched)
132 : {
133 0 : NS_ENSURE_ARG_POINTER(aLastFetched);
134 0 : *aLastFetched = mDiskEntry->mLastFetched;
135 0 : return NS_OK;
136 : }
137 :
138 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetLastModified(PRUint32 *aLastModified)
139 : {
140 0 : NS_ENSURE_ARG_POINTER(aLastModified);
141 0 : *aLastModified = mDiskEntry->mLastModified;
142 0 : return NS_OK;
143 : }
144 :
145 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetExpirationTime(PRUint32 *aExpirationTime)
146 : {
147 0 : NS_ENSURE_ARG_POINTER(aExpirationTime);
148 0 : *aExpirationTime = mDiskEntry->mExpirationTime;
149 0 : return NS_OK;
150 : }
151 :
152 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::IsStreamBased(bool *aStreamBased)
153 : {
154 0 : NS_ENSURE_ARG_POINTER(aStreamBased);
155 0 : *aStreamBased = true;
156 0 : return NS_OK;
157 : }
158 :
159 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetDataSize(PRUint32 *aDataSize)
160 : {
161 0 : NS_ENSURE_ARG_POINTER(aDataSize);
162 0 : *aDataSize = mDiskEntry->mDataSize;
163 0 : return NS_OK;
164 : }
|