Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsCache.h"
8 : #include "nsDiskCache.h"
9 : #include "nsDiskCacheEntry.h"
10 : #include "nsDiskCacheBinding.h"
11 : #include "nsCRT.h"
12 :
13 : #include "nsISerializable.h"
14 : #include "nsSerializationHelper.h"
15 :
16 : /******************************************************************************
17 : * nsDiskCacheEntry
18 : *****************************************************************************/
19 :
20 : /**
21 : * CreateCacheEntry()
22 : *
23 : * Creates an nsCacheEntry and sets all fields except for the binding.
24 : */
25 : nsCacheEntry *
26 0 : nsDiskCacheEntry::CreateCacheEntry(nsCacheDevice * device)
27 : {
28 0 : nsCacheEntry * entry = nullptr;
29 0 : nsresult rv = nsCacheEntry::Create(Key(),
30 : nsICache::STREAM_BASED,
31 : nsICache::STORE_ON_DISK,
32 : device,
33 0 : &entry);
34 0 : if (NS_FAILED(rv) || !entry) return nullptr;
35 :
36 0 : entry->SetFetchCount(mFetchCount);
37 0 : entry->SetLastFetched(mLastFetched);
38 0 : entry->SetLastModified(mLastModified);
39 0 : entry->SetExpirationTime(mExpirationTime);
40 0 : entry->SetCacheDevice(device);
41 : // XXX why does nsCacheService have to fill out device in BindEntry()?
42 0 : entry->SetDataSize(mDataSize);
43 :
44 0 : rv = entry->UnflattenMetaData(MetaData(), mMetaDataSize);
45 0 : if (NS_FAILED(rv)) {
46 0 : delete entry;
47 0 : return nullptr;
48 : }
49 :
50 : // Restore security info, if present
51 0 : const char* info = entry->GetMetaDataElement("security-info");
52 0 : if (info) {
53 0 : nsCOMPtr<nsISupports> infoObj;
54 0 : rv = NS_DeserializeObject(nsDependentCString(info),
55 0 : getter_AddRefs(infoObj));
56 0 : if (NS_FAILED(rv)) {
57 0 : delete entry;
58 0 : return nullptr;
59 : }
60 0 : entry->SetSecurityInfo(infoObj);
61 : }
62 :
63 0 : return entry;
64 : }
65 :
66 :
67 : /******************************************************************************
68 : * nsDiskCacheEntryInfo
69 : *****************************************************************************/
70 :
71 0 : NS_IMPL_ISUPPORTS(nsDiskCacheEntryInfo, nsICacheEntryInfo)
72 :
73 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetClientID(char ** clientID)
74 : {
75 0 : NS_ENSURE_ARG_POINTER(clientID);
76 0 : return ClientIDFromCacheKey(nsDependentCString(mDiskEntry->Key()), clientID);
77 : }
78 :
79 : extern const char DISK_CACHE_DEVICE_ID[];
80 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetDeviceID(char ** deviceID)
81 : {
82 0 : NS_ENSURE_ARG_POINTER(deviceID);
83 0 : *deviceID = NS_strdup(mDeviceID);
84 0 : return *deviceID ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
85 : }
86 :
87 :
88 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetKey(nsACString &clientKey)
89 : {
90 0 : return ClientKeyFromCacheKey(nsDependentCString(mDiskEntry->Key()), clientKey);
91 : }
92 :
93 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetFetchCount(int32_t *aFetchCount)
94 : {
95 0 : NS_ENSURE_ARG_POINTER(aFetchCount);
96 0 : *aFetchCount = mDiskEntry->mFetchCount;
97 0 : return NS_OK;
98 : }
99 :
100 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetLastFetched(uint32_t *aLastFetched)
101 : {
102 0 : NS_ENSURE_ARG_POINTER(aLastFetched);
103 0 : *aLastFetched = mDiskEntry->mLastFetched;
104 0 : return NS_OK;
105 : }
106 :
107 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetLastModified(uint32_t *aLastModified)
108 : {
109 0 : NS_ENSURE_ARG_POINTER(aLastModified);
110 0 : *aLastModified = mDiskEntry->mLastModified;
111 0 : return NS_OK;
112 : }
113 :
114 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetExpirationTime(uint32_t *aExpirationTime)
115 : {
116 0 : NS_ENSURE_ARG_POINTER(aExpirationTime);
117 0 : *aExpirationTime = mDiskEntry->mExpirationTime;
118 0 : return NS_OK;
119 : }
120 :
121 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::IsStreamBased(bool *aStreamBased)
122 : {
123 0 : NS_ENSURE_ARG_POINTER(aStreamBased);
124 0 : *aStreamBased = true;
125 0 : return NS_OK;
126 : }
127 :
128 0 : NS_IMETHODIMP nsDiskCacheEntryInfo::GetDataSize(uint32_t *aDataSize)
129 : {
130 0 : NS_ENSURE_ARG_POINTER(aDataSize);
131 0 : *aDataSize = mDiskEntry->mDataSize;
132 0 : return NS_OK;
133 : }
|