Bug 1547114 - Part 2: Read the cookie lifetime policy from nsContentUtils; r=baku
authorEhsan Akhgari <ehsan@mozilla.com>
Fri, 26 Apr 2019 15:49:19 +0000
changeset 530346 405b90db2106c0bb6d211d8c364b3b08c2ede455
parent 530345 8cfaa18c3b3b8866ae2ab3a389dd37c5de5e1d3f
child 530347 14ad847bd4778993dbf02286687521a56f167624
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1547114
milestone68.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 1547114 - Part 2: Read the cookie lifetime policy from nsContentUtils; r=baku Differential Revision: https://phabricator.services.mozilla.com/D28915
dom/base/nsContentUtils.h
extensions/cookie/nsCookiePermission.cpp
extensions/cookie/nsCookiePermission.h
--- a/dom/base/nsContentUtils.h
+++ b/dom/base/nsContentUtils.h
@@ -3355,16 +3355,21 @@ class nsContentUtils {
   /**
    * Returns true if the top level ancestor content document of aDocument hasn't
    * yet had the first contentful paint and there is a high priority event
    * pending in the main thread.
    */
   static bool HighPriorityEventPendingForTopLevelDocumentBeforeContentfulPaint(
       Document* aDocument);
 
+  /**
+   * Gets the global cookie lifetime policy.
+   */
+  static uint32_t GetCookieLifetimePolicy() { return sCookiesLifetimePolicy; }
+
  private:
   static bool InitializeEventTable();
 
   static nsresult EnsureStringBundle(PropertiesFile aFile);
 
   static bool CanCallerAccess(nsIPrincipal* aSubjectPrincipal,
                               nsIPrincipal* aPrincipal);
 
--- a/extensions/cookie/nsCookiePermission.cpp
+++ b/extensions/cookie/nsCookiePermission.cpp
@@ -5,55 +5,47 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsCookiePermission.h"
 
 #include "mozIThirdPartyUtil.h"
 #include "nsICookie2.h"
 #include "nsIServiceManager.h"
 #include "nsICookieManager.h"
+#include "nsICookieService.h"
 #include "nsNetUtil.h"
 #include "nsIInterfaceRequestorUtils.h"
 #include "nsIProtocolHandler.h"
 #include "nsIURI.h"
-#include "nsIPrefService.h"
-#include "nsIPrefBranch.h"
 #include "nsIChannel.h"
 #include "nsIHttpChannelInternal.h"
 #include "nsIDOMWindow.h"
 #include "nsIPrincipal.h"
 #include "nsString.h"
 #include "nsCRT.h"
 #include "nsILoadContext.h"
 #include "nsIScriptObjectPrincipal.h"
 #include "nsNetCID.h"
 #include "prtime.h"
 #include "mozilla/StaticPtr.h"
+#include "nsContentUtils.h"
 
 /****************************************************************
  ************************ nsCookiePermission ********************
  ****************************************************************/
 
-// values for mCookiesLifetimePolicy
-// 0 == accept normally
-// 1 == ask before accepting, no more supported, treated like ACCEPT_NORMALLY
-// (Bug 606655). 2 == downgrade to session 3 == limit lifetime to N days
-static const uint32_t ACCEPT_NORMALLY = 0;
-static const uint32_t ACCEPT_SESSION = 2;
-
 static const bool kDefaultPolicy = true;
-static const char kCookiesLifetimePolicy[] = "network.cookie.lifetimePolicy";
 
 static const nsLiteralCString kPermissionType(NS_LITERAL_CSTRING("cookie"));
 
 namespace {
 mozilla::StaticRefPtr<nsCookiePermission> gSingleton;
 }
 
-NS_IMPL_ISUPPORTS(nsCookiePermission, nsICookiePermission, nsIObserver)
+NS_IMPL_ISUPPORTS(nsCookiePermission, nsICookiePermission)
 
 // static
 already_AddRefed<nsICookiePermission> nsCookiePermission::GetOrCreate() {
   if (!gSingleton) {
     gSingleton = new nsCookiePermission();
   }
   return do_AddRef(gSingleton);
 }
@@ -66,41 +58,19 @@ bool nsCookiePermission::Init() {
   // required for some methods on nsICookiePermission, so it should be done
   // lazily.
   nsresult rv;
   mPermMgr = do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
   if (NS_FAILED(rv)) return false;
   mThirdPartyUtil = do_GetService(THIRDPARTYUTIL_CONTRACTID, &rv);
   if (NS_FAILED(rv)) return false;
 
-  // failure to access the pref service is non-fatal...
-  nsCOMPtr<nsIPrefBranch> prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID);
-  if (prefBranch) {
-    prefBranch->AddObserver(kCookiesLifetimePolicy, this, false);
-    PrefChanged(prefBranch, nullptr);
-  }
-
   return true;
 }
 
-void nsCookiePermission::PrefChanged(nsIPrefBranch *aPrefBranch,
-                                     const char *aPref) {
-  int32_t val;
-
-#define PREF_CHANGED(_P) (!aPref || !strcmp(aPref, _P))
-
-  if (PREF_CHANGED(kCookiesLifetimePolicy) &&
-      NS_SUCCEEDED(aPrefBranch->GetIntPref(kCookiesLifetimePolicy, &val))) {
-    if (val != static_cast<int32_t>(ACCEPT_SESSION)) {
-      val = ACCEPT_NORMALLY;
-    }
-    mCookiesLifetimePolicy = val;
-  }
-}
-
 NS_IMETHODIMP
 nsCookiePermission::SetAccess(nsIURI *aURI, nsCookieAccess aAccess) {
   // Lazily initialize ourselves
   if (!EnsureInitialized()) return NS_ERROR_UNEXPECTED;
 
   //
   // NOTE: nsCookieAccess values conveniently match up with
   //       the permission codes used by nsIPermissionManager.
@@ -154,41 +124,31 @@ nsCookiePermission::CanSetCookie(nsIURI 
       *aResult = false;
       break;
 
     default:
       // Here we can have any legacy permission value.
 
       // now we need to figure out what type of accept policy we're dealing with
       // if we accept cookies normally, just bail and return
-      if (mCookiesLifetimePolicy == ACCEPT_NORMALLY) {
+      if (nsContentUtils::GetCookieLifetimePolicy() ==
+          nsICookieService::ACCEPT_NORMALLY) {
         *aResult = true;
         return NS_OK;
       }
 
       // declare this here since it'll be used in all of the remaining cases
       int64_t currentTime = PR_Now() / PR_USEC_PER_SEC;
       int64_t delta = *aExpiry - currentTime;
 
       // We are accepting the cookie, but,
       // if it's not a session cookie, we may have to limit its lifetime.
       if (!*aIsSession && delta > 0) {
-        if (mCookiesLifetimePolicy == ACCEPT_SESSION) {
+        if (nsContentUtils::GetCookieLifetimePolicy() ==
+            nsICookieService::ACCEPT_SESSION) {
           // limit lifetime to session
           *aIsSession = true;
         }
       }
   }
 
   return NS_OK;
 }
-
-NS_IMETHODIMP
-nsCookiePermission::Observe(nsISupports *aSubject, const char *aTopic,
-                            const char16_t *aData) {
-  nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject);
-  NS_ASSERTION(!nsCRT::strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic),
-               "unexpected topic - we only deal with pref changes!");
-
-  if (prefBranch)
-    PrefChanged(prefBranch, NS_LossyConvertUTF16toASCII(aData).get());
-  return NS_OK;
-}
--- a/extensions/cookie/nsCookiePermission.h
+++ b/extensions/cookie/nsCookiePermission.h
@@ -2,45 +2,35 @@
  * 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 nsCookiePermission_h__
 #define nsCookiePermission_h__
 
 #include "nsICookiePermission.h"
 #include "nsIPermissionManager.h"
-#include "nsIObserver.h"
 #include "nsCOMPtr.h"
 #include "mozIThirdPartyUtil.h"
 
-class nsIPrefBranch;
 
-class nsCookiePermission final : public nsICookiePermission,
-                                 public nsIObserver {
+class nsCookiePermission final : public nsICookiePermission {
  public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSICOOKIEPERMISSION
-  NS_DECL_NSIOBSERVER
 
   // Singleton accessor
   static already_AddRefed<nsICookiePermission> GetOrCreate();
   static void Shutdown();
 
   bool Init();
-  void PrefChanged(nsIPrefBranch *, const char *);
 
  private:
-  nsCookiePermission()
-      : mCookiesLifetimePolicy(0)  // ACCEPT_NORMALLY
-  {}
-  virtual ~nsCookiePermission() {}
+  ~nsCookiePermission() = default;
 
   bool EnsureInitialized() {
     return (mPermMgr != nullptr && mThirdPartyUtil != nullptr) || Init();
   };
 
   nsCOMPtr<nsIPermissionManager> mPermMgr;
   nsCOMPtr<mozIThirdPartyUtil> mThirdPartyUtil;
-
-  uint8_t mCookiesLifetimePolicy;  // pref for how long cookies are stored
 };
 
 #endif