diff --git a/netwerk/cookie/nsCookie.cpp b/netwerk/cookie/nsCookie.cpp index d3b2383a32b..f864d9e8649 100644 --- a/netwerk/cookie/nsCookie.cpp +++ b/netwerk/cookie/nsCookie.cpp @@ -3,10 +3,11 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "mozilla/dom/ToJSValue.h" +#include "nsAutoPtr.h" #include "nsCookie.h" #include "nsUTF8ConverterService.h" #include -#include "nsAutoPtr.h" static const int64_t kCookieStaleThreshold = 60 * PR_USEC_PER_SEC; // 1 minute in microseconds @@ -79,7 +80,8 @@ nsCookie::Create(const nsACString &aName, int64_t aCreationTime, bool aIsSession, bool aIsSecure, - bool aIsHttpOnly) + bool aIsHttpOnly, + const OriginAttributes& aOriginAttributes) { // Ensure mValue contains a valid UTF-8 sequence. Otherwise XPConnect will // truncate the string after the first invalid octet. @@ -111,7 +113,8 @@ nsCookie::Create(const nsACString &aName, // construct the cookie. placement new, oh yeah! return new (place) nsCookie(name, value, host, path, end, aExpiry, aLastAccessed, aCreationTime, - aIsSession, aIsSecure, aIsHttpOnly); + aIsSession, aIsSecure, aIsHttpOnly, + aOriginAttributes); } size_t @@ -151,6 +154,15 @@ NS_IMETHODIMP nsCookie::GetPolicy(nsCookiePolicy *aPolicy) { *aPolicy = 0; NS_IMETHODIMP nsCookie::GetCreationTime(int64_t *aCreation){ *aCreation = CreationTime(); return NS_OK; } NS_IMETHODIMP nsCookie::GetLastAccessed(int64_t *aTime) { *aTime = LastAccessed(); return NS_OK; } +NS_IMETHODIMP +nsCookie::GetOriginAttributes(JSContext *aCx, JS::MutableHandle aVal) +{ + if (NS_WARN_IF(!ToJSValue(aCx, mOriginAttributes, aVal))) { + return NS_ERROR_FAILURE; + } + return NS_OK; +} + // compatibility method, for use with the legacy nsICookie interface. // here, expires == 0 denotes a session cookie. NS_IMETHODIMP diff --git a/netwerk/cookie/nsCookie.h b/netwerk/cookie/nsCookie.h index 6e47357fca1..3c06df5d6a0 100644 --- a/netwerk/cookie/nsCookie.h +++ b/netwerk/cookie/nsCookie.h @@ -11,6 +11,9 @@ #include "nsString.h" #include "mozilla/MemoryReporting.h" +#include "mozilla/BasePrincipal.h" + +using mozilla::OriginAttributes; /** * The nsCookie class is the main cookie storage medium for use within cookie @@ -43,7 +46,8 @@ class nsCookie : public nsICookie2 int64_t aCreationTime, bool aIsSession, bool aIsSecure, - bool aIsHttpOnly) + bool aIsHttpOnly, + const OriginAttributes& aOriginAttributes) : mName(aName) , mValue(aValue) , mHost(aHost) @@ -55,6 +59,7 @@ class nsCookie : public nsICookie2 , mIsSession(aIsSession != false) , mIsSecure(aIsSecure != false) , mIsHttpOnly(aIsHttpOnly != false) + , mOriginAttributes(aOriginAttributes) { } @@ -74,7 +79,8 @@ class nsCookie : public nsICookie2 int64_t aCreationTime, bool aIsSession, bool aIsSecure, - bool aIsHttpOnly); + bool aIsHttpOnly, + const OriginAttributes& aOriginAttributes); size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; @@ -123,6 +129,7 @@ class nsCookie : public nsICookie2 bool mIsSession; bool mIsSecure; bool mIsHttpOnly; + mozilla::OriginAttributes mOriginAttributes; }; #endif // nsCookie_h__ diff --git a/netwerk/cookie/nsCookieService.cpp b/netwerk/cookie/nsCookieService.cpp index 8292f528d23..50a3798ff8a 100644 --- a/netwerk/cookie/nsCookieService.cpp +++ b/netwerk/cookie/nsCookieService.cpp @@ -491,7 +491,8 @@ public: row->GetUTF8String(IDX_ORIGIN_ATTRIBUTES, suffix); tuple->key.mOriginAttributes.PopulateFromSuffix(suffix); - tuple->cookie = gCookieService->GetCookieFromRow(row); + tuple->cookie = + gCookieService->GetCookieFromRow(row, tuple->key.mOriginAttributes); } return NS_OK; @@ -2288,6 +2289,7 @@ nsCookieService::Add(const nsACString &aHost, NS_ENSURE_SUCCESS(rv, rv); int64_t currentTimeInUsec = PR_Now(); + nsCookieKey key = DEFAULT_APP_KEY(baseDomain); RefPtr cookie = nsCookie::Create(aName, aValue, host, aPath, @@ -2296,12 +2298,13 @@ nsCookieService::Add(const nsACString &aHost, nsCookie::GenerateUniqueCreationTime(currentTimeInUsec), aIsSession, aIsSecure, - aIsHttpOnly); + aIsHttpOnly, + key.mOriginAttributes); if (!cookie) { return NS_ERROR_OUT_OF_MEMORY; } - AddInternal(DEFAULT_APP_KEY(baseDomain), cookie, currentTimeInUsec, nullptr, nullptr, true); + AddInternal(key, cookie, currentTimeInUsec, nullptr, nullptr, true); return NS_OK; } @@ -2436,7 +2439,7 @@ nsCookieService::Read() // Extract data from a single result row and create an nsCookie. // This is templated since 'T' is different for sync vs async results. template nsCookie* -nsCookieService::GetCookieFromRow(T &aRow) +nsCookieService::GetCookieFromRow(T &aRow, const OriginAttributes& aOriginAttributes) { // Skip reading 'baseDomain' -- up to the caller. nsCString name, value, host, path; @@ -2462,7 +2465,8 @@ nsCookieService::GetCookieFromRow(T &aRow) creationTime, false, isSecure, - isHttpOnly); + isHttpOnly, + aOriginAttributes); } void @@ -2609,7 +2613,8 @@ nsCookieService::EnsureReadDomain(const nsCookieKey &aKey) if (!hasResult) break; - array.AppendElement(GetCookieFromRow(mDefaultDBState->stmtReadDomain)); + array.AppendElement(GetCookieFromRow(mDefaultDBState->stmtReadDomain, + aKey.mOriginAttributes)); } // Add the cookies to the table in a single operation. This makes sure that @@ -2699,7 +2704,7 @@ nsCookieService::EnsureReadComplete() CookieDomainTuple* tuple = array.AppendElement(); tuple->key = key; - tuple->cookie = GetCookieFromRow(stmt); + tuple->cookie = GetCookieFromRow(stmt, attrs); } // Add the cookies to the table in a single operation. This makes sure that @@ -2853,7 +2858,8 @@ nsCookieService::ImportCookies(nsIFile *aCookieFile) nsCookie::GenerateUniqueCreationTime(currentTimeInUsec), false, Substring(buffer, secureIndex, expiresIndex - secureIndex - 1).EqualsLiteral(kTrue), - isHttpOnly); + isHttpOnly, + key.mOriginAttributes); if (!newCookie) { return NS_ERROR_OUT_OF_MEMORY; } @@ -3232,7 +3238,8 @@ nsCookieService::SetCookieInternal(nsIURI *aHostURI, nsCookie::GenerateUniqueCreationTime(currentTimeInUsec), cookieAttributes.isSession, cookieAttributes.isSecure, - cookieAttributes.isHttpOnly); + cookieAttributes.isHttpOnly, + aKey.mOriginAttributes); if (!cookie) return newCookie; diff --git a/netwerk/cookie/nsCookieService.h b/netwerk/cookie/nsCookieService.h index 7973fbbc22e..485d473f328 100644 --- a/netwerk/cookie/nsCookieService.h +++ b/netwerk/cookie/nsCookieService.h @@ -33,6 +33,7 @@ #include "mozilla/MemoryReporting.h" using mozilla::NeckoOriginAttributes; +using mozilla::OriginAttributes; class nsICookiePermission; class nsIEffectiveTLDService; @@ -283,7 +284,7 @@ class nsCookieService final : public nsICookieService void HandleCorruptDB(DBState* aDBState); void RebuildCorruptDB(DBState* aDBState); OpenDBResult Read(); - template nsCookie* GetCookieFromRow(T &aRow); + template nsCookie* GetCookieFromRow(T &aRow, const OriginAttributes& aOriginAttributes); void AsyncReadComplete(); void CancelAsyncRead(bool aPurgeReadSet); void EnsureReadDomain(const nsCookieKey &aKey); diff --git a/netwerk/cookie/nsICookie.idl b/netwerk/cookie/nsICookie.idl index 20fa9cf124a..e8dadb9491e 100644 --- a/netwerk/cookie/nsICookie.idl +++ b/netwerk/cookie/nsICookie.idl @@ -14,8 +14,7 @@ typedef long nsCookieStatus; typedef long nsCookiePolicy; -[scriptable, uuid(8684966b-1877-4f0f-8155-be4490b96bf7)] - +[scriptable, uuid(adf0db5e-211e-45a3-be14-4486ac430a58)] interface nsICookie : nsISupports { /** @@ -78,4 +77,9 @@ interface nsICookie : nsISupports { const nsCookiePolicy POLICY_NO_II=5; readonly attribute nsCookiePolicy policy; + /** + * The origin attributes for this cookie + */ + [implicit_jscontext] + readonly attribute jsval originAttributes; };