Bug 1509132 - Inform the 3rd party tracker window when the storage permission is granted, r=ehsan
authorAndrea Marchesini <amarchesini@mozilla.com>
Fri, 23 Nov 2018 08:12:02 +0100
changeset 504227 5f77d9f3460c8ab31c35326e9ef079aee758ef35
parent 504226 8810195a2a42d9622f020cf704b8025396d0f139
child 504228 c2593a3058afdfeaac5c990e18794ee8257afe99
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs1509132
milestone65.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 1509132 - Inform the 3rd party tracker window when the storage permission is granted, r=ehsan This is needed for the LocalStorage Next-Generation project which sends storage notifications only to content processes with an existing LocalStorage object. I cannot write a test for this, because the current localStorage all works fine.
dom/base/nsGlobalWindowInner.cpp
dom/base/nsGlobalWindowInner.h
toolkit/components/antitracking/AntiTrackingCommon.cpp
--- a/dom/base/nsGlobalWindowInner.cpp
+++ b/dom/base/nsGlobalWindowInner.cpp
@@ -7981,16 +7981,35 @@ void
 nsGlobalWindowInner::ForgetSharedWorker(SharedWorker* aSharedWorker)
 {
   MOZ_ASSERT(aSharedWorker);
   MOZ_ASSERT(mSharedWorkers.Contains(aSharedWorker));
 
   mSharedWorkers.RemoveElement(aSharedWorker);
 }
 
+void
+nsGlobalWindowInner::StorageAccessGranted()
+{
+  // If we have a partitioned localStorage, it's time to replace it with a real
+  // one in order to receive notifications.
+
+  if (mLocalStorage &&
+      mLocalStorage->Type() == Storage::ePartitionedLocalStorage) {
+    IgnoredErrorResult error;
+    GetLocalStorage(error);
+    if (NS_WARN_IF(error.Failed())) {
+      return;
+    }
+
+    MOZ_ASSERT(mLocalStorage &&
+               mLocalStorage->Type() == Storage::eLocalStorage);
+  }
+}
+
 mozilla::dom::TabGroup*
 nsPIDOMWindowInner::TabGroup()
 {
   return nsGlobalWindowInner::Cast(this)->TabGroupInner();
 }
 
 /* static */ already_AddRefed<nsGlobalWindowInner>
 nsGlobalWindowInner::Create(nsGlobalWindowOuter *aOuterWindow, bool aIsChrome)
--- a/dom/base/nsGlobalWindowInner.h
+++ b/dom/base/nsGlobalWindowInner.h
@@ -1236,16 +1236,21 @@ public:
   // Get the toplevel principal, returns null if this is a toplevel window.
   nsIPrincipal* GetTopLevelPrincipal();
 
   // Get the parent principal, returns null if this or the parent are not a
   // toplevel window. This is mainly used to determine the anti-tracking storage
   // area.
   nsIPrincipal* GetTopLevelStorageAreaPrincipal();
 
+  // This method is called if this window loads a 3rd party tracking resource
+  // and the storage is just been granted. The window can reset the partitioned
+  // storage objects and switch to the first party cookie jar.
+  void StorageAccessGranted();
+
 protected:
   static void NotifyDOMWindowDestroyed(nsGlobalWindowInner* aWindow);
   void NotifyWindowIDDestroyed(const char* aTopic);
 
   static void NotifyDOMWindowFrozen(nsGlobalWindowInner* aWindow);
   static void NotifyDOMWindowThawed(nsGlobalWindowInner* aWindow);
 
   virtual void UpdateParentTarget() override;
--- a/toolkit/components/antitracking/AntiTrackingCommon.cpp
+++ b/toolkit/components/antitracking/AntiTrackingCommon.cpp
@@ -531,16 +531,19 @@ AntiTrackingCommon::AddFirstPartyStorage
   NS_ConvertUTF16toUTF8 grantedOrigin(origin);
 
   nsAutoCString permissionKey;
   CreatePermissionKey(trackingOrigin, grantedOrigin, permissionKey);
 
   // Let's store the permission in the current parent window.
   topInnerWindow->SaveStorageAccessGranted(permissionKey);
 
+  // Let's inform the parent window.
+  parentWindow->StorageAccessGranted();
+
   nsIChannel* channel =
     pwin->GetCurrentInnerWindow()->GetExtantDoc()->GetChannel();
 
   pwin->NotifyContentBlockingState(blockReason, channel, false, trackingURI);
 
   ReportUnblockingConsole(parentWindow, NS_ConvertUTF8toUTF16(trackingOrigin),
                           origin, aReason);