Bug 829043 - Separate wyciwyg cache into app jars. r=michal.novotny
☠☠ backed out by c1b3fd0a0195 ☠ ☠
authorJosh Matthews <josh@joshmatthews.net>
Thu, 10 Jan 2013 21:58:47 +0100
changeset 118462 b99c87fb77d33c6bb40262680a7b86aabf71cfd1
parent 118461 7e4917d358c50cc8e8ec069887c1daeb8c9b9f4a
child 118463 4fb3f83287fb7ebbe315985dc7a9d09641a686dc
push id24166
push userMs2ger@gmail.com
push dateFri, 11 Jan 2013 13:57:41 +0000
treeherdermozilla-central@63c4b0f66a0c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmichal.novotny
bugs829043
milestone21.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 829043 - Separate wyciwyg cache into app jars. r=michal.novotny
netwerk/build/nsNetModule.cpp
netwerk/protocol/http/nsHttpChannel.cpp
netwerk/protocol/wyciwyg/nsWyciwygChannel.cpp
netwerk/protocol/wyciwyg/nsWyciwygProtocolHandler.cpp
netwerk/protocol/wyciwyg/nsWyciwygProtocolHandler.h
--- a/netwerk/build/nsNetModule.cpp
+++ b/netwerk/build/nsNetModule.cpp
@@ -265,17 +265,17 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsViewSou
 #endif
 
 #ifdef NECKO_PROTOCOL_data
 #include "nsDataHandler.h"
 #endif
 
 #ifdef NECKO_PROTOCOL_wyciwyg
 #include "nsWyciwygProtocolHandler.h"
-NS_GENERIC_FACTORY_CONSTRUCTOR(nsWyciwygProtocolHandler)
+NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsWyciwygProtocolHandler, Init)
 #endif
 
 #ifdef NECKO_PROTOCOL_websocket
 #include "WebSocketChannel.h"
 #include "WebSocketChannelChild.h"
 namespace mozilla {
 namespace net {
 static BaseWebSocketChannel*
--- a/netwerk/protocol/http/nsHttpChannel.cpp
+++ b/netwerk/protocol/http/nsHttpChannel.cpp
@@ -2463,39 +2463,26 @@ nsHttpChannel::OnOfflineCacheEntryAvaila
         }
     }
 
     bool usingSSL = false;
     (void) mURI->SchemeIs("https", &usingSSL);
     return OpenNormalCacheEntry(usingSSL);
 }
 
-static void
-GetAppInfo(nsIChannel* aChannel, uint32_t* aAppId, bool* aIsInBrowser)
-{
-    nsCOMPtr<nsILoadContext> loadContext;
-    NS_QueryNotificationCallbacks(aChannel, loadContext);
-    *aAppId = NECKO_NO_APP_ID;
-    *aIsInBrowser = false;
-    if (loadContext) {
-        loadContext->GetAppId(aAppId);
-        loadContext->GetIsInBrowserElement(aIsInBrowser);
-    }
-}
-
 nsresult
 nsHttpChannel::OpenNormalCacheEntry(bool usingSSL)
 {
     NS_ASSERTION(!mCacheEntry, "We have already mCacheEntry");
 
     nsresult rv;
 
-    uint32_t appId;
-    bool isInBrowser;
-    GetAppInfo(this, &appId, &isInBrowser);
+    uint32_t appId = NECKO_NO_APP_ID;
+    bool isInBrowser = false;
+    NS_GetAppInfo(this, &appId, &isInBrowser);
 
     nsCacheStoragePolicy storagePolicy = DetermineStoragePolicy();
     nsAutoCString clientID;
     nsHttpHandler::GetCacheSessionNameForStoragePolicy(storagePolicy, mPrivateBrowsing,
                                                        appId, isInBrowser, clientID);
 
     nsAutoCString cacheKey;
     GenerateCacheKey(mPostID, cacheKey);
@@ -5856,19 +5843,19 @@ void
 nsHttpChannel::DoInvalidateCacheEntry(const nsCString &key)
 {
     // NOTE:
     // Following comments 24,32 and 33 in bug #327765, we only care about
     // the cache in the protocol-handler, not the application cache.
     // The logic below deviates from the original logic in OpenCacheEntry on
     // one point by using only READ_ONLY access-policy. I think this is safe.
 
-    uint32_t appId;
-    bool isInBrowser;
-    GetAppInfo(this, &appId, &isInBrowser);
+    uint32_t appId = NECKO_NO_APP_ID;
+    bool isInBrowser = false;
+    NS_GetAppInfo(this, &appId, &isInBrowser);
 
     // First, find session holding the cache-entry - use current storage-policy
     nsCacheStoragePolicy storagePolicy = DetermineStoragePolicy();
     nsAutoCString clientID;
     nsHttpHandler::GetCacheSessionNameForStoragePolicy(storagePolicy, mPrivateBrowsing,
                                                        appId, isInBrowser, clientID);
 
     LOG(("DoInvalidateCacheEntry [channel=%p session=%s policy=%d key=%s]",
--- a/netwerk/protocol/wyciwyg/nsWyciwygChannel.cpp
+++ b/netwerk/protocol/wyciwyg/nsWyciwygChannel.cpp
@@ -687,20 +687,28 @@ nsWyciwygChannel::OpenCacheEntry(const n
 
   // honor security settings
   nsCacheStoragePolicy storagePolicy;
   if (mPrivateBrowsing || mLoadFlags & INHIBIT_PERSISTENT_CACHING)
     storagePolicy = nsICache::STORE_IN_MEMORY;
   else
     storagePolicy = nsICache::STORE_ANYWHERE;
 
+  uint32_t appId = NECKO_NO_APP_ID;
+  bool isInBrowser = false;
+  NS_GetAppInfo(this, &appId, &isInBrowser);
+
   nsCOMPtr<nsICacheSession> cacheSession;
+  nsAutoCString sessionName;
+  nsWyciwygProtocolHandler::GetCacheSessionName(appId, isInBrowser,
+                                                mPrivateBrowsing,
+                                                sessionName);
+
   // Open a stream based cache session.
-  const char* sessionName = mPrivateBrowsing ? "wyciwyg-private" : "wyciwyg";
-  rv = cacheService->CreateSession(sessionName, storagePolicy, true,
+  rv = cacheService->CreateSession(sessionName.get(), storagePolicy, true,
                                    getter_AddRefs(cacheSession));
   if (!cacheSession) 
     return NS_ERROR_FAILURE;
 
   cacheSession->SetIsPrivate(mPrivateBrowsing);
 
   if (aAccessMode == nsICache::ACCESS_WRITE)
     rv = cacheSession->OpenCacheEntry(aCacheKey, aAccessMode, false,
--- a/netwerk/protocol/wyciwyg/nsWyciwygProtocolHandler.cpp
+++ b/netwerk/protocol/wyciwyg/nsWyciwygProtocolHandler.cpp
@@ -8,16 +8,20 @@
 #include "nsWyciwygChannel.h"
 #include "nsWyciwygProtocolHandler.h"
 #include "nsIURL.h"
 #include "nsIComponentManager.h"
 #include "nsNetCID.h"
 #include "nsServiceManagerUtils.h"
 #include "plstr.h"
 #include "nsNetUtil.h"
+#include "nsIObserverService.h"
+#include "mozIApplicationClearPrivateDataParams.h"
+#include "nsICacheService.h"
+#include "nsICacheSession.h"
 
 #include "mozilla/net/NeckoChild.h"
 
 using namespace mozilla::net;
 #include "mozilla/net/WyciwygChannelChild.h"
 
 ////////////////////////////////////////////////////////////////////////////////
 
@@ -31,17 +35,104 @@ nsWyciwygProtocolHandler::nsWyciwygProto
   LOG(("Creating nsWyciwygProtocolHandler [this=%x].\n", this));
 }
 
 nsWyciwygProtocolHandler::~nsWyciwygProtocolHandler() 
 {
   LOG(("Deleting nsWyciwygProtocolHandler [this=%x]\n", this));
 }
 
-NS_IMPL_ISUPPORTS1(nsWyciwygProtocolHandler, nsIProtocolHandler)
+nsresult
+nsWyciwygProtocolHandler::Init()
+{
+  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
+  if (obs) {
+    obs->AddObserver(this, "webapps-clear-data", true);
+  }
+  return NS_OK;
+}
+
+static void
+EvictCacheSession(uint32_t aAppId,
+                  bool aInBrowser,
+                  bool aPrivateBrowsing)
+{
+  nsAutoCString clientId;
+  nsWyciwygProtocolHandler::GetCacheSessionName(aAppId, aInBrowser,
+                                                aPrivateBrowsing,
+                                                clientId);
+  nsCOMPtr<nsICacheService> serv =
+      do_GetService(NS_CACHESERVICE_CONTRACTID);
+  nsCOMPtr<nsICacheSession> session;
+  nsresult rv = serv->CreateSession(clientId.get(),
+                                    nsICache::STORE_ANYWHERE,
+                                    nsICache::STREAM_BASED,
+                                    getter_AddRefs(session));
+  if (NS_SUCCEEDED(rv) && session) {
+    session->EvictEntries();
+  }
+}
+
+void
+nsWyciwygProtocolHandler::GetCacheSessionName(uint32_t aAppId,
+                                              bool aInBrowser,
+                                              bool aPrivateBrowsing,
+                                              nsACString& aSessionName)
+{
+  if (aPrivateBrowsing) {
+    aSessionName.AssignLiteral("wyciwyg-private");
+  } else {
+    aSessionName.AssignLiteral("wyciwyg");
+  }
+  if (aAppId == NECKO_NO_APP_ID && !aInBrowser) {
+    return;
+  }
+
+  aSessionName.Append('~');
+  aSessionName.AppendInt(aAppId);
+  aSessionName.Append('~');
+  aSessionName.AppendInt(aInBrowser);
+}
+
+NS_IMETHODIMP
+nsWyciwygProtocolHandler::Observe(nsISupports *subject,
+                                  const char *topic,
+                                  const PRUnichar *data)
+{
+  if (strcmp(topic, "webapps-clear-data") == 0) {
+    nsCOMPtr<mozIApplicationClearPrivateDataParams> params =
+        do_QueryInterface(subject);
+    if (!params) {
+      NS_ERROR("'webapps-clear-data' notification's subject should be a mozIApplicationClearPrivateDataParams");
+      return NS_ERROR_UNEXPECTED;
+    }
+
+    uint32_t appId;
+    bool browserOnly;
+    nsresult rv = params->GetAppId(&appId);
+    NS_ENSURE_SUCCESS(rv, rv);
+    rv = params->GetBrowserOnly(&browserOnly);
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    MOZ_ASSERT(appId != NECKO_UNKNOWN_APP_ID);
+
+    EvictCacheSession(appId, browserOnly, false);
+    EvictCacheSession(appId, browserOnly, true);
+    if (!browserOnly) {
+      EvictCacheSession(appId, true, false);
+      EvictCacheSession(appId, true, true);
+    }
+  }
+  return NS_OK;
+}
+
+NS_IMPL_ISUPPORTS3(nsWyciwygProtocolHandler,
+                   nsIProtocolHandler,
+                   nsIObserver,
+                   nsISupportsWeakReference)
 
 ////////////////////////////////////////////////////////////////////////////////
 // nsIProtocolHandler methods:
 ////////////////////////////////////////////////////////////////////////////////
 
 NS_IMETHODIMP
 nsWyciwygProtocolHandler::GetScheme(nsACString &result)
 {
--- a/netwerk/protocol/wyciwyg/nsWyciwygProtocolHandler.h
+++ b/netwerk/protocol/wyciwyg/nsWyciwygProtocolHandler.h
@@ -3,21 +3,32 @@
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsWyciwygProtocolHandler_h___
 #define nsWyciwygProtocolHandler_h___
 
 #include "nsIProtocolHandler.h"
-
+#include "nsIObserver.h"
+#include "nsWeakReference.h"
             
 class nsWyciwygProtocolHandler : public nsIProtocolHandler
+                               , public nsIObserver
+                               , public nsSupportsWeakReference
 {
 public:
     NS_DECL_ISUPPORTS
     NS_DECL_NSIPROTOCOLHANDLER
+    NS_DECL_NSIOBSERVER
 
     nsWyciwygProtocolHandler();
     virtual ~nsWyciwygProtocolHandler();
+
+    nsresult Init();
+
+    static void GetCacheSessionName(uint32_t aAppId,
+                                    bool aInBrowser,
+                                    bool aPrivateBrowsing,
+                                    nsACString& aSessionName);
 };
 
 #endif /* nsWyciwygProtocolHandler_h___ */