Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "CacheLog.h" |
michael@0 | 6 | #include "CacheFileContextEvictor.h" |
michael@0 | 7 | #include "CacheFileIOManager.h" |
michael@0 | 8 | #include "CacheIndex.h" |
michael@0 | 9 | #include "CacheIndexIterator.h" |
michael@0 | 10 | #include "CacheFileUtils.h" |
michael@0 | 11 | #include "nsIFile.h" |
michael@0 | 12 | #include "LoadContextInfo.h" |
michael@0 | 13 | #include "nsThreadUtils.h" |
michael@0 | 14 | #include "nsString.h" |
michael@0 | 15 | #include "nsISimpleEnumerator.h" |
michael@0 | 16 | #include "nsIDirectoryEnumerator.h" |
michael@0 | 17 | #include "mozilla/Base64.h" |
michael@0 | 18 | |
michael@0 | 19 | |
michael@0 | 20 | namespace mozilla { |
michael@0 | 21 | namespace net { |
michael@0 | 22 | |
michael@0 | 23 | const char kContextEvictionPrefix[] = "ce_"; |
michael@0 | 24 | const uint32_t kContextEvictionPrefixLength = |
michael@0 | 25 | sizeof(kContextEvictionPrefix) - 1; |
michael@0 | 26 | |
michael@0 | 27 | bool CacheFileContextEvictor::sDiskAlreadySearched = false; |
michael@0 | 28 | |
michael@0 | 29 | CacheFileContextEvictor::CacheFileContextEvictor() |
michael@0 | 30 | : mEvicting(false) |
michael@0 | 31 | , mIndexIsUpToDate(false) |
michael@0 | 32 | { |
michael@0 | 33 | LOG(("CacheFileContextEvictor::CacheFileContextEvictor() [this=%p]", this)); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | CacheFileContextEvictor::~CacheFileContextEvictor() |
michael@0 | 37 | { |
michael@0 | 38 | LOG(("CacheFileContextEvictor::~CacheFileContextEvictor() [this=%p]", this)); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | nsresult |
michael@0 | 42 | CacheFileContextEvictor::Init(nsIFile *aCacheDirectory) |
michael@0 | 43 | { |
michael@0 | 44 | LOG(("CacheFileContextEvictor::Init()")); |
michael@0 | 45 | |
michael@0 | 46 | nsresult rv; |
michael@0 | 47 | |
michael@0 | 48 | MOZ_ASSERT(CacheFileIOManager::IsOnIOThread()); |
michael@0 | 49 | |
michael@0 | 50 | CacheIndex::IsUpToDate(&mIndexIsUpToDate); |
michael@0 | 51 | |
michael@0 | 52 | mCacheDirectory = aCacheDirectory; |
michael@0 | 53 | |
michael@0 | 54 | rv = aCacheDirectory->Clone(getter_AddRefs(mEntriesDir)); |
michael@0 | 55 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 56 | return rv; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | rv = mEntriesDir->AppendNative(NS_LITERAL_CSTRING(kEntriesDir)); |
michael@0 | 60 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 61 | return rv; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | if (!sDiskAlreadySearched) { |
michael@0 | 65 | LoadEvictInfoFromDisk(); |
michael@0 | 66 | if ((mEntries.Length() != 0) && mIndexIsUpToDate) { |
michael@0 | 67 | CreateIterators(); |
michael@0 | 68 | StartEvicting(); |
michael@0 | 69 | } |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | return NS_OK; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | uint32_t |
michael@0 | 76 | CacheFileContextEvictor::ContextsCount() |
michael@0 | 77 | { |
michael@0 | 78 | MOZ_ASSERT(CacheFileIOManager::IsOnIOThread()); |
michael@0 | 79 | |
michael@0 | 80 | return mEntries.Length(); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | nsresult |
michael@0 | 84 | CacheFileContextEvictor::AddContext(nsILoadContextInfo *aLoadContextInfo) |
michael@0 | 85 | { |
michael@0 | 86 | LOG(("CacheFileContextEvictor::AddContext() [this=%p, loadContextInfo=%p]", |
michael@0 | 87 | this, aLoadContextInfo)); |
michael@0 | 88 | |
michael@0 | 89 | nsresult rv; |
michael@0 | 90 | |
michael@0 | 91 | MOZ_ASSERT(CacheFileIOManager::IsOnIOThread()); |
michael@0 | 92 | |
michael@0 | 93 | CacheFileContextEvictorEntry *entry = nullptr; |
michael@0 | 94 | for (uint32_t i = 0; i < mEntries.Length(); ++i) { |
michael@0 | 95 | if (mEntries[i]->mInfo->Equals(aLoadContextInfo)) { |
michael@0 | 96 | entry = mEntries[i]; |
michael@0 | 97 | break; |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | if (!entry) { |
michael@0 | 102 | entry = new CacheFileContextEvictorEntry(); |
michael@0 | 103 | entry->mInfo = aLoadContextInfo; |
michael@0 | 104 | mEntries.AppendElement(entry); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | entry->mTimeStamp = PR_Now() / PR_USEC_PER_MSEC; |
michael@0 | 108 | |
michael@0 | 109 | PersistEvictionInfoToDisk(aLoadContextInfo); |
michael@0 | 110 | |
michael@0 | 111 | if (mIndexIsUpToDate) { |
michael@0 | 112 | // Already existing context could be added again, in this case the iterator |
michael@0 | 113 | // would be recreated. Close the old iterator explicitely. |
michael@0 | 114 | if (entry->mIterator) { |
michael@0 | 115 | entry->mIterator->Close(); |
michael@0 | 116 | entry->mIterator = nullptr; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | rv = CacheIndex::GetIterator(aLoadContextInfo, false, |
michael@0 | 120 | getter_AddRefs(entry->mIterator)); |
michael@0 | 121 | if (NS_FAILED(rv)) { |
michael@0 | 122 | // This could probably happen during shutdown. Remove the entry from |
michael@0 | 123 | // the array, but leave the info on the disk. No entry can be opened |
michael@0 | 124 | // during shutdown and we'll load the eviction info on next start. |
michael@0 | 125 | LOG(("CacheFileContextEvictor::AddContext() - Cannot get an iterator. " |
michael@0 | 126 | "[rv=0x%08x]", rv)); |
michael@0 | 127 | mEntries.RemoveElement(entry); |
michael@0 | 128 | return rv; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | StartEvicting(); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | return NS_OK; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | nsresult |
michael@0 | 138 | CacheFileContextEvictor::CacheIndexStateChanged() |
michael@0 | 139 | { |
michael@0 | 140 | LOG(("CacheFileContextEvictor::CacheIndexStateChanged() [this=%p]", this)); |
michael@0 | 141 | |
michael@0 | 142 | MOZ_ASSERT(CacheFileIOManager::IsOnIOThread()); |
michael@0 | 143 | |
michael@0 | 144 | bool isUpToDate = false; |
michael@0 | 145 | CacheIndex::IsUpToDate(&isUpToDate); |
michael@0 | 146 | if (mEntries.Length() == 0) { |
michael@0 | 147 | // Just save the state and exit, since there is nothing to do |
michael@0 | 148 | mIndexIsUpToDate = isUpToDate; |
michael@0 | 149 | return NS_OK; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | if (!isUpToDate && !mIndexIsUpToDate) { |
michael@0 | 153 | // Index is outdated and status has not changed, nothing to do. |
michael@0 | 154 | return NS_OK; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | if (isUpToDate && mIndexIsUpToDate) { |
michael@0 | 158 | // Status has not changed, but make sure the eviction is running. |
michael@0 | 159 | if (mEvicting) { |
michael@0 | 160 | return NS_OK; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | // We're not evicting, but we should be evicting?! |
michael@0 | 164 | LOG(("CacheFileContextEvictor::CacheIndexStateChanged() - Index is up to " |
michael@0 | 165 | "date, we have some context to evict but eviction is not running! " |
michael@0 | 166 | "Starting now.")); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | mIndexIsUpToDate = isUpToDate; |
michael@0 | 170 | |
michael@0 | 171 | if (mIndexIsUpToDate) { |
michael@0 | 172 | CreateIterators(); |
michael@0 | 173 | StartEvicting(); |
michael@0 | 174 | } else { |
michael@0 | 175 | CloseIterators(); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | return NS_OK; |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | nsresult |
michael@0 | 182 | CacheFileContextEvictor::WasEvicted(const nsACString &aKey, nsIFile *aFile, |
michael@0 | 183 | bool *_retval) |
michael@0 | 184 | { |
michael@0 | 185 | LOG(("CacheFileContextEvictor::WasEvicted() [key=%s]", |
michael@0 | 186 | PromiseFlatCString(aKey).get())); |
michael@0 | 187 | |
michael@0 | 188 | nsresult rv; |
michael@0 | 189 | |
michael@0 | 190 | MOZ_ASSERT(CacheFileIOManager::IsOnIOThread()); |
michael@0 | 191 | |
michael@0 | 192 | nsCOMPtr<nsILoadContextInfo> info = CacheFileUtils::ParseKey(aKey); |
michael@0 | 193 | MOZ_ASSERT(info); |
michael@0 | 194 | if (!info) { |
michael@0 | 195 | LOG(("CacheFileContextEvictor::WasEvicted() - Cannot parse key!")); |
michael@0 | 196 | *_retval = false; |
michael@0 | 197 | return NS_OK; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | CacheFileContextEvictorEntry *entry = nullptr; |
michael@0 | 201 | for (uint32_t i = 0; i < mEntries.Length(); ++i) { |
michael@0 | 202 | if (info->Equals(mEntries[i]->mInfo)) { |
michael@0 | 203 | entry = mEntries[i]; |
michael@0 | 204 | break; |
michael@0 | 205 | } |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | if (!entry) { |
michael@0 | 209 | LOG(("CacheFileContextEvictor::WasEvicted() - Didn't find equal context, " |
michael@0 | 210 | "returning false.")); |
michael@0 | 211 | *_retval = false; |
michael@0 | 212 | return NS_OK; |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | PRTime lastModifiedTime; |
michael@0 | 216 | rv = aFile->GetLastModifiedTime(&lastModifiedTime); |
michael@0 | 217 | if (NS_FAILED(rv)) { |
michael@0 | 218 | LOG(("CacheFileContextEvictor::WasEvicted() - Cannot get last modified time" |
michael@0 | 219 | ", returning false.")); |
michael@0 | 220 | *_retval = false; |
michael@0 | 221 | return NS_OK; |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | *_retval = !(lastModifiedTime > entry->mTimeStamp); |
michael@0 | 225 | LOG(("CacheFileContextEvictor::WasEvicted() - returning %s. [mTimeStamp=%lld," |
michael@0 | 226 | " lastModifiedTime=%lld]", *_retval ? "true" : "false", |
michael@0 | 227 | mEntries[0]->mTimeStamp, lastModifiedTime)); |
michael@0 | 228 | |
michael@0 | 229 | return NS_OK; |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | nsresult |
michael@0 | 233 | CacheFileContextEvictor::PersistEvictionInfoToDisk( |
michael@0 | 234 | nsILoadContextInfo *aLoadContextInfo) |
michael@0 | 235 | { |
michael@0 | 236 | LOG(("CacheFileContextEvictor::PersistEvictionInfoToDisk() [this=%p, " |
michael@0 | 237 | "loadContextInfo=%p]", this, aLoadContextInfo)); |
michael@0 | 238 | |
michael@0 | 239 | nsresult rv; |
michael@0 | 240 | |
michael@0 | 241 | MOZ_ASSERT(CacheFileIOManager::IsOnIOThread()); |
michael@0 | 242 | |
michael@0 | 243 | nsCOMPtr<nsIFile> file; |
michael@0 | 244 | rv = GetContextFile(aLoadContextInfo, getter_AddRefs(file)); |
michael@0 | 245 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 246 | return rv; |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | #ifdef PR_LOGGING |
michael@0 | 250 | nsAutoCString path; |
michael@0 | 251 | file->GetNativePath(path); |
michael@0 | 252 | #endif |
michael@0 | 253 | |
michael@0 | 254 | PRFileDesc *fd; |
michael@0 | 255 | rv = file->OpenNSPRFileDesc(PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE, 0600, |
michael@0 | 256 | &fd); |
michael@0 | 257 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 258 | LOG(("CacheFileContextEvictor::PersistEvictionInfoToDisk() - Creating file " |
michael@0 | 259 | "failed! [path=%s, rv=0x%08x]", path.get(), rv)); |
michael@0 | 260 | return rv; |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | PR_Close(fd); |
michael@0 | 264 | |
michael@0 | 265 | LOG(("CacheFileContextEvictor::PersistEvictionInfoToDisk() - Successfully " |
michael@0 | 266 | "created file. [path=%s]", path.get())); |
michael@0 | 267 | |
michael@0 | 268 | return NS_OK; |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | nsresult |
michael@0 | 272 | CacheFileContextEvictor::RemoveEvictInfoFromDisk( |
michael@0 | 273 | nsILoadContextInfo *aLoadContextInfo) |
michael@0 | 274 | { |
michael@0 | 275 | LOG(("CacheFileContextEvictor::RemoveEvictInfoFromDisk() [this=%p, " |
michael@0 | 276 | "loadContextInfo=%p]", this, aLoadContextInfo)); |
michael@0 | 277 | |
michael@0 | 278 | nsresult rv; |
michael@0 | 279 | |
michael@0 | 280 | MOZ_ASSERT(CacheFileIOManager::IsOnIOThread()); |
michael@0 | 281 | |
michael@0 | 282 | nsCOMPtr<nsIFile> file; |
michael@0 | 283 | rv = GetContextFile(aLoadContextInfo, getter_AddRefs(file)); |
michael@0 | 284 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 285 | return rv; |
michael@0 | 286 | } |
michael@0 | 287 | |
michael@0 | 288 | #ifdef PR_LOGGING |
michael@0 | 289 | nsAutoCString path; |
michael@0 | 290 | file->GetNativePath(path); |
michael@0 | 291 | #endif |
michael@0 | 292 | |
michael@0 | 293 | rv = file->Remove(false); |
michael@0 | 294 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 295 | LOG(("CacheFileContextEvictor::RemoveEvictionInfoFromDisk() - Removing file" |
michael@0 | 296 | " failed! [path=%s, rv=0x%08x]", path.get(), rv)); |
michael@0 | 297 | return rv; |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | LOG(("CacheFileContextEvictor::RemoveEvictionInfoFromDisk() - Successfully " |
michael@0 | 301 | "removed file. [path=%s]", path.get())); |
michael@0 | 302 | |
michael@0 | 303 | return NS_OK; |
michael@0 | 304 | } |
michael@0 | 305 | |
michael@0 | 306 | nsresult |
michael@0 | 307 | CacheFileContextEvictor::LoadEvictInfoFromDisk() |
michael@0 | 308 | { |
michael@0 | 309 | LOG(("CacheFileContextEvictor::LoadEvictInfoFromDisk() [this=%p]", this)); |
michael@0 | 310 | |
michael@0 | 311 | nsresult rv; |
michael@0 | 312 | |
michael@0 | 313 | MOZ_ASSERT(CacheFileIOManager::IsOnIOThread()); |
michael@0 | 314 | |
michael@0 | 315 | sDiskAlreadySearched = true; |
michael@0 | 316 | |
michael@0 | 317 | nsCOMPtr<nsISimpleEnumerator> enumerator; |
michael@0 | 318 | rv = mCacheDirectory->GetDirectoryEntries(getter_AddRefs(enumerator)); |
michael@0 | 319 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 320 | return rv; |
michael@0 | 321 | } |
michael@0 | 322 | |
michael@0 | 323 | nsCOMPtr<nsIDirectoryEnumerator> dirEnum = do_QueryInterface(enumerator, &rv); |
michael@0 | 324 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 325 | return rv; |
michael@0 | 326 | } |
michael@0 | 327 | |
michael@0 | 328 | while (true) { |
michael@0 | 329 | nsCOMPtr<nsIFile> file; |
michael@0 | 330 | rv = dirEnum->GetNextFile(getter_AddRefs(file)); |
michael@0 | 331 | if (!file) { |
michael@0 | 332 | break; |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | bool isDir = false; |
michael@0 | 336 | file->IsDirectory(&isDir); |
michael@0 | 337 | if (isDir) { |
michael@0 | 338 | continue; |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | nsAutoCString leaf; |
michael@0 | 342 | rv = file->GetNativeLeafName(leaf); |
michael@0 | 343 | if (NS_FAILED(rv)) { |
michael@0 | 344 | LOG(("CacheFileContextEvictor::LoadEvictInfoFromDisk() - " |
michael@0 | 345 | "GetNativeLeafName() failed! Skipping file.")); |
michael@0 | 346 | continue; |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | if (leaf.Length() < kContextEvictionPrefixLength) { |
michael@0 | 350 | continue; |
michael@0 | 351 | } |
michael@0 | 352 | |
michael@0 | 353 | if (!StringBeginsWith(leaf, NS_LITERAL_CSTRING(kContextEvictionPrefix))) { |
michael@0 | 354 | continue; |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 | nsAutoCString encoded; |
michael@0 | 358 | encoded = Substring(leaf, kContextEvictionPrefixLength); |
michael@0 | 359 | encoded.ReplaceChar('-', '/'); |
michael@0 | 360 | |
michael@0 | 361 | nsAutoCString decoded; |
michael@0 | 362 | rv = Base64Decode(encoded, decoded); |
michael@0 | 363 | if (NS_FAILED(rv)) { |
michael@0 | 364 | LOG(("CacheFileContextEvictor::LoadEvictInfoFromDisk() - Base64 decoding " |
michael@0 | 365 | "failed. Removing the file. [file=%s]", leaf.get())); |
michael@0 | 366 | file->Remove(false); |
michael@0 | 367 | continue; |
michael@0 | 368 | } |
michael@0 | 369 | |
michael@0 | 370 | nsCOMPtr<nsILoadContextInfo> info = CacheFileUtils::ParseKey(decoded); |
michael@0 | 371 | |
michael@0 | 372 | if (!info) { |
michael@0 | 373 | LOG(("CacheFileContextEvictor::LoadEvictInfoFromDisk() - Cannot parse " |
michael@0 | 374 | "context key, removing file. [contextKey=%s, file=%s]", |
michael@0 | 375 | decoded.get(), leaf.get())); |
michael@0 | 376 | file->Remove(false); |
michael@0 | 377 | continue; |
michael@0 | 378 | } |
michael@0 | 379 | |
michael@0 | 380 | PRTime lastModifiedTime; |
michael@0 | 381 | rv = file->GetLastModifiedTime(&lastModifiedTime); |
michael@0 | 382 | if (NS_FAILED(rv)) { |
michael@0 | 383 | continue; |
michael@0 | 384 | } |
michael@0 | 385 | |
michael@0 | 386 | CacheFileContextEvictorEntry *entry = new CacheFileContextEvictorEntry(); |
michael@0 | 387 | entry->mInfo = info; |
michael@0 | 388 | entry->mTimeStamp = lastModifiedTime; |
michael@0 | 389 | mEntries.AppendElement(entry); |
michael@0 | 390 | } |
michael@0 | 391 | |
michael@0 | 392 | return NS_OK; |
michael@0 | 393 | } |
michael@0 | 394 | |
michael@0 | 395 | nsresult |
michael@0 | 396 | CacheFileContextEvictor::GetContextFile(nsILoadContextInfo *aLoadContextInfo, |
michael@0 | 397 | nsIFile **_retval) |
michael@0 | 398 | { |
michael@0 | 399 | nsresult rv; |
michael@0 | 400 | |
michael@0 | 401 | nsAutoCString leafName; |
michael@0 | 402 | leafName.Assign(NS_LITERAL_CSTRING(kContextEvictionPrefix)); |
michael@0 | 403 | |
michael@0 | 404 | nsAutoCString keyPrefix; |
michael@0 | 405 | CacheFileUtils::AppendKeyPrefix(aLoadContextInfo, keyPrefix); |
michael@0 | 406 | |
michael@0 | 407 | // TODO: This hack is needed because current CacheFileUtils::ParseKey() can |
michael@0 | 408 | // parse only the whole key and not just the key prefix generated by |
michael@0 | 409 | // CacheFileUtils::CreateKeyPrefix(). This should be removed once bug #968593 |
michael@0 | 410 | // is fixed. |
michael@0 | 411 | keyPrefix.Append(":foo"); |
michael@0 | 412 | |
michael@0 | 413 | nsAutoCString data64; |
michael@0 | 414 | rv = Base64Encode(keyPrefix, data64); |
michael@0 | 415 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 416 | return rv; |
michael@0 | 417 | } |
michael@0 | 418 | |
michael@0 | 419 | // Replace '/' with '-' since '/' cannot be part of the filename. |
michael@0 | 420 | data64.ReplaceChar('/', '-'); |
michael@0 | 421 | |
michael@0 | 422 | leafName.Append(data64); |
michael@0 | 423 | |
michael@0 | 424 | nsCOMPtr<nsIFile> file; |
michael@0 | 425 | rv = mCacheDirectory->Clone(getter_AddRefs(file)); |
michael@0 | 426 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 427 | return rv; |
michael@0 | 428 | } |
michael@0 | 429 | |
michael@0 | 430 | rv = file->AppendNative(leafName); |
michael@0 | 431 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 432 | return rv; |
michael@0 | 433 | } |
michael@0 | 434 | |
michael@0 | 435 | file.swap(*_retval); |
michael@0 | 436 | return NS_OK; |
michael@0 | 437 | } |
michael@0 | 438 | |
michael@0 | 439 | void |
michael@0 | 440 | CacheFileContextEvictor::CreateIterators() |
michael@0 | 441 | { |
michael@0 | 442 | LOG(("CacheFileContextEvictor::CreateIterators() [this=%p]", this)); |
michael@0 | 443 | |
michael@0 | 444 | CloseIterators(); |
michael@0 | 445 | |
michael@0 | 446 | nsresult rv; |
michael@0 | 447 | |
michael@0 | 448 | for (uint32_t i = 0; i < mEntries.Length(); ) { |
michael@0 | 449 | rv = CacheIndex::GetIterator(mEntries[i]->mInfo, false, |
michael@0 | 450 | getter_AddRefs(mEntries[i]->mIterator)); |
michael@0 | 451 | if (NS_FAILED(rv)) { |
michael@0 | 452 | LOG(("CacheFileContextEvictor::CreateIterators() - Cannot get an iterator" |
michael@0 | 453 | ". [rv=0x%08x]", rv)); |
michael@0 | 454 | mEntries.RemoveElementAt(i); |
michael@0 | 455 | continue; |
michael@0 | 456 | } |
michael@0 | 457 | |
michael@0 | 458 | ++i; |
michael@0 | 459 | } |
michael@0 | 460 | } |
michael@0 | 461 | |
michael@0 | 462 | void |
michael@0 | 463 | CacheFileContextEvictor::CloseIterators() |
michael@0 | 464 | { |
michael@0 | 465 | LOG(("CacheFileContextEvictor::CloseIterators() [this=%p]", this)); |
michael@0 | 466 | |
michael@0 | 467 | for (uint32_t i = 0; i < mEntries.Length(); ++i) { |
michael@0 | 468 | if (mEntries[i]->mIterator) { |
michael@0 | 469 | mEntries[i]->mIterator->Close(); |
michael@0 | 470 | mEntries[i]->mIterator = nullptr; |
michael@0 | 471 | } |
michael@0 | 472 | } |
michael@0 | 473 | } |
michael@0 | 474 | |
michael@0 | 475 | void |
michael@0 | 476 | CacheFileContextEvictor::StartEvicting() |
michael@0 | 477 | { |
michael@0 | 478 | LOG(("CacheFileContextEvictor::StartEvicting() [this=%p]", this)); |
michael@0 | 479 | |
michael@0 | 480 | MOZ_ASSERT(CacheFileIOManager::IsOnIOThread()); |
michael@0 | 481 | |
michael@0 | 482 | if (mEvicting) { |
michael@0 | 483 | LOG(("CacheFileContextEvictor::StartEvicting() - already evicintg.")); |
michael@0 | 484 | return; |
michael@0 | 485 | } |
michael@0 | 486 | |
michael@0 | 487 | if (mEntries.Length() == 0) { |
michael@0 | 488 | LOG(("CacheFileContextEvictor::StartEvicting() - no context to evict.")); |
michael@0 | 489 | return; |
michael@0 | 490 | } |
michael@0 | 491 | |
michael@0 | 492 | nsCOMPtr<nsIRunnable> ev; |
michael@0 | 493 | ev = NS_NewRunnableMethod(this, &CacheFileContextEvictor::EvictEntries); |
michael@0 | 494 | |
michael@0 | 495 | nsRefPtr<CacheIOThread> ioThread = CacheFileIOManager::IOThread(); |
michael@0 | 496 | |
michael@0 | 497 | nsresult rv = ioThread->Dispatch(ev, CacheIOThread::EVICT); |
michael@0 | 498 | if (NS_FAILED(rv)) { |
michael@0 | 499 | LOG(("CacheFileContextEvictor::StartEvicting() - Cannot dispatch event to " |
michael@0 | 500 | "IO thread. [rv=0x%08x]", rv)); |
michael@0 | 501 | } |
michael@0 | 502 | |
michael@0 | 503 | mEvicting = true; |
michael@0 | 504 | } |
michael@0 | 505 | |
michael@0 | 506 | nsresult |
michael@0 | 507 | CacheFileContextEvictor::EvictEntries() |
michael@0 | 508 | { |
michael@0 | 509 | LOG(("CacheFileContextEvictor::EvictEntries()")); |
michael@0 | 510 | |
michael@0 | 511 | nsresult rv; |
michael@0 | 512 | |
michael@0 | 513 | MOZ_ASSERT(CacheFileIOManager::IsOnIOThread()); |
michael@0 | 514 | |
michael@0 | 515 | mEvicting = false; |
michael@0 | 516 | |
michael@0 | 517 | if (!mIndexIsUpToDate) { |
michael@0 | 518 | LOG(("CacheFileContextEvictor::EvictEntries() - Stopping evicting due to " |
michael@0 | 519 | "outdated index.")); |
michael@0 | 520 | return NS_OK; |
michael@0 | 521 | } |
michael@0 | 522 | |
michael@0 | 523 | while (true) { |
michael@0 | 524 | if (CacheIOThread::YieldAndRerun()) { |
michael@0 | 525 | LOG(("CacheFileContextEvictor::EvictEntries() - Breaking loop for higher " |
michael@0 | 526 | "level events.")); |
michael@0 | 527 | mEvicting = true; |
michael@0 | 528 | return NS_OK; |
michael@0 | 529 | } |
michael@0 | 530 | |
michael@0 | 531 | if (mEntries.Length() == 0) { |
michael@0 | 532 | LOG(("CacheFileContextEvictor::EvictEntries() - Stopping evicting, there " |
michael@0 | 533 | "is no context to evict.")); |
michael@0 | 534 | return NS_OK; |
michael@0 | 535 | } |
michael@0 | 536 | |
michael@0 | 537 | SHA1Sum::Hash hash; |
michael@0 | 538 | rv = mEntries[0]->mIterator->GetNextHash(&hash); |
michael@0 | 539 | if (rv == NS_ERROR_NOT_AVAILABLE) { |
michael@0 | 540 | LOG(("CacheFileContextEvictor::EvictEntries() - No more entries left in " |
michael@0 | 541 | "iterator. [iterator=%p, info=%p]", mEntries[0]->mIterator.get(), |
michael@0 | 542 | mEntries[0]->mInfo.get())); |
michael@0 | 543 | RemoveEvictInfoFromDisk(mEntries[0]->mInfo); |
michael@0 | 544 | mEntries.RemoveElementAt(0); |
michael@0 | 545 | continue; |
michael@0 | 546 | } else if (NS_FAILED(rv)) { |
michael@0 | 547 | LOG(("CacheFileContextEvictor::EvictEntries() - Iterator failed to " |
michael@0 | 548 | "provide next hash (shutdown?), keeping eviction info on disk." |
michael@0 | 549 | " [iterator=%p, info=%p]", mEntries[0]->mIterator.get(), |
michael@0 | 550 | mEntries[0]->mInfo.get())); |
michael@0 | 551 | mEntries.RemoveElementAt(0); |
michael@0 | 552 | continue; |
michael@0 | 553 | } |
michael@0 | 554 | |
michael@0 | 555 | LOG(("CacheFileContextEvictor::EvictEntries() - Processing hash. " |
michael@0 | 556 | "[hash=%08x%08x%08x%08x%08x, iterator=%p, info=%p]", LOGSHA1(&hash), |
michael@0 | 557 | mEntries[0]->mIterator.get(), mEntries[0]->mInfo.get())); |
michael@0 | 558 | |
michael@0 | 559 | nsRefPtr<CacheFileHandle> handle; |
michael@0 | 560 | CacheFileIOManager::gInstance->mHandles.GetHandle(&hash, false, |
michael@0 | 561 | getter_AddRefs(handle)); |
michael@0 | 562 | if (handle) { |
michael@0 | 563 | // We doom any active handle in CacheFileIOManager::EvictByContext(), so |
michael@0 | 564 | // this must be a new one. Skip it. |
michael@0 | 565 | LOG(("CacheFileContextEvictor::EvictEntries() - Skipping entry since we " |
michael@0 | 566 | "found an active handle. [handle=%p]", handle.get())); |
michael@0 | 567 | continue; |
michael@0 | 568 | } |
michael@0 | 569 | |
michael@0 | 570 | nsAutoCString leafName; |
michael@0 | 571 | CacheFileIOManager::HashToStr(&hash, leafName); |
michael@0 | 572 | |
michael@0 | 573 | PRTime lastModifiedTime; |
michael@0 | 574 | nsCOMPtr<nsIFile> file; |
michael@0 | 575 | rv = mEntriesDir->Clone(getter_AddRefs(file)); |
michael@0 | 576 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 577 | rv = file->AppendNative(leafName); |
michael@0 | 578 | } |
michael@0 | 579 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 580 | rv = file->GetLastModifiedTime(&lastModifiedTime); |
michael@0 | 581 | } |
michael@0 | 582 | if (NS_FAILED(rv)) { |
michael@0 | 583 | LOG(("CacheFileContextEvictor::EvictEntries() - Cannot get last modified " |
michael@0 | 584 | "time, skipping entry.")); |
michael@0 | 585 | continue; |
michael@0 | 586 | } |
michael@0 | 587 | |
michael@0 | 588 | if (lastModifiedTime > mEntries[0]->mTimeStamp) { |
michael@0 | 589 | LOG(("CacheFileContextEvictor::EvictEntries() - Skipping newer entry. " |
michael@0 | 590 | "[mTimeStamp=%lld, lastModifiedTime=%lld]", mEntries[0]->mTimeStamp, |
michael@0 | 591 | lastModifiedTime)); |
michael@0 | 592 | continue; |
michael@0 | 593 | } |
michael@0 | 594 | |
michael@0 | 595 | LOG(("CacheFileContextEvictor::EvictEntries - Removing entry.")); |
michael@0 | 596 | file->Remove(false); |
michael@0 | 597 | CacheIndex::RemoveEntry(&hash); |
michael@0 | 598 | } |
michael@0 | 599 | |
michael@0 | 600 | NS_NOTREACHED("We should never get here"); |
michael@0 | 601 | return NS_OK; |
michael@0 | 602 | } |
michael@0 | 603 | |
michael@0 | 604 | } // net |
michael@0 | 605 | } // mozilla |