|
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 "AppCacheStorage.h" |
|
7 #include "CacheStorageService.h" |
|
8 |
|
9 #include "OldWrappers.h" |
|
10 |
|
11 #include "nsICacheEntryDoomCallback.h" |
|
12 |
|
13 #include "nsICacheService.h" |
|
14 #include "nsIApplicationCache.h" |
|
15 #include "nsIApplicationCacheService.h" |
|
16 #include "nsIURI.h" |
|
17 #include "nsNetCID.h" |
|
18 #include "nsServiceManagerUtils.h" |
|
19 #include "nsThreadUtils.h" |
|
20 |
|
21 namespace mozilla { |
|
22 namespace net { |
|
23 |
|
24 NS_IMPL_ISUPPORTS_INHERITED0(AppCacheStorage, CacheStorage) |
|
25 |
|
26 AppCacheStorage::AppCacheStorage(nsILoadContextInfo* aInfo, |
|
27 nsIApplicationCache* aAppCache) |
|
28 : CacheStorage(aInfo, true /* disk */, false /* lookup app cache */) |
|
29 , mAppCache(aAppCache) |
|
30 { |
|
31 MOZ_COUNT_CTOR(AppCacheStorage); |
|
32 } |
|
33 |
|
34 AppCacheStorage::~AppCacheStorage() |
|
35 { |
|
36 ProxyReleaseMainThread(mAppCache); |
|
37 MOZ_COUNT_DTOR(AppCacheStorage); |
|
38 } |
|
39 |
|
40 NS_IMETHODIMP AppCacheStorage::AsyncOpenURI(nsIURI *aURI, |
|
41 const nsACString & aIdExtension, |
|
42 uint32_t aFlags, |
|
43 nsICacheEntryOpenCallback *aCallback) |
|
44 { |
|
45 if (!CacheStorageService::Self()) |
|
46 return NS_ERROR_NOT_INITIALIZED; |
|
47 |
|
48 NS_ENSURE_ARG(aURI); |
|
49 NS_ENSURE_ARG(aCallback); |
|
50 |
|
51 nsresult rv; |
|
52 |
|
53 nsCOMPtr<nsIApplicationCache> appCache = mAppCache; |
|
54 |
|
55 if (!appCache) { |
|
56 rv = ChooseApplicationCache(aURI, getter_AddRefs(appCache)); |
|
57 NS_ENSURE_SUCCESS(rv, rv); |
|
58 } |
|
59 |
|
60 if (!appCache) { |
|
61 LOG(("AppCacheStorage::AsyncOpenURI entry not found in any appcache, giving up")); |
|
62 aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_CACHE_KEY_NOT_FOUND); |
|
63 return NS_OK; |
|
64 } |
|
65 |
|
66 nsCOMPtr<nsIURI> noRefURI; |
|
67 rv = aURI->CloneIgnoringRef(getter_AddRefs(noRefURI)); |
|
68 NS_ENSURE_SUCCESS(rv, rv); |
|
69 |
|
70 nsAutoCString cacheKey; |
|
71 rv = noRefURI->GetAsciiSpec(cacheKey); |
|
72 NS_ENSURE_SUCCESS(rv, rv); |
|
73 |
|
74 nsAutoCString scheme; |
|
75 rv = noRefURI->GetScheme(scheme); |
|
76 NS_ENSURE_SUCCESS(rv, rv); |
|
77 |
|
78 nsRefPtr<_OldCacheLoad> appCacheLoad = |
|
79 new _OldCacheLoad(scheme, cacheKey, aCallback, appCache, |
|
80 LoadInfo(), WriteToDisk(), aFlags); |
|
81 rv = appCacheLoad->Start(); |
|
82 NS_ENSURE_SUCCESS(rv, rv); |
|
83 |
|
84 return NS_OK; |
|
85 } |
|
86 |
|
87 NS_IMETHODIMP AppCacheStorage::AsyncDoomURI(nsIURI *aURI, const nsACString & aIdExtension, |
|
88 nsICacheEntryDoomCallback* aCallback) |
|
89 { |
|
90 if (!CacheStorageService::Self()) |
|
91 return NS_ERROR_NOT_INITIALIZED; |
|
92 |
|
93 if (!mAppCache) { |
|
94 return NS_ERROR_NOT_AVAILABLE; |
|
95 } |
|
96 |
|
97 // TODO - remove entry from app cache |
|
98 // I think no one is using this... |
|
99 return NS_ERROR_NOT_IMPLEMENTED; |
|
100 } |
|
101 |
|
102 NS_IMETHODIMP AppCacheStorage::AsyncEvictStorage(nsICacheEntryDoomCallback* aCallback) |
|
103 { |
|
104 if (!CacheStorageService::Self()) |
|
105 return NS_ERROR_NOT_INITIALIZED; |
|
106 |
|
107 nsresult rv; |
|
108 |
|
109 nsCOMPtr<nsIApplicationCacheService> appCacheService = |
|
110 do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv); |
|
111 NS_ENSURE_SUCCESS(rv, rv); |
|
112 |
|
113 if (!mAppCache) { |
|
114 if (LoadInfo()->AppId() == nsILoadContextInfo::NO_APP_ID && |
|
115 !LoadInfo()->IsInBrowserElement()) { |
|
116 |
|
117 // Clear everything. |
|
118 nsCOMPtr<nsICacheService> serv = |
|
119 do_GetService(NS_CACHESERVICE_CONTRACTID, &rv); |
|
120 NS_ENSURE_SUCCESS(rv, rv); |
|
121 |
|
122 rv = serv->EvictEntries(nsICache::STORE_OFFLINE); |
|
123 NS_ENSURE_SUCCESS(rv, rv); |
|
124 } |
|
125 else { |
|
126 // Clear app or inbrowser staff. |
|
127 rv = appCacheService->DiscardByAppId(LoadInfo()->AppId(), |
|
128 LoadInfo()->IsInBrowserElement()); |
|
129 NS_ENSURE_SUCCESS(rv, rv); |
|
130 } |
|
131 } |
|
132 else { |
|
133 // Discard the group |
|
134 nsAutoCString groupID; |
|
135 rv = mAppCache->GetGroupID(groupID); |
|
136 NS_ENSURE_SUCCESS(rv, rv); |
|
137 |
|
138 rv = appCacheService->DeactivateGroup(groupID); |
|
139 NS_ENSURE_SUCCESS(rv, rv); |
|
140 } |
|
141 |
|
142 if (aCallback) |
|
143 aCallback->OnCacheEntryDoomed(NS_OK); |
|
144 |
|
145 return NS_OK; |
|
146 } |
|
147 |
|
148 NS_IMETHODIMP AppCacheStorage::AsyncVisitStorage(nsICacheStorageVisitor* aVisitor, |
|
149 bool aVisitEntries) |
|
150 { |
|
151 if (!CacheStorageService::Self()) |
|
152 return NS_ERROR_NOT_INITIALIZED; |
|
153 |
|
154 LOG(("AppCacheStorage::AsyncVisitStorage [this=%p, cb=%p]", this, aVisitor)); |
|
155 |
|
156 return NS_ERROR_NOT_IMPLEMENTED; |
|
157 } |
|
158 |
|
159 } // net |
|
160 } // mozilla |