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