nsApplicationCacheService.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "nsDiskCache.h"
  5. #include "nsDiskCacheDeviceSQL.h"
  6. #include "nsCacheService.h"
  7. #include "nsApplicationCacheService.h"
  8. #include "nsCRT.h"
  9. #include "mozIApplicationClearPrivateDataParams.h"
  10. #include "nsNetCID.h"
  11. #include "nsNetUtil.h"
  12. #include "nsIObserverService.h"
  13. #include "nsIPrincipal.h"
  14. #include "mozilla/LoadContextInfo.h"
  15. using namespace mozilla;
  16. static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
  17. //-----------------------------------------------------------------------------
  18. // nsApplicationCacheService
  19. //-----------------------------------------------------------------------------
  20. NS_IMPL_ISUPPORTS(nsApplicationCacheService, nsIApplicationCacheService)
  21. nsApplicationCacheService::nsApplicationCacheService()
  22. {
  23. nsCOMPtr<nsICacheService> serv = do_GetService(kCacheServiceCID);
  24. mCacheService = nsCacheService::GlobalInstance();
  25. }
  26. nsApplicationCacheService::~nsApplicationCacheService()
  27. {
  28. }
  29. NS_IMETHODIMP
  30. nsApplicationCacheService::BuildGroupIDForInfo(
  31. nsIURI *aManifestURL,
  32. nsILoadContextInfo *aLoadContextInfo,
  33. nsACString &_result)
  34. {
  35. nsresult rv;
  36. nsAutoCString originSuffix;
  37. if (aLoadContextInfo) {
  38. aLoadContextInfo->OriginAttributesPtr()->CreateSuffix(originSuffix);
  39. }
  40. rv = nsOfflineCacheDevice::BuildApplicationCacheGroupID(
  41. aManifestURL, originSuffix, _result);
  42. NS_ENSURE_SUCCESS(rv, rv);
  43. return NS_OK;
  44. }
  45. NS_IMETHODIMP
  46. nsApplicationCacheService::BuildGroupIDForSuffix(
  47. nsIURI *aManifestURL,
  48. nsACString const &aOriginSuffix,
  49. nsACString &_result)
  50. {
  51. nsresult rv;
  52. rv = nsOfflineCacheDevice::BuildApplicationCacheGroupID(
  53. aManifestURL, aOriginSuffix, _result);
  54. NS_ENSURE_SUCCESS(rv, rv);
  55. return NS_OK;
  56. }
  57. NS_IMETHODIMP
  58. nsApplicationCacheService::CreateApplicationCache(const nsACString &group,
  59. nsIApplicationCache **out)
  60. {
  61. if (!mCacheService)
  62. return NS_ERROR_UNEXPECTED;
  63. RefPtr<nsOfflineCacheDevice> device;
  64. nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
  65. NS_ENSURE_SUCCESS(rv, rv);
  66. return device->CreateApplicationCache(group, out);
  67. }
  68. NS_IMETHODIMP
  69. nsApplicationCacheService::CreateCustomApplicationCache(const nsACString & group,
  70. nsIFile *profileDir,
  71. int32_t quota,
  72. nsIApplicationCache **out)
  73. {
  74. if (!mCacheService)
  75. return NS_ERROR_UNEXPECTED;
  76. RefPtr<nsOfflineCacheDevice> device;
  77. nsresult rv = mCacheService->GetCustomOfflineDevice(profileDir,
  78. quota,
  79. getter_AddRefs(device));
  80. NS_ENSURE_SUCCESS(rv, rv);
  81. return device->CreateApplicationCache(group, out);
  82. }
  83. NS_IMETHODIMP
  84. nsApplicationCacheService::GetApplicationCache(const nsACString &clientID,
  85. nsIApplicationCache **out)
  86. {
  87. if (!mCacheService)
  88. return NS_ERROR_UNEXPECTED;
  89. RefPtr<nsOfflineCacheDevice> device;
  90. nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
  91. NS_ENSURE_SUCCESS(rv, rv);
  92. return device->GetApplicationCache(clientID, out);
  93. }
  94. NS_IMETHODIMP
  95. nsApplicationCacheService::GetActiveCache(const nsACString &group,
  96. nsIApplicationCache **out)
  97. {
  98. if (!mCacheService)
  99. return NS_ERROR_UNEXPECTED;
  100. RefPtr<nsOfflineCacheDevice> device;
  101. nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
  102. NS_ENSURE_SUCCESS(rv, rv);
  103. return device->GetActiveCache(group, out);
  104. }
  105. NS_IMETHODIMP
  106. nsApplicationCacheService::DeactivateGroup(const nsACString &group)
  107. {
  108. if (!mCacheService)
  109. return NS_ERROR_UNEXPECTED;
  110. RefPtr<nsOfflineCacheDevice> device;
  111. nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
  112. NS_ENSURE_SUCCESS(rv, rv);
  113. return device->DeactivateGroup(group);
  114. }
  115. NS_IMETHODIMP
  116. nsApplicationCacheService::ChooseApplicationCache(const nsACString &key,
  117. nsILoadContextInfo *aLoadContextInfo,
  118. nsIApplicationCache **out)
  119. {
  120. if (!mCacheService)
  121. return NS_ERROR_UNEXPECTED;
  122. RefPtr<nsOfflineCacheDevice> device;
  123. nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
  124. NS_ENSURE_SUCCESS(rv, rv);
  125. return device->ChooseApplicationCache(key, aLoadContextInfo, out);
  126. }
  127. NS_IMETHODIMP
  128. nsApplicationCacheService::CacheOpportunistically(nsIApplicationCache* cache,
  129. const nsACString &key)
  130. {
  131. if (!mCacheService)
  132. return NS_ERROR_UNEXPECTED;
  133. RefPtr<nsOfflineCacheDevice> device;
  134. nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
  135. NS_ENSURE_SUCCESS(rv, rv);
  136. return device->CacheOpportunistically(cache, key);
  137. }
  138. NS_IMETHODIMP
  139. nsApplicationCacheService::Evict(nsILoadContextInfo *aInfo)
  140. {
  141. if (!mCacheService)
  142. return NS_ERROR_UNEXPECTED;
  143. RefPtr<nsOfflineCacheDevice> device;
  144. nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
  145. NS_ENSURE_SUCCESS(rv, rv);
  146. return device->Evict(aInfo);
  147. }
  148. NS_IMETHODIMP
  149. nsApplicationCacheService::EvictMatchingOriginAttributes(nsAString const &aPattern)
  150. {
  151. if (!mCacheService)
  152. return NS_ERROR_UNEXPECTED;
  153. RefPtr<nsOfflineCacheDevice> device;
  154. nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
  155. NS_ENSURE_SUCCESS(rv, rv);
  156. mozilla::OriginAttributesPattern pattern;
  157. if (!pattern.Init(aPattern)) {
  158. NS_ERROR("Could not parse OriginAttributesPattern JSON in clear-origin-attributes-data notification");
  159. return NS_ERROR_FAILURE;
  160. }
  161. return device->Evict(pattern);
  162. }
  163. NS_IMETHODIMP
  164. nsApplicationCacheService::GetGroups(uint32_t *count,
  165. char ***keys)
  166. {
  167. if (!mCacheService)
  168. return NS_ERROR_UNEXPECTED;
  169. RefPtr<nsOfflineCacheDevice> device;
  170. nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
  171. NS_ENSURE_SUCCESS(rv, rv);
  172. return device->GetGroups(count, keys);
  173. }
  174. NS_IMETHODIMP
  175. nsApplicationCacheService::GetGroupsTimeOrdered(uint32_t *count,
  176. char ***keys)
  177. {
  178. if (!mCacheService)
  179. return NS_ERROR_UNEXPECTED;
  180. RefPtr<nsOfflineCacheDevice> device;
  181. nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
  182. NS_ENSURE_SUCCESS(rv, rv);
  183. return device->GetGroupsTimeOrdered(count, keys);
  184. }
  185. //-----------------------------------------------------------------------------
  186. // AppCacheClearDataObserver: handles clearing appcache data for app uninstall
  187. // and clearing user data events.
  188. //-----------------------------------------------------------------------------
  189. namespace {
  190. class AppCacheClearDataObserver final : public nsIObserver {
  191. public:
  192. NS_DECL_ISUPPORTS
  193. // nsIObserver implementation.
  194. NS_IMETHOD
  195. Observe(nsISupports *aSubject, const char *aTopic, const char16_t *aData) override
  196. {
  197. MOZ_ASSERT(!nsCRT::strcmp(aTopic, "clear-origin-attributes-data"));
  198. nsresult rv;
  199. nsCOMPtr<nsIApplicationCacheService> cacheService =
  200. do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
  201. NS_ENSURE_SUCCESS(rv, rv);
  202. return cacheService->EvictMatchingOriginAttributes(nsDependentString(aData));
  203. }
  204. private:
  205. ~AppCacheClearDataObserver() {}
  206. };
  207. NS_IMPL_ISUPPORTS(AppCacheClearDataObserver, nsIObserver)
  208. } // namespace
  209. // Instantiates and registers AppCacheClearDataObserver for notifications
  210. void
  211. nsApplicationCacheService::AppClearDataObserverInit()
  212. {
  213. nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
  214. if (observerService) {
  215. RefPtr<AppCacheClearDataObserver> obs = new AppCacheClearDataObserver();
  216. observerService->AddObserver(obs, "clear-origin-attributes-data", /*ownsWeak=*/ false);
  217. }
  218. }