netwerk/cache/nsApplicationCacheService.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     5 #include "nsDiskCache.h"
     6 #include "nsDiskCacheDeviceSQL.h"
     7 #include "nsCacheService.h"
     8 #include "nsApplicationCacheService.h"
     9 #include "nsCRT.h"
    10 #include "nsNetUtil.h"
    11 #include "nsIObserverService.h"
    12 #include "nsILoadContextInfo.h"
    14 using namespace mozilla;
    16 static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
    18 //-----------------------------------------------------------------------------
    19 // nsApplicationCacheService
    20 //-----------------------------------------------------------------------------
    22 NS_IMPL_ISUPPORTS(nsApplicationCacheService, nsIApplicationCacheService)
    24 nsApplicationCacheService::nsApplicationCacheService()
    25 {
    26     nsCOMPtr<nsICacheService> serv = do_GetService(kCacheServiceCID);
    27     mCacheService = nsCacheService::GlobalInstance();
    28 }
    30 NS_IMETHODIMP
    31 nsApplicationCacheService::BuildGroupID(nsIURI *aManifestURL,
    32                                         nsILoadContextInfo *aLoadContextInfo,
    33                                         nsACString &_result)
    34 {
    35     nsresult rv;
    37     uint32_t appId = NECKO_NO_APP_ID;
    38     bool isInBrowserElement = false;
    40     if (aLoadContextInfo) {
    41         appId = aLoadContextInfo->AppId();
    42         isInBrowserElement = aLoadContextInfo->IsInBrowserElement();
    43     }
    45     rv = nsOfflineCacheDevice::BuildApplicationCacheGroupID(
    46         aManifestURL, appId, isInBrowserElement, _result);
    47     NS_ENSURE_SUCCESS(rv, rv);
    49     return NS_OK;
    50 }
    52 NS_IMETHODIMP
    53 nsApplicationCacheService::BuildGroupIDForApp(nsIURI *aManifestURL,
    54                                               uint32_t aAppId,
    55                                               bool aIsInBrowser,
    56                                               nsACString &_result)
    57 {
    58     nsresult rv = nsOfflineCacheDevice::BuildApplicationCacheGroupID(
    59         aManifestURL, aAppId, aIsInBrowser, _result);
    60     NS_ENSURE_SUCCESS(rv, rv);
    62     return NS_OK;
    63 }
    65 NS_IMETHODIMP
    66 nsApplicationCacheService::CreateApplicationCache(const nsACString &group,
    67                                                   nsIApplicationCache **out)
    68 {
    69     if (!mCacheService)
    70         return NS_ERROR_UNEXPECTED;
    72     nsRefPtr<nsOfflineCacheDevice> device;
    73     nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
    74     NS_ENSURE_SUCCESS(rv, rv);
    75     return device->CreateApplicationCache(group, out);
    76 }
    78 NS_IMETHODIMP
    79 nsApplicationCacheService::CreateCustomApplicationCache(const nsACString & group,
    80                                                         nsIFile *profileDir,
    81                                                         int32_t quota,
    82                                                         nsIApplicationCache **out)
    83 {
    84     if (!mCacheService)
    85         return NS_ERROR_UNEXPECTED;
    87     nsRefPtr<nsOfflineCacheDevice> device;
    88     nsresult rv = mCacheService->GetCustomOfflineDevice(profileDir,
    89                                                         quota,
    90                                                         getter_AddRefs(device));
    91     NS_ENSURE_SUCCESS(rv, rv);
    92     return device->CreateApplicationCache(group, out);
    93 }
    95 NS_IMETHODIMP
    96 nsApplicationCacheService::GetApplicationCache(const nsACString &clientID,
    97                                                nsIApplicationCache **out)
    98 {
    99     if (!mCacheService)
   100         return NS_ERROR_UNEXPECTED;
   102     nsRefPtr<nsOfflineCacheDevice> device;
   103     nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
   104     NS_ENSURE_SUCCESS(rv, rv);
   105     return device->GetApplicationCache(clientID, out);
   106 }
   108 NS_IMETHODIMP
   109 nsApplicationCacheService::GetActiveCache(const nsACString &group,
   110                                           nsIApplicationCache **out)
   111 {
   112     if (!mCacheService)
   113         return NS_ERROR_UNEXPECTED;
   115     nsRefPtr<nsOfflineCacheDevice> device;
   116     nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
   117     NS_ENSURE_SUCCESS(rv, rv);
   118     return device->GetActiveCache(group, out);
   119 }
   121 NS_IMETHODIMP
   122 nsApplicationCacheService::DeactivateGroup(const nsACString &group)
   123 {
   124     if (!mCacheService)
   125         return NS_ERROR_UNEXPECTED;
   127     nsRefPtr<nsOfflineCacheDevice> device;
   128     nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
   129     NS_ENSURE_SUCCESS(rv, rv);
   130     return device->DeactivateGroup(group);
   131 }
   133 NS_IMETHODIMP
   134 nsApplicationCacheService::ChooseApplicationCache(const nsACString &key,
   135                                                   nsILoadContextInfo *aLoadContextInfo,
   136                                                   nsIApplicationCache **out)
   137 {
   138     if (!mCacheService)
   139         return NS_ERROR_UNEXPECTED;
   141     nsRefPtr<nsOfflineCacheDevice> device;
   142     nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
   143     NS_ENSURE_SUCCESS(rv, rv);
   145     return device->ChooseApplicationCache(key, aLoadContextInfo, out);
   146 }
   148 NS_IMETHODIMP
   149 nsApplicationCacheService::CacheOpportunistically(nsIApplicationCache* cache,
   150                                                   const nsACString &key)
   151 {
   152     if (!mCacheService)
   153         return NS_ERROR_UNEXPECTED;
   155     nsRefPtr<nsOfflineCacheDevice> device;
   156     nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
   157     NS_ENSURE_SUCCESS(rv, rv);
   158     return device->CacheOpportunistically(cache, key);
   159 }
   161 NS_IMETHODIMP
   162 nsApplicationCacheService::DiscardByAppId(int32_t appID, bool isInBrowser)
   163 {
   164     if (!mCacheService)
   165         return NS_ERROR_UNEXPECTED;
   167     nsRefPtr<nsOfflineCacheDevice> device;
   168     nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
   169     NS_ENSURE_SUCCESS(rv, rv);
   170     return device->DiscardByAppId(appID, isInBrowser);
   171 }
   173 NS_IMETHODIMP
   174 nsApplicationCacheService::GetGroups(uint32_t *count,
   175                                      char ***keys)
   176 {
   177     if (!mCacheService)
   178         return NS_ERROR_UNEXPECTED;
   180     nsRefPtr<nsOfflineCacheDevice> device;
   181     nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
   182     NS_ENSURE_SUCCESS(rv, rv);
   183     return device->GetGroups(count, keys);
   184 }
   186 NS_IMETHODIMP
   187 nsApplicationCacheService::GetGroupsTimeOrdered(uint32_t *count,
   188                                                 char ***keys)
   189 {
   190     if (!mCacheService)
   191         return NS_ERROR_UNEXPECTED;
   193     nsRefPtr<nsOfflineCacheDevice> device;
   194     nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
   195     NS_ENSURE_SUCCESS(rv, rv);
   196     return device->GetGroupsTimeOrdered(count, keys);
   197 }
   199 //-----------------------------------------------------------------------------
   200 // AppCacheClearDataObserver: handles clearing appcache data for app uninstall
   201 // and clearing user data events.
   202 //-----------------------------------------------------------------------------
   204 namespace {
   206 class AppCacheClearDataObserver MOZ_FINAL : public nsIObserver {
   207 public:
   208     NS_DECL_ISUPPORTS
   210     // nsIObserver implementation.
   211     NS_IMETHODIMP
   212     Observe(nsISupports *aSubject, const char *aTopic, const char16_t *aData)
   213     {
   214         MOZ_ASSERT(!nsCRT::strcmp(aTopic, TOPIC_WEB_APP_CLEAR_DATA));
   216         uint32_t appId = NECKO_UNKNOWN_APP_ID;
   217         bool browserOnly = false;
   218         nsresult rv = NS_GetAppInfoFromClearDataNotification(aSubject, &appId,
   219                                                              &browserOnly);
   220         NS_ENSURE_SUCCESS(rv, rv);
   222         nsCOMPtr<nsIApplicationCacheService> cacheService =
   223             do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
   224         NS_ENSURE_SUCCESS(rv, rv);
   226         return cacheService->DiscardByAppId(appId, browserOnly);
   227     }
   228 };
   230 NS_IMPL_ISUPPORTS(AppCacheClearDataObserver, nsIObserver)
   232 } // anonymous namespace
   234 // Instantiates and registers AppCacheClearDataObserver for notifications
   235 void
   236 nsApplicationCacheService::AppClearDataObserverInit()
   237 {
   238     nsCOMPtr<nsIObserverService> observerService =
   239         do_GetService("@mozilla.org/observer-service;1");
   240     if (observerService) {
   241         nsRefPtr<AppCacheClearDataObserver> obs
   242             = new AppCacheClearDataObserver();
   243         observerService->AddObserver(obs, TOPIC_WEB_APP_CLEAR_DATA,
   244                                      /*holdsWeak=*/ false);
   245     }
   246 }

mercurial