Bug 1199466 - part 1 - Expose originAttributes in nsICookie, r=jduell

This commit is contained in:
Andrea Marchesini 2016-01-14 08:03:50 +00:00
parent 4dc034c5c3
commit c77ebf3fee
5 changed files with 48 additions and 17 deletions

View File

@ -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 <stdlib.h>
#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<JS::Value> 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

View File

@ -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__

View File

@ -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<nsCookie> 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<class T> 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;

View File

@ -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<class T> nsCookie* GetCookieFromRow(T &aRow);
template<class T> nsCookie* GetCookieFromRow(T &aRow, const OriginAttributes& aOriginAttributes);
void AsyncReadComplete();
void CancelAsyncRead(bool aPurgeReadSet);
void EnsureReadDomain(const nsCookieKey &aKey);

View File

@ -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;
};