nsCookie.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsCookie_h__
  6. #define nsCookie_h__
  7. #include "nsICookie.h"
  8. #include "nsICookie2.h"
  9. #include "nsString.h"
  10. #include "mozilla/MemoryReporting.h"
  11. #include "mozilla/BasePrincipal.h"
  12. #include "mozilla/Preferences.h"
  13. using mozilla::OriginAttributes;
  14. /**
  15. * The nsCookie class is the main cookie storage medium for use within cookie
  16. * code. It implements nsICookie2, which extends nsICookie, a frozen interface
  17. * for xpcom access of cookie objects.
  18. */
  19. /******************************************************************************
  20. * nsCookie:
  21. * implementation
  22. ******************************************************************************/
  23. class nsCookie : public nsICookie2
  24. {
  25. public:
  26. // nsISupports
  27. NS_DECL_ISUPPORTS
  28. NS_DECL_NSICOOKIE
  29. NS_DECL_NSICOOKIE2
  30. private:
  31. // for internal use only. see nsCookie::Create().
  32. nsCookie(const char *aName,
  33. const char *aValue,
  34. const char *aHost,
  35. const char *aPath,
  36. const char *aEnd,
  37. int64_t aExpiry,
  38. int64_t aLastAccessed,
  39. int64_t aCreationTime,
  40. bool aIsSession,
  41. bool aIsSecure,
  42. bool aIsHttpOnly,
  43. const OriginAttributes& aOriginAttributes)
  44. : mName(aName)
  45. , mValue(aValue)
  46. , mHost(aHost)
  47. , mPath(aPath)
  48. , mEnd(aEnd)
  49. , mExpiry(aExpiry)
  50. , mLastAccessed(aLastAccessed)
  51. , mCreationTime(aCreationTime)
  52. // Defaults to 60s
  53. , mCookieStaleThreshold(mozilla::Preferences::GetInt("network.cookie.staleThreshold", 60))
  54. , mIsSession(aIsSession)
  55. , mIsSecure(aIsSecure)
  56. , mIsHttpOnly(aIsHttpOnly)
  57. , mOriginAttributes(aOriginAttributes)
  58. {
  59. }
  60. public:
  61. // Generate a unique and monotonically increasing creation time. See comment
  62. // in nsCookie.cpp.
  63. static int64_t GenerateUniqueCreationTime(int64_t aCreationTime);
  64. // public helper to create an nsCookie object. use |operator delete|
  65. // to destroy an object created by this method.
  66. static nsCookie * Create(const nsACString &aName,
  67. const nsACString &aValue,
  68. const nsACString &aHost,
  69. const nsACString &aPath,
  70. int64_t aExpiry,
  71. int64_t aLastAccessed,
  72. int64_t aCreationTime,
  73. bool aIsSession,
  74. bool aIsSecure,
  75. bool aIsHttpOnly,
  76. const OriginAttributes& aOriginAttributes);
  77. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
  78. // fast (inline, non-xpcom) getters
  79. inline const nsDependentCString Name() const { return nsDependentCString(mName, mValue - 1); }
  80. inline const nsDependentCString Value() const { return nsDependentCString(mValue, mHost - 1); }
  81. inline const nsDependentCString Host() const { return nsDependentCString(mHost, mPath - 1); }
  82. inline const nsDependentCString RawHost() const { return nsDependentCString(IsDomain() ? mHost + 1 : mHost, mPath - 1); }
  83. inline const nsDependentCString Path() const { return nsDependentCString(mPath, mEnd); }
  84. inline int64_t Expiry() const { return mExpiry; } // in seconds
  85. inline int64_t LastAccessed() const { return mLastAccessed; } // in microseconds
  86. inline int64_t CreationTime() const { return mCreationTime; } // in microseconds
  87. inline bool IsSession() const { return mIsSession; }
  88. inline bool IsDomain() const { return *mHost == '.'; }
  89. inline bool IsSecure() const { return mIsSecure; }
  90. inline bool IsHttpOnly() const { return mIsHttpOnly; }
  91. // setters
  92. inline void SetExpiry(int64_t aExpiry) { mExpiry = aExpiry; }
  93. inline void SetLastAccessed(int64_t aTime) { mLastAccessed = aTime; }
  94. inline void SetIsSession(bool aIsSession) { mIsSession = aIsSession; }
  95. // Set the creation time manually, overriding the monotonicity checks in
  96. // Create(). Use with caution!
  97. inline void SetCreationTime(int64_t aTime) { mCreationTime = aTime; }
  98. bool IsStale() const;
  99. protected:
  100. virtual ~nsCookie() {}
  101. private:
  102. // member variables
  103. // we use char* ptrs to store the strings in a contiguous block,
  104. // so we save on the overhead of using nsCStrings. However, we
  105. // store a terminating null for each string, so we can hand them
  106. // out as nsAFlatCStrings.
  107. //
  108. // Please update SizeOfIncludingThis if this strategy changes.
  109. const char *mName;
  110. const char *mValue;
  111. const char *mHost;
  112. const char *mPath;
  113. const char *mEnd;
  114. int64_t mExpiry;
  115. int64_t mLastAccessed;
  116. int64_t mCreationTime;
  117. int64_t mCookieStaleThreshold;
  118. bool mIsSession;
  119. bool mIsSecure;
  120. bool mIsHttpOnly;
  121. mozilla::OriginAttributes mOriginAttributes;
  122. };
  123. #endif // nsCookie_h__