|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "CacheLog.h" |
|
6 #include "CacheStorage.h" |
|
7 #include "CacheStorageService.h" |
|
8 #include "CacheEntry.h" |
|
9 #include "CacheObserver.h" |
|
10 |
|
11 #include "OldWrappers.h" |
|
12 |
|
13 #include "nsICacheEntryDoomCallback.h" |
|
14 |
|
15 #include "nsIApplicationCache.h" |
|
16 #include "nsIApplicationCacheService.h" |
|
17 #include "nsIURI.h" |
|
18 #include "nsNetCID.h" |
|
19 #include "nsServiceManagerUtils.h" |
|
20 |
|
21 namespace mozilla { |
|
22 namespace net { |
|
23 |
|
24 NS_IMPL_ISUPPORTS(CacheStorage, nsICacheStorage) |
|
25 |
|
26 CacheStorage::CacheStorage(nsILoadContextInfo* aInfo, |
|
27 bool aAllowDisk, |
|
28 bool aLookupAppCache) |
|
29 : mLoadContextInfo(GetLoadContextInfo(aInfo)) |
|
30 , mWriteToDisk(aAllowDisk) |
|
31 , mLookupAppCache(aLookupAppCache) |
|
32 { |
|
33 } |
|
34 |
|
35 CacheStorage::~CacheStorage() |
|
36 { |
|
37 } |
|
38 |
|
39 NS_IMETHODIMP CacheStorage::AsyncOpenURI(nsIURI *aURI, |
|
40 const nsACString & aIdExtension, |
|
41 uint32_t aFlags, |
|
42 nsICacheEntryOpenCallback *aCallback) |
|
43 { |
|
44 if (!CacheStorageService::Self()) |
|
45 return NS_ERROR_NOT_INITIALIZED; |
|
46 |
|
47 if (MOZ_UNLIKELY(!CacheObserver::UseDiskCache()) && mWriteToDisk) { |
|
48 aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_NOT_AVAILABLE); |
|
49 return NS_OK; |
|
50 } |
|
51 |
|
52 if (MOZ_UNLIKELY(!CacheObserver::UseMemoryCache()) && !mWriteToDisk) { |
|
53 aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_NOT_AVAILABLE); |
|
54 return NS_OK; |
|
55 } |
|
56 |
|
57 NS_ENSURE_ARG(aURI); |
|
58 NS_ENSURE_ARG(aCallback); |
|
59 |
|
60 nsresult rv; |
|
61 |
|
62 bool truncate = aFlags & nsICacheStorage::OPEN_TRUNCATE; |
|
63 |
|
64 nsCOMPtr<nsIURI> noRefURI; |
|
65 rv = aURI->CloneIgnoringRef(getter_AddRefs(noRefURI)); |
|
66 NS_ENSURE_SUCCESS(rv, rv); |
|
67 |
|
68 nsCOMPtr<nsIApplicationCache> appCache; |
|
69 if (LookupAppCache()) { |
|
70 rv = ChooseApplicationCache(noRefURI, getter_AddRefs(appCache)); |
|
71 NS_ENSURE_SUCCESS(rv, rv); |
|
72 } |
|
73 |
|
74 if (appCache) { |
|
75 nsAutoCString cacheKey; |
|
76 rv = noRefURI->GetAsciiSpec(cacheKey); |
|
77 NS_ENSURE_SUCCESS(rv, rv); |
|
78 |
|
79 nsAutoCString scheme; |
|
80 rv = noRefURI->GetScheme(scheme); |
|
81 NS_ENSURE_SUCCESS(rv, rv); |
|
82 |
|
83 nsRefPtr<_OldCacheLoad> appCacheLoad = |
|
84 new _OldCacheLoad(scheme, cacheKey, aCallback, appCache, |
|
85 LoadInfo(), WriteToDisk(), aFlags); |
|
86 rv = appCacheLoad->Start(); |
|
87 NS_ENSURE_SUCCESS(rv, rv); |
|
88 |
|
89 LOG(("CacheStorage::AsyncOpenURI loading from appcache")); |
|
90 return NS_OK; |
|
91 } |
|
92 |
|
93 nsRefPtr<CacheEntryHandle> entry; |
|
94 rv = CacheStorageService::Self()->AddStorageEntry( |
|
95 this, noRefURI, aIdExtension, |
|
96 true, // create always |
|
97 truncate, // replace any existing one? |
|
98 getter_AddRefs(entry)); |
|
99 NS_ENSURE_SUCCESS(rv, rv); |
|
100 |
|
101 // May invoke the callback synchronously |
|
102 entry->Entry()->AsyncOpen(aCallback, aFlags); |
|
103 |
|
104 return NS_OK; |
|
105 } |
|
106 |
|
107 NS_IMETHODIMP CacheStorage::AsyncDoomURI(nsIURI *aURI, const nsACString & aIdExtension, |
|
108 nsICacheEntryDoomCallback* aCallback) |
|
109 { |
|
110 if (!CacheStorageService::Self()) |
|
111 return NS_ERROR_NOT_INITIALIZED; |
|
112 |
|
113 nsresult rv = CacheStorageService::Self()->DoomStorageEntry( |
|
114 this, aURI, aIdExtension, aCallback); |
|
115 NS_ENSURE_SUCCESS(rv, rv); |
|
116 |
|
117 return NS_OK; |
|
118 } |
|
119 |
|
120 NS_IMETHODIMP CacheStorage::AsyncEvictStorage(nsICacheEntryDoomCallback* aCallback) |
|
121 { |
|
122 if (!CacheStorageService::Self()) |
|
123 return NS_ERROR_NOT_INITIALIZED; |
|
124 |
|
125 nsresult rv = CacheStorageService::Self()->DoomStorageEntries( |
|
126 this, aCallback); |
|
127 NS_ENSURE_SUCCESS(rv, rv); |
|
128 |
|
129 return NS_OK; |
|
130 } |
|
131 |
|
132 NS_IMETHODIMP CacheStorage::AsyncVisitStorage(nsICacheStorageVisitor* aVisitor, |
|
133 bool aVisitEntries) |
|
134 { |
|
135 LOG(("CacheStorage::AsyncVisitStorage [this=%p, cb=%p, disk=%d]", this, aVisitor, (bool)mWriteToDisk)); |
|
136 if (!CacheStorageService::Self()) |
|
137 return NS_ERROR_NOT_INITIALIZED; |
|
138 |
|
139 nsresult rv = CacheStorageService::Self()->WalkStorageEntries( |
|
140 this, aVisitEntries, aVisitor); |
|
141 NS_ENSURE_SUCCESS(rv, rv); |
|
142 |
|
143 return NS_OK; |
|
144 } |
|
145 |
|
146 // Internal |
|
147 |
|
148 nsresult CacheStorage::ChooseApplicationCache(nsIURI* aURI, |
|
149 nsIApplicationCache** aCache) |
|
150 { |
|
151 nsresult rv; |
|
152 |
|
153 nsCOMPtr<nsIApplicationCacheService> appCacheService = |
|
154 do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv); |
|
155 NS_ENSURE_SUCCESS(rv, rv); |
|
156 |
|
157 nsAutoCString cacheKey; |
|
158 rv = aURI->GetAsciiSpec(cacheKey); |
|
159 NS_ENSURE_SUCCESS(rv, rv); |
|
160 |
|
161 rv = appCacheService->ChooseApplicationCache(cacheKey, LoadInfo(), aCache); |
|
162 NS_ENSURE_SUCCESS(rv, rv); |
|
163 |
|
164 return NS_OK; |
|
165 } |
|
166 |
|
167 } // net |
|
168 } // mozilla |