nsCache.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  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
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #include "nsCache.h"
  7. #include "nsReadableUtils.h"
  8. #include "nsDependentSubstring.h"
  9. #include "nsString.h"
  10. /**
  11. * Cache Service Utility Functions
  12. */
  13. mozilla::LazyLogModule gCacheLog("cache");
  14. void
  15. CacheLogPrintPath(mozilla::LogLevel level, const char * format, nsIFile * item)
  16. {
  17. nsAutoCString path;
  18. nsresult rv = item->GetNativePath(path);
  19. if (NS_SUCCEEDED(rv)) {
  20. MOZ_LOG(gCacheLog, level, (format, path.get()));
  21. } else {
  22. MOZ_LOG(gCacheLog, level, ("GetNativePath failed: %x", rv));
  23. }
  24. }
  25. uint32_t
  26. SecondsFromPRTime(PRTime prTime)
  27. {
  28. int64_t microSecondsPerSecond = PR_USEC_PER_SEC;
  29. return uint32_t(prTime / microSecondsPerSecond);
  30. }
  31. PRTime
  32. PRTimeFromSeconds(uint32_t seconds)
  33. {
  34. int64_t intermediateResult = seconds;
  35. PRTime prTime = intermediateResult * PR_USEC_PER_SEC;
  36. return prTime;
  37. }
  38. nsresult
  39. ClientIDFromCacheKey(const nsACString& key, char ** result)
  40. {
  41. nsresult rv = NS_OK;
  42. *result = nullptr;
  43. nsReadingIterator<char> colon;
  44. key.BeginReading(colon);
  45. nsReadingIterator<char> start;
  46. key.BeginReading(start);
  47. nsReadingIterator<char> end;
  48. key.EndReading(end);
  49. if (FindCharInReadable(':', colon, end)) {
  50. *result = ToNewCString( Substring(start, colon));
  51. if (!*result) rv = NS_ERROR_OUT_OF_MEMORY;
  52. } else {
  53. NS_ASSERTION(false, "FindCharInRead failed to find ':'");
  54. rv = NS_ERROR_UNEXPECTED;
  55. }
  56. return rv;
  57. }
  58. nsresult
  59. ClientKeyFromCacheKey(const nsCString& key, nsACString &result)
  60. {
  61. nsresult rv = NS_OK;
  62. nsReadingIterator<char> start;
  63. key.BeginReading(start);
  64. nsReadingIterator<char> end;
  65. key.EndReading(end);
  66. if (FindCharInReadable(':', start, end)) {
  67. ++start; // advance past clientID ':' delimiter
  68. result.Assign(Substring(start, end));
  69. } else {
  70. NS_ASSERTION(false, "FindCharInRead failed to find ':'");
  71. rv = NS_ERROR_UNEXPECTED;
  72. result.Truncate(0);
  73. }
  74. return rv;
  75. }