|
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 "nsCacheSession.h" |
|
8 #include "nsCacheService.h" |
|
9 #include "nsCRT.h" |
|
10 #include "nsThreadUtils.h" |
|
11 |
|
12 NS_IMPL_ISUPPORTS(nsCacheSession, nsICacheSession) |
|
13 |
|
14 nsCacheSession::nsCacheSession(const char * clientID, |
|
15 nsCacheStoragePolicy storagePolicy, |
|
16 bool streamBased) |
|
17 : mClientID(clientID), |
|
18 mInfo(0) |
|
19 { |
|
20 SetStoragePolicy(storagePolicy); |
|
21 |
|
22 if (streamBased) MarkStreamBased(); |
|
23 else SetStoragePolicy(nsICache::STORE_IN_MEMORY); |
|
24 |
|
25 MarkPublic(); |
|
26 |
|
27 MarkDoomEntriesIfExpired(); |
|
28 } |
|
29 |
|
30 nsCacheSession::~nsCacheSession() |
|
31 { |
|
32 /* destructor code */ |
|
33 // notify service we are going away? |
|
34 } |
|
35 |
|
36 |
|
37 NS_IMETHODIMP nsCacheSession::GetDoomEntriesIfExpired(bool *result) |
|
38 { |
|
39 NS_ENSURE_ARG_POINTER(result); |
|
40 *result = WillDoomEntriesIfExpired(); |
|
41 return NS_OK; |
|
42 } |
|
43 |
|
44 |
|
45 NS_IMETHODIMP nsCacheSession::SetProfileDirectory(nsIFile *profileDir) |
|
46 { |
|
47 if (StoragePolicy() != nsICache::STORE_OFFLINE && profileDir) { |
|
48 // Profile directory override is currently implemented only for |
|
49 // offline cache. This is an early failure to prevent the request |
|
50 // being processed before it would fail later because of inability |
|
51 // to assign a cache base dir. |
|
52 return NS_ERROR_UNEXPECTED; |
|
53 } |
|
54 |
|
55 mProfileDir = profileDir; |
|
56 return NS_OK; |
|
57 } |
|
58 |
|
59 NS_IMETHODIMP nsCacheSession::GetProfileDirectory(nsIFile **profileDir) |
|
60 { |
|
61 if (mProfileDir) |
|
62 NS_ADDREF(*profileDir = mProfileDir); |
|
63 else |
|
64 *profileDir = nullptr; |
|
65 |
|
66 return NS_OK; |
|
67 } |
|
68 |
|
69 |
|
70 NS_IMETHODIMP nsCacheSession::SetDoomEntriesIfExpired(bool doomEntriesIfExpired) |
|
71 { |
|
72 if (doomEntriesIfExpired) MarkDoomEntriesIfExpired(); |
|
73 else ClearDoomEntriesIfExpired(); |
|
74 return NS_OK; |
|
75 } |
|
76 |
|
77 |
|
78 NS_IMETHODIMP |
|
79 nsCacheSession::OpenCacheEntry(const nsACString & key, |
|
80 nsCacheAccessMode accessRequested, |
|
81 bool blockingMode, |
|
82 nsICacheEntryDescriptor ** result) |
|
83 { |
|
84 nsresult rv; |
|
85 |
|
86 if (NS_IsMainThread()) |
|
87 rv = NS_ERROR_NOT_AVAILABLE; |
|
88 else |
|
89 rv = nsCacheService::OpenCacheEntry(this, |
|
90 key, |
|
91 accessRequested, |
|
92 blockingMode, |
|
93 nullptr, // no listener |
|
94 result); |
|
95 return rv; |
|
96 } |
|
97 |
|
98 |
|
99 NS_IMETHODIMP nsCacheSession::AsyncOpenCacheEntry(const nsACString & key, |
|
100 nsCacheAccessMode accessRequested, |
|
101 nsICacheListener *listener, |
|
102 bool noWait) |
|
103 { |
|
104 nsresult rv; |
|
105 rv = nsCacheService::OpenCacheEntry(this, |
|
106 key, |
|
107 accessRequested, |
|
108 !noWait, |
|
109 listener, |
|
110 nullptr); // no result |
|
111 |
|
112 if (rv == NS_ERROR_CACHE_WAIT_FOR_VALIDATION) rv = NS_OK; |
|
113 return rv; |
|
114 } |
|
115 |
|
116 NS_IMETHODIMP nsCacheSession::EvictEntries() |
|
117 { |
|
118 return nsCacheService::EvictEntriesForSession(this); |
|
119 } |
|
120 |
|
121 |
|
122 NS_IMETHODIMP nsCacheSession::IsStorageEnabled(bool *result) |
|
123 { |
|
124 |
|
125 return nsCacheService::IsStorageEnabledForPolicy(StoragePolicy(), result); |
|
126 } |
|
127 |
|
128 NS_IMETHODIMP nsCacheSession::DoomEntry(const nsACString &key, |
|
129 nsICacheListener *listener) |
|
130 { |
|
131 return nsCacheService::DoomEntry(this, key, listener); |
|
132 } |
|
133 |
|
134 NS_IMETHODIMP nsCacheSession::GetIsPrivate(bool* aPrivate) |
|
135 { |
|
136 *aPrivate = IsPrivate(); |
|
137 return NS_OK; |
|
138 } |
|
139 |
|
140 NS_IMETHODIMP nsCacheSession::SetIsPrivate(bool aPrivate) |
|
141 { |
|
142 if (aPrivate) |
|
143 MarkPrivate(); |
|
144 else |
|
145 MarkPublic(); |
|
146 return NS_OK; |
|
147 } |