Bug 405695: Use asciiHost/asciiSpec for offline cache ownership keys. p=Honza Bambas, r=me, sr=biesi
authordcamp@mozilla.com
Tue, 22 Jan 2008 20:31:50 -0800
changeset 10564 de87fc326c1270157451b6b283986b40b2220bbb
parent 10563 25b6bca5517db621925b6f6b90b2128aba9a3c59
child 10565 4ba1e59d2ea4f81cb96db8874ee5982b09cd7a89
push idunknown
push userunknown
push dateunknown
reviewersme, biesi
bugs405695
milestone1.9b3pre
Bug 405695: Use asciiHost/asciiSpec for offline cache ownership keys. p=Honza Bambas, r=me, sr=biesi
dom/src/offline/nsDOMOfflineResourceList.cpp
dom/src/offline/nsDOMOfflineResourceList.h
dom/tests/mochitest/ajax/offline/offlineTests.js
netwerk/cache/public/nsIOfflineCacheSession.idl
uriloader/prefetch/nsOfflineCacheUpdate.cpp
--- a/dom/src/offline/nsDOMOfflineResourceList.cpp
+++ b/dom/src/offline/nsDOMOfflineResourceList.cpp
@@ -68,17 +68,17 @@
 
 // To prevent abuse of the resource list for data storage, the number
 // of offline urls and their length are limited.
 
 static const char kMaxEntriesPref[] =  "offline.max_site_resources";
 #define DEFAULT_MAX_ENTRIES 100
 #define MAX_URI_LENGTH 2048
 
-static nsCAutoString gCachedHostPort;
+static nsCAutoString gCachedAsciiHost;
 static char **gCachedKeys = nsnull;
 static PRUint32 gCachedKeysCount = 0;
 
 //
 // nsDOMOfflineResourceList
 //
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMOfflineResourceList)
@@ -182,26 +182,26 @@ nsDOMOfflineResourceList::Init()
   }
 
   nsresult rv = nsContentUtils::GetSecurityManager()->
                    CheckSameOriginURI(mManifestURI, mDocumentURI, PR_TRUE);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Dynamically-managed resources are stored as a separate ownership list
   // from the manifest.
-  rv = mManifestURI->GetSpec(mDynamicOwnerSpec);
+  rv = mManifestURI->GetAsciiSpec(mDynamicOwnerSpec);
   NS_ENSURE_SUCCESS(rv, rv);
 
   mDynamicOwnerSpec.Append("#dynamic");
 
   nsCOMPtr<nsIURI> innerURI = NS_GetInnermostURI(mDocumentURI);
   if (!innerURI)
     return NS_ERROR_FAILURE;
 
-  rv = innerURI->GetHostPort(mHostPort);
+  rv = innerURI->GetAsciiHost(mAsciiHost);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsICacheService> serv = do_GetService(NS_CACHESERVICE_CONTRACTID,
                                                  &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsICacheSession> session;
   rv = serv->CreateSession("HTTP-offline",
@@ -378,17 +378,17 @@ nsDOMOfflineResourceList::Remove(const n
   }
 
   nsCAutoString key;
   rv = GetCacheKey(aURI, key);
   NS_ENSURE_SUCCESS(rv, rv);
 
   ClearCachedKeys();
 
-  rv = mCacheSession->RemoveOwnedKey(mHostPort, mDynamicOwnerSpec, key);
+  rv = mCacheSession->RemoveOwnedKey(mAsciiHost, mDynamicOwnerSpec, key);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = mCacheSession->EvictUnownedEntries();
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
@@ -964,17 +964,17 @@ nsDOMOfflineResourceList::UpdateComplete
   }
 
   return NS_OK;
 }
 
 nsresult
 nsDOMOfflineResourceList::GetCacheKey(nsIURI *aURI, nsCString &aKey)
 {
-  nsresult rv = aURI->GetSpec(aKey);
+  nsresult rv = aURI->GetAsciiSpec(aKey);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // url fragments aren't used in cache keys
   nsCAutoString::const_iterator specStart, specEnd;
   aKey.BeginReading(specStart);
   aKey.EndReading(specEnd);
   if (FindCharInReadable('#', specStart, specEnd)) {
     aKey.BeginReading(specEnd);
@@ -982,36 +982,36 @@ nsDOMOfflineResourceList::GetCacheKey(ns
   }
 
   return NS_OK;
 }
 
 nsresult
 nsDOMOfflineResourceList::CacheKeys()
 {
-  if (gCachedKeys && mHostPort == gCachedHostPort)
+  if (gCachedKeys && mAsciiHost == gCachedAsciiHost)
     return NS_OK;
 
   ClearCachedKeys();
 
-  nsresult rv = mCacheSession->GetOwnedKeys(mHostPort, mDynamicOwnerSpec,
+  nsresult rv = mCacheSession->GetOwnedKeys(mAsciiHost, mDynamicOwnerSpec,
                                             &gCachedKeysCount, &gCachedKeys);
 
   if (NS_SUCCEEDED(rv))
-    gCachedHostPort = mHostPort;
+    gCachedAsciiHost = mAsciiHost;
 
   return rv;
 }
 
 void
 nsDOMOfflineResourceList::ClearCachedKeys()
 {
   if (gCachedKeys) {
     NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(gCachedKeysCount, gCachedKeys);
     gCachedKeys = nsnull;
     gCachedKeysCount = 0;
   }
 
-  gCachedHostPort = "";
+  gCachedAsciiHost = "";
 }
 
 
 
--- a/dom/src/offline/nsDOMOfflineResourceList.h
+++ b/dom/src/offline/nsDOMOfflineResourceList.h
@@ -105,17 +105,17 @@ private:
 
   PRBool mInitialized;
   PRBool mToplevel;
   nsCOMPtr<nsIURI> mManifestURI;
   nsCOMPtr<nsIURI> mDocumentURI;
   nsCOMPtr<nsIWeakReference> mWindow;
   nsCOMPtr<nsIOfflineCacheSession> mCacheSession;
   nsCOMPtr<nsIOfflineCacheUpdate> mCacheUpdate;
-  nsCAutoString mHostPort;
+  nsCAutoString mAsciiHost;
   nsCAutoString mDynamicOwnerSpec;
 
   nsCOMArray<nsIDOMEventListener> mCheckingListeners;
   nsCOMArray<nsIDOMEventListener> mErrorListeners;
   nsCOMArray<nsIDOMEventListener> mNoUpdateListeners;
   nsCOMArray<nsIDOMEventListener> mDownloadingListeners;
   nsCOMArray<nsIDOMEventListener> mProgressListeners;
   nsCOMArray<nsIDOMEventListener> mCachedListeners;
--- a/dom/tests/mochitest/ajax/offline/offlineTests.js
+++ b/dom/tests/mochitest/ajax/offline/offlineTests.js
@@ -108,22 +108,28 @@ clear: function()
   // Clear the ownership list
   var cacheService = Cc["@mozilla.org/network/cache-service;1"]
                      .getService(Ci.nsICacheService);
   var cacheSession = cacheService.createSession("HTTP-offline",
                                                 Ci.nsICache.STORE_OFFLINE,
                                                 true)
                      .QueryInterface(Ci.nsIOfflineCacheSession);
 
+  // Get the asciiHost from the page URL
+  var locationURI = Cc["@mozilla.org/network/standard-url;1"]
+                     .createInstance(Ci.nsIURI);
+  locationURI.spec = window.location.href;
+  var asciiHost = locationURI.asciiHost;
+
   // Clear manifest-owned urls
-  cacheSession.setOwnedKeys(window.location.host,
+  cacheSession.setOwnedKeys(asciiHost,
                             this.getManifestUrl() + "#manifest", 0, []);
 
   // Clear dynamically-owned urls
-  cacheSession.setOwnedKeys(window.location.host,
+  cacheSession.setOwnedKeys(asciiHost,
                             this.getManifestUrl() + "#dynamic", 0, []);
 
   cacheSession.evictUnownedEntries();
 },
 
 failEvent: function(e)
 {
   OfflineTest.ok(false, "Unexpected event: " + e.type);
--- a/netwerk/cache/public/nsIOfflineCacheSession.idl
+++ b/netwerk/cache/public/nsIOfflineCacheSession.idl
@@ -75,128 +75,141 @@ interface nsIOfflineCacheSession : nsISu
      *        The domains that own resources in the cache
      */
     void getOwnerDomains(out unsigned long count,
                          [array, size_is(count)]out string domains);
 
     /**
      * Gets the list of owner URIs associated with a domain.
      *
-     * @param ownerDomain
+     * @param ownerAsciiDomain
      *        The domain to query
+     *        !! IMPORTANT !! : This must be ascii encoded host - nsIURI.asciiHost
      * @param count
      *        The number of uris returned
      * @param uris
      *        The uris in this domain that own resources
      */
-    void getOwnerURIs(in ACString ownerDomain,
+    void getOwnerURIs(in ACString ownerAsciiDomain,
                       out unsigned long count,
                       [array, size_is(count)]out string uris);
 
     /**
      * Sets the resources owned by a given domain/URI pair.
      *
      * Setting a list will remove any resources previously owned by this
      * domain/URI pair.
      *
      * A key can be added while there is no associated entry.  When
      * an entry is created with this key, it will be owned by the
      * domain/URI pair.
      *
-     * @param ownerDomain
-     *        The domain that owns the resources.
-     * @param ownerURI
-     *        The specific URI that owns the resources.  This can be empty if
-     *        no URI specifically owns the resources.
+     * @param ownerAsciiDomain
+     *        The domain that owns the resources
+     *        !! IMPORTANT !! : This must be ascii encoded host - nsIURI.asciiHost
+     * @param ownerAsciiKey
+     *        The specific key that owns the resources.  You may use 
+     *        ascii encoded URI spec of the owner - nsIURI.asciiSpec.
+     *        This can be empty if none specifically owns the resources.
      * @param count
      *        The number of keys in keys.
      * @param keys
      *        The keys that the domain/URI pair own.  This can be empty to
      *        clear ownership for the domain/URI pair.
      */
-    void setOwnedKeys(in ACString ownerDomain,
-                      in ACString ownerURI,
+    void setOwnedKeys(in ACString ownerAsciiDomain,
+                      in ACString ownerAsciiKey,
                       in unsigned long count,
                       [array, size_is(count)]in string keys);
 
     /**
      * Gets the list of resources owned by a given domain/URI pair.
      *
-     * @param ownerDomain
-     *        The domain that owns the resources.
-     * @param ownerURI
-     *        The specific URI that owns the resources.  This can be empty
-     *        if no URI specifically owns the resources.
+     * @param ownerAsciiDomain
+     *        The domain that owns the resources
+     *        !! IMPORTANT !! : This must be ascii encoded host - nsIURI.asciiHost
+     * @param ownerAsciiKey
+     *        The specific key that owns the resources.  You may use 
+     *        ascii encoded URI spec of the owner - nsIURI.asciiSpec.
+     *        This can be empty if none specifically owns the resources.
      * @param count
      *        The number of keys in keys.
      * @param keys
      *        The keys that the domain/URI pair own.
      */
-    void getOwnedKeys(in ACString ownerDomain,
-                      in ACString ownerURI,
+    void getOwnedKeys(in ACString ownerAsciiDomain,
+                      in ACString ownerAsciiKey,
                       out unsigned long count,
                       [array, size_is(count)]out string keys);
 
     /**
      * Adds an owned key to a domain/URI pair.
      *
      * A key can be added while there is no associated entry.  When
      * an entry is created with this key, it will be owned by the
      * domain/URI pair.
      *
-     * @param ownerDomain
-     *        The domain that owns the resources.
-     * @param ownerURI
-     *        The specific URI that owns the resources.  This can be empty
-     *        if no URI specifically owns the resources.
+     * @param ownerAsciiDomain
+     *        The domain that owns the resources
+     *        !! IMPORTANT !! : This must be ascii encoded host - nsIURI.asciiHost
+     * @param ownerAsciiKey
+     *        The specific key that owns the resources.  You may use 
+     *        ascii encoded URI spec of the owner - nsIURI.asciiSpec.
+     *        This can be empty if none specifically owns the resources.
      * @param key
      *        The key to add.
      */
-    void addOwnedKey(in ACString ownerDomain,
-                     in ACString ownerURI,
+    void addOwnedKey(in ACString ownerAsciiDomain,
+                     in ACString ownerAsciiKey,
                      in ACString key);
 
     /**
      * Removes an owned key from a domain/URI pair.
      *
      * If the key does not exist, an NS_ERROR_NOT_AVAILABLE exception
      * will be thrown.
      *
-     * @param ownerDomain
-     *        The domain that owns the resources.
-     * @param ownerURI
-     *        The specific URI that owns the resources.  This can be empty
-     *        if no URI specifically owns the resources.
+     * @param ownerAsciiDomain
+     *        The domain that owns the resources
+     *        !! IMPORTANT !! : This must be ascii encoded host - nsIURI.asciiHost
+     * @param ownerAsciiKey
+     *        The specific key that owns the resources.  You may use 
+     *        ascii encoded URI spec of the owner - nsIURI.asciiSpec.
+     *        This can be empty if none specifically owns the resources.
      * @param key The key to remove.
      */
-    void removeOwnedKey(in ACString ownerDomain,
-                        in ACString ownerURI,
+    void removeOwnedKey(in ACString ownerAsciiDomain,
+                        in ACString ownerAsciiKey,
                         in ACString key);
 
     /**
      * Checks whether a key is owned by a given domain/URI pair.
      *
-     * @param ownerDomain
-     *        The domain that owns the resources.
-     * @param ownerURI
-     *        The specific URI that owns the resources.  This can be empty
-     *        if no URI specifically owns the resources.
+     * @param ownerAsciiDomain
+     *        The domain that owns the resources
+     *        !! IMPORTANT !! : This must be ascii encoded host - nsIURI.asciiHost
+     * @param ownerAsciiKey
+     *        The specific key that owns the resources.  You may use 
+     *        ascii encoded URI spec of the owner - nsIURI.asciiSpec.
+     *        This can be empty if none specifically owns the resources.
      * @param key The key to check
      */
-    boolean keyIsOwned(in ACString ownerDomain,
-                       in ACString ownerURI,
+    boolean keyIsOwned(in ACString ownerAsciiDomain,
+                       in ACString ownerAsciiKey,
                        in ACString key);
 
     /**
      * Remove all keys owned by a domain, including keys owned by
      * a specific URI.
      *
-     * @param domain The domain for which keys should be removed.
+     * @param ownerAsciiDomain
+     *        The domain for which keys should be removed
+     *        !! IMPORTANT !! : This must be ascii encoded host - nsIURI.asciiHost
      */
-    void clearKeysOwnedByDomain(in ACString ownerDomain);
+    void clearKeysOwnedByDomain(in ACString ownerAsciiDomain);
 
     /**
      * Evict all entries that are not owned by a domain.
      */
     void evictUnownedEntries();
 
     /**
      * Merge the items from a temporary clientID in to this client.  This lets
--- a/uriloader/prefetch/nsOfflineCacheUpdate.cpp
+++ b/uriloader/prefetch/nsOfflineCacheUpdate.cpp
@@ -681,22 +681,22 @@ nsOfflineCacheUpdate::Init(PRBool aParti
         rv = aManifestURI->SchemeIs("https", &match);
         NS_ENSURE_SUCCESS(rv, rv);
         if (!match)
             return NS_ERROR_ABORT;
     }
 
     mManifestURI = aManifestURI;
 
-    rv = mManifestURI->GetHostPort(mUpdateDomain);
+    rv = mManifestURI->GetAsciiHost(mUpdateDomain);
     NS_ENSURE_SUCCESS(rv, rv);
 
     nsCAutoString manifestSpec;
 
-    rv = mManifestURI->GetSpec(manifestSpec);
+    rv = mManifestURI->GetAsciiSpec(manifestSpec);
     NS_ENSURE_SUCCESS(rv, rv);
 
     PRInt32 ref = manifestSpec.FindChar('#');
     if (ref != kNotFound)
         manifestSpec.Truncate(ref);
 
     mManifestOwnerSpec = manifestSpec;
     mManifestOwnerSpec.AppendLiteral("#manifest");