summaryrefslogtreecommitdiffstats
path: root/netwerk/cache/nsCacheSession.cpp
blob: d82203aec14c7de573a5c568975466374471e219 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * 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/. */

#include "nsCacheSession.h"
#include "nsCacheService.h"
#include "nsCRT.h"
#include "nsThreadUtils.h"

NS_IMPL_ISUPPORTS(nsCacheSession, nsICacheSession)

nsCacheSession::nsCacheSession(const char *         clientID,
                               nsCacheStoragePolicy storagePolicy,
                               bool                 streamBased)
    : mClientID(clientID),
      mInfo(0)
{
  SetStoragePolicy(storagePolicy);

  if (streamBased) MarkStreamBased();
  else SetStoragePolicy(nsICache::STORE_IN_MEMORY);

  MarkPublic();

  MarkDoomEntriesIfExpired();
}

nsCacheSession::~nsCacheSession()
{
  /* destructor code */
    // notify service we are going away?
}


NS_IMETHODIMP nsCacheSession::GetDoomEntriesIfExpired(bool *result)
{
    NS_ENSURE_ARG_POINTER(result);
    *result = WillDoomEntriesIfExpired();
    return NS_OK;
}


NS_IMETHODIMP nsCacheSession::SetProfileDirectory(nsIFile *profileDir)
{
  if (StoragePolicy() != nsICache::STORE_OFFLINE && profileDir) {
        // Profile directory override is currently implemented only for
        // offline cache.  This is an early failure to prevent the request
        // being processed before it would fail later because of inability
        // to assign a cache base dir.
        return NS_ERROR_UNEXPECTED;
    }

    mProfileDir = profileDir;
    return NS_OK;
}

NS_IMETHODIMP nsCacheSession::GetProfileDirectory(nsIFile **profileDir)
{
    if (mProfileDir)
        NS_ADDREF(*profileDir = mProfileDir);
    else
        *profileDir = nullptr;

    return NS_OK;
}


NS_IMETHODIMP nsCacheSession::SetDoomEntriesIfExpired(bool doomEntriesIfExpired)
{
    if (doomEntriesIfExpired)  MarkDoomEntriesIfExpired();
    else                       ClearDoomEntriesIfExpired();
    return NS_OK;
}


NS_IMETHODIMP
nsCacheSession::OpenCacheEntry(const nsACString &         key, 
                               nsCacheAccessMode          accessRequested,
                               bool                       blockingMode,
                               nsICacheEntryDescriptor ** result)
{
    nsresult rv;

    if (NS_IsMainThread())
        rv = NS_ERROR_NOT_AVAILABLE;
    else
        rv = nsCacheService::OpenCacheEntry(this,
                                            key,
                                            accessRequested,
                                            blockingMode,
                                            nullptr, // no listener
                                            result);
    return rv;
}


NS_IMETHODIMP nsCacheSession::AsyncOpenCacheEntry(const nsACString & key,
                                                  nsCacheAccessMode accessRequested,
                                                  nsICacheListener *listener,
                                                  bool              noWait)
{
    nsresult rv;
    rv = nsCacheService::OpenCacheEntry(this,
                                        key,
                                        accessRequested,
                                        !noWait,
                                        listener,
                                        nullptr); // no result

    if (rv == NS_ERROR_CACHE_WAIT_FOR_VALIDATION) rv = NS_OK;
    return rv;
}

NS_IMETHODIMP nsCacheSession::EvictEntries()
{
    return nsCacheService::EvictEntriesForSession(this);
}


NS_IMETHODIMP nsCacheSession::IsStorageEnabled(bool *result)
{

    return nsCacheService::IsStorageEnabledForPolicy(StoragePolicy(), result);
}

NS_IMETHODIMP nsCacheSession::DoomEntry(const nsACString &key,
                                        nsICacheListener *listener)
{
    return nsCacheService::DoomEntry(this, key, listener);
}

NS_IMETHODIMP nsCacheSession::GetIsPrivate(bool* aPrivate)
{
    *aPrivate = IsPrivate();
    return NS_OK;
}

NS_IMETHODIMP nsCacheSession::SetIsPrivate(bool aPrivate)
{
    if (aPrivate)
        MarkPrivate();
    else
        MarkPublic();
    return NS_OK;
}