Sat, 03 Jan 2015 20:18:00 +0100
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.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "QuotaObject.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "QuotaManager.h" |
michael@0 | 10 | #include "Utilities.h" |
michael@0 | 11 | |
michael@0 | 12 | USING_QUOTA_NAMESPACE |
michael@0 | 13 | |
michael@0 | 14 | void |
michael@0 | 15 | QuotaObject::AddRef() |
michael@0 | 16 | { |
michael@0 | 17 | QuotaManager* quotaManager = QuotaManager::Get(); |
michael@0 | 18 | if (!quotaManager) { |
michael@0 | 19 | NS_ERROR("Null quota manager, this shouldn't happen, possible leak!"); |
michael@0 | 20 | |
michael@0 | 21 | ++mRefCnt; |
michael@0 | 22 | |
michael@0 | 23 | return; |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | MutexAutoLock lock(quotaManager->mQuotaMutex); |
michael@0 | 27 | |
michael@0 | 28 | ++mRefCnt; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | void |
michael@0 | 32 | QuotaObject::Release() |
michael@0 | 33 | { |
michael@0 | 34 | QuotaManager* quotaManager = QuotaManager::Get(); |
michael@0 | 35 | if (!quotaManager) { |
michael@0 | 36 | NS_ERROR("Null quota manager, this shouldn't happen, possible leak!"); |
michael@0 | 37 | |
michael@0 | 38 | nsrefcnt count = --mRefCnt; |
michael@0 | 39 | if (count == 0) { |
michael@0 | 40 | mRefCnt = 1; |
michael@0 | 41 | delete this; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | return; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | { |
michael@0 | 48 | MutexAutoLock lock(quotaManager->mQuotaMutex); |
michael@0 | 49 | |
michael@0 | 50 | --mRefCnt; |
michael@0 | 51 | |
michael@0 | 52 | if (mRefCnt > 0) { |
michael@0 | 53 | return; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | if (mOriginInfo) { |
michael@0 | 57 | mOriginInfo->mQuotaObjects.Remove(mPath); |
michael@0 | 58 | } |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | delete this; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | void |
michael@0 | 65 | QuotaObject::UpdateSize(int64_t aSize) |
michael@0 | 66 | { |
michael@0 | 67 | QuotaManager* quotaManager = QuotaManager::Get(); |
michael@0 | 68 | NS_ASSERTION(quotaManager, "Shouldn't be null!"); |
michael@0 | 69 | |
michael@0 | 70 | MutexAutoLock lock(quotaManager->mQuotaMutex); |
michael@0 | 71 | |
michael@0 | 72 | if (!mOriginInfo) { |
michael@0 | 73 | return; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | GroupInfo* groupInfo = mOriginInfo->mGroupInfo; |
michael@0 | 77 | |
michael@0 | 78 | if (groupInfo->IsForTemporaryStorage()) { |
michael@0 | 79 | quotaManager->mTemporaryStorageUsage -= mSize; |
michael@0 | 80 | } |
michael@0 | 81 | groupInfo->mUsage -= mSize; |
michael@0 | 82 | mOriginInfo->mUsage -= mSize; |
michael@0 | 83 | |
michael@0 | 84 | mSize = aSize; |
michael@0 | 85 | |
michael@0 | 86 | mOriginInfo->mUsage += mSize; |
michael@0 | 87 | groupInfo->mUsage += mSize; |
michael@0 | 88 | if (groupInfo->IsForTemporaryStorage()) { |
michael@0 | 89 | quotaManager->mTemporaryStorageUsage += mSize; |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | bool |
michael@0 | 94 | QuotaObject::MaybeAllocateMoreSpace(int64_t aOffset, int32_t aCount) |
michael@0 | 95 | { |
michael@0 | 96 | int64_t end = aOffset + aCount; |
michael@0 | 97 | |
michael@0 | 98 | QuotaManager* quotaManager = QuotaManager::Get(); |
michael@0 | 99 | NS_ASSERTION(quotaManager, "Shouldn't be null!"); |
michael@0 | 100 | |
michael@0 | 101 | MutexAutoLock lock(quotaManager->mQuotaMutex); |
michael@0 | 102 | |
michael@0 | 103 | if (mSize >= end || !mOriginInfo) { |
michael@0 | 104 | return true; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | GroupInfo* groupInfo = mOriginInfo->mGroupInfo; |
michael@0 | 108 | |
michael@0 | 109 | if (groupInfo->IsForPersistentStorage()) { |
michael@0 | 110 | uint64_t newUsage = mOriginInfo->mUsage - mSize + end; |
michael@0 | 111 | |
michael@0 | 112 | if (newUsage > mOriginInfo->mLimit) { |
michael@0 | 113 | // This will block the thread, but it will also drop the mutex while |
michael@0 | 114 | // waiting. The mutex will be reacquired again when the waiting is |
michael@0 | 115 | // finished. |
michael@0 | 116 | if (!quotaManager->LockedQuotaIsLifted()) { |
michael@0 | 117 | return false; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | // Threads raced, the origin info removal has been done by some other |
michael@0 | 121 | // thread. |
michael@0 | 122 | if (!mOriginInfo) { |
michael@0 | 123 | // The other thread could allocate more space. |
michael@0 | 124 | if (end > mSize) { |
michael@0 | 125 | mSize = end; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | return true; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | nsCString group = mOriginInfo->mGroupInfo->mGroup; |
michael@0 | 132 | nsCString origin = mOriginInfo->mOrigin; |
michael@0 | 133 | |
michael@0 | 134 | mOriginInfo->LockedClearOriginInfos(); |
michael@0 | 135 | NS_ASSERTION(!mOriginInfo, |
michael@0 | 136 | "Should have cleared in LockedClearOriginInfos!"); |
michael@0 | 137 | |
michael@0 | 138 | quotaManager->LockedRemoveQuotaForOrigin(PERSISTENCE_TYPE_PERSISTENT, |
michael@0 | 139 | group, origin); |
michael@0 | 140 | |
michael@0 | 141 | // Some other thread could increase the size without blocking (increasing |
michael@0 | 142 | // the origin usage without hitting the limit), but no more than this one. |
michael@0 | 143 | NS_ASSERTION(mSize < end, "This shouldn't happen!"); |
michael@0 | 144 | |
michael@0 | 145 | mSize = end; |
michael@0 | 146 | |
michael@0 | 147 | return true; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | mOriginInfo->mUsage = newUsage; |
michael@0 | 151 | |
michael@0 | 152 | groupInfo->mUsage = groupInfo->mUsage - mSize + end; |
michael@0 | 153 | |
michael@0 | 154 | mSize = end; |
michael@0 | 155 | |
michael@0 | 156 | return true; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | NS_ASSERTION(groupInfo->mPersistenceType == PERSISTENCE_TYPE_TEMPORARY, |
michael@0 | 160 | "Huh?"); |
michael@0 | 161 | |
michael@0 | 162 | uint64_t delta = end - mSize; |
michael@0 | 163 | |
michael@0 | 164 | uint64_t newUsage = mOriginInfo->mUsage + delta; |
michael@0 | 165 | |
michael@0 | 166 | // Temporary storage has no limit for origin usage (there's a group and the |
michael@0 | 167 | // global limit though). |
michael@0 | 168 | |
michael@0 | 169 | uint64_t newGroupUsage = groupInfo->mUsage + delta; |
michael@0 | 170 | |
michael@0 | 171 | // Temporary storage has a hard limit for group usage (20 % of the global |
michael@0 | 172 | // limit). |
michael@0 | 173 | if (newGroupUsage > quotaManager->GetGroupLimit()) { |
michael@0 | 174 | return false; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | uint64_t newTemporaryStorageUsage = quotaManager->mTemporaryStorageUsage + |
michael@0 | 178 | delta; |
michael@0 | 179 | |
michael@0 | 180 | if (newTemporaryStorageUsage > quotaManager->mTemporaryStorageLimit) { |
michael@0 | 181 | // This will block the thread without holding the lock while waitting. |
michael@0 | 182 | |
michael@0 | 183 | nsAutoTArray<OriginInfo*, 10> originInfos; |
michael@0 | 184 | uint64_t sizeToBeFreed = |
michael@0 | 185 | quotaManager->LockedCollectOriginsForEviction(delta, originInfos); |
michael@0 | 186 | |
michael@0 | 187 | if (!sizeToBeFreed) { |
michael@0 | 188 | return false; |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | NS_ASSERTION(sizeToBeFreed >= delta, "Huh?"); |
michael@0 | 192 | |
michael@0 | 193 | { |
michael@0 | 194 | MutexAutoUnlock autoUnlock(quotaManager->mQuotaMutex); |
michael@0 | 195 | |
michael@0 | 196 | for (uint32_t i = 0; i < originInfos.Length(); i++) { |
michael@0 | 197 | quotaManager->DeleteTemporaryFilesForOrigin(originInfos[i]->mOrigin); |
michael@0 | 198 | } |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | // Relocked. |
michael@0 | 202 | |
michael@0 | 203 | NS_ASSERTION(mOriginInfo, "How come?!"); |
michael@0 | 204 | |
michael@0 | 205 | nsTArray<nsCString> origins; |
michael@0 | 206 | for (uint32_t i = 0; i < originInfos.Length(); i++) { |
michael@0 | 207 | OriginInfo* originInfo = originInfos[i]; |
michael@0 | 208 | |
michael@0 | 209 | NS_ASSERTION(originInfo != mOriginInfo, "Deleted itself!"); |
michael@0 | 210 | |
michael@0 | 211 | nsCString group = originInfo->mGroupInfo->mGroup; |
michael@0 | 212 | nsCString origin = originInfo->mOrigin; |
michael@0 | 213 | quotaManager->LockedRemoveQuotaForOrigin(PERSISTENCE_TYPE_TEMPORARY, |
michael@0 | 214 | group, origin); |
michael@0 | 215 | |
michael@0 | 216 | #ifdef DEBUG |
michael@0 | 217 | originInfos[i] = nullptr; |
michael@0 | 218 | #endif |
michael@0 | 219 | |
michael@0 | 220 | origins.AppendElement(origin); |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | // We unlocked and relocked several times so we need to recompute all the |
michael@0 | 224 | // essential variables and recheck the group limit. |
michael@0 | 225 | |
michael@0 | 226 | delta = end - mSize; |
michael@0 | 227 | |
michael@0 | 228 | newUsage = mOriginInfo->mUsage + delta; |
michael@0 | 229 | |
michael@0 | 230 | newGroupUsage = groupInfo->mUsage + delta; |
michael@0 | 231 | |
michael@0 | 232 | if (newGroupUsage > quotaManager->GetGroupLimit()) { |
michael@0 | 233 | // Unfortunately some other thread increased the group usage in the |
michael@0 | 234 | // meantime and we are not below the group limit anymore. |
michael@0 | 235 | |
michael@0 | 236 | // However, the origin eviction must be finalized in this case too. |
michael@0 | 237 | MutexAutoUnlock autoUnlock(quotaManager->mQuotaMutex); |
michael@0 | 238 | |
michael@0 | 239 | quotaManager->FinalizeOriginEviction(origins); |
michael@0 | 240 | |
michael@0 | 241 | return false; |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | newTemporaryStorageUsage = quotaManager->mTemporaryStorageUsage + delta; |
michael@0 | 245 | |
michael@0 | 246 | NS_ASSERTION(newTemporaryStorageUsage <= |
michael@0 | 247 | quotaManager->mTemporaryStorageLimit, "How come?!"); |
michael@0 | 248 | |
michael@0 | 249 | // Ok, we successfully freed enough space and the operation can continue |
michael@0 | 250 | // without throwing the quota error. |
michael@0 | 251 | |
michael@0 | 252 | mOriginInfo->mUsage = newUsage; |
michael@0 | 253 | groupInfo->mUsage = newGroupUsage; |
michael@0 | 254 | quotaManager->mTemporaryStorageUsage = newTemporaryStorageUsage;; |
michael@0 | 255 | |
michael@0 | 256 | // Some other thread could increase the size in the meantime, but no more |
michael@0 | 257 | // than this one. |
michael@0 | 258 | NS_ASSERTION(mSize < end, "This shouldn't happen!"); |
michael@0 | 259 | mSize = end; |
michael@0 | 260 | |
michael@0 | 261 | // Finally, release IO thread only objects and allow next synchronized |
michael@0 | 262 | // ops for the evicted origins. |
michael@0 | 263 | MutexAutoUnlock autoUnlock(quotaManager->mQuotaMutex); |
michael@0 | 264 | |
michael@0 | 265 | quotaManager->FinalizeOriginEviction(origins); |
michael@0 | 266 | |
michael@0 | 267 | return true; |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | mOriginInfo->mUsage = newUsage; |
michael@0 | 271 | groupInfo->mUsage = newGroupUsage; |
michael@0 | 272 | quotaManager->mTemporaryStorageUsage = newTemporaryStorageUsage; |
michael@0 | 273 | |
michael@0 | 274 | mSize = end; |
michael@0 | 275 | |
michael@0 | 276 | return true; |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | void |
michael@0 | 280 | OriginInfo::LockedDecreaseUsage(int64_t aSize) |
michael@0 | 281 | { |
michael@0 | 282 | AssertCurrentThreadOwnsQuotaMutex(); |
michael@0 | 283 | |
michael@0 | 284 | mUsage -= aSize; |
michael@0 | 285 | |
michael@0 | 286 | mGroupInfo->mUsage -= aSize; |
michael@0 | 287 | |
michael@0 | 288 | if (mGroupInfo->IsForTemporaryStorage()) { |
michael@0 | 289 | QuotaManager* quotaManager = QuotaManager::Get(); |
michael@0 | 290 | NS_ASSERTION(quotaManager, "Shouldn't be null!"); |
michael@0 | 291 | |
michael@0 | 292 | quotaManager->mTemporaryStorageUsage -= aSize; |
michael@0 | 293 | } |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | // static |
michael@0 | 297 | PLDHashOperator |
michael@0 | 298 | OriginInfo::ClearOriginInfoCallback(const nsAString& aKey, |
michael@0 | 299 | QuotaObject* aValue, |
michael@0 | 300 | void* aUserArg) |
michael@0 | 301 | { |
michael@0 | 302 | NS_ASSERTION(!aKey.IsEmpty(), "Empty key!"); |
michael@0 | 303 | NS_ASSERTION(aValue, "Null pointer!"); |
michael@0 | 304 | |
michael@0 | 305 | aValue->mOriginInfo = nullptr; |
michael@0 | 306 | |
michael@0 | 307 | return PL_DHASH_NEXT; |
michael@0 | 308 | } |
michael@0 | 309 | |
michael@0 | 310 | already_AddRefed<OriginInfo> |
michael@0 | 311 | GroupInfo::LockedGetOriginInfo(const nsACString& aOrigin) |
michael@0 | 312 | { |
michael@0 | 313 | AssertCurrentThreadOwnsQuotaMutex(); |
michael@0 | 314 | |
michael@0 | 315 | for (uint32_t index = 0; index < mOriginInfos.Length(); index++) { |
michael@0 | 316 | nsRefPtr<OriginInfo>& originInfo = mOriginInfos[index]; |
michael@0 | 317 | |
michael@0 | 318 | if (originInfo->mOrigin == aOrigin) { |
michael@0 | 319 | nsRefPtr<OriginInfo> result = originInfo; |
michael@0 | 320 | return result.forget(); |
michael@0 | 321 | } |
michael@0 | 322 | } |
michael@0 | 323 | |
michael@0 | 324 | return nullptr; |
michael@0 | 325 | } |
michael@0 | 326 | |
michael@0 | 327 | void |
michael@0 | 328 | GroupInfo::LockedAddOriginInfo(OriginInfo* aOriginInfo) |
michael@0 | 329 | { |
michael@0 | 330 | AssertCurrentThreadOwnsQuotaMutex(); |
michael@0 | 331 | |
michael@0 | 332 | NS_ASSERTION(!mOriginInfos.Contains(aOriginInfo), |
michael@0 | 333 | "Replacing an existing entry!"); |
michael@0 | 334 | mOriginInfos.AppendElement(aOriginInfo); |
michael@0 | 335 | |
michael@0 | 336 | mUsage += aOriginInfo->mUsage; |
michael@0 | 337 | |
michael@0 | 338 | if (IsForTemporaryStorage()) { |
michael@0 | 339 | QuotaManager* quotaManager = QuotaManager::Get(); |
michael@0 | 340 | NS_ASSERTION(quotaManager, "Shouldn't be null!"); |
michael@0 | 341 | |
michael@0 | 342 | quotaManager->mTemporaryStorageUsage += aOriginInfo->mUsage; |
michael@0 | 343 | } |
michael@0 | 344 | } |
michael@0 | 345 | |
michael@0 | 346 | void |
michael@0 | 347 | GroupInfo::LockedRemoveOriginInfo(const nsACString& aOrigin) |
michael@0 | 348 | { |
michael@0 | 349 | AssertCurrentThreadOwnsQuotaMutex(); |
michael@0 | 350 | |
michael@0 | 351 | for (uint32_t index = 0; index < mOriginInfos.Length(); index++) { |
michael@0 | 352 | if (mOriginInfos[index]->mOrigin == aOrigin) { |
michael@0 | 353 | mUsage -= mOriginInfos[index]->mUsage; |
michael@0 | 354 | |
michael@0 | 355 | if (IsForTemporaryStorage()) { |
michael@0 | 356 | QuotaManager* quotaManager = QuotaManager::Get(); |
michael@0 | 357 | NS_ASSERTION(quotaManager, "Shouldn't be null!"); |
michael@0 | 358 | |
michael@0 | 359 | quotaManager->mTemporaryStorageUsage -= mOriginInfos[index]->mUsage; |
michael@0 | 360 | } |
michael@0 | 361 | |
michael@0 | 362 | mOriginInfos.RemoveElementAt(index); |
michael@0 | 363 | |
michael@0 | 364 | return; |
michael@0 | 365 | } |
michael@0 | 366 | } |
michael@0 | 367 | } |
michael@0 | 368 | |
michael@0 | 369 | void |
michael@0 | 370 | GroupInfo::LockedRemoveOriginInfos() |
michael@0 | 371 | { |
michael@0 | 372 | AssertCurrentThreadOwnsQuotaMutex(); |
michael@0 | 373 | |
michael@0 | 374 | for (uint32_t index = mOriginInfos.Length(); index > 0; index--) { |
michael@0 | 375 | mUsage -= mOriginInfos[index - 1]->mUsage; |
michael@0 | 376 | |
michael@0 | 377 | if (IsForTemporaryStorage()) { |
michael@0 | 378 | QuotaManager* quotaManager = QuotaManager::Get(); |
michael@0 | 379 | NS_ASSERTION(quotaManager, "Shouldn't be null!"); |
michael@0 | 380 | |
michael@0 | 381 | quotaManager->mTemporaryStorageUsage -= mOriginInfos[index - 1]->mUsage; |
michael@0 | 382 | } |
michael@0 | 383 | |
michael@0 | 384 | mOriginInfos.RemoveElementAt(index - 1); |
michael@0 | 385 | } |
michael@0 | 386 | } |
michael@0 | 387 | |
michael@0 | 388 | void |
michael@0 | 389 | GroupInfo::LockedRemoveOriginInfosForPattern(const nsACString& aPattern) |
michael@0 | 390 | { |
michael@0 | 391 | AssertCurrentThreadOwnsQuotaMutex(); |
michael@0 | 392 | |
michael@0 | 393 | for (uint32_t index = mOriginInfos.Length(); index > 0; index--) { |
michael@0 | 394 | if (PatternMatchesOrigin(aPattern, mOriginInfos[index - 1]->mOrigin)) { |
michael@0 | 395 | mUsage -= mOriginInfos[index - 1]->mUsage; |
michael@0 | 396 | |
michael@0 | 397 | if (IsForTemporaryStorage()) { |
michael@0 | 398 | QuotaManager* quotaManager = QuotaManager::Get(); |
michael@0 | 399 | NS_ASSERTION(quotaManager, "Shouldn't be null!"); |
michael@0 | 400 | |
michael@0 | 401 | quotaManager->mTemporaryStorageUsage -= mOriginInfos[index - 1]->mUsage; |
michael@0 | 402 | } |
michael@0 | 403 | |
michael@0 | 404 | mOriginInfos.RemoveElementAt(index - 1); |
michael@0 | 405 | } |
michael@0 | 406 | } |
michael@0 | 407 | } |
michael@0 | 408 | |
michael@0 | 409 | nsRefPtr<GroupInfo>& |
michael@0 | 410 | GroupInfoPair::GetGroupInfoForPersistenceType(PersistenceType aPersistenceType) |
michael@0 | 411 | { |
michael@0 | 412 | switch (aPersistenceType) { |
michael@0 | 413 | case PERSISTENCE_TYPE_PERSISTENT: |
michael@0 | 414 | return mPersistentStorageGroupInfo; |
michael@0 | 415 | case PERSISTENCE_TYPE_TEMPORARY: |
michael@0 | 416 | return mTemporaryStorageGroupInfo; |
michael@0 | 417 | |
michael@0 | 418 | case PERSISTENCE_TYPE_INVALID: |
michael@0 | 419 | default: |
michael@0 | 420 | MOZ_CRASH("Bad persistence type value!"); |
michael@0 | 421 | return mPersistentStorageGroupInfo; |
michael@0 | 422 | } |
michael@0 | 423 | } |