dom/quota/QuotaObject.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim: set ts=2 et sw=2 tw=80: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef mozilla_dom_quota_quotaobject_h__
     8 #define mozilla_dom_quota_quotaobject_h__
    10 #include "mozilla/dom/quota/QuotaCommon.h"
    12 #include "nsDataHashtable.h"
    14 #include "PersistenceType.h"
    16 BEGIN_QUOTA_NAMESPACE
    18 class GroupInfo;
    19 class GroupInfoPair;
    20 class OriginInfo;
    21 class QuotaManager;
    23 class QuotaObject
    24 {
    25   friend class OriginInfo;
    26   friend class QuotaManager;
    28 public:
    29   void
    30   AddRef();
    32   void
    33   Release();
    35   void
    36   UpdateSize(int64_t aSize);
    38   bool
    39   MaybeAllocateMoreSpace(int64_t aOffset, int32_t aCount);
    41 private:
    42   QuotaObject(OriginInfo* aOriginInfo, const nsAString& aPath, int64_t aSize)
    43   : mOriginInfo(aOriginInfo), mPath(aPath), mSize(aSize)
    44   {
    45     MOZ_COUNT_CTOR(QuotaObject);
    46   }
    48   ~QuotaObject()
    49   {
    50     MOZ_COUNT_DTOR(QuotaObject);
    51   }
    53   already_AddRefed<QuotaObject>
    54   LockedAddRef()
    55   {
    56     AssertCurrentThreadOwnsQuotaMutex();
    58     ++mRefCnt;
    60     nsRefPtr<QuotaObject> result = dont_AddRef(this);
    61     return result.forget();
    62   }
    64   mozilla::ThreadSafeAutoRefCnt mRefCnt;
    66   OriginInfo* mOriginInfo;
    67   nsString mPath;
    68   int64_t mSize;
    69 };
    71 class OriginInfo MOZ_FINAL
    72 {
    73   friend class GroupInfo;
    74   friend class QuotaManager;
    75   friend class QuotaObject;
    77 public:
    78   OriginInfo(GroupInfo* aGroupInfo, const nsACString& aOrigin, uint64_t aLimit,
    79              uint64_t aUsage, int64_t aAccessTime)
    80   : mGroupInfo(aGroupInfo), mOrigin(aOrigin), mLimit(aLimit), mUsage(aUsage),
    81     mAccessTime(aAccessTime)
    82   {
    83     MOZ_COUNT_CTOR(OriginInfo);
    84   }
    86   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(OriginInfo)
    88   int64_t
    89   AccessTime() const
    90   {
    91     return mAccessTime;
    92   }
    94 private:
    95   // Private destructor, to discourage deletion outside of Release():
    96   ~OriginInfo()
    97   {
    98     MOZ_COUNT_DTOR(OriginInfo);
    99   }
   101   void
   102   LockedDecreaseUsage(int64_t aSize);
   104   void
   105   LockedUpdateAccessTime(int64_t aAccessTime)
   106   {
   107     AssertCurrentThreadOwnsQuotaMutex();
   109     mAccessTime = aAccessTime;
   110   }
   112   void
   113   LockedClearOriginInfos()
   114   {
   115     AssertCurrentThreadOwnsQuotaMutex();
   117     mQuotaObjects.EnumerateRead(ClearOriginInfoCallback, nullptr);
   118   }
   120   static PLDHashOperator
   121   ClearOriginInfoCallback(const nsAString& aKey,
   122                           QuotaObject* aValue, void* aUserArg);
   124   nsDataHashtable<nsStringHashKey, QuotaObject*> mQuotaObjects;
   126   GroupInfo* mGroupInfo;
   127   nsCString mOrigin;
   128   uint64_t mLimit;
   129   uint64_t mUsage;
   130   int64_t mAccessTime;
   131 };
   133 class OriginInfoLRUComparator
   134 {
   135 public:
   136   bool
   137   Equals(const OriginInfo* a, const OriginInfo* b) const
   138   {
   139     return
   140       a && b ? a->AccessTime() == b->AccessTime() : !a && !b ? true : false;
   141   }
   143   bool
   144   LessThan(const OriginInfo* a, const OriginInfo* b) const
   145   {
   146     return a && b ? a->AccessTime() < b->AccessTime() : b ? true : false;
   147   }
   148 };
   150 class GroupInfo MOZ_FINAL
   151 {
   152   friend class GroupInfoPair;
   153   friend class OriginInfo;
   154   friend class QuotaManager;
   155   friend class QuotaObject;
   157 public:
   158   GroupInfo(PersistenceType aPersistenceType, const nsACString& aGroup)
   159   : mPersistenceType(aPersistenceType), mGroup(aGroup), mUsage(0)
   160   {
   161     MOZ_COUNT_CTOR(GroupInfo);
   162   }
   164   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(GroupInfo)
   166   bool
   167   IsForPersistentStorage() const
   168   {
   169     return mPersistenceType == PERSISTENCE_TYPE_PERSISTENT;
   170   }
   172   bool
   173   IsForTemporaryStorage() const
   174   {
   175     return mPersistenceType == PERSISTENCE_TYPE_TEMPORARY;
   176   }
   178 private:
   179   // Private destructor, to discourage deletion outside of Release():
   180   ~GroupInfo()
   181   {
   182     MOZ_COUNT_DTOR(GroupInfo);
   183   }
   185   already_AddRefed<OriginInfo>
   186   LockedGetOriginInfo(const nsACString& aOrigin);
   188   void
   189   LockedAddOriginInfo(OriginInfo* aOriginInfo);
   191   void
   192   LockedRemoveOriginInfo(const nsACString& aOrigin);
   194   void
   195   LockedRemoveOriginInfos();
   197   void
   198   LockedRemoveOriginInfosForPattern(const nsACString& aPattern);
   200   bool
   201   LockedHasOriginInfos()
   202   {
   203     AssertCurrentThreadOwnsQuotaMutex();
   205     return !mOriginInfos.IsEmpty();
   206   }
   208   nsTArray<nsRefPtr<OriginInfo> > mOriginInfos;
   210   PersistenceType mPersistenceType;
   211   nsCString mGroup;
   212   uint64_t mUsage;
   213 };
   215 class GroupInfoPair
   216 {
   217   friend class QuotaManager;
   219 public:
   220   GroupInfoPair()
   221   {
   222     MOZ_COUNT_CTOR(GroupInfoPair);
   223   }
   225   ~GroupInfoPair()
   226   {
   227     MOZ_COUNT_DTOR(GroupInfoPair);
   228   }
   230 private:
   231   already_AddRefed<GroupInfo>
   232   LockedGetGroupInfo(PersistenceType aPersistenceType)
   233   {
   234     AssertCurrentThreadOwnsQuotaMutex();
   236     nsRefPtr<GroupInfo> groupInfo =
   237       GetGroupInfoForPersistenceType(aPersistenceType);
   238     return groupInfo.forget();
   239   }
   241   void
   242   LockedSetGroupInfo(GroupInfo* aGroupInfo)
   243   {
   244     AssertCurrentThreadOwnsQuotaMutex();
   246     nsRefPtr<GroupInfo>& groupInfo =
   247       GetGroupInfoForPersistenceType(aGroupInfo->mPersistenceType);
   248     groupInfo = aGroupInfo;
   249   }
   251   void
   252   LockedClearGroupInfo(PersistenceType aPersistenceType)
   253   {
   254     AssertCurrentThreadOwnsQuotaMutex();
   256     nsRefPtr<GroupInfo>& groupInfo =
   257       GetGroupInfoForPersistenceType(aPersistenceType);
   258     groupInfo = nullptr;
   259   }
   261   bool
   262   LockedHasGroupInfos()
   263   {
   264     AssertCurrentThreadOwnsQuotaMutex();
   266     return mPersistentStorageGroupInfo || mTemporaryStorageGroupInfo;
   267   }
   269   nsRefPtr<GroupInfo>&
   270   GetGroupInfoForPersistenceType(PersistenceType aPersistenceType);
   272   nsRefPtr<GroupInfo> mPersistentStorageGroupInfo;
   273   nsRefPtr<GroupInfo> mTemporaryStorageGroupInfo;
   274 };
   276 END_QUOTA_NAMESPACE
   278 #endif // mozilla_dom_quota_quotaobject_h__

mercurial