2007-11-18 00:08:34 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-09-18 09:04:04 -07:00
|
|
|
/* vim: set sw=2 ts=8 et tw=80 : */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-12-14 15:58:45 -08:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
#include "mozilla/Likely.h"
|
2010-08-07 08:33:09 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_LOGGING
|
|
|
|
// this next define has to appear before the include of prlog.h
|
|
|
|
#define FORCE_PR_LOG // Allow logging in the release build
|
|
|
|
#endif
|
|
|
|
|
2010-03-25 16:02:28 -07:00
|
|
|
#include "mozilla/net/CookieServiceChild.h"
|
|
|
|
#include "mozilla/net/NeckoCommon.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCookieService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
|
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsICookiePermission.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsILineInputStream.h"
|
2007-12-04 16:03:22 -08:00
|
|
|
#include "nsIEffectiveTLDService.h"
|
2010-01-12 10:29:20 -08:00
|
|
|
#include "nsIIDNService.h"
|
2010-10-19 09:37:03 -07:00
|
|
|
#include "mozIThirdPartyUtil.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-12-15 14:21:28 -08:00
|
|
|
#include "nsTArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCOMArray.h"
|
2009-12-15 14:21:28 -08:00
|
|
|
#include "nsIMutableArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsArrayEnumerator.h"
|
2009-11-18 10:52:33 -08:00
|
|
|
#include "nsEnumeratorUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "prprf.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2008-10-19 14:12:53 -07:00
|
|
|
#include "nsNetCID.h"
|
2010-03-12 07:18:38 -08:00
|
|
|
#include "mozilla/storage.h"
|
2012-02-08 10:37:07 -08:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2013-03-06 11:54:44 -08:00
|
|
|
#include "mozilla/FileUtils.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
2012-09-27 14:37:28 -07:00
|
|
|
#include "nsIAppsService.h"
|
|
|
|
#include "mozIApplication.h"
|
2013-12-02 09:48:12 -08:00
|
|
|
#include "nsIConsoleService.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-04-07 14:25:00 -07:00
|
|
|
using namespace mozilla;
|
2010-03-25 16:02:28 -07:00
|
|
|
using namespace mozilla::net;
|
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
// Create key from baseDomain that will access the default cookie namespace.
|
|
|
|
// TODO: When we figure out what the API will look like for nsICookieManager{2}
|
|
|
|
// on content processes (see bug 777620), change to use the appropriate app
|
|
|
|
// namespace. For now those IDLs aren't supported on child processes.
|
|
|
|
#define DEFAULT_APP_KEY(baseDomain) \
|
|
|
|
nsCookieKey(baseDomain, NECKO_NO_APP_ID, false)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/******************************************************************************
|
|
|
|
* nsCookieService impl:
|
|
|
|
* useful types & constants
|
|
|
|
******************************************************************************/
|
|
|
|
|
2010-07-29 12:17:42 -07:00
|
|
|
static nsCookieService *gCookieService;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// XXX_hack. See bug 178993.
|
|
|
|
// This is a hack to hide HttpOnly cookies from older browsers
|
|
|
|
static const char kHttpOnlyPrefix[] = "#HttpOnly_";
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
#define COOKIES_FILE "cookies.sqlite"
|
2012-09-18 09:04:04 -07:00
|
|
|
#define COOKIES_SCHEMA_VERSION 5
|
2007-11-18 00:08:34 -08:00
|
|
|
|
2013-05-29 17:37:34 -07:00
|
|
|
// parameter indexes; see EnsureReadDomain, EnsureReadComplete and
|
|
|
|
// ReadCookieDBListener::HandleResult
|
|
|
|
#define IDX_NAME 0
|
|
|
|
#define IDX_VALUE 1
|
|
|
|
#define IDX_HOST 2
|
|
|
|
#define IDX_PATH 3
|
|
|
|
#define IDX_EXPIRY 4
|
|
|
|
#define IDX_LAST_ACCESSED 5
|
|
|
|
#define IDX_CREATION_TIME 6
|
|
|
|
#define IDX_SECURE 7
|
|
|
|
#define IDX_HTTPONLY 8
|
|
|
|
#define IDX_BASE_DOMAIN 9
|
|
|
|
#define IDX_APP_ID 10
|
|
|
|
#define IDX_BROWSER_ELEM 11
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static const int64_t kCookieStaleThreshold = 60 * PR_USEC_PER_SEC; // 1 minute in microseconds
|
|
|
|
static const int64_t kCookiePurgeAge =
|
|
|
|
int64_t(30 * 24 * 60 * 60) * PR_USEC_PER_SEC; // 30 days in microseconds
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-06-17 14:52:22 -07:00
|
|
|
static const char kOldCookieFileName[] = "cookies.txt";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#undef LIMIT
|
|
|
|
#define LIMIT(x, low, high, default) ((x) >= (low) && (x) <= (high) ? (x) : (default))
|
|
|
|
|
2009-12-15 14:21:28 -08:00
|
|
|
#undef ADD_TEN_PERCENT
|
2013-02-08 03:49:30 -08:00
|
|
|
#define ADD_TEN_PERCENT(i) static_cast<uint32_t>((i) + (i)/10)
|
2009-12-15 14:21:28 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// default limits for the cookie list. these can be tuned by the
|
|
|
|
// network.cookie.maxNumber and network.cookie.maxPerHost prefs respectively.
|
2012-08-22 08:56:38 -07:00
|
|
|
static const uint32_t kMaxNumberOfCookies = 3000;
|
|
|
|
static const uint32_t kMaxCookiesPerHost = 150;
|
|
|
|
static const uint32_t kMaxBytesPerCookie = 4096;
|
|
|
|
static const uint32_t kMaxBytesPerPath = 1024;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-07 12:32:12 -07:00
|
|
|
// behavior pref constants
|
2013-02-22 08:16:01 -08:00
|
|
|
static const uint32_t BEHAVIOR_ACCEPT = 0; // allow all cookies
|
|
|
|
static const uint32_t BEHAVIOR_REJECTFOREIGN = 1; // reject all third-party cookies
|
|
|
|
static const uint32_t BEHAVIOR_REJECT = 2; // reject all cookies
|
|
|
|
static const uint32_t BEHAVIOR_LIMITFOREIGN = 3; // reject third-party cookies unless the
|
|
|
|
// eTLD already has at least one cookie
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// pref string constants
|
2010-06-07 12:32:12 -07:00
|
|
|
static const char kPrefCookieBehavior[] = "network.cookie.cookieBehavior";
|
2007-03-22 10:30:00 -07:00
|
|
|
static const char kPrefMaxNumberOfCookies[] = "network.cookie.maxNumber";
|
|
|
|
static const char kPrefMaxCookiesPerHost[] = "network.cookie.maxPerHost";
|
2009-12-15 14:21:28 -08:00
|
|
|
static const char kPrefCookiePurgeAge[] = "network.cookie.purgeAge";
|
2010-06-07 12:32:12 -07:00
|
|
|
static const char kPrefThirdPartySession[] = "network.cookie.thirdparty.sessionOnly";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
static void
|
|
|
|
bindCookieParameters(mozIStorageBindingParamsArray *aParamsArray,
|
2012-09-18 09:04:04 -07:00
|
|
|
const nsCookieKey &aKey,
|
2010-11-12 09:32:35 -08:00
|
|
|
const nsCookie *aCookie);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// struct for temporarily storing cookie attributes during header parsing
|
|
|
|
struct nsCookieAttributes
|
|
|
|
{
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString name;
|
|
|
|
nsAutoCString value;
|
|
|
|
nsAutoCString host;
|
|
|
|
nsAutoCString path;
|
|
|
|
nsAutoCString expires;
|
|
|
|
nsAutoCString maxage;
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t expiryTime;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isSession;
|
|
|
|
bool isSecure;
|
|
|
|
bool isHttpOnly;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// stores the nsCookieEntry entryclass and an index into the cookie array
|
|
|
|
// within that entryclass, for purposes of storing an iteration state that
|
|
|
|
// points to a certain cookie.
|
2007-03-22 10:30:00 -07:00
|
|
|
struct nsListIter
|
|
|
|
{
|
2009-12-19 15:55:56 -08:00
|
|
|
// default (non-initializing) constructor.
|
|
|
|
nsListIter()
|
|
|
|
{
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// explicit constructor to a given iterator state with entryclass 'aEntry'
|
|
|
|
// and index 'aIndex'.
|
2009-07-15 17:57:29 -07:00
|
|
|
explicit
|
2009-12-19 15:55:56 -08:00
|
|
|
nsListIter(nsCookieEntry *aEntry, nsCookieEntry::IndexType aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
: entry(aEntry)
|
2009-12-19 15:55:56 -08:00
|
|
|
, index(aIndex)
|
|
|
|
{
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// get the nsCookie * the iterator currently points to.
|
|
|
|
nsCookie * Cookie() const
|
|
|
|
{
|
|
|
|
return entry->GetCookies()[index];
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
nsCookieEntry *entry;
|
|
|
|
nsCookieEntry::IndexType index;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Cookie logging handlers
|
|
|
|
* used for logging in nsCookieService
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
// logging handlers
|
|
|
|
#ifdef MOZ_LOGGING
|
|
|
|
// in order to do logging, the following environment variables need to be set:
|
|
|
|
//
|
|
|
|
// set NSPR_LOG_MODULES=cookie:3 -- shows rejected cookies
|
|
|
|
// set NSPR_LOG_MODULES=cookie:4 -- shows accepted and rejected cookies
|
|
|
|
// set NSPR_LOG_FILE=cookie.log
|
|
|
|
//
|
|
|
|
#include "prlog.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// define logging macros for convenience
|
2011-10-17 07:59:28 -07:00
|
|
|
#define SET_COOKIE true
|
|
|
|
#define GET_COOKIE false
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2012-10-29 16:32:10 -07:00
|
|
|
static PRLogModuleInfo *
|
|
|
|
GetCookieLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sCookieLog;
|
|
|
|
if (!sCookieLog)
|
|
|
|
sCookieLog = PR_NewLogModule("cookie");
|
|
|
|
return sCookieLog;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-01 15:34:16 -07:00
|
|
|
#define COOKIE_LOGFAILURE(a, b, c, d) LogFailure(a, b, c, d)
|
|
|
|
#define COOKIE_LOGSUCCESS(a, b, c, d, e) LogSuccess(a, b, c, d, e)
|
2009-12-15 14:21:28 -08:00
|
|
|
|
2010-08-25 02:32:01 -07:00
|
|
|
#define COOKIE_LOGEVICTED(a, details) \
|
2009-12-15 14:21:28 -08:00
|
|
|
PR_BEGIN_MACRO \
|
2012-10-29 16:32:10 -07:00
|
|
|
if (PR_LOG_TEST(GetCookieLog(), PR_LOG_DEBUG)) \
|
2010-08-25 02:32:01 -07:00
|
|
|
LogEvicted(a, details); \
|
2009-12-15 14:21:28 -08:00
|
|
|
PR_END_MACRO
|
|
|
|
|
2007-11-05 11:29:12 -08:00
|
|
|
#define COOKIE_LOGSTRING(lvl, fmt) \
|
|
|
|
PR_BEGIN_MACRO \
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), lvl, fmt); \
|
|
|
|
PR_LOG(GetCookieLog(), lvl, ("\n")); \
|
2007-11-05 11:29:12 -08:00
|
|
|
PR_END_MACRO
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static void
|
2011-09-28 23:19:26 -07:00
|
|
|
LogFailure(bool aSetCookie, nsIURI *aHostURI, const char *aCookieString, const char *aReason)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// if logging isn't enabled, return now to save cycles
|
2012-10-29 16:32:10 -07:00
|
|
|
if (!PR_LOG_TEST(GetCookieLog(), PR_LOG_WARNING))
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString spec;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aHostURI)
|
|
|
|
aHostURI->GetAsciiSpec(spec);
|
|
|
|
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_WARNING,
|
2007-06-17 14:52:22 -07:00
|
|
|
("===== %s =====\n", aSetCookie ? "COOKIE NOT ACCEPTED" : "COOKIE NOT SENT"));
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_WARNING,("request URL: %s\n", spec.get()));
|
2007-06-17 14:52:22 -07:00
|
|
|
if (aSetCookie)
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_WARNING,("cookie string: %s\n", aCookieString));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PRExplodedTime explodedTime;
|
|
|
|
PR_ExplodeTime(PR_Now(), PR_GMTParameters, &explodedTime);
|
|
|
|
char timeString[40];
|
|
|
|
PR_FormatTimeUSEnglish(timeString, 40, "%c GMT", &explodedTime);
|
|
|
|
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_WARNING,("current time: %s", timeString));
|
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_WARNING,("rejected because %s\n", aReason));
|
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_WARNING,("\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-01 15:34:16 -07:00
|
|
|
LogCookie(nsCookie *aCookie)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PRExplodedTime explodedTime;
|
|
|
|
PR_ExplodeTime(PR_Now(), PR_GMTParameters, &explodedTime);
|
|
|
|
char timeString[40];
|
|
|
|
PR_FormatTimeUSEnglish(timeString, 40, "%c GMT", &explodedTime);
|
|
|
|
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("current time: %s", timeString));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-01 15:34:16 -07:00
|
|
|
if (aCookie) {
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("----------------\n"));
|
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("name: %s\n", aCookie->Name().get()));
|
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("value: %s\n", aCookie->Value().get()));
|
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("%s: %s\n", aCookie->IsDomain() ? "domain" : "host", aCookie->Host().get()));
|
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("path: %s\n", aCookie->Path().get()));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
PR_ExplodeTime(aCookie->Expiry() * int64_t(PR_USEC_PER_SEC),
|
2011-07-26 13:34:11 -07:00
|
|
|
PR_GMTParameters, &explodedTime);
|
2007-06-17 14:52:22 -07:00
|
|
|
PR_FormatTimeUSEnglish(timeString, 40, "%c GMT", &explodedTime);
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,
|
2007-06-17 14:52:22 -07:00
|
|
|
("expires: %s%s", timeString, aCookie->IsSession() ? " (at end of session)" : ""));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
PR_ExplodeTime(aCookie->CreationTime(), PR_GMTParameters, &explodedTime);
|
2007-06-17 14:52:22 -07:00
|
|
|
PR_FormatTimeUSEnglish(timeString, 40, "%c GMT", &explodedTime);
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("created: %s", timeString));
|
2007-06-17 14:52:22 -07:00
|
|
|
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("is secure: %s\n", aCookie->IsSecure() ? "true" : "false"));
|
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("is httpOnly: %s\n", aCookie->IsHttpOnly() ? "true" : "false"));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-11-01 15:34:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-09-28 23:19:26 -07:00
|
|
|
LogSuccess(bool aSetCookie, nsIURI *aHostURI, const char *aCookieString, nsCookie *aCookie, bool aReplacing)
|
2007-11-01 15:34:16 -07:00
|
|
|
{
|
|
|
|
// if logging isn't enabled, return now to save cycles
|
2012-10-29 16:32:10 -07:00
|
|
|
if (!PR_LOG_TEST(GetCookieLog(), PR_LOG_DEBUG)) {
|
2007-11-01 15:34:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString spec;
|
2007-11-01 15:34:16 -07:00
|
|
|
if (aHostURI)
|
|
|
|
aHostURI->GetAsciiSpec(spec);
|
|
|
|
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,
|
2007-11-01 15:34:16 -07:00
|
|
|
("===== %s =====\n", aSetCookie ? "COOKIE ACCEPTED" : "COOKIE SENT"));
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("request URL: %s\n", spec.get()));
|
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("cookie string: %s\n", aCookieString));
|
2007-11-01 15:34:16 -07:00
|
|
|
if (aSetCookie)
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("replaces existing cookie: %s\n", aReplacing ? "true" : "false"));
|
2007-11-01 15:34:16 -07:00
|
|
|
|
|
|
|
LogCookie(aCookie);
|
|
|
|
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("\n"));
|
2007-11-01 15:34:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-25 02:32:01 -07:00
|
|
|
LogEvicted(nsCookie *aCookie, const char* details)
|
2007-11-01 15:34:16 -07:00
|
|
|
{
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("===== COOKIE EVICTED =====\n"));
|
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("%s\n", details));
|
2007-11-01 15:34:16 -07:00
|
|
|
|
|
|
|
LogCookie(aCookie);
|
|
|
|
|
2012-10-29 16:32:10 -07:00
|
|
|
PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// inline wrappers to make passing in nsAFlatCStrings easier
|
|
|
|
static inline void
|
2011-09-28 23:19:26 -07:00
|
|
|
LogFailure(bool aSetCookie, nsIURI *aHostURI, const nsAFlatCString &aCookieString, const char *aReason)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
LogFailure(aSetCookie, aHostURI, aCookieString.get(), aReason);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2011-09-28 23:19:26 -07:00
|
|
|
LogSuccess(bool aSetCookie, nsIURI *aHostURI, const nsAFlatCString &aCookieString, nsCookie *aCookie, bool aReplacing)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-11-01 15:34:16 -07:00
|
|
|
LogSuccess(aSetCookie, aHostURI, aCookieString.get(), aCookie, aReplacing);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2007-11-05 11:29:12 -08:00
|
|
|
#define COOKIE_LOGFAILURE(a, b, c, d) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
|
|
|
|
#define COOKIE_LOGSUCCESS(a, b, c, d, e) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
|
2010-08-31 10:31:48 -07:00
|
|
|
#define COOKIE_LOGEVICTED(a, b) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
|
2007-11-05 11:29:12 -08:00
|
|
|
#define COOKIE_LOGSTRING(a, b) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2010-03-12 07:18:38 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
#define NS_ASSERT_SUCCESS(res) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
nsresult __rv = res; /* Do not evaluate |res| more than once! */ \
|
|
|
|
if (NS_FAILED(__rv)) { \
|
|
|
|
char *msg = PR_smprintf("NS_ASSERT_SUCCESS(%s) failed with result 0x%X", \
|
|
|
|
#res, __rv); \
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(__rv), msg); \
|
|
|
|
PR_smprintf_free(msg); \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
#else
|
|
|
|
#define NS_ASSERT_SUCCESS(res) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/******************************************************************************
|
2010-07-29 12:17:42 -07:00
|
|
|
* DBListenerErrorHandler impl:
|
2010-03-12 07:18:38 -08:00
|
|
|
* Parent class for our async storage listeners that handles the logging of
|
|
|
|
* errors.
|
|
|
|
******************************************************************************/
|
|
|
|
class DBListenerErrorHandler : public mozIStorageStatementCallback
|
|
|
|
{
|
|
|
|
protected:
|
2010-11-12 09:32:35 -08:00
|
|
|
DBListenerErrorHandler(DBState* dbState) : mDBState(dbState) { }
|
|
|
|
nsRefPtr<DBState> mDBState;
|
2010-03-12 07:18:38 -08:00
|
|
|
virtual const char *GetOpType() = 0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_IMETHOD HandleError(mozIStorageError* aError)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t result = -1;
|
2010-03-12 07:18:38 -08:00
|
|
|
aError->GetResult(&result);
|
2010-11-12 09:32:35 -08:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString message;
|
2010-03-12 07:18:38 -08:00
|
|
|
aError->GetMessage(message);
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_WARNING,
|
2010-11-12 09:32:35 -08:00
|
|
|
("DBListenerErrorHandler::HandleError(): Error %d occurred while "
|
|
|
|
"performing operation '%s' with message '%s'; rebuilding database.",
|
|
|
|
result, GetOpType(), message.get()));
|
2010-03-12 07:18:38 -08:00
|
|
|
#endif
|
2010-11-12 09:32:35 -08:00
|
|
|
|
|
|
|
// Rebuild the database.
|
|
|
|
gCookieService->HandleCorruptDB(mDBState);
|
|
|
|
|
2010-03-12 07:18:38 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
2010-06-21 17:50:35 -07:00
|
|
|
|
2010-03-12 07:18:38 -08:00
|
|
|
/******************************************************************************
|
2010-07-29 12:17:42 -07:00
|
|
|
* InsertCookieDBListener impl:
|
|
|
|
* mozIStorageStatementCallback used to track asynchronous insertion operations.
|
2010-03-12 07:18:38 -08:00
|
|
|
******************************************************************************/
|
2012-06-05 20:18:25 -07:00
|
|
|
class InsertCookieDBListener MOZ_FINAL : public DBListenerErrorHandler
|
2010-03-12 07:18:38 -08:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
virtual const char *GetOpType() { return "INSERT"; }
|
|
|
|
|
|
|
|
public:
|
2012-06-05 20:18:25 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
InsertCookieDBListener(DBState* dbState) : DBListenerErrorHandler(dbState) { }
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_IMETHOD HandleResult(mozIStorageResultSet*)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Unexpected call to InsertCookieDBListener::HandleResult");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_IMETHOD HandleCompletion(uint16_t aReason)
|
2010-03-12 07:18:38 -08:00
|
|
|
{
|
2010-11-12 09:32:35 -08:00
|
|
|
// If we were rebuilding the db and we succeeded, make our corruptFlag say
|
|
|
|
// so.
|
|
|
|
if (mDBState->corruptFlag == DBState::REBUILDING &&
|
|
|
|
aReason == mozIStorageStatementCallback::REASON_FINISHED) {
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
|
|
|
("InsertCookieDBListener::HandleCompletion(): rebuild complete"));
|
|
|
|
mDBState->corruptFlag = DBState::OK;
|
|
|
|
}
|
2010-03-12 07:18:38 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(InsertCookieDBListener, mozIStorageStatementCallback)
|
2012-06-05 20:18:25 -07:00
|
|
|
|
2010-03-12 07:18:38 -08:00
|
|
|
/******************************************************************************
|
2010-07-29 12:17:42 -07:00
|
|
|
* UpdateCookieDBListener impl:
|
|
|
|
* mozIStorageStatementCallback used to track asynchronous update operations.
|
2010-03-12 07:18:38 -08:00
|
|
|
******************************************************************************/
|
2012-06-05 20:18:25 -07:00
|
|
|
class UpdateCookieDBListener MOZ_FINAL : public DBListenerErrorHandler
|
2010-03-12 07:18:38 -08:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
virtual const char *GetOpType() { return "UPDATE"; }
|
|
|
|
|
|
|
|
public:
|
2012-06-05 20:18:25 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
UpdateCookieDBListener(DBState* dbState) : DBListenerErrorHandler(dbState) { }
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_IMETHOD HandleResult(mozIStorageResultSet*)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Unexpected call to UpdateCookieDBListener::HandleResult");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_IMETHOD HandleCompletion(uint16_t aReason)
|
2010-03-12 07:18:38 -08:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(UpdateCookieDBListener, mozIStorageStatementCallback)
|
2012-06-05 20:18:25 -07:00
|
|
|
|
2010-03-12 07:18:38 -08:00
|
|
|
/******************************************************************************
|
2010-07-29 12:17:42 -07:00
|
|
|
* RemoveCookieDBListener impl:
|
|
|
|
* mozIStorageStatementCallback used to track asynchronous removal operations.
|
2010-03-12 07:18:38 -08:00
|
|
|
******************************************************************************/
|
2012-06-05 20:18:25 -07:00
|
|
|
class RemoveCookieDBListener MOZ_FINAL : public DBListenerErrorHandler
|
2010-03-12 07:18:38 -08:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
virtual const char *GetOpType() { return "REMOVE"; }
|
|
|
|
|
|
|
|
public:
|
2012-06-05 20:18:25 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
RemoveCookieDBListener(DBState* dbState) : DBListenerErrorHandler(dbState) { }
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_IMETHOD HandleResult(mozIStorageResultSet*)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Unexpected call to RemoveCookieDBListener::HandleResult");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_IMETHOD HandleCompletion(uint16_t aReason)
|
2010-03-12 07:18:38 -08:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(RemoveCookieDBListener, mozIStorageStatementCallback)
|
2012-06-05 20:18:25 -07:00
|
|
|
|
2010-07-29 12:17:42 -07:00
|
|
|
/******************************************************************************
|
|
|
|
* ReadCookieDBListener impl:
|
|
|
|
* mozIStorageStatementCallback used to track asynchronous removal operations.
|
|
|
|
******************************************************************************/
|
2012-06-05 20:18:25 -07:00
|
|
|
class ReadCookieDBListener MOZ_FINAL : public DBListenerErrorHandler
|
2010-07-29 12:17:42 -07:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
virtual const char *GetOpType() { return "READ"; }
|
|
|
|
bool mCanceled;
|
|
|
|
|
|
|
|
public:
|
2012-06-05 20:18:25 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
ReadCookieDBListener(DBState* dbState)
|
|
|
|
: DBListenerErrorHandler(dbState)
|
|
|
|
, mCanceled(false)
|
|
|
|
{
|
|
|
|
}
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
void Cancel() { mCanceled = true; }
|
|
|
|
|
|
|
|
NS_IMETHOD HandleResult(mozIStorageResultSet *aResult)
|
|
|
|
{
|
|
|
|
nsCOMPtr<mozIStorageRow> row;
|
|
|
|
|
|
|
|
while (1) {
|
2012-10-25 12:47:55 -07:00
|
|
|
DebugOnly<nsresult> rv = aResult->GetNextRow(getter_AddRefs(row));
|
2010-07-29 12:17:42 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
|
|
|
if (!row)
|
|
|
|
break;
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
CookieDomainTuple *tuple = mDBState->hostArray.AppendElement();
|
2013-05-29 17:37:34 -07:00
|
|
|
row->GetUTF8String(IDX_BASE_DOMAIN, tuple->key.mBaseDomain);
|
|
|
|
tuple->key.mAppId = static_cast<uint32_t>(row->AsInt32(IDX_APP_ID));
|
|
|
|
tuple->key.mInBrowserElement = static_cast<bool>(row->AsInt32(IDX_BROWSER_ELEM));
|
2010-07-29 12:17:42 -07:00
|
|
|
tuple->cookie = gCookieService->GetCookieFromRow(row);
|
2010-07-29 12:17:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_IMETHOD HandleCompletion(uint16_t aReason)
|
2010-07-29 12:17:42 -07:00
|
|
|
{
|
|
|
|
// Process the completion of the read operation. If we have been canceled,
|
|
|
|
// we cannot assume that the cookieservice still has an open connection
|
|
|
|
// or that it even refers to the same database, so we must return early.
|
|
|
|
// Conversely, the cookieservice guarantees that if we have not been
|
|
|
|
// canceled, the database connection is still alive and we can safely
|
|
|
|
// operate on it.
|
|
|
|
|
|
|
|
if (mCanceled) {
|
|
|
|
// We may receive a REASON_FINISHED after being canceled;
|
|
|
|
// tweak the reason accordingly.
|
|
|
|
aReason = mozIStorageStatementCallback::REASON_CANCELED;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aReason) {
|
|
|
|
case mozIStorageStatementCallback::REASON_FINISHED:
|
2010-07-29 12:17:42 -07:00
|
|
|
gCookieService->AsyncReadComplete();
|
2010-07-29 12:17:42 -07:00
|
|
|
break;
|
|
|
|
case mozIStorageStatementCallback::REASON_CANCELED:
|
|
|
|
// Nothing more to do here. The partially read data has already been
|
|
|
|
// thrown away.
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG, ("Read canceled"));
|
|
|
|
break;
|
|
|
|
case mozIStorageStatementCallback::REASON_ERROR:
|
|
|
|
// Nothing more to do here. DBListenerErrorHandler::HandleError()
|
2010-11-12 09:32:35 -08:00
|
|
|
// can handle it.
|
2010-07-29 12:17:42 -07:00
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG, ("Read error"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("invalid reason");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(ReadCookieDBListener, mozIStorageStatementCallback)
|
2012-06-05 20:18:25 -07:00
|
|
|
|
2010-06-21 17:50:35 -07:00
|
|
|
/******************************************************************************
|
|
|
|
* CloseCookieDBListener imp:
|
|
|
|
* Static mozIStorageCompletionCallback used to notify when the database is
|
|
|
|
* successfully closed.
|
|
|
|
******************************************************************************/
|
2012-06-05 20:18:25 -07:00
|
|
|
class CloseCookieDBListener MOZ_FINAL : public mozIStorageCompletionCallback
|
2010-06-21 17:50:35 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-11-12 09:32:35 -08:00
|
|
|
CloseCookieDBListener(DBState* dbState) : mDBState(dbState) { }
|
|
|
|
nsRefPtr<DBState> mDBState;
|
2010-06-21 17:50:35 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2013-06-27 06:00:59 -07:00
|
|
|
NS_IMETHOD Complete(nsresult, nsISupports*)
|
2010-06-21 17:50:35 -07:00
|
|
|
{
|
2010-11-12 09:32:35 -08:00
|
|
|
gCookieService->HandleDBClosed(mDBState);
|
2010-06-21 17:50:35 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(CloseCookieDBListener, mozIStorageCompletionCallback)
|
2010-03-12 07:18:38 -08:00
|
|
|
|
2012-09-27 14:37:28 -07:00
|
|
|
namespace {
|
|
|
|
|
2012-09-28 17:03:00 -07:00
|
|
|
class AppClearDataObserver MOZ_FINAL : public nsIObserver {
|
2012-09-27 14:37:28 -07:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIObserver implementation.
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 07:02:17 -08:00
|
|
|
Observe(nsISupports *aSubject, const char *aTopic, const char16_t *data)
|
2012-09-27 14:37:28 -07:00
|
|
|
{
|
2012-09-28 17:03:00 -07:00
|
|
|
MOZ_ASSERT(!nsCRT::strcmp(aTopic, TOPIC_WEB_APP_CLEAR_DATA));
|
|
|
|
|
|
|
|
uint32_t appId = NECKO_UNKNOWN_APP_ID;
|
|
|
|
bool browserOnly = false;
|
|
|
|
nsresult rv = NS_GetAppInfoFromClearDataNotification(aSubject, &appId,
|
|
|
|
&browserOnly);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsICookieManager2> cookieManager
|
|
|
|
= do_GetService(NS_COOKIEMANAGER_CONTRACTID);
|
|
|
|
MOZ_ASSERT(cookieManager);
|
|
|
|
return cookieManager->RemoveCookiesForApp(appId, browserOnly);
|
2012-09-27 14:37:28 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(AppClearDataObserver, nsIObserver)
|
2012-09-27 14:37:28 -07:00
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2013-12-03 13:58:41 -08:00
|
|
|
size_t
|
|
|
|
nsCookieKey::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return mBaseDomain.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
nsCookieEntry::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = nsCookieKey::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
|
|
|
|
amount += mCookies.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
for (uint32_t i = 0; i < mCookies.Length(); ++i) {
|
|
|
|
amount += mCookies[i]->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
HostTableEntrySizeOfExcludingThis(nsCookieEntry *aEntry,
|
|
|
|
MallocSizeOf aMallocSizeOf,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
return aEntry->SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
CookieDomainTuple::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = 0;
|
|
|
|
|
|
|
|
amount += key.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += cookie->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
ReadSetEntrySizeOfExcludingThis(nsCookieKey *aEntry,
|
|
|
|
MallocSizeOf aMallocSizeOf,
|
|
|
|
void *)
|
|
|
|
{
|
|
|
|
return aEntry->SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
DBState::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = 0;
|
|
|
|
|
|
|
|
amount += aMallocSizeOf(this);
|
|
|
|
amount += hostTable.SizeOfExcludingThis(HostTableEntrySizeOfExcludingThis,
|
|
|
|
aMallocSizeOf);
|
|
|
|
amount += hostArray.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
for (uint32_t i = 0; i < hostArray.Length(); ++i) {
|
|
|
|
amount += hostArray[i].SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
amount += readSet.SizeOfExcludingThis(ReadSetEntrySizeOfExcludingThis,
|
|
|
|
aMallocSizeOf);
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/******************************************************************************
|
|
|
|
* nsCookieService impl:
|
|
|
|
* singleton instance ctor/dtor methods
|
|
|
|
******************************************************************************/
|
|
|
|
|
2010-03-25 16:02:28 -07:00
|
|
|
nsICookieService*
|
|
|
|
nsCookieService::GetXPCOMSingleton()
|
|
|
|
{
|
|
|
|
if (IsNeckoChild())
|
|
|
|
return CookieServiceChild::GetSingleton();
|
|
|
|
|
|
|
|
return GetSingleton();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCookieService*
|
|
|
|
nsCookieService::GetSingleton()
|
|
|
|
{
|
2010-03-25 16:02:28 -07:00
|
|
|
NS_ASSERTION(!IsNeckoChild(), "not a parent process");
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gCookieService) {
|
|
|
|
NS_ADDREF(gCookieService);
|
|
|
|
return gCookieService;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new singleton nsCookieService.
|
|
|
|
// We AddRef only once since XPCOM has rules about the ordering of module
|
|
|
|
// teardowns - by the time our module destructor is called, it's too late to
|
|
|
|
// Release our members (e.g. nsIObserverService and nsIPrefBranch), since GC
|
|
|
|
// cycles have already been completed and would result in serious leaks.
|
|
|
|
// See bug 209571.
|
|
|
|
gCookieService = new nsCookieService();
|
|
|
|
if (gCookieService) {
|
|
|
|
NS_ADDREF(gCookieService);
|
|
|
|
if (NS_FAILED(gCookieService->Init())) {
|
|
|
|
NS_RELEASE(gCookieService);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gCookieService;
|
|
|
|
}
|
|
|
|
|
2012-09-27 14:37:28 -07:00
|
|
|
/* static */ void
|
2012-09-28 17:03:00 -07:00
|
|
|
nsCookieService::AppClearDataObserverInit()
|
2012-09-27 14:37:28 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService = do_GetService("@mozilla.org/observer-service;1");
|
2014-04-03 15:06:26 -07:00
|
|
|
nsCOMPtr<nsIObserver> obs = new AppClearDataObserver();
|
2012-09-28 17:03:00 -07:00
|
|
|
observerService->AddObserver(obs, TOPIC_WEB_APP_CLEAR_DATA,
|
|
|
|
/* holdsWeak= */ false);
|
2012-09-27 14:37:28 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/******************************************************************************
|
|
|
|
* nsCookieService impl:
|
|
|
|
* public methods
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsCookieService,
|
|
|
|
nsICookieService,
|
|
|
|
nsICookieManager,
|
|
|
|
nsICookieManager2,
|
|
|
|
nsIObserver,
|
|
|
|
nsISupportsWeakReference,
|
|
|
|
nsIMemoryReporter)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCookieService::nsCookieService()
|
2013-12-07 22:09:10 -08:00
|
|
|
: mDBState(nullptr)
|
2010-06-07 12:32:12 -07:00
|
|
|
, mCookieBehavior(BEHAVIOR_ACCEPT)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mThirdPartySession(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mMaxNumberOfCookies(kMaxNumberOfCookies)
|
|
|
|
, mMaxCookiesPerHost(kMaxCookiesPerHost)
|
2009-12-15 14:21:28 -08:00
|
|
|
, mCookiePurgeAge(kCookiePurgeAge)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCookieService::Init()
|
|
|
|
{
|
2007-12-04 16:03:22 -08:00
|
|
|
nsresult rv;
|
|
|
|
mTLDService = do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-01-12 10:29:20 -08:00
|
|
|
mIDNService = do_GetService(NS_IDNSERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-14 13:57:56 -07:00
|
|
|
mThirdPartyUtil = do_GetService(THIRDPARTYUTIL_CONTRACTID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// init our pref and observer
|
2012-01-16 17:48:29 -08:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (prefBranch) {
|
2011-10-17 07:59:28 -07:00
|
|
|
prefBranch->AddObserver(kPrefCookieBehavior, this, true);
|
|
|
|
prefBranch->AddObserver(kPrefMaxNumberOfCookies, this, true);
|
|
|
|
prefBranch->AddObserver(kPrefMaxCookiesPerHost, this, true);
|
|
|
|
prefBranch->AddObserver(kPrefCookiePurgeAge, this, true);
|
|
|
|
prefBranch->AddObserver(kPrefThirdPartySession, this, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
PrefChanged(prefBranch);
|
|
|
|
}
|
|
|
|
|
2010-07-29 13:43:34 -07:00
|
|
|
mStorageService = do_GetService("@mozilla.org/storage/service;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
// Init our default, and possibly private DBStates.
|
|
|
|
InitDBStates();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-12-03 13:58:41 -08:00
|
|
|
RegisterWeakMemoryReporter(this);
|
|
|
|
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 09:59:13 -07:00
|
|
|
mObserverService = mozilla::services::GetObserverService();
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_STATE(mObserverService);
|
2011-10-17 07:59:28 -07:00
|
|
|
mObserverService->AddObserver(this, "profile-before-change", true);
|
|
|
|
mObserverService->AddObserver(this, "profile-do-change", true);
|
2012-02-08 10:37:07 -08:00
|
|
|
mObserverService->AddObserver(this, "last-pb-context-exited", true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mPermissionService = do_GetService(NS_COOKIEPERMISSION_CONTRACTID);
|
2008-03-16 02:30:29 -07:00
|
|
|
if (!mPermissionService) {
|
2008-03-12 00:53:50 -07:00
|
|
|
NS_WARNING("nsICookiePermission implementation not available - some features won't work!");
|
2008-03-16 02:30:29 -07:00
|
|
|
COOKIE_LOGSTRING(PR_LOG_WARNING, ("Init(): nsICookiePermission implementation not available"));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
void
|
|
|
|
nsCookieService::InitDBStates()
|
2007-06-17 14:52:22 -07:00
|
|
|
{
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ASSERTION(!mDBState, "already have a DBState");
|
|
|
|
NS_ASSERTION(!mDefaultDBState, "already have a default DBState");
|
|
|
|
NS_ASSERTION(!mPrivateDBState, "already have a private DBState");
|
2010-11-12 09:32:35 -08:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
// Create a new default DBState and set our current one.
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState = new DBState();
|
|
|
|
mDBState = mDefaultDBState;
|
2009-10-16 14:01:47 -07:00
|
|
|
|
2012-02-08 10:37:07 -08:00
|
|
|
mPrivateDBState = new DBState();
|
2010-11-12 09:32:35 -08:00
|
|
|
|
|
|
|
// Get our cookie file.
|
|
|
|
nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
|
|
|
|
getter_AddRefs(mDefaultDBState->cookieFile));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// We've already set up our DBStates appropriately; nothing more to do.
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_WARNING,
|
|
|
|
("InitDBStates(): couldn't get cookie file"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mDefaultDBState->cookieFile->AppendNative(NS_LITERAL_CSTRING(COOKIES_FILE));
|
|
|
|
|
|
|
|
// Attempt to open and read the database. If TryInitDB() returns RESULT_RETRY,
|
|
|
|
// do so.
|
|
|
|
OpenDBResult result = TryInitDB(false);
|
|
|
|
if (result == RESULT_RETRY) {
|
|
|
|
// Database may be corrupt. Synchronously close the connection, clean up the
|
|
|
|
// default DBState, and try again.
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_WARNING, ("InitDBStates(): retrying TryInitDB()"));
|
2013-07-23 13:41:23 -07:00
|
|
|
CleanupCachedStatements();
|
|
|
|
CleanupDefaultDBConnection();
|
2010-11-12 09:32:35 -08:00
|
|
|
result = TryInitDB(true);
|
|
|
|
if (result == RESULT_RETRY) {
|
|
|
|
// We're done. Change the code to failure so we clean up below.
|
|
|
|
result = RESULT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == RESULT_FAILURE) {
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_WARNING,
|
|
|
|
("InitDBStates(): TryInitDB() failed, closing connection"));
|
|
|
|
|
|
|
|
// Connection failure is unrecoverable. Clean up our connection. We can run
|
|
|
|
// fine without persistent storage -- e.g. if there's no profile.
|
2013-07-23 13:41:23 -07:00
|
|
|
CleanupCachedStatements();
|
|
|
|
CleanupDefaultDBConnection();
|
2010-11-12 09:32:35 -08:00
|
|
|
}
|
2009-10-16 14:01:47 -07:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
/* Attempt to open and read the database. If 'aRecreateDB' is true, try to
|
|
|
|
* move the existing database file out of the way and create a new one.
|
|
|
|
*
|
|
|
|
* @returns RESULT_OK if opening or creating the database succeeded;
|
|
|
|
* RESULT_RETRY if the database cannot be opened, is corrupt, or some
|
|
|
|
* other failure occurred that might be resolved by recreating the
|
|
|
|
* database; or RESULT_FAILED if there was an unrecoverable error and
|
|
|
|
* we must run without a database.
|
|
|
|
*
|
|
|
|
* If RESULT_RETRY or RESULT_FAILED is returned, the caller should perform
|
|
|
|
* cleanup of the default DBState.
|
|
|
|
*/
|
|
|
|
OpenDBResult
|
|
|
|
nsCookieService::TryInitDB(bool aRecreateDB)
|
2009-10-16 14:01:47 -07:00
|
|
|
{
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ASSERTION(!mDefaultDBState->dbConn, "nonnull dbConn");
|
|
|
|
NS_ASSERTION(!mDefaultDBState->stmtInsert, "nonnull stmtInsert");
|
|
|
|
NS_ASSERTION(!mDefaultDBState->insertListener, "nonnull insertListener");
|
|
|
|
NS_ASSERTION(!mDefaultDBState->syncConn, "nonnull syncConn");
|
2008-12-10 15:54:57 -08:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
// Ditch an existing db, if we've been told to (i.e. it's corrupt). We don't
|
|
|
|
// want to delete it outright, since it may be useful for debugging purposes,
|
|
|
|
// so we move it out of the way.
|
|
|
|
nsresult rv;
|
|
|
|
if (aRecreateDB) {
|
|
|
|
nsCOMPtr<nsIFile> backupFile;
|
|
|
|
mDefaultDBState->cookieFile->Clone(getter_AddRefs(backupFile));
|
2013-09-19 12:28:26 -07:00
|
|
|
rv = backupFile->MoveToNative(nullptr,
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_LITERAL_CSTRING(COOKIES_FILE ".bak"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_FAILURE);
|
2009-02-03 19:26:59 -08:00
|
|
|
}
|
|
|
|
|
2013-04-02 19:44:08 -07:00
|
|
|
// This block provides scope for the Telemetry AutoTimer
|
|
|
|
{
|
|
|
|
Telemetry::AutoTimer<Telemetry::MOZ_SQLITE_COOKIES_OPEN_READAHEAD_MS>
|
|
|
|
telemetry;
|
2013-03-06 11:54:44 -08:00
|
|
|
ReadAheadFile(mDefaultDBState->cookieFile);
|
|
|
|
|
2013-04-02 19:44:08 -07:00
|
|
|
// open a connection to the cookie database, and only cache our connection
|
|
|
|
// and statements upon success. The connection is opened unshared to eliminate
|
|
|
|
// cache contention between the main and background threads.
|
|
|
|
rv = mStorageService->OpenUnsharedDatabase(mDefaultDBState->cookieFile,
|
|
|
|
getter_AddRefs(mDefaultDBState->dbConn));
|
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
|
|
|
}
|
2007-06-17 14:52:22 -07:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
// Set up our listeners.
|
|
|
|
mDefaultDBState->insertListener = new InsertCookieDBListener(mDefaultDBState);
|
|
|
|
mDefaultDBState->updateListener = new UpdateCookieDBListener(mDefaultDBState);
|
|
|
|
mDefaultDBState->removeListener = new RemoveCookieDBListener(mDefaultDBState);
|
|
|
|
mDefaultDBState->closeListener = new CloseCookieDBListener(mDefaultDBState);
|
|
|
|
|
2010-09-01 18:35:46 -07:00
|
|
|
// Grow cookie db in 512KB increments
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->dbConn->SetGrowthIncrement(512 * 1024, EmptyCString());
|
2010-09-01 18:35:46 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool tableExists = false;
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->dbConn->TableExists(NS_LITERAL_CSTRING("moz_cookies"),
|
|
|
|
&tableExists);
|
2007-06-17 14:52:22 -07:00
|
|
|
if (!tableExists) {
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = CreateTable();
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2007-06-17 14:52:22 -07:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// table already exists; check the schema version before reading
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t dbSchemaVersion;
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->GetSchemaVersion(&dbSchemaVersion);
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2007-06-17 14:52:22 -07:00
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
// Start a transaction for the whole migration block.
|
2011-10-17 07:59:28 -07:00
|
|
|
mozStorageTransaction transaction(mDefaultDBState->dbConn, true);
|
2010-10-19 17:24:52 -07:00
|
|
|
|
2007-11-18 00:08:34 -08:00
|
|
|
switch (dbSchemaVersion) {
|
2010-11-12 09:32:35 -08:00
|
|
|
// Upgrading.
|
|
|
|
// Every time you increment the database schema, you need to implement
|
|
|
|
// the upgrading code from the previous version to the new one. If migration
|
|
|
|
// fails for any reason, it's a bug -- so we return RESULT_RETRY such that
|
|
|
|
// the original database will be saved, in the hopes that we might one day
|
|
|
|
// see it and fix it.
|
2007-11-18 00:08:34 -08:00
|
|
|
case 1:
|
|
|
|
{
|
2010-07-29 12:17:42 -07:00
|
|
|
// Add the lastAccessed column to the table.
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2007-11-18 00:08:34 -08:00
|
|
|
"ALTER TABLE moz_cookies ADD lastAccessed INTEGER"));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-07-29 12:17:42 -07:00
|
|
|
}
|
|
|
|
// Fall through to the next upgrade.
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
// Add the baseDomain column and index to the table.
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2010-07-29 12:17:42 -07:00
|
|
|
"ALTER TABLE moz_cookies ADD baseDomain TEXT"));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
// Compute the baseDomains for the table. This must be done eagerly
|
|
|
|
// otherwise we won't be able to synchronously read in individual
|
|
|
|
// domains on demand.
|
2013-05-29 17:37:34 -07:00
|
|
|
const int64_t SCHEMA2_IDX_ID = 0;
|
|
|
|
const int64_t SCHEMA2_IDX_HOST = 1;
|
2010-07-29 12:17:42 -07:00
|
|
|
nsCOMPtr<mozIStorageStatement> select;
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->CreateStatement(NS_LITERAL_CSTRING(
|
2010-07-29 12:17:42 -07:00
|
|
|
"SELECT id, host FROM moz_cookies"), getter_AddRefs(select));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> update;
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->CreateStatement(NS_LITERAL_CSTRING(
|
2010-10-19 17:24:52 -07:00
|
|
|
"UPDATE moz_cookies SET baseDomain = :baseDomain WHERE id = :id"),
|
2010-07-29 12:17:42 -07:00
|
|
|
getter_AddRefs(update));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
nsCString baseDomain, host;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasResult;
|
2010-07-29 12:17:42 -07:00
|
|
|
while (1) {
|
|
|
|
rv = select->ExecuteStep(&hasResult);
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
if (!hasResult)
|
|
|
|
break;
|
|
|
|
|
2013-05-29 17:37:34 -07:00
|
|
|
int64_t id = select->AsInt64(SCHEMA2_IDX_ID);
|
|
|
|
select->GetUTF8String(SCHEMA2_IDX_HOST, host);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
rv = GetBaseDomainFromHost(host, baseDomain);
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(update);
|
2007-11-18 00:08:34 -08:00
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
rv = update->BindUTF8StringByName(NS_LITERAL_CSTRING("baseDomain"),
|
|
|
|
baseDomain);
|
2010-07-29 12:17:42 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2010-10-19 17:24:52 -07:00
|
|
|
rv = update->BindInt64ByName(NS_LITERAL_CSTRING("id"),
|
|
|
|
id);
|
2010-07-29 12:17:42 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
|
|
|
rv = update->ExecuteStep(&hasResult);
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-07-29 12:17:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create an index on baseDomain.
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2010-07-29 12:17:42 -07:00
|
|
|
"CREATE INDEX moz_basedomain ON moz_cookies (baseDomain)"));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-10-19 17:24:52 -07:00
|
|
|
}
|
|
|
|
// Fall through to the next upgrade.
|
2010-10-07 14:00:17 -07:00
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
case 3:
|
|
|
|
{
|
2010-10-19 17:24:53 -07:00
|
|
|
// Add the creationTime column to the table, and create a unique index
|
|
|
|
// on (name, host, path). Before we do this, we have to purge the table
|
|
|
|
// of expired cookies such that we know that the (name, host, path)
|
|
|
|
// index is truly unique -- otherwise we can't create the index. Note
|
|
|
|
// that we can't just execute a statement to delete all rows where the
|
|
|
|
// expiry column is in the past -- doing so would rely on the clock
|
|
|
|
// (both now and when previous cookies were set) being monotonic.
|
|
|
|
|
|
|
|
// Select the whole table, and order by the fields we're interested in.
|
|
|
|
// This means we can simply do a linear traversal of the results and
|
|
|
|
// check for duplicates as we go.
|
2013-05-29 17:37:34 -07:00
|
|
|
const int64_t SCHEMA3_IDX_ID = 0;
|
|
|
|
const int64_t SCHEMA3_IDX_NAME = 1;
|
|
|
|
const int64_t SCHEMA3_IDX_HOST = 2;
|
|
|
|
const int64_t SCHEMA3_IDX_PATH = 3;
|
2010-10-19 17:24:52 -07:00
|
|
|
nsCOMPtr<mozIStorageStatement> select;
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->CreateStatement(NS_LITERAL_CSTRING(
|
2010-10-19 17:24:53 -07:00
|
|
|
"SELECT id, name, host, path FROM moz_cookies "
|
|
|
|
"ORDER BY name ASC, host ASC, path ASC, expiry ASC"),
|
|
|
|
getter_AddRefs(select));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-10-19 17:24:52 -07:00
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
nsCOMPtr<mozIStorageStatement> deleteExpired;
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->CreateStatement(NS_LITERAL_CSTRING(
|
2010-10-19 17:24:53 -07:00
|
|
|
"DELETE FROM moz_cookies WHERE id = :id"),
|
|
|
|
getter_AddRefs(deleteExpired));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-10-19 17:24:52 -07:00
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
// Read the first row.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasResult;
|
2010-10-19 17:24:53 -07:00
|
|
|
rv = select->ExecuteStep(&hasResult);
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-10-19 17:24:52 -07:00
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
if (hasResult) {
|
|
|
|
nsCString name1, host1, path1;
|
2013-05-29 17:37:34 -07:00
|
|
|
int64_t id1 = select->AsInt64(SCHEMA3_IDX_ID);
|
|
|
|
select->GetUTF8String(SCHEMA3_IDX_NAME, name1);
|
|
|
|
select->GetUTF8String(SCHEMA3_IDX_HOST, host1);
|
|
|
|
select->GetUTF8String(SCHEMA3_IDX_PATH, path1);
|
2010-10-19 17:24:53 -07:00
|
|
|
|
|
|
|
nsCString name2, host2, path2;
|
|
|
|
while (1) {
|
|
|
|
// Read the second row.
|
|
|
|
rv = select->ExecuteStep(&hasResult);
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-10-19 17:24:53 -07:00
|
|
|
|
|
|
|
if (!hasResult)
|
|
|
|
break;
|
|
|
|
|
2013-05-29 17:37:34 -07:00
|
|
|
int64_t id2 = select->AsInt64(SCHEMA3_IDX_ID);
|
|
|
|
select->GetUTF8String(SCHEMA3_IDX_NAME, name2);
|
|
|
|
select->GetUTF8String(SCHEMA3_IDX_HOST, host2);
|
|
|
|
select->GetUTF8String(SCHEMA3_IDX_PATH, path2);
|
2010-10-19 17:24:53 -07:00
|
|
|
|
|
|
|
// If the two rows match in (name, host, path), we know the earlier
|
|
|
|
// row has an earlier expiry time. Delete it.
|
|
|
|
if (name1 == name2 && host1 == host2 && path1 == path2) {
|
|
|
|
mozStorageStatementScoper scoper(deleteExpired);
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = deleteExpired->BindInt64ByName(NS_LITERAL_CSTRING("id"),
|
|
|
|
id1);
|
2010-10-19 17:24:53 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
|
|
|
rv = deleteExpired->ExecuteStep(&hasResult);
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-10-19 17:24:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the second row the first for the next iteration.
|
|
|
|
name1 = name2;
|
|
|
|
host1 = host2;
|
|
|
|
path1 = path2;
|
|
|
|
id1 = id2;
|
|
|
|
}
|
|
|
|
}
|
2010-10-19 17:24:52 -07:00
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
// Add the creationTime column to the table.
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2010-10-19 17:24:53 -07:00
|
|
|
"ALTER TABLE moz_cookies ADD creationTime INTEGER"));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-10-19 17:24:52 -07:00
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
// Copy the id of each row into the new creationTime column.
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2010-10-19 17:24:53 -07:00
|
|
|
"UPDATE moz_cookies SET creationTime = "
|
|
|
|
"(SELECT id WHERE id = moz_cookies.id)"));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-10-19 17:24:52 -07:00
|
|
|
|
|
|
|
// Create a unique index on (name, host, path) to allow fast lookup.
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2010-10-19 17:24:52 -07:00
|
|
|
"CREATE UNIQUE INDEX moz_uniqueid "
|
|
|
|
"ON moz_cookies (name, host, path)"));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2007-11-18 00:08:34 -08:00
|
|
|
}
|
2010-07-29 12:17:42 -07:00
|
|
|
// Fall through to the next upgrade.
|
2007-11-18 00:08:34 -08:00
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
// We need to add appId/inBrowserElement, plus change a constraint on
|
|
|
|
// the table (unique entries now include appId/inBrowserElement):
|
|
|
|
// this requires creating a new table and copying the data to it. We
|
|
|
|
// then rename the new table to the old name.
|
|
|
|
//
|
|
|
|
// Why we made this change: appId/inBrowserElement allow "cookie jars"
|
|
|
|
// for Firefox OS. We create a separate cookie namespace per {appId,
|
|
|
|
// inBrowserElement}. When upgrading, we convert existing cookies
|
|
|
|
// (which imply we're on desktop/mobile) to use {0, false}, as that is
|
|
|
|
// the only namespace used by a non-Firefox-OS implementation.
|
|
|
|
|
|
|
|
// Rename existing table
|
|
|
|
rv = mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"ALTER TABLE moz_cookies RENAME TO moz_cookies_old"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
|
|
|
|
|
|
|
// Drop existing index (CreateTable will create new one for new table)
|
|
|
|
rv = mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP INDEX moz_basedomain"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
|
|
|
|
|
|
|
// Create new table (with new fields and new unique constraint)
|
|
|
|
rv = CreateTable();
|
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
|
|
|
|
|
|
|
// Copy data from old table, using appId/inBrowser=0 for existing rows
|
|
|
|
rv = mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO moz_cookies "
|
|
|
|
"(baseDomain, appId, inBrowserElement, name, value, host, path, expiry,"
|
|
|
|
" lastAccessed, creationTime, isSecure, isHttpOnly) "
|
|
|
|
"SELECT baseDomain, 0, 0, name, value, host, path, expiry,"
|
|
|
|
" lastAccessed, creationTime, isSecure, isHttpOnly "
|
|
|
|
"FROM moz_cookies_old"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
|
|
|
|
|
|
|
// Drop old table
|
|
|
|
rv = mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE moz_cookies_old"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
|
|
|
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
|
|
|
("Upgraded database to schema version 5"));
|
|
|
|
}
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
// No more upgrades. Update the schema version.
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->SetSchemaVersion(COOKIES_SCHEMA_VERSION);
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-10-19 17:24:52 -07:00
|
|
|
|
2007-11-18 00:08:34 -08:00
|
|
|
case COOKIES_SCHEMA_VERSION:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
NS_WARNING("couldn't get schema version!");
|
|
|
|
|
|
|
|
// the table may be usable; someone might've just clobbered the schema
|
|
|
|
// version. we can treat this case like a downgrade using the codepath
|
|
|
|
// below, by verifying the columns we care about are all there. for now,
|
|
|
|
// re-set the schema version in the db, in case the checks succeed (if
|
|
|
|
// they don't, we're dropping the table anyway).
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->SetSchemaVersion(COOKIES_SCHEMA_VERSION);
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2007-11-18 00:08:34 -08:00
|
|
|
}
|
|
|
|
// fall through to downgrade check
|
|
|
|
|
|
|
|
// downgrading.
|
|
|
|
// if columns have been added to the table, we can still use the ones we
|
|
|
|
// understand safely. if columns have been deleted or altered, just
|
|
|
|
// blow away the table and start from scratch! if you change the way
|
|
|
|
// a column is interpreted, make sure you also change its name so this
|
|
|
|
// check will catch it.
|
|
|
|
default:
|
|
|
|
{
|
2007-06-17 14:52:22 -07:00
|
|
|
// check if all the expected columns exist
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->CreateStatement(NS_LITERAL_CSTRING(
|
2009-12-19 15:55:56 -08:00
|
|
|
"SELECT "
|
|
|
|
"id, "
|
2010-07-29 12:17:42 -07:00
|
|
|
"baseDomain, "
|
2012-09-18 09:04:04 -07:00
|
|
|
"appId, "
|
|
|
|
"inBrowserElement, "
|
2009-12-19 15:55:56 -08:00
|
|
|
"name, "
|
|
|
|
"value, "
|
|
|
|
"host, "
|
|
|
|
"path, "
|
|
|
|
"expiry, "
|
2010-08-02 17:03:24 -07:00
|
|
|
"lastAccessed, "
|
2010-10-19 17:24:52 -07:00
|
|
|
"creationTime, "
|
2009-12-19 15:55:56 -08:00
|
|
|
"isSecure, "
|
|
|
|
"isHttpOnly "
|
2007-06-17 14:52:22 -07:00
|
|
|
"FROM moz_cookies"), getter_AddRefs(stmt));
|
2007-11-18 00:08:34 -08:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// our columns aren't there - drop the table!
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE moz_cookies"));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2007-11-18 00:08:34 -08:00
|
|
|
|
|
|
|
rv = CreateTable();
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2007-06-17 14:52:22 -07:00
|
|
|
}
|
2007-11-18 00:08:34 -08:00
|
|
|
break;
|
2007-06-17 14:52:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// make operations on the table asynchronous, for performance
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"PRAGMA synchronous = OFF"));
|
2007-06-17 14:52:22 -07:00
|
|
|
|
2010-10-30 19:32:45 -07:00
|
|
|
// Use write-ahead-logging for performance. We cap the autocheckpoint limit at
|
|
|
|
// 16 pages (around 500KB).
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2012-01-05 15:46:35 -08:00
|
|
|
MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA journal_mode = WAL"));
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2010-10-30 19:32:45 -07:00
|
|
|
"PRAGMA wal_autocheckpoint = 16"));
|
2010-10-19 17:24:53 -07:00
|
|
|
|
2007-06-17 14:52:22 -07:00
|
|
|
// cache frequently used statements (for insertion, deletion, and updating)
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->CreateAsyncStatement(NS_LITERAL_CSTRING(
|
2009-12-19 15:55:56 -08:00
|
|
|
"INSERT INTO moz_cookies ("
|
2010-07-29 12:17:42 -07:00
|
|
|
"baseDomain, "
|
2012-09-18 09:04:04 -07:00
|
|
|
"appId, "
|
|
|
|
"inBrowserElement, "
|
2009-12-19 15:55:56 -08:00
|
|
|
"name, "
|
|
|
|
"value, "
|
|
|
|
"host, "
|
|
|
|
"path, "
|
|
|
|
"expiry, "
|
|
|
|
"lastAccessed, "
|
2010-10-19 17:24:52 -07:00
|
|
|
"creationTime, "
|
2009-12-19 15:55:56 -08:00
|
|
|
"isSecure, "
|
|
|
|
"isHttpOnly"
|
2010-10-19 17:24:52 -07:00
|
|
|
") VALUES ("
|
|
|
|
":baseDomain, "
|
2012-09-18 09:04:04 -07:00
|
|
|
":appId, "
|
|
|
|
":inBrowserElement, "
|
2010-10-19 17:24:52 -07:00
|
|
|
":name, "
|
|
|
|
":value, "
|
|
|
|
":host, "
|
|
|
|
":path, "
|
|
|
|
":expiry, "
|
|
|
|
":lastAccessed, "
|
|
|
|
":creationTime, "
|
|
|
|
":isSecure, "
|
|
|
|
":isHttpOnly"
|
|
|
|
")"),
|
2010-11-12 09:32:35 -08:00
|
|
|
getter_AddRefs(mDefaultDBState->stmtInsert));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2007-06-17 14:52:22 -07:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->CreateAsyncStatement(NS_LITERAL_CSTRING(
|
2010-10-19 17:24:52 -07:00
|
|
|
"DELETE FROM moz_cookies "
|
|
|
|
"WHERE name = :name AND host = :host AND path = :path"),
|
2010-11-12 09:32:35 -08:00
|
|
|
getter_AddRefs(mDefaultDBState->stmtDelete));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2007-06-17 14:52:22 -07:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->CreateAsyncStatement(NS_LITERAL_CSTRING(
|
2010-10-19 17:24:52 -07:00
|
|
|
"UPDATE moz_cookies SET lastAccessed = :lastAccessed "
|
|
|
|
"WHERE name = :name AND host = :host AND path = :path"),
|
2010-11-12 09:32:35 -08:00
|
|
|
getter_AddRefs(mDefaultDBState->stmtUpdate));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2007-11-18 00:08:34 -08:00
|
|
|
|
2009-02-03 19:26:59 -08:00
|
|
|
// if we deleted a corrupt db, don't attempt to import - return now
|
2010-11-12 09:32:35 -08:00
|
|
|
if (aRecreateDB)
|
|
|
|
return RESULT_OK;
|
2009-02-03 19:26:59 -08:00
|
|
|
|
2007-06-17 14:52:22 -07:00
|
|
|
// check whether to import or just read in the db
|
2007-11-12 01:28:22 -08:00
|
|
|
if (tableExists)
|
|
|
|
return Read();
|
2007-06-17 14:52:22 -07:00
|
|
|
|
2009-02-03 19:26:59 -08:00
|
|
|
nsCOMPtr<nsIFile> oldCookieFile;
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
|
|
|
|
getter_AddRefs(oldCookieFile));
|
2010-11-12 09:32:35 -08:00
|
|
|
if (NS_FAILED(rv)) return RESULT_OK;
|
2007-11-12 01:28:22 -08:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
// Import cookies, and clean up the old file regardless of success or failure.
|
|
|
|
// Note that we have to switch out our DBState temporarily, in case we're in
|
|
|
|
// private browsing mode; otherwise ImportCookies() won't be happy.
|
|
|
|
DBState* initialState = mDBState;
|
|
|
|
mDBState = mDefaultDBState;
|
2009-02-03 19:26:59 -08:00
|
|
|
oldCookieFile->AppendNative(NS_LITERAL_CSTRING(kOldCookieFileName));
|
2010-11-12 09:32:35 -08:00
|
|
|
ImportCookies(oldCookieFile);
|
2011-10-17 07:59:28 -07:00
|
|
|
oldCookieFile->Remove(false);
|
2010-11-12 09:32:35 -08:00
|
|
|
mDBState = initialState;
|
|
|
|
|
|
|
|
return RESULT_OK;
|
2007-06-17 14:52:22 -07:00
|
|
|
}
|
|
|
|
|
2010-07-29 12:17:42 -07:00
|
|
|
// Sets the schema version and creates the moz_cookies table.
|
2007-06-17 14:52:22 -07:00
|
|
|
nsresult
|
|
|
|
nsCookieService::CreateTable()
|
|
|
|
{
|
2010-07-29 12:17:42 -07:00
|
|
|
// Set the schema version, before creating the table.
|
2010-11-12 09:32:35 -08:00
|
|
|
nsresult rv = mDefaultDBState->dbConn->SetSchemaVersion(
|
|
|
|
COOKIES_SCHEMA_VERSION);
|
2007-06-17 14:52:22 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
// Create the table. We default appId/inBrowserElement to 0: this is so if
|
|
|
|
// users revert to an older Firefox version that doesn't know about these
|
|
|
|
// fields, any cookies set will still work once they upgrade back.
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2007-06-17 14:52:22 -07:00
|
|
|
"CREATE TABLE moz_cookies ("
|
2009-12-19 15:55:56 -08:00
|
|
|
"id INTEGER PRIMARY KEY, "
|
2010-07-29 12:17:42 -07:00
|
|
|
"baseDomain TEXT, "
|
2012-09-18 09:04:04 -07:00
|
|
|
"appId INTEGER DEFAULT 0, "
|
|
|
|
"inBrowserElement INTEGER DEFAULT 0, "
|
2009-12-19 15:55:56 -08:00
|
|
|
"name TEXT, "
|
|
|
|
"value TEXT, "
|
|
|
|
"host TEXT, "
|
|
|
|
"path TEXT, "
|
|
|
|
"expiry INTEGER, "
|
|
|
|
"lastAccessed INTEGER, "
|
2010-10-19 17:24:52 -07:00
|
|
|
"creationTime INTEGER, "
|
2009-12-19 15:55:56 -08:00
|
|
|
"isSecure INTEGER, "
|
2010-10-19 17:24:52 -07:00
|
|
|
"isHttpOnly INTEGER, "
|
2012-09-18 09:04:04 -07:00
|
|
|
"CONSTRAINT moz_uniqueid UNIQUE (name, host, path, appId, inBrowserElement)"
|
2009-12-19 15:55:56 -08:00
|
|
|
")"));
|
2010-07-29 12:17:42 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Create an index on baseDomain.
|
2010-11-12 09:32:35 -08:00
|
|
|
return mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2012-09-18 09:04:04 -07:00
|
|
|
"CREATE INDEX moz_basedomain ON moz_cookies (baseDomain, "
|
|
|
|
"appId, "
|
|
|
|
"inBrowserElement)"));
|
2007-06-17 14:52:22 -07:00
|
|
|
}
|
|
|
|
|
2008-12-10 15:54:57 -08:00
|
|
|
void
|
2010-11-12 09:32:35 -08:00
|
|
|
nsCookieService::CloseDBStates()
|
2008-12-10 15:54:57 -08:00
|
|
|
{
|
2010-11-12 09:32:35 -08:00
|
|
|
// Null out our private and pointer DBStates regardless.
|
2013-09-19 12:28:26 -07:00
|
|
|
mPrivateDBState = nullptr;
|
|
|
|
mDBState = nullptr;
|
2009-10-16 14:01:47 -07:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
// If we don't have a default DBState, we're done.
|
|
|
|
if (!mDefaultDBState)
|
|
|
|
return;
|
|
|
|
|
2013-07-23 13:41:23 -07:00
|
|
|
// Cleanup cached statements before we can close anything.
|
|
|
|
CleanupCachedStatements();
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
if (mDefaultDBState->dbConn) {
|
2010-07-29 12:17:42 -07:00
|
|
|
// Cancel any pending read. No further results will be received by our
|
|
|
|
// read listener.
|
2010-11-12 09:32:35 -08:00
|
|
|
if (mDefaultDBState->pendingRead) {
|
2011-10-17 07:59:28 -07:00
|
|
|
CancelAsyncRead(true);
|
2010-07-29 13:43:34 -07:00
|
|
|
}
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
// Asynchronously close the connection. We will null it below.
|
|
|
|
mDefaultDBState->dbConn->AsyncClose(mDefaultDBState->closeListener);
|
2010-03-12 07:18:38 -08:00
|
|
|
}
|
2010-11-12 09:32:35 -08:00
|
|
|
|
2013-07-23 13:41:23 -07:00
|
|
|
CleanupDefaultDBConnection();
|
2010-11-12 09:32:35 -08:00
|
|
|
|
2013-09-19 12:28:26 -07:00
|
|
|
mDefaultDBState = nullptr;
|
2010-11-12 09:32:35 -08:00
|
|
|
}
|
|
|
|
|
2013-07-23 13:41:23 -07:00
|
|
|
// Null out the statements.
|
|
|
|
// This must be done before closing the connection.
|
2010-11-12 09:32:35 -08:00
|
|
|
void
|
2013-07-23 13:41:23 -07:00
|
|
|
nsCookieService::CleanupCachedStatements()
|
2010-11-12 09:32:35 -08:00
|
|
|
{
|
2013-07-23 13:41:23 -07:00
|
|
|
mDefaultDBState->stmtInsert = nullptr;
|
|
|
|
mDefaultDBState->stmtDelete = nullptr;
|
|
|
|
mDefaultDBState->stmtUpdate = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Null out the listeners, and the database connection itself. This
|
|
|
|
// will not null out the statements, cancel a pending read or
|
|
|
|
// asynchronously close the connection -- these must be done
|
|
|
|
// beforehand if necessary.
|
|
|
|
void
|
|
|
|
nsCookieService::CleanupDefaultDBConnection()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mDefaultDBState->stmtInsert, "stmtInsert has been cleaned up");
|
|
|
|
MOZ_ASSERT(!mDefaultDBState->stmtDelete, "stmtDelete has been cleaned up");
|
|
|
|
MOZ_ASSERT(!mDefaultDBState->stmtUpdate, "stmtUpdate has been cleaned up");
|
2010-11-12 09:32:35 -08:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
// Null out the database connections. If 'dbConn' has not been used for any
|
|
|
|
// asynchronous operations yet, this will synchronously close it; otherwise,
|
|
|
|
// it's expected that the caller has performed an AsyncClose prior.
|
2013-09-19 12:28:26 -07:00
|
|
|
mDefaultDBState->dbConn = nullptr;
|
|
|
|
mDefaultDBState->syncConn = nullptr;
|
2010-11-12 09:32:35 -08:00
|
|
|
|
|
|
|
// Manually null out our listeners. This is necessary because they hold a
|
2010-11-12 09:32:35 -08:00
|
|
|
// strong ref to the DBState itself. They'll stay alive until whatever
|
|
|
|
// statements are still executing complete.
|
2013-09-19 12:28:26 -07:00
|
|
|
mDefaultDBState->readListener = nullptr;
|
|
|
|
mDefaultDBState->insertListener = nullptr;
|
|
|
|
mDefaultDBState->updateListener = nullptr;
|
|
|
|
mDefaultDBState->removeListener = nullptr;
|
|
|
|
mDefaultDBState->closeListener = nullptr;
|
2008-12-10 15:54:57 -08:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
void
|
|
|
|
nsCookieService::HandleDBClosed(DBState* aDBState)
|
|
|
|
{
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
|
|
|
("HandleDBClosed(): DBState %x closed", aDBState));
|
|
|
|
|
|
|
|
switch (aDBState->corruptFlag) {
|
|
|
|
case DBState::OK: {
|
|
|
|
// Database is healthy. Notify of closure.
|
2012-07-30 07:20:58 -07:00
|
|
|
mObserverService->NotifyObservers(nullptr, "cookie-db-closed", nullptr);
|
2010-11-12 09:32:35 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DBState::CLOSING_FOR_REBUILD: {
|
|
|
|
// Our close finished. Start the rebuild, and notify of db closure later.
|
|
|
|
RebuildCorruptDB(aDBState);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DBState::REBUILDING: {
|
|
|
|
// We encountered an error during rebuild, closed the database, and now
|
|
|
|
// here we are. We already have a 'cookies.sqlite.bak' from the original
|
|
|
|
// dead database; we don't want to overwrite it, so let's move this one to
|
|
|
|
// 'cookies.sqlite.bak-rebuild'.
|
|
|
|
nsCOMPtr<nsIFile> backupFile;
|
|
|
|
aDBState->cookieFile->Clone(getter_AddRefs(backupFile));
|
2013-09-19 12:28:26 -07:00
|
|
|
nsresult rv = backupFile->MoveToNative(nullptr,
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_LITERAL_CSTRING(COOKIES_FILE ".bak-rebuild"));
|
|
|
|
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_WARNING,
|
|
|
|
("HandleDBClosed(): DBState %x encountered error rebuilding db; move to "
|
|
|
|
"'cookies.sqlite.bak-rebuild' gave rv 0x%x", aDBState, rv));
|
2012-07-30 07:20:58 -07:00
|
|
|
mObserverService->NotifyObservers(nullptr, "cookie-db-closed", nullptr);
|
2010-11-12 09:32:35 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCookieService::HandleCorruptDB(DBState* aDBState)
|
|
|
|
{
|
|
|
|
if (mDefaultDBState != aDBState) {
|
|
|
|
// We've either closed the state or we've switched profiles. It's getting
|
|
|
|
// a bit late to rebuild -- bail instead.
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_WARNING,
|
|
|
|
("HandleCorruptDB(): DBState %x is already closed, aborting", aDBState));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
|
|
|
("HandleCorruptDB(): DBState %x has corruptFlag %u", aDBState,
|
|
|
|
aDBState->corruptFlag));
|
|
|
|
|
|
|
|
// Mark the database corrupt, so the close listener can begin reconstructing
|
|
|
|
// it.
|
|
|
|
switch (mDefaultDBState->corruptFlag) {
|
|
|
|
case DBState::OK: {
|
|
|
|
// Move to 'closing' state.
|
|
|
|
mDefaultDBState->corruptFlag = DBState::CLOSING_FOR_REBUILD;
|
|
|
|
|
|
|
|
// Cancel any pending read and close the database. If we do have an
|
|
|
|
// in-flight read we want to throw away all the results so far -- we have no
|
|
|
|
// idea how consistent the database is. Note that we may have already
|
|
|
|
// canceled the read but not emptied our readSet; do so now.
|
|
|
|
mDefaultDBState->readSet.Clear();
|
|
|
|
if (mDefaultDBState->pendingRead) {
|
2011-10-17 07:59:28 -07:00
|
|
|
CancelAsyncRead(true);
|
2012-07-30 07:20:58 -07:00
|
|
|
mDefaultDBState->syncConn = nullptr;
|
2010-11-12 09:32:35 -08:00
|
|
|
}
|
|
|
|
|
2013-07-23 13:41:23 -07:00
|
|
|
CleanupCachedStatements();
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->dbConn->AsyncClose(mDefaultDBState->closeListener);
|
2013-07-23 13:41:23 -07:00
|
|
|
CleanupDefaultDBConnection();
|
2010-11-12 09:32:35 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DBState::CLOSING_FOR_REBUILD: {
|
|
|
|
// We had an error while waiting for close completion. That's OK, just
|
|
|
|
// ignore it -- we're rebuilding anyway.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case DBState::REBUILDING: {
|
|
|
|
// We had an error while rebuilding the DB. Game over. Close the database
|
|
|
|
// and let the close handler do nothing; then we'll move it out of the way.
|
2013-07-23 13:41:23 -07:00
|
|
|
CleanupCachedStatements();
|
2011-03-28 13:24:05 -07:00
|
|
|
if (mDefaultDBState->dbConn) {
|
|
|
|
mDefaultDBState->dbConn->AsyncClose(mDefaultDBState->closeListener);
|
|
|
|
}
|
2013-07-23 13:41:23 -07:00
|
|
|
CleanupDefaultDBConnection();
|
2010-11-12 09:32:35 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
RebuildDBCallback(nsCookieEntry *aEntry,
|
|
|
|
void *aArg)
|
|
|
|
{
|
|
|
|
mozIStorageBindingParamsArray* paramsArray =
|
|
|
|
static_cast<mozIStorageBindingParamsArray*>(aArg);
|
|
|
|
|
|
|
|
const nsCookieEntry::ArrayType &cookies = aEntry->GetCookies();
|
|
|
|
for (nsCookieEntry::IndexType i = 0; i < cookies.Length(); ++i) {
|
|
|
|
nsCookie* cookie = cookies[i];
|
|
|
|
|
|
|
|
if (!cookie->IsSession()) {
|
2012-09-18 09:04:04 -07:00
|
|
|
bindCookieParameters(paramsArray, aEntry, cookie);
|
2010-11-12 09:32:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCookieService::RebuildCorruptDB(DBState* aDBState)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!aDBState->dbConn, "shouldn't have an open db connection");
|
|
|
|
NS_ASSERTION(aDBState->corruptFlag == DBState::CLOSING_FOR_REBUILD,
|
|
|
|
"should be in CLOSING_FOR_REBUILD state");
|
|
|
|
|
|
|
|
aDBState->corruptFlag = DBState::REBUILDING;
|
|
|
|
|
|
|
|
if (mDefaultDBState != aDBState) {
|
|
|
|
// We've either closed the state or we've switched profiles. It's getting
|
|
|
|
// a bit late to rebuild -- bail instead. In any case, we were waiting
|
|
|
|
// on rebuild completion to notify of the db closure, which won't happen --
|
|
|
|
// do so now.
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_WARNING,
|
|
|
|
("RebuildCorruptDB(): DBState %x is stale, aborting", aDBState));
|
2012-07-30 07:20:58 -07:00
|
|
|
mObserverService->NotifyObservers(nullptr, "cookie-db-closed", nullptr);
|
2010-11-12 09:32:35 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
|
|
|
("RebuildCorruptDB(): creating new database"));
|
|
|
|
|
|
|
|
// The database has been closed, and we're ready to rebuild. Open a
|
|
|
|
// connection.
|
|
|
|
OpenDBResult result = TryInitDB(true);
|
|
|
|
if (result != RESULT_OK) {
|
|
|
|
// We're done. Reset our DB connection and statements, and notify of
|
|
|
|
// closure.
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_WARNING,
|
|
|
|
("RebuildCorruptDB(): TryInitDB() failed with result %u", result));
|
2013-07-23 13:41:23 -07:00
|
|
|
CleanupCachedStatements();
|
|
|
|
CleanupDefaultDBConnection();
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->corruptFlag = DBState::OK;
|
2012-07-30 07:20:58 -07:00
|
|
|
mObserverService->NotifyObservers(nullptr, "cookie-db-closed", nullptr);
|
2010-11-12 09:32:35 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify observers that we're beginning the rebuild.
|
2012-07-30 07:20:58 -07:00
|
|
|
mObserverService->NotifyObservers(nullptr, "cookie-db-rebuilding", nullptr);
|
2010-11-12 09:32:35 -08:00
|
|
|
|
|
|
|
// Enumerate the hash, and add cookies to the params array.
|
|
|
|
mozIStorageAsyncStatement* stmt = aDBState->stmtInsert;
|
|
|
|
nsCOMPtr<mozIStorageBindingParamsArray> paramsArray;
|
|
|
|
stmt->NewBindingParamsArray(getter_AddRefs(paramsArray));
|
|
|
|
aDBState->hostTable.EnumerateEntries(RebuildDBCallback, paramsArray.get());
|
|
|
|
|
|
|
|
// Make sure we've got something to write. If we don't, we're done.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length;
|
2010-11-12 09:32:35 -08:00
|
|
|
paramsArray->GetLength(&length);
|
|
|
|
if (length == 0) {
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
|
|
|
("RebuildCorruptDB(): nothing to write, rebuild complete"));
|
|
|
|
mDefaultDBState->corruptFlag = DBState::OK;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the statement. If any errors crop up, we won't try again.
|
2012-10-25 12:47:55 -07:00
|
|
|
DebugOnly<nsresult> rv = stmt->BindParameters(paramsArray);
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
nsCOMPtr<mozIStoragePendingStatement> handle;
|
|
|
|
rv = stmt->ExecuteAsync(aDBState->insertListener, getter_AddRefs(handle));
|
2013-07-23 13:41:23 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2010-11-12 09:32:35 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCookieService::~nsCookieService()
|
|
|
|
{
|
2010-11-12 09:32:35 -08:00
|
|
|
CloseDBStates();
|
2008-12-10 15:54:57 -08:00
|
|
|
|
2013-12-03 13:58:41 -08:00
|
|
|
UnregisterWeakMemoryReporter(this);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
gCookieService = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *aData)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// check the topic
|
2007-06-17 14:52:22 -07:00
|
|
|
if (!strcmp(aTopic, "profile-before-change")) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// The profile is about to change,
|
|
|
|
// or is going away because the application is shutting down.
|
2013-01-26 11:05:31 -08:00
|
|
|
if (mDBState && mDBState->dbConn &&
|
2013-12-12 17:50:01 -08:00
|
|
|
!nsCRT::strcmp(aData, MOZ_UTF16("shutdown-cleanse"))) {
|
2013-01-26 11:05:31 -08:00
|
|
|
// Clear the cookie db if we're in the default DBState.
|
|
|
|
RemoveAll();
|
|
|
|
}
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
// Close the default DB connection and null out our DBStates before
|
|
|
|
// changing.
|
|
|
|
CloseDBStates();
|
|
|
|
|
2007-06-17 14:52:22 -07:00
|
|
|
} else if (!strcmp(aTopic, "profile-do-change")) {
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ASSERTION(!mDefaultDBState, "shouldn't have a default DBState");
|
|
|
|
NS_ASSERTION(!mPrivateDBState, "shouldn't have a private DBState");
|
|
|
|
|
2009-10-16 14:01:47 -07:00
|
|
|
// the profile has already changed; init the db from the new location.
|
|
|
|
// if we are in the private browsing state, however, we do not want to read
|
|
|
|
// data into it - we should instead put it into the default state, so it's
|
|
|
|
// ready for us if and when we switch back to it.
|
2010-11-12 09:32:35 -08:00
|
|
|
InitDBStates();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-06-17 14:52:22 -07:00
|
|
|
} else if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject);
|
|
|
|
if (prefBranch)
|
|
|
|
PrefChanged(prefBranch);
|
2010-06-07 12:32:12 -07:00
|
|
|
|
2012-02-08 10:37:07 -08:00
|
|
|
} else if (!strcmp(aTopic, "last-pb-context-exited")) {
|
|
|
|
// Flush all the cookies stored by private browsing contexts
|
|
|
|
mPrivateDBState = new DBState();
|
2012-11-16 06:18:41 -08:00
|
|
|
}
|
2012-02-08 10:37:07 -08:00
|
|
|
|
2012-02-08 10:37:07 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::GetCookieString(nsIURI *aHostURI,
|
|
|
|
nsIChannel *aChannel,
|
|
|
|
char **aCookie)
|
|
|
|
{
|
2010-09-16 13:21:12 -07:00
|
|
|
return GetCookieStringCommon(aHostURI, aChannel, false, aCookie);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::GetCookieStringFromHttp(nsIURI *aHostURI,
|
|
|
|
nsIURI *aFirstURI,
|
|
|
|
nsIChannel *aChannel,
|
|
|
|
char **aCookie)
|
2010-09-16 13:21:12 -07:00
|
|
|
{
|
|
|
|
return GetCookieStringCommon(aHostURI, aChannel, true, aCookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCookieService::GetCookieStringCommon(nsIURI *aHostURI,
|
|
|
|
nsIChannel *aChannel,
|
|
|
|
bool aHttpBound,
|
|
|
|
char** aCookie)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-25 16:02:28 -07:00
|
|
|
NS_ENSURE_ARG(aHostURI);
|
|
|
|
NS_ENSURE_ARG(aCookie);
|
|
|
|
|
2010-10-19 09:37:03 -07:00
|
|
|
// Determine whether the request is foreign. Failure is acceptable.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isForeign = true;
|
2012-09-14 13:57:56 -07:00
|
|
|
mThirdPartyUtil->IsThirdPartyChannel(aChannel, aHostURI, &isForeign);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
// Get app info, if channel is present. Else assume default namespace.
|
|
|
|
uint32_t appId = NECKO_NO_APP_ID;
|
|
|
|
bool inBrowserElement = false;
|
|
|
|
if (aChannel) {
|
|
|
|
NS_GetAppInfo(aChannel, &appId, &inBrowserElement);
|
|
|
|
}
|
|
|
|
|
2012-02-08 10:37:07 -08:00
|
|
|
bool isPrivate = aChannel && NS_UsePrivateBrowsing(aChannel);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString result;
|
2012-09-18 09:04:04 -07:00
|
|
|
GetCookieStringInternal(aHostURI, isForeign, aHttpBound, appId,
|
2012-02-08 10:37:07 -08:00
|
|
|
inBrowserElement, isPrivate, result);
|
2012-07-30 07:20:58 -07:00
|
|
|
*aCookie = result.IsEmpty() ? nullptr : ToNewCString(result);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::SetCookieString(nsIURI *aHostURI,
|
|
|
|
nsIPrompt *aPrompt,
|
|
|
|
const char *aCookieHeader,
|
|
|
|
nsIChannel *aChannel)
|
|
|
|
{
|
2013-12-02 09:48:12 -08:00
|
|
|
// The aPrompt argument is deprecated and unused. Avoid introducing new
|
|
|
|
// code that uses this argument by warning if the value is non-null.
|
|
|
|
MOZ_ASSERT(!aPrompt);
|
|
|
|
if (aPrompt) {
|
|
|
|
nsCOMPtr<nsIConsoleService> aConsoleService =
|
|
|
|
do_GetService("@mozilla.org/consoleservice;1");
|
|
|
|
if (aConsoleService) {
|
|
|
|
aConsoleService->LogStringMessage(
|
2013-12-12 17:50:01 -08:00
|
|
|
MOZ_UTF16("Non-null prompt ignored by nsCookieService."));
|
2013-12-02 09:48:12 -08:00
|
|
|
}
|
|
|
|
}
|
2013-09-19 12:28:26 -07:00
|
|
|
return SetCookieStringCommon(aHostURI, aCookieHeader, nullptr, aChannel,
|
|
|
|
false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::SetCookieStringFromHttp(nsIURI *aHostURI,
|
|
|
|
nsIURI *aFirstURI,
|
|
|
|
nsIPrompt *aPrompt,
|
|
|
|
const char *aCookieHeader,
|
|
|
|
const char *aServerTime,
|
|
|
|
nsIChannel *aChannel)
|
2010-09-16 13:21:12 -07:00
|
|
|
{
|
2013-12-02 09:48:12 -08:00
|
|
|
// The aPrompt argument is deprecated and unused. Avoid introducing new
|
|
|
|
// code that uses this argument by warning if the value is non-null.
|
|
|
|
MOZ_ASSERT(!aPrompt);
|
|
|
|
if (aPrompt) {
|
|
|
|
nsCOMPtr<nsIConsoleService> aConsoleService =
|
|
|
|
do_GetService("@mozilla.org/consoleservice;1");
|
|
|
|
if (aConsoleService) {
|
|
|
|
aConsoleService->LogStringMessage(
|
2013-12-12 17:50:01 -08:00
|
|
|
MOZ_UTF16("Non-null prompt ignored by nsCookieService."));
|
2013-12-02 09:48:12 -08:00
|
|
|
}
|
|
|
|
}
|
2010-09-16 13:21:12 -07:00
|
|
|
return SetCookieStringCommon(aHostURI, aCookieHeader, aServerTime, aChannel,
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCookieService::SetCookieStringCommon(nsIURI *aHostURI,
|
|
|
|
const char *aCookieHeader,
|
|
|
|
const char *aServerTime,
|
|
|
|
nsIChannel *aChannel,
|
|
|
|
bool aFromHttp)
|
2007-06-26 01:36:50 -07:00
|
|
|
{
|
2010-03-25 16:02:28 -07:00
|
|
|
NS_ENSURE_ARG(aHostURI);
|
|
|
|
NS_ENSURE_ARG(aCookieHeader);
|
|
|
|
|
2010-10-19 09:37:03 -07:00
|
|
|
// Determine whether the request is foreign. Failure is acceptable.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isForeign = true;
|
2012-09-14 13:57:56 -07:00
|
|
|
mThirdPartyUtil->IsThirdPartyChannel(aChannel, aHostURI, &isForeign);
|
2010-03-25 16:02:28 -07:00
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
// Get app info, if channel is present. Else assume default namespace.
|
|
|
|
uint32_t appId = NECKO_NO_APP_ID;
|
|
|
|
bool inBrowserElement = false;
|
|
|
|
if (aChannel) {
|
|
|
|
NS_GetAppInfo(aChannel, &appId, &inBrowserElement);
|
|
|
|
}
|
|
|
|
|
2012-02-08 10:37:07 -08:00
|
|
|
bool isPrivate = aChannel && NS_UsePrivateBrowsing(aChannel);
|
|
|
|
|
2010-03-25 16:02:28 -07:00
|
|
|
nsDependentCString cookieString(aCookieHeader);
|
|
|
|
nsDependentCString serverTime(aServerTime ? aServerTime : "");
|
2010-10-19 09:37:03 -07:00
|
|
|
SetCookieStringInternal(aHostURI, isForeign, cookieString,
|
2012-02-08 10:37:07 -08:00
|
|
|
serverTime, aFromHttp, appId, inBrowserElement,
|
2012-02-01 02:20:01 -08:00
|
|
|
isPrivate, aChannel);
|
2010-03-25 16:02:28 -07:00
|
|
|
return NS_OK;
|
2007-06-26 01:36:50 -07:00
|
|
|
}
|
|
|
|
|
2010-03-25 16:02:28 -07:00
|
|
|
void
|
2011-09-15 00:40:17 -07:00
|
|
|
nsCookieService::SetCookieStringInternal(nsIURI *aHostURI,
|
|
|
|
bool aIsForeign,
|
|
|
|
nsDependentCString &aCookieHeader,
|
|
|
|
const nsCString &aServerTime,
|
2012-09-18 09:04:04 -07:00
|
|
|
bool aFromHttp,
|
|
|
|
uint32_t aAppId,
|
2012-02-08 10:37:07 -08:00
|
|
|
bool aInBrowserElement,
|
2012-02-01 02:20:01 -08:00
|
|
|
bool aIsPrivate,
|
|
|
|
nsIChannel *aChannel)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-16 13:21:12 -07:00
|
|
|
NS_ASSERTION(aHostURI, "null host!");
|
|
|
|
|
2010-11-22 13:50:25 -08:00
|
|
|
if (!mDBState) {
|
|
|
|
NS_WARNING("No DBState! Profile already closed?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-08 10:37:07 -08:00
|
|
|
AutoRestore<DBState*> savePrevDBState(mDBState);
|
|
|
|
mDBState = aIsPrivate ? mPrivateDBState : mDefaultDBState;
|
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// get the base domain for the host URI.
|
|
|
|
// e.g. for "www.bbc.co.uk", this would be "bbc.co.uk".
|
2010-01-12 10:29:20 -08:00
|
|
|
// file:// URI's (i.e. with an empty host) are allowed, but any other
|
|
|
|
// scheme must have a non-empty host. A trailing dot in the host
|
2010-12-22 13:50:56 -08:00
|
|
|
// is acceptable.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool requireHostMatch;
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString baseDomain;
|
2010-01-12 10:29:20 -08:00
|
|
|
nsresult rv = GetBaseDomain(aHostURI, baseDomain, requireHostMatch);
|
2009-12-19 15:55:56 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, aCookieHeader,
|
|
|
|
"couldn't get base domain from URI");
|
2010-03-25 16:02:28 -07:00
|
|
|
return;
|
2009-12-19 15:55:56 -08:00
|
|
|
}
|
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieKey key(baseDomain, aAppId, aInBrowserElement);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// check default prefs
|
2012-09-18 09:04:04 -07:00
|
|
|
CookieStatus cookieStatus = CheckPrefs(aHostURI, aIsForeign, requireHostMatch,
|
|
|
|
aCookieHeader.get());
|
2013-06-21 22:11:12 -07:00
|
|
|
// fire a notification if third party or if cookie was rejected
|
|
|
|
// (but not if there was an error)
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (cookieStatus) {
|
2007-06-20 22:17:08 -07:00
|
|
|
case STATUS_REJECTED:
|
2007-03-22 10:30:00 -07:00
|
|
|
NotifyRejected(aHostURI);
|
2013-06-21 22:11:12 -07:00
|
|
|
if (aIsForeign) {
|
|
|
|
NotifyThirdParty(aHostURI, false, aChannel);
|
|
|
|
}
|
|
|
|
return; // Stop here
|
2007-03-22 10:30:00 -07:00
|
|
|
case STATUS_REJECTED_WITH_ERROR:
|
2010-03-25 16:02:28 -07:00
|
|
|
return;
|
2013-06-21 22:11:12 -07:00
|
|
|
case STATUS_ACCEPTED: // Fallthrough
|
|
|
|
case STATUS_ACCEPT_SESSION:
|
|
|
|
if (aIsForeign) {
|
|
|
|
NotifyThirdParty(aHostURI, true, aChannel);
|
|
|
|
}
|
|
|
|
break;
|
2010-06-07 12:32:12 -07:00
|
|
|
default:
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse server local time. this is not just done here for efficiency
|
|
|
|
// reasons - if there's an error parsing it, and we need to default it
|
|
|
|
// to the current time, we must do it here since the current time in
|
|
|
|
// SetCookieInternal() will change for each cookie processed (e.g. if the
|
|
|
|
// user is prompted).
|
|
|
|
PRTime tempServerTime;
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t serverTime;
|
2011-10-17 07:59:28 -07:00
|
|
|
PRStatus result = PR_ParseTimeString(aServerTime.get(), true,
|
2010-03-25 16:02:28 -07:00
|
|
|
&tempServerTime);
|
|
|
|
if (result == PR_SUCCESS) {
|
2012-08-22 08:56:38 -07:00
|
|
|
serverTime = tempServerTime / int64_t(PR_USEC_PER_SEC);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2007-06-17 14:52:22 -07:00
|
|
|
serverTime = PR_Now() / PR_USEC_PER_SEC;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-25 16:02:28 -07:00
|
|
|
// process each cookie in the header
|
2012-09-18 09:04:04 -07:00
|
|
|
while (SetCookieInternal(aHostURI, key, requireHostMatch, cookieStatus,
|
2012-02-01 02:20:01 -08:00
|
|
|
aCookieHeader, serverTime, aFromHttp, aChannel)) {
|
2011-04-11 16:26:11 -07:00
|
|
|
// document.cookie can only set one cookie at a time
|
|
|
|
if (!aFromHttp)
|
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// notify observers that a cookie was rejected due to the users' prefs.
|
|
|
|
void
|
|
|
|
nsCookieService::NotifyRejected(nsIURI *aHostURI)
|
|
|
|
{
|
2013-06-21 22:11:12 -07:00
|
|
|
if (mObserverService) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mObserverService->NotifyObservers(aHostURI, "cookie-rejected", nullptr);
|
2013-06-21 22:11:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify observers that a third-party cookie was accepted/rejected
|
|
|
|
// if the cookie issuer is unknown, it defaults to "?"
|
|
|
|
void
|
|
|
|
nsCookieService::NotifyThirdParty(nsIURI *aHostURI, bool aIsAccepted, nsIChannel *aChannel)
|
|
|
|
{
|
|
|
|
if (!mObserverService) {
|
|
|
|
return;
|
|
|
|
}
|
2013-07-10 07:03:04 -07:00
|
|
|
|
|
|
|
const char* topic;
|
|
|
|
|
|
|
|
if (mDBState != mPrivateDBState) {
|
|
|
|
// Regular (non-private) browsing
|
|
|
|
if (aIsAccepted) {
|
|
|
|
topic = "third-party-cookie-accepted";
|
|
|
|
} else {
|
|
|
|
topic = "third-party-cookie-rejected";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Private browsing
|
|
|
|
if (aIsAccepted) {
|
|
|
|
topic = "private-third-party-cookie-accepted";
|
|
|
|
} else {
|
|
|
|
topic = "private-third-party-cookie-rejected";
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 22:11:12 -07:00
|
|
|
|
|
|
|
do {
|
|
|
|
// Attempt to find the host of aChannel.
|
|
|
|
if (!aChannel) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIURI> channelURI;
|
|
|
|
nsresult rv = aChannel->GetURI(getter_AddRefs(channelURI));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString referringHost;
|
|
|
|
rv = channelURI->GetHost(referringHost);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString referringHostUTF16 = NS_ConvertUTF8toUTF16(referringHost);
|
|
|
|
mObserverService->NotifyObservers(aHostURI,
|
|
|
|
topic,
|
|
|
|
referringHostUTF16.get());
|
|
|
|
return;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
// This can fail for a number of reasons, in which kind we fallback to "?"
|
|
|
|
mObserverService->NotifyObservers(aHostURI,
|
|
|
|
topic,
|
2013-12-12 17:50:01 -08:00
|
|
|
MOZ_UTF16("?"));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 14:21:28 -08:00
|
|
|
// notify observers that the cookie list changed. there are five possible
|
2007-03-22 10:30:00 -07:00
|
|
|
// values for aData:
|
2009-12-15 14:21:28 -08:00
|
|
|
// "deleted" means a cookie was deleted. aSubject is the deleted cookie.
|
|
|
|
// "added" means a cookie was added. aSubject is the added cookie.
|
|
|
|
// "changed" means a cookie was altered. aSubject is the new cookie.
|
|
|
|
// "cleared" means the entire cookie list was cleared. aSubject is null.
|
2010-11-02 00:06:54 -07:00
|
|
|
// "batch-deleted" means a set of cookies was purged. aSubject is the list of
|
2009-12-15 14:21:28 -08:00
|
|
|
// cookies.
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2009-12-15 14:21:28 -08:00
|
|
|
nsCookieService::NotifyChanged(nsISupports *aSubject,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *aData)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-02-05 02:44:21 -08:00
|
|
|
const char* topic = mDBState == mPrivateDBState ?
|
|
|
|
"private-cookie-changed" : "cookie-changed";
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mObserverService)
|
2013-02-05 02:44:21 -08:00
|
|
|
mObserverService->NotifyObservers(aSubject, topic, aData);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
already_AddRefed<nsIArray>
|
|
|
|
nsCookieService::CreatePurgeList(nsICookie2* aCookie)
|
2010-11-02 10:46:19 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMutableArray> removedList =
|
|
|
|
do_CreateInstance(NS_ARRAY_CONTRACTID);
|
2011-10-17 07:59:28 -07:00
|
|
|
removedList->AppendElement(aCookie, false);
|
2010-11-12 09:32:35 -08:00
|
|
|
return removedList.forget();
|
2010-11-02 10:46:19 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/******************************************************************************
|
|
|
|
* nsCookieService:
|
|
|
|
* pref observer impl
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCookieService::PrefChanged(nsIPrefBranch *aPrefBranch)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t val;
|
2010-06-07 12:32:12 -07:00
|
|
|
if (NS_SUCCEEDED(aPrefBranch->GetIntPref(kPrefCookieBehavior, &val)))
|
2013-02-22 08:16:01 -08:00
|
|
|
mCookieBehavior = (uint8_t) LIMIT(val, 0, 3, 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aPrefBranch->GetIntPref(kPrefMaxNumberOfCookies, &val)))
|
2012-08-22 08:56:38 -07:00
|
|
|
mMaxNumberOfCookies = (uint16_t) LIMIT(val, 1, 0xFFFF, kMaxNumberOfCookies);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aPrefBranch->GetIntPref(kPrefMaxCookiesPerHost, &val)))
|
2012-08-22 08:56:38 -07:00
|
|
|
mMaxCookiesPerHost = (uint16_t) LIMIT(val, 1, 0xFFFF, kMaxCookiesPerHost);
|
2009-12-15 14:21:28 -08:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
if (NS_SUCCEEDED(aPrefBranch->GetIntPref(kPrefCookiePurgeAge, &val))) {
|
|
|
|
mCookiePurgeAge =
|
2012-09-27 23:57:33 -07:00
|
|
|
int64_t(LIMIT(val, 0, INT32_MAX, INT32_MAX)) * PR_USEC_PER_SEC;
|
2010-11-12 09:32:35 -08:00
|
|
|
}
|
2010-06-07 12:32:12 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool boolval;
|
2010-06-07 12:32:12 -07:00
|
|
|
if (NS_SUCCEEDED(aPrefBranch->GetBoolPref(kPrefThirdPartySession, &boolval)))
|
|
|
|
mThirdPartySession = boolval;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsICookieManager impl:
|
|
|
|
* nsICookieManager
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::RemoveAll()
|
|
|
|
{
|
2010-11-22 13:50:25 -08:00
|
|
|
if (!mDBState) {
|
|
|
|
NS_WARNING("No DBState! Profile already closed?");
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
RemoveAllFromMemory();
|
2007-06-17 14:52:22 -07:00
|
|
|
|
|
|
|
// clear the cookie file
|
2009-10-16 14:01:47 -07:00
|
|
|
if (mDBState->dbConn) {
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ASSERTION(mDBState == mDefaultDBState, "not in default DB state");
|
2009-10-16 14:01:47 -07:00
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
// Cancel any pending read. No further results will be received by our
|
|
|
|
// read listener.
|
2010-11-12 09:32:35 -08:00
|
|
|
if (mDefaultDBState->pendingRead) {
|
2011-10-17 07:59:28 -07:00
|
|
|
CancelAsyncRead(true);
|
2010-10-19 17:24:53 -07:00
|
|
|
}
|
|
|
|
|
2011-10-03 14:50:23 -07:00
|
|
|
nsCOMPtr<mozIStorageAsyncStatement> stmt;
|
|
|
|
nsresult rv = mDefaultDBState->dbConn->CreateAsyncStatement(NS_LITERAL_CSTRING(
|
2010-07-29 12:17:43 -07:00
|
|
|
"DELETE FROM moz_cookies"), getter_AddRefs(stmt));
|
2010-11-12 09:32:35 -08:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<mozIStoragePendingStatement> handle;
|
|
|
|
rv = stmt->ExecuteAsync(mDefaultDBState->removeListener,
|
|
|
|
getter_AddRefs(handle));
|
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
} else {
|
|
|
|
// Recreate the database.
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
|
|
|
("RemoveAll(): corruption detected with rv 0x%x", rv));
|
|
|
|
HandleCorruptDB(mDefaultDBState);
|
|
|
|
}
|
2007-06-17 14:52:22 -07:00
|
|
|
}
|
|
|
|
|
2013-12-12 17:50:01 -08:00
|
|
|
NotifyChanged(nullptr, MOZ_UTF16("cleared"));
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashOperator
|
2007-03-22 10:30:00 -07:00
|
|
|
COMArrayCallback(nsCookieEntry *aEntry,
|
|
|
|
void *aArg)
|
|
|
|
{
|
2010-10-19 17:24:53 -07:00
|
|
|
nsCOMArray<nsICookie> *data = static_cast<nsCOMArray<nsICookie> *>(aArg);
|
2008-03-08 01:47:23 -08:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
const nsCookieEntry::ArrayType &cookies = aEntry->GetCookies();
|
|
|
|
for (nsCookieEntry::IndexType i = 0; i < cookies.Length(); ++i) {
|
2010-10-19 17:24:53 -07:00
|
|
|
data->AppendObject(cookies[i]);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-10-19 17:24:53 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::GetEnumerator(nsISimpleEnumerator **aEnumerator)
|
|
|
|
{
|
2010-11-22 13:50:25 -08:00
|
|
|
if (!mDBState) {
|
|
|
|
NS_WARNING("No DBState! Profile already closed?");
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2010-07-29 12:17:42 -07:00
|
|
|
EnsureReadComplete();
|
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
nsCOMArray<nsICookie> cookieList(mDBState->cookieCount);
|
|
|
|
mDBState->hostTable.EnumerateEntries(COMArrayCallback, &cookieList);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_NewArrayEnumerator(aEnumerator, cookieList);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-01-12 10:29:20 -08:00
|
|
|
nsCookieService::Add(const nsACString &aHost,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsACString &aPath,
|
|
|
|
const nsACString &aName,
|
|
|
|
const nsACString &aValue,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aIsSecure,
|
|
|
|
bool aIsHttpOnly,
|
|
|
|
bool aIsSession,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aExpiry)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-11-22 13:50:25 -08:00
|
|
|
if (!mDBState) {
|
|
|
|
NS_WARNING("No DBState! Profile already closed?");
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2010-01-12 10:29:20 -08:00
|
|
|
// first, normalize the hostname, and fail if it contains illegal characters.
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString host(aHost);
|
2010-01-12 10:29:20 -08:00
|
|
|
nsresult rv = NormalizeHost(host);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// get the base domain for the host URI.
|
|
|
|
// e.g. for "www.bbc.co.uk", this would be "bbc.co.uk".
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString baseDomain;
|
2010-01-12 10:29:20 -08:00
|
|
|
rv = GetBaseDomainFromHost(host, baseDomain);
|
2009-12-19 15:55:56 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-11-18 10:52:33 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t currentTimeInUsec = PR_Now();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsRefPtr<nsCookie> cookie =
|
2010-01-12 10:29:20 -08:00
|
|
|
nsCookie::Create(aName, aValue, host, aPath,
|
2007-06-17 14:52:22 -07:00
|
|
|
aExpiry,
|
|
|
|
currentTimeInUsec,
|
2010-10-19 17:24:52 -07:00
|
|
|
nsCookie::GenerateUniqueCreationTime(currentTimeInUsec),
|
2007-03-22 10:30:00 -07:00
|
|
|
aIsSession,
|
|
|
|
aIsSecure,
|
2007-06-20 22:17:08 -07:00
|
|
|
aIsHttpOnly);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!cookie) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
AddInternal(DEFAULT_APP_KEY(baseDomain), cookie, currentTimeInUsec, nullptr, nullptr, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-27 14:37:02 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCookieService::Remove(const nsACString& aHost, uint32_t aAppId,
|
|
|
|
bool aInBrowserElement, const nsACString& aName,
|
|
|
|
const nsACString& aPath, bool aBlocked)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-11-22 13:50:25 -08:00
|
|
|
if (!mDBState) {
|
|
|
|
NS_WARNING("No DBState! Profile already closed?");
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2010-01-12 10:29:20 -08:00
|
|
|
// first, normalize the hostname, and fail if it contains illegal characters.
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString host(aHost);
|
2010-01-12 10:29:20 -08:00
|
|
|
nsresult rv = NormalizeHost(host);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString baseDomain;
|
2010-01-12 10:29:20 -08:00
|
|
|
rv = GetBaseDomainFromHost(host, baseDomain);
|
2009-12-19 15:55:56 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-11-18 10:52:33 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsListIter matchIter;
|
2010-11-12 09:32:35 -08:00
|
|
|
nsRefPtr<nsCookie> cookie;
|
2012-09-27 14:37:02 -07:00
|
|
|
if (FindCookie(nsCookieKey(baseDomain, aAppId, aInBrowserElement),
|
2010-01-12 10:29:20 -08:00
|
|
|
host,
|
2007-03-22 10:30:00 -07:00
|
|
|
PromiseFlatCString(aName),
|
|
|
|
PromiseFlatCString(aPath),
|
2010-10-19 17:24:52 -07:00
|
|
|
matchIter)) {
|
2010-11-12 09:32:35 -08:00
|
|
|
cookie = matchIter.Cookie();
|
2007-03-22 10:30:00 -07:00
|
|
|
RemoveCookieFromList(matchIter);
|
2008-04-08 12:24:58 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-04-08 12:24:58 -07:00
|
|
|
// check if we need to add the host to the permissions blacklist.
|
|
|
|
if (aBlocked && mPermissionService) {
|
|
|
|
// strip off the domain dot, if necessary
|
2010-01-12 10:29:20 -08:00
|
|
|
if (!host.IsEmpty() && host.First() == '.')
|
|
|
|
host.Cut(0, 1);
|
|
|
|
|
|
|
|
host.Insert(NS_LITERAL_CSTRING("http://"), 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-04-08 12:24:58 -07:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
NS_NewURI(getter_AddRefs(uri), host);
|
|
|
|
|
|
|
|
if (uri)
|
|
|
|
mPermissionService->SetAccess(uri, nsICookiePermission::ACCESS_DENY);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-04-08 12:24:58 -07:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
if (cookie) {
|
|
|
|
// Everything's done. Notify observers.
|
2013-12-12 17:50:01 -08:00
|
|
|
NotifyChanged(cookie, MOZ_UTF16("deleted"));
|
2010-11-12 09:32:35 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-27 14:37:02 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::Remove(const nsACString &aHost,
|
|
|
|
const nsACString &aName,
|
|
|
|
const nsACString &aPath,
|
|
|
|
bool aBlocked)
|
|
|
|
{
|
|
|
|
return Remove(aHost, NECKO_NO_APP_ID, false, aName, aPath, aBlocked);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/******************************************************************************
|
|
|
|
* nsCookieService impl:
|
|
|
|
* private file I/O functions
|
|
|
|
******************************************************************************/
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
// Begin an asynchronous read from the database.
|
|
|
|
OpenDBResult
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCookieService::Read()
|
|
|
|
{
|
2010-11-12 09:32:35 -08:00
|
|
|
// Set up a statement for the read. Note that our query specifies that
|
2013-09-19 12:28:26 -07:00
|
|
|
// 'baseDomain' not be nullptr -- see below for why.
|
2011-10-03 14:50:23 -07:00
|
|
|
nsCOMPtr<mozIStorageAsyncStatement> stmtRead;
|
|
|
|
nsresult rv = mDefaultDBState->dbConn->CreateAsyncStatement(NS_LITERAL_CSTRING(
|
2010-07-29 12:17:42 -07:00
|
|
|
"SELECT "
|
|
|
|
"name, "
|
|
|
|
"value, "
|
|
|
|
"host, "
|
|
|
|
"path, "
|
|
|
|
"expiry, "
|
|
|
|
"lastAccessed, "
|
2010-10-19 17:24:52 -07:00
|
|
|
"creationTime, "
|
2010-07-29 12:17:42 -07:00
|
|
|
"isSecure, "
|
|
|
|
"isHttpOnly, "
|
2012-09-18 09:04:04 -07:00
|
|
|
"baseDomain, "
|
|
|
|
"appId, "
|
|
|
|
"inBrowserElement "
|
2010-10-19 17:24:53 -07:00
|
|
|
"FROM moz_cookies "
|
2010-11-12 09:32:35 -08:00
|
|
|
"WHERE baseDomain NOTNULL"), getter_AddRefs(stmtRead));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2013-09-19 12:28:26 -07:00
|
|
|
// Set up a statement to delete any rows with a nullptr 'baseDomain'
|
2010-10-19 17:24:53 -07:00
|
|
|
// column. This takes care of any cookies set by browsers that don't
|
|
|
|
// understand the 'baseDomain' column, where the database schema version
|
|
|
|
// is from one that does. (This would occur when downgrading.)
|
2011-10-03 14:50:23 -07:00
|
|
|
nsCOMPtr<mozIStorageAsyncStatement> stmtDeleteNull;
|
|
|
|
rv = mDefaultDBState->dbConn->CreateAsyncStatement(NS_LITERAL_CSTRING(
|
2010-11-12 09:32:35 -08:00
|
|
|
"DELETE FROM moz_cookies WHERE baseDomain ISNULL"),
|
|
|
|
getter_AddRefs(stmtDeleteNull));
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
2010-10-19 17:24:53 -07:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
// Start a new connection for sync reads, to reduce contention with the
|
|
|
|
// background thread. We need to do this before we kick off write statements,
|
|
|
|
// since they can lock the database and prevent connections from being opened.
|
|
|
|
rv = mStorageService->OpenUnsharedDatabase(mDefaultDBState->cookieFile,
|
|
|
|
getter_AddRefs(mDefaultDBState->syncConn));
|
|
|
|
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
// Init our readSet hash and execute the statements. Note that, after this
|
|
|
|
// point, we cannot fail without altering the cleanup code in InitDBStates()
|
|
|
|
// to handle closing of the now-asynchronous connection.
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->hostArray.SetCapacity(kMaxNumberOfCookies);
|
2010-11-12 09:32:35 -08:00
|
|
|
|
|
|
|
mDefaultDBState->readListener = new ReadCookieDBListener(mDefaultDBState);
|
|
|
|
rv = stmtRead->ExecuteAsync(mDefaultDBState->readListener,
|
|
|
|
getter_AddRefs(mDefaultDBState->pendingRead));
|
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
nsCOMPtr<mozIStoragePendingStatement> handle;
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = stmtDeleteNull->ExecuteAsync(mDefaultDBState->removeListener,
|
|
|
|
getter_AddRefs(handle));
|
2010-10-19 17:24:53 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
return RESULT_OK;
|
2010-07-29 12:17:42 -07:00
|
|
|
}
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2010-07-29 12:17:42 -07:00
|
|
|
// 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)
|
2010-07-29 12:17:42 -07:00
|
|
|
{
|
2010-07-29 12:17:42 -07:00
|
|
|
// Skip reading 'baseDomain' -- up to the caller.
|
2010-07-29 12:17:42 -07:00
|
|
|
nsCString name, value, host, path;
|
2013-05-29 17:37:34 -07:00
|
|
|
DebugOnly<nsresult> rv = aRow->GetUTF8String(IDX_NAME, name);
|
2010-07-29 12:17:42 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2013-05-29 17:37:34 -07:00
|
|
|
rv = aRow->GetUTF8String(IDX_VALUE, value);
|
2010-07-29 12:17:42 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2013-05-29 17:37:34 -07:00
|
|
|
rv = aRow->GetUTF8String(IDX_HOST, host);
|
2010-07-29 12:17:42 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2013-05-29 17:37:34 -07:00
|
|
|
rv = aRow->GetUTF8String(IDX_PATH, path);
|
2010-07-29 12:17:42 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2013-05-29 17:37:34 -07:00
|
|
|
int64_t expiry = aRow->AsInt64(IDX_EXPIRY);
|
|
|
|
int64_t lastAccessed = aRow->AsInt64(IDX_LAST_ACCESSED);
|
|
|
|
int64_t creationTime = aRow->AsInt64(IDX_CREATION_TIME);
|
|
|
|
bool isSecure = 0 != aRow->AsInt32(IDX_SECURE);
|
|
|
|
bool isHttpOnly = 0 != aRow->AsInt32(IDX_HTTPONLY);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
// Create a new nsCookie and assign the data.
|
2010-07-29 12:17:42 -07:00
|
|
|
return nsCookie::Create(name, value, host, path,
|
|
|
|
expiry,
|
|
|
|
lastAccessed,
|
2010-10-19 17:24:52 -07:00
|
|
|
creationTime,
|
2011-10-17 07:59:28 -07:00
|
|
|
false,
|
2010-07-29 12:17:42 -07:00
|
|
|
isSecure,
|
|
|
|
isHttpOnly);
|
2010-07-29 12:17:42 -07:00
|
|
|
}
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2010-07-29 12:17:42 -07:00
|
|
|
void
|
2010-07-29 12:17:42 -07:00
|
|
|
nsCookieService::AsyncReadComplete()
|
2010-07-29 12:17:42 -07:00
|
|
|
{
|
2010-10-19 17:24:52 -07:00
|
|
|
// We may be in the private browsing DB state, with a pending read on the
|
|
|
|
// default DB state. (This would occur if we started up in private browsing
|
|
|
|
// mode.) As long as we do all our operations on the default state, we're OK.
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ASSERTION(mDefaultDBState, "no default DBState");
|
|
|
|
NS_ASSERTION(mDefaultDBState->pendingRead, "no pending read");
|
|
|
|
NS_ASSERTION(mDefaultDBState->readListener, "no read listener");
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
// Merge the data read on the background thread with the data synchronously
|
|
|
|
// read on the main thread. Note that transactions on the cookie table may
|
|
|
|
// have occurred on the main thread since, making the background data stale.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mDefaultDBState->hostArray.Length(); ++i) {
|
2010-11-12 09:32:35 -08:00
|
|
|
const CookieDomainTuple &tuple = mDefaultDBState->hostArray[i];
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
// Tiebreak: if the given base domain has already been read in, ignore
|
|
|
|
// the background data. Note that readSet may contain domains that were
|
|
|
|
// queried but found not to be in the db -- that's harmless.
|
2012-09-18 09:04:04 -07:00
|
|
|
if (mDefaultDBState->readSet.GetEntry(tuple.key))
|
2010-07-29 12:17:42 -07:00
|
|
|
continue;
|
|
|
|
|
2013-09-19 12:28:26 -07:00
|
|
|
AddCookieToList(tuple.key, tuple.cookie, mDefaultDBState, nullptr, false);
|
2010-08-01 12:24:38 -07:00
|
|
|
}
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mDefaultDBState->stmtReadDomain = nullptr;
|
|
|
|
mDefaultDBState->pendingRead = nullptr;
|
|
|
|
mDefaultDBState->readListener = nullptr;
|
|
|
|
mDefaultDBState->syncConn = nullptr;
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->hostArray.Clear();
|
|
|
|
mDefaultDBState->readSet.Clear();
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2010-07-29 12:17:42 -07:00
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG, ("Read(): %ld cookies read",
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->cookieCount));
|
2010-11-12 09:32:35 -08:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mObserverService->NotifyObservers(nullptr, "cookie-db-read", nullptr);
|
2010-07-29 12:17:42 -07:00
|
|
|
}
|
|
|
|
|
2010-07-29 12:17:42 -07:00
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsCookieService::CancelAsyncRead(bool aPurgeReadSet)
|
2010-07-29 12:17:42 -07:00
|
|
|
{
|
2010-10-19 17:24:52 -07:00
|
|
|
// We may be in the private browsing DB state, with a pending read on the
|
|
|
|
// default DB state. (This would occur if we started up in private browsing
|
|
|
|
// mode.) As long as we do all our operations on the default state, we're OK.
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ASSERTION(mDefaultDBState, "no default DBState");
|
|
|
|
NS_ASSERTION(mDefaultDBState->pendingRead, "no pending read");
|
|
|
|
NS_ASSERTION(mDefaultDBState->readListener, "no read listener");
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
// Cancel the pending read, kill the read listener, and empty the array
|
|
|
|
// of data already read in on the background thread.
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->readListener->Cancel();
|
2012-10-25 12:47:55 -07:00
|
|
|
DebugOnly<nsresult> rv = mDefaultDBState->pendingRead->Cancel();
|
2010-07-29 12:17:42 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mDefaultDBState->stmtReadDomain = nullptr;
|
|
|
|
mDefaultDBState->pendingRead = nullptr;
|
|
|
|
mDefaultDBState->readListener = nullptr;
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->hostArray.Clear();
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
// Only clear the 'readSet' table if we no longer need to know what set of
|
|
|
|
// data is already accounted for.
|
|
|
|
if (aPurgeReadSet)
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->readSet.Clear();
|
2010-07-29 12:17:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieService::EnsureReadDomain(const nsCookieKey &aKey)
|
2010-07-29 12:17:42 -07:00
|
|
|
{
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ASSERTION(!mDBState->dbConn || mDBState == mDefaultDBState,
|
2010-07-29 12:17:42 -07:00
|
|
|
"not in default db state");
|
|
|
|
|
|
|
|
// Fast path 1: nothing to read, or we've already finished reading.
|
2012-10-26 06:32:10 -07:00
|
|
|
if (MOZ_LIKELY(!mDBState->dbConn || !mDefaultDBState->pendingRead))
|
2010-07-29 12:17:42 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Fast path 2: already read in this particular domain.
|
2012-10-26 06:32:10 -07:00
|
|
|
if (MOZ_LIKELY(mDefaultDBState->readSet.GetEntry(aKey)))
|
2010-07-29 12:17:42 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Read in the data synchronously.
|
2013-05-29 17:37:34 -07:00
|
|
|
// see IDX_NAME, etc. for parameter indexes
|
2010-07-29 12:17:42 -07:00
|
|
|
nsresult rv;
|
2010-11-12 09:32:35 -08:00
|
|
|
if (!mDefaultDBState->stmtReadDomain) {
|
2010-07-29 12:17:42 -07:00
|
|
|
// Cache the statement, since it's likely to be used again.
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->syncConn->CreateStatement(NS_LITERAL_CSTRING(
|
2010-07-29 12:17:42 -07:00
|
|
|
"SELECT "
|
|
|
|
"name, "
|
|
|
|
"value, "
|
|
|
|
"host, "
|
|
|
|
"path, "
|
|
|
|
"expiry, "
|
|
|
|
"lastAccessed, "
|
2010-10-19 17:24:52 -07:00
|
|
|
"creationTime, "
|
2010-07-29 12:17:42 -07:00
|
|
|
"isSecure, "
|
|
|
|
"isHttpOnly "
|
|
|
|
"FROM moz_cookies "
|
2012-09-18 09:04:04 -07:00
|
|
|
"WHERE baseDomain = :baseDomain "
|
|
|
|
" AND appId = :appId "
|
|
|
|
" AND inBrowserElement = :inBrowserElement"),
|
2010-11-12 09:32:35 -08:00
|
|
|
getter_AddRefs(mDefaultDBState->stmtReadDomain));
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Recreate the database.
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
|
|
|
("EnsureReadDomain(): corruption detected when creating statement "
|
|
|
|
"with rv 0x%x", rv));
|
|
|
|
HandleCorruptDB(mDefaultDBState);
|
|
|
|
return;
|
|
|
|
}
|
2010-07-29 12:17:42 -07:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ASSERTION(mDefaultDBState->syncConn, "should have a sync db connection");
|
2010-07-29 13:43:34 -07:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
mozStorageStatementScoper scoper(mDefaultDBState->stmtReadDomain);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->stmtReadDomain->BindUTF8StringByName(
|
2012-09-18 09:04:04 -07:00
|
|
|
NS_LITERAL_CSTRING("baseDomain"), aKey.mBaseDomain);
|
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
rv = mDefaultDBState->stmtReadDomain->BindInt32ByName(
|
|
|
|
NS_LITERAL_CSTRING("appId"), aKey.mAppId);
|
2010-07-29 12:17:42 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2012-09-18 09:04:04 -07:00
|
|
|
rv = mDefaultDBState->stmtReadDomain->BindInt32ByName(
|
|
|
|
NS_LITERAL_CSTRING("inBrowserElement"), aKey.mInBrowserElement ? 1 : 0);
|
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasResult;
|
2010-07-29 12:17:42 -07:00
|
|
|
nsCString name, value, host, path;
|
2010-11-12 09:32:35 -08:00
|
|
|
nsAutoTArray<nsRefPtr<nsCookie>, kMaxCookiesPerHost> array;
|
2010-07-29 12:17:42 -07:00
|
|
|
while (1) {
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->stmtReadDomain->ExecuteStep(&hasResult);
|
2010-11-12 09:32:35 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Recreate the database.
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
|
|
|
("EnsureReadDomain(): corruption detected when reading result "
|
|
|
|
"with rv 0x%x", rv));
|
|
|
|
HandleCorruptDB(mDefaultDBState);
|
|
|
|
return;
|
|
|
|
}
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
if (!hasResult)
|
|
|
|
break;
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
array.AppendElement(GetCookieFromRow(mDefaultDBState->stmtReadDomain));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the cookies to the table in a single operation. This makes sure that
|
|
|
|
// either all the cookies get added, or in the case of corruption, none.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < array.Length(); ++i) {
|
2013-09-19 12:28:26 -07:00
|
|
|
AddCookieToList(aKey, array[i], mDefaultDBState, nullptr, false);
|
2010-07-29 12:17:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add it to the hashset of read entries, so we don't read it again.
|
2012-09-18 09:04:04 -07:00
|
|
|
mDefaultDBState->readSet.PutEntry(aKey);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
2012-09-18 09:04:04 -07:00
|
|
|
("EnsureReadDomain(): %ld cookies read for base domain %s, "
|
|
|
|
" appId=%u, inBrowser=%d", array.Length(), aKey.mBaseDomain.get(),
|
|
|
|
(unsigned)aKey.mAppId, (int)aKey.mInBrowserElement));
|
2010-07-29 12:17:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCookieService::EnsureReadComplete()
|
|
|
|
{
|
2010-11-12 09:32:35 -08:00
|
|
|
NS_ASSERTION(!mDBState->dbConn || mDBState == mDefaultDBState,
|
2010-07-29 12:17:42 -07:00
|
|
|
"not in default db state");
|
|
|
|
|
|
|
|
// Fast path 1: nothing to read, or we've already finished reading.
|
2012-10-26 06:32:10 -07:00
|
|
|
if (MOZ_LIKELY(!mDBState->dbConn || !mDefaultDBState->pendingRead))
|
2010-07-29 12:17:42 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Cancel the pending read, so we don't get any more results.
|
2011-10-17 07:59:28 -07:00
|
|
|
CancelAsyncRead(false);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
// Read in the data synchronously.
|
2013-05-29 17:37:34 -07:00
|
|
|
// see IDX_NAME, etc. for parameter indexes
|
2010-07-29 12:17:42 -07:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
2010-11-12 09:32:35 -08:00
|
|
|
nsresult rv = mDefaultDBState->syncConn->CreateStatement(NS_LITERAL_CSTRING(
|
2010-07-29 12:17:42 -07:00
|
|
|
"SELECT "
|
|
|
|
"name, "
|
|
|
|
"value, "
|
|
|
|
"host, "
|
|
|
|
"path, "
|
|
|
|
"expiry, "
|
|
|
|
"lastAccessed, "
|
2010-10-19 17:24:52 -07:00
|
|
|
"creationTime, "
|
2010-07-29 12:17:42 -07:00
|
|
|
"isSecure, "
|
|
|
|
"isHttpOnly, "
|
2012-09-18 09:04:04 -07:00
|
|
|
"baseDomain, "
|
|
|
|
"appId, "
|
|
|
|
"inBrowserElement "
|
2010-11-12 09:32:35 -08:00
|
|
|
"FROM moz_cookies "
|
|
|
|
"WHERE baseDomain NOTNULL"), getter_AddRefs(stmt));
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Recreate the database.
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
|
|
|
("EnsureReadComplete(): corruption detected when creating statement "
|
|
|
|
"with rv 0x%x", rv));
|
|
|
|
HandleCorruptDB(mDefaultDBState);
|
|
|
|
return;
|
|
|
|
}
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
nsCString baseDomain, name, value, host, path;
|
2012-09-18 09:04:04 -07:00
|
|
|
uint32_t appId;
|
|
|
|
bool inBrowserElement, hasResult;
|
2010-11-12 09:32:35 -08:00
|
|
|
nsAutoTArray<CookieDomainTuple, kMaxNumberOfCookies> array;
|
2010-07-29 12:17:42 -07:00
|
|
|
while (1) {
|
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
2010-11-12 09:32:35 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Recreate the database.
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
|
|
|
("EnsureReadComplete(): corruption detected when reading result "
|
|
|
|
"with rv 0x%x", rv));
|
|
|
|
HandleCorruptDB(mDefaultDBState);
|
|
|
|
return;
|
|
|
|
}
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
if (!hasResult)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Make sure we haven't already read the data.
|
2013-05-29 17:37:34 -07:00
|
|
|
stmt->GetUTF8String(IDX_BASE_DOMAIN, baseDomain);
|
|
|
|
appId = static_cast<uint32_t>(stmt->AsInt32(IDX_APP_ID));
|
|
|
|
inBrowserElement = static_cast<bool>(stmt->AsInt32(IDX_BROWSER_ELEM));
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieKey key(baseDomain, appId, inBrowserElement);
|
|
|
|
if (mDefaultDBState->readSet.GetEntry(key))
|
2010-07-29 12:17:42 -07:00
|
|
|
continue;
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
CookieDomainTuple* tuple = array.AppendElement();
|
2012-09-18 09:04:04 -07:00
|
|
|
tuple->key = key;
|
2010-11-12 09:32:35 -08:00
|
|
|
tuple->cookie = GetCookieFromRow(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the cookies to the table in a single operation. This makes sure that
|
|
|
|
// either all the cookies get added, or in the case of corruption, none.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < array.Length(); ++i) {
|
2010-11-12 09:32:35 -08:00
|
|
|
CookieDomainTuple& tuple = array[i];
|
2013-09-19 12:28:26 -07:00
|
|
|
AddCookieToList(tuple.key, tuple.cookie, mDefaultDBState, nullptr,
|
2011-10-17 07:59:28 -07:00
|
|
|
false);
|
2010-07-29 12:17:42 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mDefaultDBState->syncConn = nullptr;
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->readSet.Clear();
|
2010-07-29 12:17:42 -07:00
|
|
|
|
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
2010-11-12 09:32:35 -08:00
|
|
|
("EnsureReadComplete(): %ld cookies read", array.Length()));
|
2010-07-29 12:17:42 -07:00
|
|
|
}
|
|
|
|
|
2007-11-12 01:28:22 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::ImportCookies(nsIFile *aCookieFile)
|
2007-06-17 14:52:22 -07:00
|
|
|
{
|
2010-11-22 13:50:25 -08:00
|
|
|
if (!mDBState) {
|
|
|
|
NS_WARNING("No DBState! Profile already closed?");
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
// Make sure we're in the default DB state. We don't want people importing
|
|
|
|
// cookies into a private browsing session!
|
2010-11-12 09:32:35 -08:00
|
|
|
if (mDBState != mDefaultDBState) {
|
2010-10-19 17:24:52 -07:00
|
|
|
NS_WARNING("Trying to import cookies in a private browsing session!");
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-06-17 14:52:22 -07:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIInputStream> fileInputStream;
|
2007-11-12 01:28:22 -08:00
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream), aCookieFile);
|
2007-06-17 14:52:22 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsILineInputStream> lineInputStream = do_QueryInterface(fileInputStream, &rv);
|
2007-06-17 14:52:22 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
// First, ensure we've read in everything from the database, if we have one.
|
|
|
|
EnsureReadComplete();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static const char kTrue[] = "TRUE";
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString buffer, baseDomain;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isMore = true;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t hostIndex, isDomainIndex, pathIndex, secureIndex, expiresIndex, nameIndex, cookieIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsASingleFragmentCString::char_iterator iter;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t numInts;
|
|
|
|
int64_t expires;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isDomain, isHttpOnly = false;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t originalCookieCount = mDefaultDBState->cookieCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t currentTimeInUsec = PR_Now();
|
|
|
|
int64_t currentTime = currentTimeInUsec / PR_USEC_PER_SEC;
|
2007-11-18 00:08:34 -08:00
|
|
|
// we use lastAccessedCounter to keep cookies in recently-used order,
|
|
|
|
// so we start by initializing to currentTime (somewhat arbitrary)
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t lastAccessedCounter = currentTimeInUsec;
|
2007-06-17 14:52:22 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* file format is:
|
|
|
|
*
|
|
|
|
* host \t isDomain \t path \t secure \t expires \t name \t cookie
|
|
|
|
*
|
|
|
|
* if this format isn't respected we move onto the next line in the file.
|
|
|
|
* isDomain is "TRUE" or "FALSE" (default to "FALSE")
|
|
|
|
* isSecure is "TRUE" or "FALSE" (default to "TRUE")
|
2012-08-22 08:56:38 -07:00
|
|
|
* expires is a int64_t integer
|
2007-11-18 00:08:34 -08:00
|
|
|
* note 1: cookie can contain tabs.
|
|
|
|
* note 2: cookies will be stored in order of lastAccessed time:
|
|
|
|
* most-recently used come first; least-recently-used come last.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ...but due to bug 178933, we hide HttpOnly cookies from older code
|
|
|
|
* in a comment, so they don't expose HttpOnly cookies to JS.
|
|
|
|
*
|
|
|
|
* The format for HttpOnly cookies is
|
|
|
|
*
|
|
|
|
* #HttpOnly_host \t isDomain \t path \t secure \t expires \t name \t cookie
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-03-12 07:18:38 -08:00
|
|
|
// We will likely be adding a bunch of cookies to the DB, so we use async
|
|
|
|
// batching with storage to make this super fast.
|
|
|
|
nsCOMPtr<mozIStorageBindingParamsArray> paramsArray;
|
2010-11-12 09:32:35 -08:00
|
|
|
if (originalCookieCount == 0 && mDefaultDBState->dbConn) {
|
|
|
|
mDefaultDBState->stmtInsert->NewBindingParamsArray(getter_AddRefs(paramsArray));
|
2010-03-12 07:18:38 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
while (isMore && NS_SUCCEEDED(lineInputStream->ReadLine(buffer, &isMore))) {
|
|
|
|
if (StringBeginsWith(buffer, NS_LITERAL_CSTRING(kHttpOnlyPrefix))) {
|
2011-10-17 07:59:28 -07:00
|
|
|
isHttpOnly = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
hostIndex = sizeof(kHttpOnlyPrefix) - 1;
|
|
|
|
} else if (buffer.IsEmpty() || buffer.First() == '#') {
|
|
|
|
continue;
|
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
isHttpOnly = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
hostIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is a cheap, cheesy way of parsing a tab-delimited line into
|
|
|
|
// string indexes, which can be lopped off into substrings. just for
|
|
|
|
// purposes of obfuscation, it also checks that each token was found.
|
|
|
|
// todo: use iterators?
|
|
|
|
if ((isDomainIndex = buffer.FindChar('\t', hostIndex) + 1) == 0 ||
|
|
|
|
(pathIndex = buffer.FindChar('\t', isDomainIndex) + 1) == 0 ||
|
|
|
|
(secureIndex = buffer.FindChar('\t', pathIndex) + 1) == 0 ||
|
|
|
|
(expiresIndex = buffer.FindChar('\t', secureIndex) + 1) == 0 ||
|
|
|
|
(nameIndex = buffer.FindChar('\t', expiresIndex) + 1) == 0 ||
|
|
|
|
(cookieIndex = buffer.FindChar('\t', nameIndex) + 1) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the expirytime first - if it's expired, ignore
|
|
|
|
// nullstomp the trailing tab, to avoid copying the string
|
|
|
|
buffer.BeginWriting(iter);
|
|
|
|
*(iter += nameIndex - 1) = char(0);
|
|
|
|
numInts = PR_sscanf(buffer.get() + expiresIndex, "%lld", &expires);
|
2007-06-17 14:52:22 -07:00
|
|
|
if (numInts != 1 || expires < currentTime) {
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-06-17 14:52:22 -07:00
|
|
|
isDomain = Substring(buffer, isDomainIndex, pathIndex - isDomainIndex - 1).EqualsLiteral(kTrue);
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsASingleFragmentCString &host = Substring(buffer, hostIndex, isDomainIndex - hostIndex - 1);
|
|
|
|
// check for bad legacy cookies (domain not starting with a dot, or containing a port),
|
|
|
|
// and discard
|
2008-02-02 21:06:57 -08:00
|
|
|
if ((isDomain && !host.IsEmpty() && host.First() != '.') ||
|
2007-03-22 10:30:00 -07:00
|
|
|
host.FindChar(':') != kNotFound) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// compute the baseDomain from the host
|
|
|
|
rv = GetBaseDomainFromHost(host, baseDomain);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
continue;
|
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
// pre-existing cookies have appId=0, inBrowser=false
|
|
|
|
nsCookieKey key = DEFAULT_APP_KEY(baseDomain);
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
// Create a new nsCookie and assign the data. We don't know the cookie
|
|
|
|
// creation time, so just use the current time to generate a unique one.
|
2007-11-12 01:28:22 -08:00
|
|
|
nsRefPtr<nsCookie> newCookie =
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCookie::Create(Substring(buffer, nameIndex, cookieIndex - nameIndex - 1),
|
|
|
|
Substring(buffer, cookieIndex, buffer.Length() - cookieIndex),
|
|
|
|
host,
|
|
|
|
Substring(buffer, pathIndex, secureIndex - pathIndex - 1),
|
2007-06-17 14:52:22 -07:00
|
|
|
expires,
|
2007-11-18 00:08:34 -08:00
|
|
|
lastAccessedCounter,
|
2010-10-19 17:24:52 -07:00
|
|
|
nsCookie::GenerateUniqueCreationTime(currentTimeInUsec),
|
2011-10-17 07:59:28 -07:00
|
|
|
false,
|
2007-03-22 10:30:00 -07:00
|
|
|
Substring(buffer, secureIndex, expiresIndex - secureIndex - 1).EqualsLiteral(kTrue),
|
2007-06-20 22:17:08 -07:00
|
|
|
isHttpOnly);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!newCookie) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2007-06-17 14:52:22 -07:00
|
|
|
|
2007-11-18 00:08:34 -08:00
|
|
|
// trick: preserve the most-recently-used cookie ordering,
|
|
|
|
// by successively decrementing the lastAccessed time
|
|
|
|
lastAccessedCounter--;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-12 07:18:38 -08:00
|
|
|
if (originalCookieCount == 0) {
|
2012-09-18 09:04:04 -07:00
|
|
|
AddCookieToList(key, newCookie, mDefaultDBState, paramsArray);
|
2010-03-12 07:18:38 -08:00
|
|
|
}
|
|
|
|
else {
|
2012-09-18 09:04:04 -07:00
|
|
|
AddInternal(key, newCookie, currentTimeInUsec,
|
2013-09-19 12:28:26 -07:00
|
|
|
nullptr, nullptr, true);
|
2010-03-12 07:18:38 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-12 07:18:38 -08:00
|
|
|
// If we need to write to disk, do so now.
|
|
|
|
if (paramsArray) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length;
|
2010-03-12 07:18:38 -08:00
|
|
|
paramsArray->GetLength(&length);
|
|
|
|
if (length) {
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->stmtInsert->BindParameters(paramsArray);
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
nsCOMPtr<mozIStoragePendingStatement> handle;
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = mDefaultDBState->stmtInsert->ExecuteAsync(
|
|
|
|
mDefaultDBState->insertListener, getter_AddRefs(handle));
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG, ("ImportCookies(): %ld cookies imported",
|
2010-11-12 09:32:35 -08:00
|
|
|
mDefaultDBState->cookieCount));
|
2007-11-01 15:34:16 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsCookieService impl:
|
|
|
|
* private GetCookie/SetCookie helpers
|
|
|
|
******************************************************************************/
|
|
|
|
|
2007-06-26 03:41:58 -07:00
|
|
|
// helper function for GetCookieList
|
2011-09-28 23:19:26 -07:00
|
|
|
static inline bool ispathdelimiter(char c) { return c == '/' || c == '?' || c == '#' || c == ';'; }
|
2007-06-26 03:41:58 -07:00
|
|
|
|
2009-02-22 17:05:28 -08:00
|
|
|
// Comparator class for sorting cookies before sending to a server.
|
2009-12-15 14:21:28 -08:00
|
|
|
class CompareCookiesForSending
|
2009-02-22 17:05:28 -08:00
|
|
|
{
|
2009-12-15 14:21:28 -08:00
|
|
|
public:
|
2011-09-28 23:19:26 -07:00
|
|
|
bool Equals(const nsCookie* aCookie1, const nsCookie* aCookie2) const
|
2009-12-15 14:21:28 -08:00
|
|
|
{
|
2010-10-19 17:24:52 -07:00
|
|
|
return aCookie1->CreationTime() == aCookie2->CreationTime() &&
|
|
|
|
aCookie2->Path().Length() == aCookie1->Path().Length();
|
2009-02-22 17:05:28 -08:00
|
|
|
}
|
2009-12-15 14:21:28 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool LessThan(const nsCookie* aCookie1, const nsCookie* aCookie2) const
|
2009-12-15 14:21:28 -08:00
|
|
|
{
|
2009-02-22 17:05:28 -08:00
|
|
|
// compare by cookie path length in accordance with RFC2109
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t result = aCookie2->Path().Length() - aCookie1->Path().Length();
|
2009-12-15 14:21:28 -08:00
|
|
|
if (result != 0)
|
|
|
|
return result < 0;
|
|
|
|
|
|
|
|
// when path lengths match, older cookies should be listed first. this is
|
|
|
|
// required for backwards compatibility since some websites erroneously
|
|
|
|
// depend on receiving cookies in the order in which they were sent to the
|
|
|
|
// browser! see bug 236772.
|
2010-10-19 17:24:52 -07:00
|
|
|
return aCookie1->CreationTime() < aCookie2->CreationTime();
|
2009-02-22 17:05:28 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-06-26 03:41:58 -07:00
|
|
|
void
|
2010-09-16 13:21:12 -07:00
|
|
|
nsCookieService::GetCookieStringInternal(nsIURI *aHostURI,
|
2010-10-19 09:37:03 -07:00
|
|
|
bool aIsForeign,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aHttpBound,
|
2012-09-18 09:04:04 -07:00
|
|
|
uint32_t aAppId,
|
|
|
|
bool aInBrowserElement,
|
2012-02-08 10:37:07 -08:00
|
|
|
bool aIsPrivate,
|
2010-09-16 13:21:12 -07:00
|
|
|
nsCString &aCookieString)
|
2007-06-26 03:41:58 -07:00
|
|
|
{
|
2010-09-16 13:21:12 -07:00
|
|
|
NS_ASSERTION(aHostURI, "null host!");
|
2007-06-26 03:41:58 -07:00
|
|
|
|
2010-11-22 13:50:25 -08:00
|
|
|
if (!mDBState) {
|
|
|
|
NS_WARNING("No DBState! Profile already closed?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-08 10:37:07 -08:00
|
|
|
AutoRestore<DBState*> savePrevDBState(mDBState);
|
|
|
|
mDBState = aIsPrivate ? mPrivateDBState : mDefaultDBState;
|
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// get the base domain, host, and path from the URI.
|
|
|
|
// e.g. for "www.bbc.co.uk", the base domain would be "bbc.co.uk".
|
2010-01-12 10:29:20 -08:00
|
|
|
// file:// URI's (i.e. with an empty host) are allowed, but any other
|
|
|
|
// scheme must have a non-empty host. A trailing dot in the host
|
2010-12-22 13:50:56 -08:00
|
|
|
// is acceptable.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool requireHostMatch;
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString baseDomain, hostFromURI, pathFromURI;
|
2010-01-12 10:29:20 -08:00
|
|
|
nsresult rv = GetBaseDomain(aHostURI, baseDomain, requireHostMatch);
|
2009-12-19 15:55:56 -08:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = aHostURI->GetAsciiHost(hostFromURI);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = aHostURI->GetPath(pathFromURI);
|
2010-01-12 10:29:20 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
COOKIE_LOGFAILURE(GET_COOKIE, aHostURI, nullptr, "invalid host/path from URI");
|
2009-12-19 15:55:56 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-26 03:41:58 -07:00
|
|
|
// check default prefs
|
2012-09-18 09:04:04 -07:00
|
|
|
CookieStatus cookieStatus = CheckPrefs(aHostURI, aIsForeign, requireHostMatch,
|
|
|
|
nullptr);
|
2007-06-26 03:41:58 -07:00
|
|
|
// for GetCookie(), we don't fire rejection notifications.
|
|
|
|
switch (cookieStatus) {
|
|
|
|
case STATUS_REJECTED:
|
|
|
|
case STATUS_REJECTED_WITH_ERROR:
|
|
|
|
return;
|
2010-06-07 12:32:12 -07:00
|
|
|
default:
|
|
|
|
break;
|
2007-06-26 03:41:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if aHostURI is using an https secure protocol.
|
|
|
|
// if it isn't, then we can't send a secure cookie over the connection.
|
|
|
|
// if SchemeIs fails, assume an insecure connection, to be on the safe side
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isSecure;
|
2007-06-26 03:41:58 -07:00
|
|
|
if (NS_FAILED(aHostURI->SchemeIs("https", &isSecure))) {
|
2011-10-17 07:59:28 -07:00
|
|
|
isSecure = false;
|
2007-06-26 03:41:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCookie *cookie;
|
2009-02-22 17:05:28 -08:00
|
|
|
nsAutoTArray<nsCookie*, 8> foundCookieList;
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t currentTimeInUsec = PR_Now();
|
|
|
|
int64_t currentTime = currentTimeInUsec / PR_USEC_PER_SEC;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool stale = false;
|
2007-06-26 03:41:58 -07:00
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieKey key(baseDomain, aAppId, aInBrowserElement);
|
|
|
|
EnsureReadDomain(key);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// perform the hash lookup
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieEntry *entry = mDBState->hostTable.GetEntry(key);
|
2009-12-19 15:55:56 -08:00
|
|
|
if (!entry)
|
|
|
|
return;
|
2007-06-26 03:41:58 -07:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// iterate the cookies!
|
|
|
|
const nsCookieEntry::ArrayType &cookies = entry->GetCookies();
|
|
|
|
for (nsCookieEntry::IndexType i = 0; i < cookies.Length(); ++i) {
|
|
|
|
cookie = cookies[i];
|
|
|
|
|
|
|
|
// check the host, since the base domain lookup is conservative.
|
|
|
|
// first, check for an exact host or domain cookie match, e.g. "google.com"
|
|
|
|
// or ".google.com"; second a subdomain match, e.g.
|
|
|
|
// host = "mail.google.com", cookie domain = ".google.com".
|
|
|
|
if (cookie->RawHost() != hostFromURI &&
|
|
|
|
!(cookie->IsDomain() && StringEndsWith(hostFromURI, cookie->Host())))
|
|
|
|
continue;
|
2007-06-26 03:41:58 -07:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// if the cookie is secure and the host scheme isn't, we can't send it
|
|
|
|
if (cookie->IsSecure() && !isSecure)
|
|
|
|
continue;
|
2007-06-26 03:41:58 -07:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// if the cookie is httpOnly and it's not going directly to the HTTP
|
|
|
|
// connection, don't send it
|
|
|
|
if (cookie->IsHttpOnly() && !aHttpBound)
|
|
|
|
continue;
|
2007-06-26 03:41:58 -07:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// calculate cookie path length, excluding trailing '/'
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t cookiePathLen = cookie->Path().Length();
|
2009-12-19 15:55:56 -08:00
|
|
|
if (cookiePathLen > 0 && cookie->Path().Last() == '/')
|
|
|
|
--cookiePathLen;
|
2007-06-26 03:41:58 -07:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// if the nsIURI path is shorter than the cookie path, don't send it back
|
|
|
|
if (!StringBeginsWith(pathFromURI, Substring(cookie->Path(), 0, cookiePathLen)))
|
|
|
|
continue;
|
2007-06-26 03:41:58 -07:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
if (pathFromURI.Length() > cookiePathLen &&
|
|
|
|
!ispathdelimiter(pathFromURI.CharAt(cookiePathLen))) {
|
|
|
|
/*
|
|
|
|
* |ispathdelimiter| tests four cases: '/', '?', '#', and ';'.
|
|
|
|
* '/' is the "standard" case; the '?' test allows a site at host/abc?def
|
|
|
|
* to receive a cookie that has a path attribute of abc. this seems
|
|
|
|
* strange but at least one major site (citibank, bug 156725) depends
|
|
|
|
* on it. The test for # and ; are put in to proactively avoid problems
|
|
|
|
* with other sites - these are the only other chars allowed in the path.
|
|
|
|
*/
|
|
|
|
continue;
|
2007-06-26 03:41:58 -07:00
|
|
|
}
|
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// check if the cookie has expired
|
|
|
|
if (cookie->Expiry() <= currentTime) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-06-26 03:41:58 -07:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// all checks passed - add to list and check if lastAccessed stamp needs updating
|
|
|
|
foundCookieList.AppendElement(cookie);
|
|
|
|
if (currentTimeInUsec - cookie->LastAccessed() > kCookieStaleThreshold)
|
2011-10-17 07:59:28 -07:00
|
|
|
stale = true;
|
2009-12-19 15:55:56 -08:00
|
|
|
}
|
2007-06-26 03:41:58 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = foundCookieList.Length();
|
2007-11-18 00:08:34 -08:00
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// update lastAccessed timestamps. we only do this if the timestamp is stale
|
|
|
|
// by a certain amount, to avoid thrashing the db during pageload.
|
|
|
|
if (stale) {
|
2010-06-07 12:32:12 -07:00
|
|
|
// Create an array of parameters to bind to our update statement. Batching
|
|
|
|
// is OK here since we're updating cookies with no interleaved operations.
|
2010-03-12 07:18:38 -08:00
|
|
|
nsCOMPtr<mozIStorageBindingParamsArray> paramsArray;
|
2010-11-12 09:32:35 -08:00
|
|
|
mozIStorageAsyncStatement* stmt = mDBState->stmtUpdate;
|
2010-03-12 07:18:38 -08:00
|
|
|
if (mDBState->dbConn) {
|
|
|
|
stmt->NewBindingParamsArray(getter_AddRefs(paramsArray));
|
|
|
|
}
|
2007-11-18 00:08:34 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < count; ++i) {
|
2009-02-22 17:05:28 -08:00
|
|
|
cookie = foundCookieList.ElementAt(i);
|
2007-11-18 00:08:34 -08:00
|
|
|
|
|
|
|
if (currentTimeInUsec - cookie->LastAccessed() > kCookieStaleThreshold)
|
2010-03-12 07:18:38 -08:00
|
|
|
UpdateCookieInList(cookie, currentTimeInUsec, paramsArray);
|
|
|
|
}
|
|
|
|
// Update the database now if necessary.
|
|
|
|
if (paramsArray) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length;
|
2010-03-12 07:18:38 -08:00
|
|
|
paramsArray->GetLength(&length);
|
|
|
|
if (length) {
|
2012-10-25 12:47:55 -07:00
|
|
|
DebugOnly<nsresult> rv = stmt->BindParameters(paramsArray);
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
nsCOMPtr<mozIStoragePendingStatement> handle;
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = stmt->ExecuteAsync(mDBState->updateListener,
|
|
|
|
getter_AddRefs(handle));
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
}
|
2007-11-18 00:08:34 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-26 03:41:58 -07:00
|
|
|
// return cookies in order of path length; longest to shortest.
|
|
|
|
// this is required per RFC2109. if cookies match in length,
|
|
|
|
// then sort by creation time (see bug 236772).
|
2009-12-15 14:21:28 -08:00
|
|
|
foundCookieList.Sort(CompareCookiesForSending());
|
2007-06-26 03:41:58 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < count; ++i) {
|
2009-02-22 17:05:28 -08:00
|
|
|
cookie = foundCookieList.ElementAt(i);
|
2007-06-26 03:41:58 -07:00
|
|
|
|
|
|
|
// check if we have anything to write
|
|
|
|
if (!cookie->Name().IsEmpty() || !cookie->Value().IsEmpty()) {
|
|
|
|
// if we've already added a cookie to the return list, append a "; " so
|
|
|
|
// that subsequent cookies are delimited in the final list.
|
2010-03-25 16:02:28 -07:00
|
|
|
if (!aCookieString.IsEmpty()) {
|
|
|
|
aCookieString.AppendLiteral("; ");
|
2007-06-26 03:41:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!cookie->Name().IsEmpty()) {
|
|
|
|
// we have a name and value - write both
|
2010-03-25 16:02:28 -07:00
|
|
|
aCookieString += cookie->Name() + NS_LITERAL_CSTRING("=") + cookie->Value();
|
2007-06-26 03:41:58 -07:00
|
|
|
} else {
|
|
|
|
// just write value
|
2010-03-25 16:02:28 -07:00
|
|
|
aCookieString += cookie->Value();
|
2007-06-26 03:41:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-25 16:02:28 -07:00
|
|
|
if (!aCookieString.IsEmpty())
|
2012-09-11 11:00:37 -07:00
|
|
|
COOKIE_LOGSUCCESS(GET_COOKIE, aHostURI, aCookieString, nullptr, false);
|
2007-06-26 03:41:58 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
// processes a single cookie, and returns true if there are more cookies
|
2007-03-22 10:30:00 -07:00
|
|
|
// to be processed
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-03-12 07:18:38 -08:00
|
|
|
nsCookieService::SetCookieInternal(nsIURI *aHostURI,
|
2012-09-18 09:04:04 -07:00
|
|
|
const nsCookieKey &aKey,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aRequireHostMatch,
|
2010-06-07 12:32:12 -07:00
|
|
|
CookieStatus aStatus,
|
2010-03-12 07:18:38 -08:00
|
|
|
nsDependentCString &aCookieHeader,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aServerTime,
|
2012-02-01 02:20:01 -08:00
|
|
|
bool aFromHttp,
|
|
|
|
nsIChannel *aChannel)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-16 13:21:12 -07:00
|
|
|
NS_ASSERTION(aHostURI, "null host!");
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// create a stack-based nsCookieAttributes, to store all the
|
|
|
|
// attributes parsed from the cookie
|
|
|
|
nsCookieAttributes cookieAttributes;
|
|
|
|
|
|
|
|
// init expiryTime such that session cookies won't prematurely expire
|
2012-09-28 12:55:23 -07:00
|
|
|
cookieAttributes.expiryTime = INT64_MAX;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// aCookieHeader is an in/out param to point to the next cookie, if
|
2007-06-26 03:41:58 -07:00
|
|
|
// there is one. Save the present value for logging purposes
|
2007-03-22 10:30:00 -07:00
|
|
|
nsDependentCString savedCookieHeader(aCookieHeader);
|
|
|
|
|
|
|
|
// newCookie says whether there are multiple cookies in the header;
|
|
|
|
// so we can handle them separately.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool newCookie = ParseAttributes(aCookieHeader, cookieAttributes);
|
2007-06-17 14:52:22 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t currentTimeInUsec = PR_Now();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-06-20 22:17:08 -07:00
|
|
|
// calculate expiry time of cookie.
|
2007-03-22 10:30:00 -07:00
|
|
|
cookieAttributes.isSession = GetExpiry(cookieAttributes, aServerTime,
|
2007-06-20 22:17:08 -07:00
|
|
|
currentTimeInUsec / PR_USEC_PER_SEC);
|
2010-06-07 12:32:12 -07:00
|
|
|
if (aStatus == STATUS_ACCEPT_SESSION) {
|
|
|
|
// force lifetime to session. note that the expiration time, if set above,
|
|
|
|
// will still apply.
|
2011-10-17 07:59:28 -07:00
|
|
|
cookieAttributes.isSession = true;
|
2010-06-07 12:32:12 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// reject cookie if it's over the size limit, per RFC2109
|
2007-06-26 03:41:58 -07:00
|
|
|
if ((cookieAttributes.name.Length() + cookieAttributes.value.Length()) > kMaxBytesPerCookie) {
|
|
|
|
COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, savedCookieHeader, "cookie too big (> 4kb)");
|
|
|
|
return newCookie;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-06-26 03:41:58 -07:00
|
|
|
if (cookieAttributes.name.FindChar('\t') != kNotFound) {
|
|
|
|
COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, savedCookieHeader, "invalid name character");
|
|
|
|
return newCookie;
|
2007-05-01 01:14:49 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// domain & path checks
|
2012-09-18 09:04:04 -07:00
|
|
|
if (!CheckDomain(cookieAttributes, aHostURI, aKey.mBaseDomain, aRequireHostMatch)) {
|
2007-06-26 03:41:58 -07:00
|
|
|
COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, savedCookieHeader, "failed the domain tests");
|
|
|
|
return newCookie;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-06-26 03:41:58 -07:00
|
|
|
if (!CheckPath(cookieAttributes, aHostURI)) {
|
|
|
|
COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, savedCookieHeader, "failed the path tests");
|
|
|
|
return newCookie;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// create a new nsCookie and copy attributes
|
|
|
|
nsRefPtr<nsCookie> cookie =
|
2007-06-26 03:41:58 -07:00
|
|
|
nsCookie::Create(cookieAttributes.name,
|
|
|
|
cookieAttributes.value,
|
|
|
|
cookieAttributes.host,
|
|
|
|
cookieAttributes.path,
|
|
|
|
cookieAttributes.expiryTime,
|
2007-11-18 00:08:34 -08:00
|
|
|
currentTimeInUsec,
|
2010-10-19 17:24:52 -07:00
|
|
|
nsCookie::GenerateUniqueCreationTime(currentTimeInUsec),
|
2007-06-26 03:41:58 -07:00
|
|
|
cookieAttributes.isSession,
|
|
|
|
cookieAttributes.isSecure,
|
|
|
|
cookieAttributes.isHttpOnly);
|
|
|
|
if (!cookie)
|
|
|
|
return newCookie;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// check permissions from site permission list, or ask the user,
|
|
|
|
// to determine if we can set the cookie
|
|
|
|
if (mPermissionService) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool permission;
|
2007-03-22 10:30:00 -07:00
|
|
|
mPermissionService->CanSetCookie(aHostURI,
|
2012-02-01 02:20:01 -08:00
|
|
|
aChannel,
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsICookie2*>(static_cast<nsCookie*>(cookie)),
|
2007-06-26 03:41:58 -07:00
|
|
|
&cookieAttributes.isSession,
|
|
|
|
&cookieAttributes.expiryTime,
|
2007-03-22 10:30:00 -07:00
|
|
|
&permission);
|
|
|
|
if (!permission) {
|
2007-06-26 03:41:58 -07:00
|
|
|
COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, savedCookieHeader, "cookie rejected by permission manager");
|
2007-03-22 10:30:00 -07:00
|
|
|
NotifyRejected(aHostURI);
|
2007-06-26 03:41:58 -07:00
|
|
|
return newCookie;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// update isSession and expiry attributes, in case they changed
|
2007-06-26 03:41:58 -07:00
|
|
|
cookie->SetIsSession(cookieAttributes.isSession);
|
|
|
|
cookie->SetExpiry(cookieAttributes.expiryTime);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// add the cookie to the list. AddInternal() takes care of logging.
|
2007-06-17 14:52:22 -07:00
|
|
|
// we get the current time again here, since it may have changed during prompting
|
2012-09-18 09:04:04 -07:00
|
|
|
AddInternal(aKey, cookie, PR_Now(), aHostURI, savedCookieHeader.get(),
|
2010-06-07 12:32:12 -07:00
|
|
|
aFromHttp);
|
2007-06-26 03:41:58 -07:00
|
|
|
return newCookie;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// this is a backend function for adding a cookie to the list, via SetCookie.
|
|
|
|
// also used in the cookie manager, for profile migration from IE.
|
|
|
|
// it either replaces an existing cookie; or adds the cookie to the hashtable,
|
|
|
|
// and deletes a cookie (if maximum number of cookies has been
|
|
|
|
// reached). also performs list maintenance by removing expired cookies.
|
|
|
|
void
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieService::AddInternal(const nsCookieKey &aKey,
|
2010-03-12 07:18:38 -08:00
|
|
|
nsCookie *aCookie,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aCurrentTimeInUsec,
|
2010-03-12 07:18:38 -08:00
|
|
|
nsIURI *aHostURI,
|
|
|
|
const char *aCookieHeader,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aFromHttp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t currentTime = aCurrentTimeInUsec / PR_USEC_PER_SEC;
|
2009-12-15 14:21:28 -08:00
|
|
|
|
2007-07-10 20:21:07 -07:00
|
|
|
// if the new cookie is httponly, make sure we're not coming from script
|
|
|
|
if (!aFromHttp && aCookie->IsHttpOnly()) {
|
2010-10-20 12:11:54 -07:00
|
|
|
COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, aCookieHeader,
|
|
|
|
"cookie is httponly; coming from script");
|
2007-07-10 20:21:07 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsListIter matchIter;
|
2012-09-18 09:04:04 -07:00
|
|
|
bool foundCookie = FindCookie(aKey, aCookie->Host(),
|
2010-10-19 17:24:52 -07:00
|
|
|
aCookie->Name(), aCookie->Path(), matchIter);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsRefPtr<nsCookie> oldCookie;
|
2010-11-12 09:32:35 -08:00
|
|
|
nsCOMPtr<nsIArray> purgedList;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (foundCookie) {
|
2009-12-19 15:55:56 -08:00
|
|
|
oldCookie = matchIter.Cookie();
|
2007-06-26 01:36:50 -07:00
|
|
|
|
2010-10-20 12:11:54 -07:00
|
|
|
// Check if the old cookie is stale (i.e. has already expired). If so, we
|
|
|
|
// need to be careful about the semantics of removing it and adding the new
|
|
|
|
// cookie: we want the behavior wrt adding the new cookie to be the same as
|
|
|
|
// if it didn't exist, but we still want to fire a removal notification.
|
|
|
|
if (oldCookie->Expiry() <= currentTime) {
|
|
|
|
if (aCookie->Expiry() <= currentTime) {
|
|
|
|
// The new cookie has expired and the old one is stale. Nothing to do.
|
|
|
|
COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, aCookieHeader,
|
|
|
|
"cookie has already expired");
|
|
|
|
return;
|
|
|
|
}
|
2007-06-26 01:36:50 -07:00
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
// Remove the stale cookie. We save notification for later, once all list
|
|
|
|
// modifications are complete.
|
2010-10-20 12:11:54 -07:00
|
|
|
RemoveCookieFromList(matchIter);
|
|
|
|
COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, aCookieHeader,
|
2010-11-02 00:06:54 -07:00
|
|
|
"stale cookie was purged");
|
2010-11-12 09:32:35 -08:00
|
|
|
purgedList = CreatePurgeList(oldCookie);
|
2010-10-19 17:24:52 -07:00
|
|
|
|
2010-10-20 12:11:54 -07:00
|
|
|
// We've done all we need to wrt removing and notifying the stale cookie.
|
|
|
|
// From here on out, we pretend pretend it didn't exist, so that we
|
|
|
|
// preserve expected notification semantics when adding the new cookie.
|
2011-10-17 07:59:28 -07:00
|
|
|
foundCookie = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-10-20 12:11:54 -07:00
|
|
|
} else {
|
|
|
|
// If the old cookie is httponly, make sure we're not coming from script.
|
|
|
|
if (!aFromHttp && oldCookie->IsHttpOnly()) {
|
|
|
|
COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, aCookieHeader,
|
|
|
|
"previously stored cookie is httponly; coming from script");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-02 00:06:54 -07:00
|
|
|
// Remove the old cookie.
|
2010-10-20 12:11:54 -07:00
|
|
|
RemoveCookieFromList(matchIter);
|
|
|
|
|
|
|
|
// If the new cookie has expired -- i.e. the intent was simply to delete
|
|
|
|
// the old cookie -- then we're done.
|
|
|
|
if (aCookie->Expiry() <= currentTime) {
|
2010-11-02 00:06:54 -07:00
|
|
|
COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, aCookieHeader,
|
|
|
|
"previously stored cookie was deleted");
|
2013-12-12 17:50:01 -08:00
|
|
|
NotifyChanged(oldCookie, MOZ_UTF16("deleted"));
|
2010-10-20 12:11:54 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preserve creation time of cookie for ordering purposes.
|
2010-10-19 17:24:52 -07:00
|
|
|
aCookie->SetCreationTime(oldCookie->CreationTime());
|
2010-10-20 12:11:54 -07:00
|
|
|
}
|
2007-11-17 03:05:01 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
// check if cookie has already expired
|
2009-12-15 14:21:28 -08:00
|
|
|
if (aCookie->Expiry() <= currentTime) {
|
2010-10-20 12:11:54 -07:00
|
|
|
COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, aCookieHeader,
|
|
|
|
"cookie has already expired");
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we have to delete an old cookie.
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieEntry *entry = mDBState->hostTable.GetEntry(aKey);
|
2010-10-19 17:24:53 -07:00
|
|
|
if (entry && entry->GetCookies().Length() >= mMaxCookiesPerHost) {
|
|
|
|
nsListIter iter;
|
|
|
|
FindStaleCookie(entry, currentTime, iter);
|
2010-11-02 00:06:54 -07:00
|
|
|
oldCookie = iter.Cookie();
|
2010-10-19 17:24:53 -07:00
|
|
|
|
2010-08-25 02:32:01 -07:00
|
|
|
// remove the oldest cookie from the domain
|
2010-10-19 17:24:53 -07:00
|
|
|
RemoveCookieFromList(iter);
|
2010-11-02 00:06:54 -07:00
|
|
|
COOKIE_LOGEVICTED(oldCookie, "Too many cookies for this domain");
|
2010-11-12 09:32:35 -08:00
|
|
|
purgedList = CreatePurgeList(oldCookie);
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
} else if (mDBState->cookieCount >= ADD_TEN_PERCENT(mMaxNumberOfCookies)) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t maxAge = aCurrentTimeInUsec - mDBState->cookieOldestTime;
|
|
|
|
int64_t purgeAge = ADD_TEN_PERCENT(mCookiePurgeAge);
|
2009-12-15 14:21:28 -08:00
|
|
|
if (maxAge >= purgeAge) {
|
|
|
|
// we're over both size and age limits by 10%; time to purge the table!
|
|
|
|
// do this by:
|
|
|
|
// 1) removing expired cookies;
|
2010-10-20 12:11:54 -07:00
|
|
|
// 2) evicting the balance of old cookies until we reach the size limit.
|
2009-12-15 14:21:28 -08:00
|
|
|
// note that the cookieOldestTime indicator can be pessimistic - if it's
|
|
|
|
// older than the actual oldest cookie, we'll just purge more eagerly.
|
2010-11-12 09:32:35 -08:00
|
|
|
purgedList = PurgeCookies(aCurrentTimeInUsec);
|
2009-12-15 14:21:28 -08:00
|
|
|
}
|
2007-11-01 15:34:16 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-06-07 12:32:12 -07:00
|
|
|
// Add the cookie to the db. We do not supply a params array for batching
|
|
|
|
// because this might result in removals and additions being out of order.
|
2013-09-19 12:28:26 -07:00
|
|
|
AddCookieToList(aKey, aCookie, mDBState, nullptr);
|
2010-11-12 09:32:35 -08:00
|
|
|
COOKIE_LOGSUCCESS(SET_COOKIE, aHostURI, aCookieHeader, aCookie, foundCookie);
|
|
|
|
|
|
|
|
// Now that list mutations are complete, notify observers. We do it here
|
|
|
|
// because observers may themselves attempt to mutate the list.
|
|
|
|
if (purgedList) {
|
2013-12-12 17:50:01 -08:00
|
|
|
NotifyChanged(purgedList, MOZ_UTF16("batch-deleted"));
|
2010-11-12 09:32:35 -08:00
|
|
|
}
|
|
|
|
|
2013-12-12 17:50:01 -08:00
|
|
|
NotifyChanged(aCookie, foundCookie ? MOZ_UTF16("changed")
|
|
|
|
: MOZ_UTF16("added"));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsCookieService impl:
|
|
|
|
* private cookie header parsing functions
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
// The following comment block elucidates the function of ParseAttributes.
|
|
|
|
/******************************************************************************
|
|
|
|
** Augmented BNF, modified from RFC2109 Section 4.2.2 and RFC2616 Section 2.1
|
|
|
|
** please note: this BNF deviates from both specifications, and reflects this
|
2007-04-29 00:52:23 -07:00
|
|
|
** implementation. <bnf> indicates a reference to the defined grammar "bnf".
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
** Differences from RFC2109/2616 and explanations:
|
|
|
|
1. implied *LWS
|
|
|
|
The grammar described by this specification is word-based. Except
|
|
|
|
where noted otherwise, linear white space (<LWS>) can be included
|
|
|
|
between any two adjacent words (token or quoted-string), and
|
|
|
|
between adjacent words and separators, without changing the
|
|
|
|
interpretation of a field.
|
|
|
|
<LWS> according to spec is SP|HT|CR|LF, but here, we allow only SP | HT.
|
|
|
|
|
|
|
|
2. We use CR | LF as cookie separators, not ',' per spec, since ',' is in
|
|
|
|
common use inside values.
|
|
|
|
|
|
|
|
3. tokens and values have looser restrictions on allowed characters than
|
|
|
|
spec. This is also due to certain characters being in common use inside
|
|
|
|
values. We allow only '=' to separate token/value pairs, and ';' to
|
|
|
|
terminate tokens or values. <LWS> is allowed within tokens and values
|
|
|
|
(see bug 206022).
|
|
|
|
|
|
|
|
4. where appropriate, full <OCTET>s are allowed, where the spec dictates to
|
|
|
|
reject control chars or non-ASCII chars. This is erring on the loose
|
|
|
|
side, since there's probably no good reason to enforce this strictness.
|
|
|
|
|
|
|
|
5. cookie <NAME> is optional, where spec requires it. This is a fairly
|
|
|
|
trivial case, but allows the flexibility of setting only a cookie <VALUE>
|
|
|
|
with a blank <NAME> and is required by some sites (see bug 169091).
|
|
|
|
|
|
|
|
6. Attribute "HttpOnly", not covered in the RFCs, is supported
|
|
|
|
(see bug 178993).
|
|
|
|
|
|
|
|
** Begin BNF:
|
|
|
|
token = 1*<any allowed-chars except separators>
|
2010-02-09 13:07:16 -08:00
|
|
|
value = 1*<any allowed-chars except value-sep>
|
2007-03-22 10:30:00 -07:00
|
|
|
separators = ";" | "="
|
|
|
|
value-sep = ";"
|
|
|
|
cookie-sep = CR | LF
|
|
|
|
allowed-chars = <any OCTET except NUL or cookie-sep>
|
|
|
|
OCTET = <any 8-bit sequence of data>
|
|
|
|
LWS = SP | HT
|
|
|
|
NUL = <US-ASCII NUL, null control character (0)>
|
|
|
|
CR = <US-ASCII CR, carriage return (13)>
|
|
|
|
LF = <US-ASCII LF, linefeed (10)>
|
|
|
|
SP = <US-ASCII SP, space (32)>
|
|
|
|
HT = <US-ASCII HT, horizontal-tab (9)>
|
|
|
|
|
|
|
|
set-cookie = "Set-Cookie:" cookies
|
|
|
|
cookies = cookie *( cookie-sep cookie )
|
|
|
|
cookie = [NAME "="] VALUE *(";" cookie-av) ; cookie NAME/VALUE must come first
|
|
|
|
NAME = token ; cookie name
|
|
|
|
VALUE = value ; cookie value
|
|
|
|
cookie-av = token ["=" value]
|
|
|
|
|
|
|
|
valid values for cookie-av (checked post-parsing) are:
|
|
|
|
cookie-av = "Path" "=" value
|
|
|
|
| "Domain" "=" value
|
|
|
|
| "Expires" "=" value
|
|
|
|
| "Max-Age" "=" value
|
|
|
|
| "Comment" "=" value
|
|
|
|
| "Version" "=" value
|
|
|
|
| "Secure"
|
|
|
|
| "HttpOnly"
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
// helper functions for GetTokenValue
|
2011-09-28 23:19:26 -07:00
|
|
|
static inline bool iswhitespace (char c) { return c == ' ' || c == '\t'; }
|
|
|
|
static inline bool isterminator (char c) { return c == '\n' || c == '\r'; }
|
|
|
|
static inline bool isvalueseparator (char c) { return isterminator(c) || c == ';'; }
|
|
|
|
static inline bool istokenseparator (char c) { return isvalueseparator(c) || c == '='; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Parse a single token/value pair.
|
2011-10-17 07:59:28 -07:00
|
|
|
// Returns true if a cookie terminator is found, so caller can parse new cookie.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCookieService::GetTokenValue(nsASingleFragmentCString::const_char_iterator &aIter,
|
|
|
|
nsASingleFragmentCString::const_char_iterator &aEndIter,
|
|
|
|
nsDependentCSubstring &aTokenString,
|
|
|
|
nsDependentCSubstring &aTokenValue,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool &aEqualsFound)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsASingleFragmentCString::const_char_iterator start, lastSpace;
|
|
|
|
// initialize value string to clear garbage
|
|
|
|
aTokenValue.Rebind(aIter, aIter);
|
|
|
|
|
|
|
|
// find <token>, including any <LWS> between the end-of-token and the
|
|
|
|
// token separator. we'll remove trailing <LWS> next
|
|
|
|
while (aIter != aEndIter && iswhitespace(*aIter))
|
|
|
|
++aIter;
|
|
|
|
start = aIter;
|
|
|
|
while (aIter != aEndIter && !istokenseparator(*aIter))
|
|
|
|
++aIter;
|
|
|
|
|
|
|
|
// remove trailing <LWS>; first check we're not at the beginning
|
|
|
|
lastSpace = aIter;
|
|
|
|
if (lastSpace != start) {
|
2012-08-25 11:19:00 -07:00
|
|
|
while (--lastSpace != start && iswhitespace(*lastSpace))
|
|
|
|
continue;
|
2007-03-22 10:30:00 -07:00
|
|
|
++lastSpace;
|
|
|
|
}
|
|
|
|
aTokenString.Rebind(start, lastSpace);
|
|
|
|
|
|
|
|
aEqualsFound = (*aIter == '=');
|
|
|
|
if (aEqualsFound) {
|
|
|
|
// find <value>
|
2012-08-25 11:19:00 -07:00
|
|
|
while (++aIter != aEndIter && iswhitespace(*aIter))
|
|
|
|
continue;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
start = aIter;
|
|
|
|
|
2010-02-09 13:07:16 -08:00
|
|
|
// process <token>
|
|
|
|
// just look for ';' to terminate ('=' allowed)
|
|
|
|
while (aIter != aEndIter && !isvalueseparator(*aIter))
|
|
|
|
++aIter;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-02-09 13:07:16 -08:00
|
|
|
// remove trailing <LWS>; first check we're not at the beginning
|
|
|
|
if (aIter != start) {
|
|
|
|
lastSpace = aIter;
|
2012-08-25 11:19:00 -07:00
|
|
|
while (--lastSpace != start && iswhitespace(*lastSpace))
|
|
|
|
continue;
|
2010-02-09 13:07:16 -08:00
|
|
|
aTokenValue.Rebind(start, ++lastSpace);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// aIter is on ';', or terminator, or EOS
|
|
|
|
if (aIter != aEndIter) {
|
2011-10-17 07:59:28 -07:00
|
|
|
// if on terminator, increment past & return true to process new cookie
|
2007-03-22 10:30:00 -07:00
|
|
|
if (isterminator(*aIter)) {
|
|
|
|
++aIter;
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
// fall-through: aIter is on ';', increment and return false
|
2007-03-22 10:30:00 -07:00
|
|
|
++aIter;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parses attributes from cookie header. expires/max-age attributes aren't folded into the
|
|
|
|
// cookie struct here, because we don't know which one to use until we've parsed the header.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCookieService::ParseAttributes(nsDependentCString &aCookieHeader,
|
|
|
|
nsCookieAttributes &aCookieAttributes)
|
|
|
|
{
|
|
|
|
static const char kPath[] = "path";
|
|
|
|
static const char kDomain[] = "domain";
|
|
|
|
static const char kExpires[] = "expires";
|
|
|
|
static const char kMaxage[] = "max-age";
|
|
|
|
static const char kSecure[] = "secure";
|
|
|
|
static const char kHttpOnly[] = "httponly";
|
|
|
|
|
|
|
|
nsASingleFragmentCString::const_char_iterator tempBegin, tempEnd;
|
|
|
|
nsASingleFragmentCString::const_char_iterator cookieStart, cookieEnd;
|
|
|
|
aCookieHeader.BeginReading(cookieStart);
|
|
|
|
aCookieHeader.EndReading(cookieEnd);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
aCookieAttributes.isSecure = false;
|
|
|
|
aCookieAttributes.isHttpOnly = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsDependentCSubstring tokenString(cookieStart, cookieStart);
|
|
|
|
nsDependentCSubstring tokenValue (cookieStart, cookieStart);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool newCookie, equalsFound;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// extract cookie <NAME> & <VALUE> (first attribute), and copy the strings.
|
|
|
|
// if we find multiple cookies, return for processing
|
|
|
|
// note: if there's no '=', we assume token is <VALUE>. this is required by
|
|
|
|
// some sites (see bug 169091).
|
|
|
|
// XXX fix the parser to parse according to <VALUE> grammar for this case
|
|
|
|
newCookie = GetTokenValue(cookieStart, cookieEnd, tokenString, tokenValue, equalsFound);
|
|
|
|
if (equalsFound) {
|
|
|
|
aCookieAttributes.name = tokenString;
|
|
|
|
aCookieAttributes.value = tokenValue;
|
|
|
|
} else {
|
|
|
|
aCookieAttributes.value = tokenString;
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract remaining attributes
|
|
|
|
while (cookieStart != cookieEnd && !newCookie) {
|
|
|
|
newCookie = GetTokenValue(cookieStart, cookieEnd, tokenString, tokenValue, equalsFound);
|
|
|
|
|
|
|
|
if (!tokenValue.IsEmpty()) {
|
|
|
|
tokenValue.BeginReading(tempBegin);
|
|
|
|
tokenValue.EndReading(tempEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// decide which attribute we have, and copy the string
|
|
|
|
if (tokenString.LowerCaseEqualsLiteral(kPath))
|
|
|
|
aCookieAttributes.path = tokenValue;
|
|
|
|
|
|
|
|
else if (tokenString.LowerCaseEqualsLiteral(kDomain))
|
|
|
|
aCookieAttributes.host = tokenValue;
|
|
|
|
|
|
|
|
else if (tokenString.LowerCaseEqualsLiteral(kExpires))
|
|
|
|
aCookieAttributes.expires = tokenValue;
|
|
|
|
|
|
|
|
else if (tokenString.LowerCaseEqualsLiteral(kMaxage))
|
|
|
|
aCookieAttributes.maxage = tokenValue;
|
|
|
|
|
|
|
|
// ignore any tokenValue for isSecure; just set the boolean
|
|
|
|
else if (tokenString.LowerCaseEqualsLiteral(kSecure))
|
2011-10-17 07:59:28 -07:00
|
|
|
aCookieAttributes.isSecure = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// ignore any tokenValue for isHttpOnly (see bug 178993);
|
|
|
|
// just set the boolean
|
|
|
|
else if (tokenString.LowerCaseEqualsLiteral(kHttpOnly))
|
2011-10-17 07:59:28 -07:00
|
|
|
aCookieAttributes.isHttpOnly = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// rebind aCookieHeader, in case we need to process another cookie
|
|
|
|
aCookieHeader.Rebind(cookieStart, cookieEnd);
|
|
|
|
return newCookie;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsCookieService impl:
|
|
|
|
* private domain & permission compliance enforcement functions
|
|
|
|
******************************************************************************/
|
|
|
|
|
2010-01-12 10:29:20 -08:00
|
|
|
// Get the base domain for aHostURI; e.g. for "www.bbc.co.uk", this would be
|
|
|
|
// "bbc.co.uk". Only properly-formed URI's are tolerated, though a trailing
|
2010-12-22 13:50:56 -08:00
|
|
|
// dot may be present. If aHostURI is an IP address, an alias such as
|
|
|
|
// 'localhost', an eTLD such as 'co.uk', or the empty string, aBaseDomain will
|
|
|
|
// be the exact host, and aRequireHostMatch will be true to indicate that
|
|
|
|
// substring matches should not be performed.
|
2009-12-19 15:55:56 -08:00
|
|
|
nsresult
|
|
|
|
nsCookieService::GetBaseDomain(nsIURI *aHostURI,
|
|
|
|
nsCString &aBaseDomain,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool &aRequireHostMatch)
|
2009-12-19 15:55:56 -08:00
|
|
|
{
|
2010-01-12 10:29:20 -08:00
|
|
|
// get the base domain. this will fail if the host contains a leading dot,
|
|
|
|
// more than one trailing dot, or is otherwise malformed.
|
2009-12-19 15:55:56 -08:00
|
|
|
nsresult rv = mTLDService->GetBaseDomain(aHostURI, 0, aBaseDomain);
|
2010-01-12 10:29:20 -08:00
|
|
|
aRequireHostMatch = rv == NS_ERROR_HOST_IS_IP_ADDRESS ||
|
|
|
|
rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS;
|
|
|
|
if (aRequireHostMatch) {
|
|
|
|
// aHostURI is either an IP address, an alias such as 'localhost', an eTLD
|
|
|
|
// such as 'co.uk', or the empty string. use the host as a key in such
|
|
|
|
// cases.
|
2009-12-19 15:55:56 -08:00
|
|
|
rv = aHostURI->GetAsciiHost(aBaseDomain);
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-12-22 13:50:56 -08:00
|
|
|
// aHost (and thus aBaseDomain) may be the string '.'. If so, fail.
|
|
|
|
if (aBaseDomain.Length() == 1 && aBaseDomain.Last() == '.')
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2010-01-12 10:29:20 -08:00
|
|
|
|
|
|
|
// block any URIs without a host that aren't file:// URIs.
|
|
|
|
if (aBaseDomain.IsEmpty()) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isFileURI = false;
|
2010-01-12 10:29:20 -08:00
|
|
|
aHostURI->SchemeIs("file", &isFileURI);
|
|
|
|
if (!isFileURI)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2009-12-19 15:55:56 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-01-12 10:29:20 -08:00
|
|
|
// Get the base domain for aHost; e.g. for "www.bbc.co.uk", this would be
|
|
|
|
// "bbc.co.uk". This is done differently than GetBaseDomain(): it is assumed
|
|
|
|
// that aHost is already normalized, and it may contain a leading dot
|
2010-12-22 13:50:56 -08:00
|
|
|
// (indicating that it represents a domain). A trailing dot may be present.
|
2010-01-12 10:29:20 -08:00
|
|
|
// If aHost is an IP address, an alias such as 'localhost', an eTLD such as
|
|
|
|
// 'co.uk', or the empty string, aBaseDomain will be the exact host, and a
|
|
|
|
// leading dot will be treated as an error.
|
|
|
|
nsresult
|
2009-12-19 15:55:56 -08:00
|
|
|
nsCookieService::GetBaseDomainFromHost(const nsACString &aHost,
|
|
|
|
nsCString &aBaseDomain)
|
|
|
|
{
|
2010-12-22 13:50:56 -08:00
|
|
|
// aHost must not be the string '.'.
|
|
|
|
if (aHost.Length() == 1 && aHost.Last() == '.')
|
2009-12-19 15:55:56 -08:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2010-01-12 10:29:20 -08:00
|
|
|
// aHost may contain a leading dot; if so, strip it now.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool domain = !aHost.IsEmpty() && aHost.First() == '.';
|
2010-01-12 10:29:20 -08:00
|
|
|
|
|
|
|
// get the base domain. this will fail if the host contains a leading dot,
|
|
|
|
// more than one trailing dot, or is otherwise malformed.
|
2011-03-25 04:43:15 -07:00
|
|
|
nsresult rv = mTLDService->GetBaseDomainFromHost(Substring(aHost, domain), 0, aBaseDomain);
|
2009-12-19 15:55:56 -08:00
|
|
|
if (rv == NS_ERROR_HOST_IS_IP_ADDRESS ||
|
|
|
|
rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS) {
|
2010-01-12 10:29:20 -08:00
|
|
|
// aHost is either an IP address, an alias such as 'localhost', an eTLD
|
|
|
|
// such as 'co.uk', or the empty string. use the host as a key in such
|
|
|
|
// cases; however, we reject any such hosts with a leading dot, since it
|
|
|
|
// doesn't make sense for them to be domain cookies.
|
|
|
|
if (domain)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2011-03-25 04:43:15 -07:00
|
|
|
aBaseDomain = aHost;
|
2009-12-19 15:55:56 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-01-12 10:29:20 -08:00
|
|
|
// Normalizes the given hostname, component by component. ASCII/ACE
|
|
|
|
// components are lower-cased, and UTF-8 components are normalized per
|
2010-12-22 13:50:56 -08:00
|
|
|
// RFC 3454 and converted to ACE.
|
2010-01-12 10:29:20 -08:00
|
|
|
nsresult
|
|
|
|
nsCookieService::NormalizeHost(nsCString &aHost)
|
|
|
|
{
|
|
|
|
if (!IsASCII(aHost)) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString host;
|
2010-01-12 10:29:20 -08:00
|
|
|
nsresult rv = mIDNService->ConvertUTF8toACE(aHost, host);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
aHost = host;
|
|
|
|
}
|
|
|
|
|
|
|
|
ToLowerCase(aHost);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
// returns true if 'a' is equal to or a subdomain of 'b',
|
2010-12-22 13:50:56 -08:00
|
|
|
// assuming no leading dots are present.
|
2011-09-28 23:19:26 -07:00
|
|
|
static inline bool IsSubdomainOf(const nsCString &a, const nsCString &b)
|
2009-12-19 15:55:56 -08:00
|
|
|
{
|
|
|
|
if (a == b)
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-12-19 15:55:56 -08:00
|
|
|
if (a.Length() > b.Length())
|
|
|
|
return a[a.Length() - b.Length() - 1] == '.' && StringEndsWith(a, b);
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-12-19 15:55:56 -08:00
|
|
|
}
|
|
|
|
|
2010-06-07 12:32:12 -07:00
|
|
|
CookieStatus
|
2009-12-19 15:55:56 -08:00
|
|
|
nsCookieService::CheckPrefs(nsIURI *aHostURI,
|
2010-10-19 09:37:03 -07:00
|
|
|
bool aIsForeign,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aRequireHostMatch,
|
2009-12-19 15:55:56 -08:00
|
|
|
const char *aCookieHeader)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-03-12 00:53:50 -07:00
|
|
|
nsresult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// don't let ftp sites get/set cookies (could be a security issue)
|
2011-09-28 23:19:26 -07:00
|
|
|
bool ftp;
|
2008-03-12 00:53:50 -07:00
|
|
|
if (NS_SUCCEEDED(aHostURI->SchemeIs("ftp", &ftp)) && ftp) {
|
2007-03-22 10:30:00 -07:00
|
|
|
COOKIE_LOGFAILURE(aCookieHeader ? SET_COOKIE : GET_COOKIE, aHostURI, aCookieHeader, "ftp sites cannot read cookies");
|
|
|
|
return STATUS_REJECTED_WITH_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the permission list first; if we find an entry, it overrides
|
|
|
|
// default prefs. see bug 184059.
|
|
|
|
if (mPermissionService) {
|
|
|
|
nsCookieAccess access;
|
2010-03-25 16:02:28 -07:00
|
|
|
// Not passing an nsIChannel here is probably OK; our implementation
|
|
|
|
// doesn't do anything with it anyway.
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = mPermissionService->CanAccess(aHostURI, nullptr, &access);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// if we found an entry, use it
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
switch (access) {
|
|
|
|
case nsICookiePermission::ACCESS_DENY:
|
2012-09-14 13:57:56 -07:00
|
|
|
COOKIE_LOGFAILURE(aCookieHeader ? SET_COOKIE : GET_COOKIE, aHostURI,
|
|
|
|
aCookieHeader, "cookies are blocked for this site");
|
2007-06-20 22:17:08 -07:00
|
|
|
return STATUS_REJECTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
case nsICookiePermission::ACCESS_ALLOW:
|
2007-06-20 22:17:08 -07:00
|
|
|
return STATUS_ACCEPTED;
|
2012-09-14 13:57:56 -07:00
|
|
|
|
|
|
|
case nsICookiePermission::ACCESS_ALLOW_FIRST_PARTY_ONLY:
|
|
|
|
if (aIsForeign) {
|
|
|
|
COOKIE_LOGFAILURE(aCookieHeader ? SET_COOKIE : GET_COOKIE, aHostURI,
|
|
|
|
aCookieHeader, "third party cookies are blocked "
|
|
|
|
"for this site");
|
|
|
|
return STATUS_REJECTED;
|
|
|
|
|
|
|
|
}
|
|
|
|
return STATUS_ACCEPTED;
|
|
|
|
|
2013-02-22 08:16:01 -08:00
|
|
|
case nsICookiePermission::ACCESS_LIMIT_THIRD_PARTY:
|
|
|
|
if (!aIsForeign)
|
|
|
|
return STATUS_ACCEPTED;
|
|
|
|
uint32_t priorCookieCount = 0;
|
|
|
|
nsAutoCString hostFromURI;
|
|
|
|
aHostURI->GetHost(hostFromURI);
|
|
|
|
CountCookiesFromHost(hostFromURI, &priorCookieCount);
|
|
|
|
if (priorCookieCount == 0) {
|
|
|
|
COOKIE_LOGFAILURE(aCookieHeader ? SET_COOKIE : GET_COOKIE, aHostURI,
|
|
|
|
aCookieHeader, "third party cookies are blocked "
|
|
|
|
"for this site");
|
|
|
|
return STATUS_REJECTED;
|
|
|
|
}
|
|
|
|
return STATUS_ACCEPTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-12 00:53:50 -07:00
|
|
|
// check default prefs
|
2010-06-07 12:32:12 -07:00
|
|
|
if (mCookieBehavior == BEHAVIOR_REJECT) {
|
2007-03-22 10:30:00 -07:00
|
|
|
COOKIE_LOGFAILURE(aCookieHeader ? SET_COOKIE : GET_COOKIE, aHostURI, aCookieHeader, "cookies are disabled");
|
2007-06-20 22:17:08 -07:00
|
|
|
return STATUS_REJECTED;
|
2010-06-07 12:32:12 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-14 13:57:56 -07:00
|
|
|
// check if cookie is foreign
|
|
|
|
if (aIsForeign) {
|
2010-10-19 09:37:03 -07:00
|
|
|
if (mCookieBehavior == BEHAVIOR_ACCEPT && mThirdPartySession)
|
|
|
|
return STATUS_ACCEPT_SESSION;
|
2010-06-07 12:32:12 -07:00
|
|
|
|
2012-09-14 13:57:56 -07:00
|
|
|
if (mCookieBehavior == BEHAVIOR_REJECTFOREIGN) {
|
|
|
|
COOKIE_LOGFAILURE(aCookieHeader ? SET_COOKIE : GET_COOKIE, aHostURI, aCookieHeader, "context is third party");
|
|
|
|
return STATUS_REJECTED;
|
|
|
|
}
|
2013-02-22 08:16:01 -08:00
|
|
|
|
|
|
|
if (mCookieBehavior == BEHAVIOR_LIMITFOREIGN) {
|
|
|
|
uint32_t priorCookieCount = 0;
|
|
|
|
nsAutoCString hostFromURI;
|
|
|
|
aHostURI->GetHost(hostFromURI);
|
|
|
|
CountCookiesFromHost(hostFromURI, &priorCookieCount);
|
|
|
|
if (priorCookieCount == 0) {
|
|
|
|
COOKIE_LOGFAILURE(aCookieHeader ? SET_COOKIE : GET_COOKIE, aHostURI, aCookieHeader, "context is third party");
|
|
|
|
return STATUS_REJECTED;
|
|
|
|
}
|
|
|
|
if (mThirdPartySession)
|
|
|
|
return STATUS_ACCEPT_SESSION;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// if nothing has complained, accept cookie
|
2007-06-20 22:17:08 -07:00
|
|
|
return STATUS_ACCEPTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
// processes domain attribute, and returns true if host has permission to set for this domain.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCookieService::CheckDomain(nsCookieAttributes &aCookieAttributes,
|
2009-12-19 15:55:56 -08:00
|
|
|
nsIURI *aHostURI,
|
|
|
|
const nsCString &aBaseDomain,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aRequireHostMatch)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// get host from aHostURI
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString hostFromURI;
|
2009-12-19 15:55:56 -08:00
|
|
|
aHostURI->GetAsciiHost(hostFromURI);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// if a domain is given, check the host has permission
|
|
|
|
if (!aCookieAttributes.host.IsEmpty()) {
|
2010-12-22 13:50:56 -08:00
|
|
|
// Tolerate leading '.' characters, but not if it's otherwise an empty host.
|
|
|
|
if (aCookieAttributes.host.Length() > 1 &&
|
|
|
|
aCookieAttributes.host.First() == '.') {
|
2009-12-26 13:39:35 -08:00
|
|
|
aCookieAttributes.host.Cut(0, 1);
|
2010-12-22 13:50:56 -08:00
|
|
|
}
|
2009-12-26 13:39:35 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// switch to lowercase now, to avoid case-insensitive compares everywhere
|
|
|
|
ToLowerCase(aCookieAttributes.host);
|
|
|
|
|
2010-01-12 10:29:20 -08:00
|
|
|
// check whether the host is either an IP address, an alias such as
|
|
|
|
// 'localhost', an eTLD such as 'co.uk', or the empty string. in these
|
|
|
|
// cases, require an exact string match for the domain, and leave the cookie
|
|
|
|
// as a non-domain one. bug 105917 originally noted the requirement to deal
|
|
|
|
// with IP addresses.
|
|
|
|
if (aRequireHostMatch)
|
2009-12-19 15:55:56 -08:00
|
|
|
return hostFromURI.Equals(aCookieAttributes.host);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 16:03:22 -08:00
|
|
|
// ensure the proposed domain is derived from the base domain; and also
|
|
|
|
// that the host domain is derived from the proposed domain (per RFC2109).
|
2009-12-19 15:55:56 -08:00
|
|
|
if (IsSubdomainOf(aCookieAttributes.host, aBaseDomain) &&
|
|
|
|
IsSubdomainOf(hostFromURI, aCookieAttributes.host)) {
|
|
|
|
// prepend a dot to indicate a domain cookie
|
|
|
|
aCookieAttributes.host.Insert(NS_LITERAL_CSTRING("."), 0);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-12-19 15:55:56 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* note: RFC2109 section 4.3.2 requires that we check the following:
|
|
|
|
* that the portion of host not in domain does not contain a dot.
|
|
|
|
* this prevents hosts of the form x.y.co.nz from setting cookies in the
|
|
|
|
* entire .co.nz domain. however, it's only a only a partial solution and
|
|
|
|
* it breaks sites (IE doesn't enforce it), so we don't perform this check.
|
|
|
|
*/
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-12-04 15:02:24 -08:00
|
|
|
}
|
2007-12-04 14:26:42 -08:00
|
|
|
|
2007-12-04 16:03:22 -08:00
|
|
|
// no domain specified, use hostFromURI
|
|
|
|
aCookieAttributes.host = hostFromURI;
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCookieService::CheckPath(nsCookieAttributes &aCookieAttributes,
|
|
|
|
nsIURI *aHostURI)
|
|
|
|
{
|
|
|
|
// if a path is given, check the host has permission
|
2011-04-14 12:58:42 -07:00
|
|
|
if (aCookieAttributes.path.IsEmpty() || aCookieAttributes.path.First() != '/') {
|
2007-03-22 10:30:00 -07:00
|
|
|
// strip down everything after the last slash to get the path,
|
|
|
|
// ignoring slashes in the query string part.
|
|
|
|
// if we can QI to nsIURL, that'll take care of the query string portion.
|
|
|
|
// otherwise, it's not an nsIURL and can't have a query string, so just find the last slash.
|
|
|
|
nsCOMPtr<nsIURL> hostURL = do_QueryInterface(aHostURI);
|
|
|
|
if (hostURL) {
|
|
|
|
hostURL->GetDirectory(aCookieAttributes.path);
|
|
|
|
} else {
|
|
|
|
aHostURI->GetPath(aCookieAttributes.path);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t slash = aCookieAttributes.path.RFindChar('/');
|
2007-03-22 10:30:00 -07:00
|
|
|
if (slash != kNotFound) {
|
|
|
|
aCookieAttributes.path.Truncate(slash + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-01 01:14:49 -07:00
|
|
|
#if 0
|
2007-05-07 04:14:01 -07:00
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* The following test is part of the RFC2109 spec. Loosely speaking, it says that a site
|
|
|
|
* cannot set a cookie for a path that it is not on. See bug 155083. However this patch
|
|
|
|
* broke several sites -- nordea (bug 155768) and citibank (bug 156725). So this test has
|
|
|
|
* been disabled, unless we can evangelize these sites.
|
|
|
|
*/
|
|
|
|
// get path from aHostURI
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString pathFromURI;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(aHostURI->GetPath(pathFromURI)) ||
|
|
|
|
!StringBeginsWith(pathFromURI, aCookieAttributes.path)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-05-07 04:14:01 -07:00
|
|
|
if (aCookieAttributes.path.Length() > kMaxBytesPerPath ||
|
|
|
|
aCookieAttributes.path.FindChar('\t') != kNotFound )
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-05-07 04:14:01 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCookieService::GetExpiry(nsCookieAttributes &aCookieAttributes,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aServerTime,
|
|
|
|
int64_t aCurrentTime)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
/* Determine when the cookie should expire. This is done by taking the difference between
|
|
|
|
* the server time and the time the server wants the cookie to expire, and adding that
|
|
|
|
* difference to the client time. This localizes the client time regardless of whether or
|
|
|
|
* not the TZ environment variable was set on the client.
|
|
|
|
*
|
|
|
|
* Note: We need to consider accounting for network lag here, per RFC.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t delta;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// check for max-age attribute first; this overrides expires attribute
|
|
|
|
if (!aCookieAttributes.maxage.IsEmpty()) {
|
|
|
|
// obtain numeric value of maxageAttribute
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t maxage;
|
|
|
|
int32_t numInts = PR_sscanf(aCookieAttributes.maxage.get(), "%lld", &maxage);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// default to session cookie if the conversion failed
|
|
|
|
if (numInts != 1) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-06-17 14:52:22 -07:00
|
|
|
delta = maxage;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// check for expires attribute
|
|
|
|
} else if (!aCookieAttributes.expires.IsEmpty()) {
|
2010-02-10 12:22:49 -08:00
|
|
|
PRTime expires;
|
2010-02-09 13:07:16 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// parse expiry time
|
2011-10-17 07:59:28 -07:00
|
|
|
if (PR_ParseTimeString(aCookieAttributes.expires.get(), true, &expires) != PR_SUCCESS) {
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
delta = expires / int64_t(PR_USEC_PER_SEC) - aServerTime;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// default to session cookie if no attributes found
|
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// if this addition overflows, expiryTime will be less than currentTime
|
|
|
|
// and the cookie will be expired - that's okay.
|
|
|
|
aCookieAttributes.expiryTime = aCurrentTime + delta;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsCookieService impl:
|
|
|
|
* private cookielist management functions
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCookieService::RemoveAllFromMemory()
|
|
|
|
{
|
|
|
|
// clearing the hashtable will call each nsCookieEntry's dtor,
|
|
|
|
// which releases all their respective children.
|
2009-10-16 14:01:47 -07:00
|
|
|
mDBState->hostTable.Clear();
|
|
|
|
mDBState->cookieCount = 0;
|
2012-09-28 12:55:23 -07:00
|
|
|
mDBState->cookieOldestTime = INT64_MAX;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 14:21:28 -08:00
|
|
|
// stores temporary data for enumerating over the hash entries,
|
|
|
|
// since enumeration is done using callback functions
|
|
|
|
struct nsPurgeData
|
|
|
|
{
|
2009-12-19 15:55:56 -08:00
|
|
|
typedef nsTArray<nsListIter> ArrayType;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsPurgeData(int64_t aCurrentTime,
|
|
|
|
int64_t aPurgeTime,
|
2009-12-19 15:55:56 -08:00
|
|
|
ArrayType &aPurgeList,
|
2010-03-12 07:18:38 -08:00
|
|
|
nsIMutableArray *aRemovedList,
|
|
|
|
mozIStorageBindingParamsArray *aParamsArray)
|
2009-12-15 14:21:28 -08:00
|
|
|
: currentTime(aCurrentTime)
|
|
|
|
, purgeTime(aPurgeTime)
|
2012-09-28 12:55:23 -07:00
|
|
|
, oldestTime(INT64_MAX)
|
2009-12-15 14:21:28 -08:00
|
|
|
, purgeList(aPurgeList)
|
2010-03-12 07:18:38 -08:00
|
|
|
, removedList(aRemovedList)
|
|
|
|
, paramsArray(aParamsArray)
|
|
|
|
{
|
|
|
|
}
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
// the current time, in seconds
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t currentTime;
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
// lastAccessed time older than which cookies are eligible for purge
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t purgeTime;
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
// lastAccessed time of the oldest cookie found during purge, to update our indicator
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t oldestTime;
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
// list of cookies over the age limit, for purging
|
2009-12-19 15:55:56 -08:00
|
|
|
ArrayType &purgeList;
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
// list of all cookies we've removed, for notification
|
|
|
|
nsIMutableArray *removedList;
|
2010-03-12 07:18:38 -08:00
|
|
|
|
|
|
|
// The array of parameters to be bound to the statement for deletion later.
|
|
|
|
mozIStorageBindingParamsArray *paramsArray;
|
2009-12-15 14:21:28 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// comparator class for lastaccessed times of cookies.
|
|
|
|
class CompareCookiesByAge {
|
|
|
|
public:
|
2011-09-28 23:19:26 -07:00
|
|
|
bool Equals(const nsListIter &a, const nsListIter &b) const
|
2009-12-15 14:21:28 -08:00
|
|
|
{
|
2010-10-19 17:24:52 -07:00
|
|
|
return a.Cookie()->LastAccessed() == b.Cookie()->LastAccessed() &&
|
|
|
|
a.Cookie()->CreationTime() == b.Cookie()->CreationTime();
|
2009-12-15 14:21:28 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool LessThan(const nsListIter &a, const nsListIter &b) const
|
2009-12-15 14:21:28 -08:00
|
|
|
{
|
2010-10-19 17:24:52 -07:00
|
|
|
// compare by lastAccessed time, and tiebreak by creationTime.
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t result = a.Cookie()->LastAccessed() - b.Cookie()->LastAccessed();
|
2009-12-15 14:21:28 -08:00
|
|
|
if (result != 0)
|
|
|
|
return result < 0;
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
return a.Cookie()->CreationTime() < b.Cookie()->CreationTime();
|
2009-12-19 15:55:56 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// comparator class for sorting cookies by entry and index.
|
|
|
|
class CompareCookiesByIndex {
|
|
|
|
public:
|
2011-09-28 23:19:26 -07:00
|
|
|
bool Equals(const nsListIter &a, const nsListIter &b) const
|
2009-12-19 15:55:56 -08:00
|
|
|
{
|
2010-11-10 10:31:11 -08:00
|
|
|
NS_ASSERTION(a.entry != b.entry || a.index != b.index,
|
|
|
|
"cookie indexes should never be equal");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-12-19 15:55:56 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool LessThan(const nsListIter &a, const nsListIter &b) const
|
2009-12-19 15:55:56 -08:00
|
|
|
{
|
|
|
|
// compare by entryclass pointer, then by index.
|
2009-12-22 14:54:26 -08:00
|
|
|
if (a.entry != b.entry)
|
|
|
|
return a.entry < b.entry;
|
2009-12-19 15:55:56 -08:00
|
|
|
|
|
|
|
return a.index < b.index;
|
2009-12-15 14:21:28 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
PLDHashOperator
|
2009-12-15 14:21:28 -08:00
|
|
|
purgeCookiesCallback(nsCookieEntry *aEntry,
|
|
|
|
void *aArg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-12-15 14:21:28 -08:00
|
|
|
nsPurgeData &data = *static_cast<nsPurgeData*>(aArg);
|
2009-12-19 15:55:56 -08:00
|
|
|
|
|
|
|
const nsCookieEntry::ArrayType &cookies = aEntry->GetCookies();
|
2010-03-12 07:18:38 -08:00
|
|
|
mozIStorageBindingParamsArray *array = data.paramsArray;
|
2009-12-19 15:55:56 -08:00
|
|
|
for (nsCookieEntry::IndexType i = 0; i < cookies.Length(); ) {
|
|
|
|
nsListIter iter(aEntry, i);
|
|
|
|
nsCookie *cookie = cookies[i];
|
|
|
|
|
2009-12-15 14:21:28 -08:00
|
|
|
// check if the cookie has expired
|
2009-12-19 15:55:56 -08:00
|
|
|
if (cookie->Expiry() <= data.currentTime) {
|
2011-10-17 07:59:28 -07:00
|
|
|
data.removedList->AppendElement(cookie, false);
|
2010-08-25 02:32:01 -07:00
|
|
|
COOKIE_LOGEVICTED(cookie, "Cookie expired");
|
2009-12-15 14:21:28 -08:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// remove from list; do not increment our iterator
|
2010-03-25 16:02:28 -07:00
|
|
|
gCookieService->RemoveCookieFromList(iter, array);
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// check if the cookie is over the age limit
|
2009-12-19 15:55:56 -08:00
|
|
|
if (cookie->LastAccessed() <= data.purgeTime) {
|
2009-12-15 14:21:28 -08:00
|
|
|
data.purgeList.AppendElement(iter);
|
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
} else if (cookie->LastAccessed() < data.oldestTime) {
|
2009-12-15 14:21:28 -08:00
|
|
|
// reset our indicator
|
2009-12-19 15:55:56 -08:00
|
|
|
data.oldestTime = cookie->LastAccessed();
|
2009-12-15 14:21:28 -08:00
|
|
|
}
|
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
++i;
|
2009-12-15 14:21:28 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2009-12-15 14:21:28 -08:00
|
|
|
// purges expired and old cookies in a batch operation.
|
2010-11-12 09:32:35 -08:00
|
|
|
already_AddRefed<nsIArray>
|
2012-08-22 08:56:38 -07:00
|
|
|
nsCookieService::PurgeCookies(int64_t aCurrentTimeInUsec)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-10-22 13:53:58 -07:00
|
|
|
NS_ASSERTION(mDBState->hostTable.Count() > 0, "table is empty");
|
2010-11-12 09:32:35 -08:00
|
|
|
EnsureReadComplete();
|
|
|
|
|
2007-11-02 19:15:47 -07:00
|
|
|
#ifdef PR_LOGGING
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t initialCookieCount = mDBState->cookieCount;
|
2009-12-15 14:21:28 -08:00
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
2010-10-19 17:24:52 -07:00
|
|
|
("PurgeCookies(): beginning purge with %ld cookies and %lld oldest age",
|
2009-12-15 14:21:28 -08:00
|
|
|
mDBState->cookieCount, aCurrentTimeInUsec - mDBState->cookieOldestTime));
|
2007-11-02 19:15:47 -07:00
|
|
|
#endif
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
nsAutoTArray<nsListIter, kMaxNumberOfCookies> purgeList;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMutableArray> removedList = do_CreateInstance(NS_ARRAY_CONTRACTID);
|
|
|
|
|
2010-06-07 12:32:12 -07:00
|
|
|
// Create a params array to batch the removals. This is OK here because
|
|
|
|
// all the removals are in order, and there are no interleaved additions.
|
2010-11-12 09:32:35 -08:00
|
|
|
mozIStorageAsyncStatement *stmt = mDBState->stmtDelete;
|
2010-03-12 07:18:38 -08:00
|
|
|
nsCOMPtr<mozIStorageBindingParamsArray> paramsArray;
|
|
|
|
if (mDBState->dbConn) {
|
|
|
|
stmt->NewBindingParamsArray(getter_AddRefs(paramsArray));
|
|
|
|
}
|
|
|
|
|
2009-12-15 14:21:28 -08:00
|
|
|
nsPurgeData data(aCurrentTimeInUsec / PR_USEC_PER_SEC,
|
2010-03-12 07:18:38 -08:00
|
|
|
aCurrentTimeInUsec - mCookiePurgeAge, purgeList, removedList, paramsArray);
|
2009-12-15 14:21:28 -08:00
|
|
|
mDBState->hostTable.EnumerateEntries(purgeCookiesCallback, &data);
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t postExpiryCookieCount = mDBState->cookieCount;
|
2009-12-15 14:21:28 -08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// now we have a list of iterators for cookies over the age limit.
|
|
|
|
// sort them by age, and then we'll see how many to remove...
|
|
|
|
purgeList.Sort(CompareCookiesByAge());
|
|
|
|
|
|
|
|
// only remove old cookies until we reach the max cookie limit, no more.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t excess = mDBState->cookieCount > mMaxNumberOfCookies ?
|
2010-11-10 10:31:11 -08:00
|
|
|
mDBState->cookieCount - mMaxNumberOfCookies : 0;
|
2009-12-15 14:21:28 -08:00
|
|
|
if (purgeList.Length() > excess) {
|
2010-11-10 10:31:11 -08:00
|
|
|
// We're not purging everything in the list, so update our indicator.
|
2009-12-19 15:55:56 -08:00
|
|
|
data.oldestTime = purgeList[excess].Cookie()->LastAccessed();
|
2009-12-15 14:21:28 -08:00
|
|
|
|
|
|
|
purgeList.SetLength(excess);
|
|
|
|
}
|
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// sort the list again, this time grouping cookies with a common entryclass
|
|
|
|
// together, and with ascending index. this allows us to iterate backwards
|
|
|
|
// over the list removing cookies, without having to adjust indexes as we go.
|
|
|
|
purgeList.Sort(CompareCookiesByIndex());
|
|
|
|
for (nsPurgeData::ArrayType::index_type i = purgeList.Length(); i--; ) {
|
|
|
|
nsCookie *cookie = purgeList[i].Cookie();
|
2011-10-17 07:59:28 -07:00
|
|
|
removedList->AppendElement(cookie, false);
|
2010-10-19 17:24:52 -07:00
|
|
|
COOKIE_LOGEVICTED(cookie, "Cookie too old");
|
2009-12-19 15:55:56 -08:00
|
|
|
|
2010-03-12 07:18:38 -08:00
|
|
|
RemoveCookieFromList(purgeList[i], paramsArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the database if we have entries to purge.
|
|
|
|
if (paramsArray) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length;
|
2010-03-12 07:18:38 -08:00
|
|
|
paramsArray->GetLength(&length);
|
|
|
|
if (length) {
|
2012-10-25 12:47:55 -07:00
|
|
|
DebugOnly<nsresult> rv = stmt->BindParameters(paramsArray);
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
nsCOMPtr<mozIStoragePendingStatement> handle;
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = stmt->ExecuteAsync(mDBState->removeListener, getter_AddRefs(handle));
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
}
|
2009-12-15 14:21:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// reset the oldest time indicator
|
|
|
|
mDBState->cookieOldestTime = data.oldestTime;
|
|
|
|
|
2009-10-16 14:01:47 -07:00
|
|
|
COOKIE_LOGSTRING(PR_LOG_DEBUG,
|
2009-12-15 14:21:28 -08:00
|
|
|
("PurgeCookies(): %ld expired; %ld purged; %ld remain; %lld oldest age",
|
|
|
|
initialCookieCount - postExpiryCookieCount,
|
2010-10-19 17:24:52 -07:00
|
|
|
postExpiryCookieCount - mDBState->cookieCount,
|
2009-12-15 14:21:28 -08:00
|
|
|
mDBState->cookieCount,
|
|
|
|
aCurrentTimeInUsec - mDBState->cookieOldestTime));
|
2010-11-12 09:32:35 -08:00
|
|
|
|
|
|
|
return removedList.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-05-05 14:09:54 -07:00
|
|
|
// find whether a given cookie has been previously set. this is provided by the
|
|
|
|
// nsICookieManager2 interface.
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2007-05-05 14:09:54 -07:00
|
|
|
nsCookieService::CookieExists(nsICookie2 *aCookie,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aFoundCookie)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aCookie);
|
|
|
|
|
2010-11-22 13:50:25 -08:00
|
|
|
if (!mDBState) {
|
|
|
|
NS_WARNING("No DBState! Profile already closed?");
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString host, name, path;
|
2008-09-17 06:30:47 -07:00
|
|
|
nsresult rv = aCookie->GetHost(host);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aCookie->GetName(name);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aCookie->GetPath(path);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString baseDomain;
|
2009-12-19 15:55:56 -08:00
|
|
|
rv = GetBaseDomainFromHost(host, baseDomain);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsListIter iter;
|
2012-09-18 09:04:04 -07:00
|
|
|
*aFoundCookie = FindCookie(DEFAULT_APP_KEY(baseDomain), host, name, path, iter);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
// For a given base domain, find either an expired cookie or the oldest cookie
|
|
|
|
// by lastAccessed time.
|
|
|
|
void
|
|
|
|
nsCookieService::FindStaleCookie(nsCookieEntry *aEntry,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aCurrentTime,
|
2010-10-19 17:24:53 -07:00
|
|
|
nsListIter &aIter)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-09-19 12:28:26 -07:00
|
|
|
aIter.entry = nullptr;
|
2009-11-17 15:16:44 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t oldestTime = 0;
|
2010-10-19 17:24:53 -07:00
|
|
|
const nsCookieEntry::ArrayType &cookies = aEntry->GetCookies();
|
2009-12-19 15:55:56 -08:00
|
|
|
for (nsCookieEntry::IndexType i = 0; i < cookies.Length(); ++i) {
|
|
|
|
nsCookie *cookie = cookies[i];
|
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
// If we found an expired cookie, we're done.
|
2010-11-10 10:31:11 -08:00
|
|
|
if (cookie->Expiry() <= aCurrentTime) {
|
2010-10-19 17:24:53 -07:00
|
|
|
aIter.entry = aEntry;
|
|
|
|
aIter.index = i;
|
|
|
|
return;
|
|
|
|
}
|
2009-12-19 15:55:56 -08:00
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
// Check if we've found the oldest cookie so far.
|
|
|
|
if (!aIter.entry || oldestTime > cookie->LastAccessed()) {
|
|
|
|
oldestTime = cookie->LastAccessed();
|
|
|
|
aIter.entry = aEntry;
|
|
|
|
aIter.index = i;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-12-19 15:55:56 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-05-05 14:09:54 -07:00
|
|
|
// count the number of cookies stored by a particular host. this is provided by the
|
|
|
|
// nsICookieManager2 interface.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::CountCookiesFromHost(const nsACString &aHost,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t *aCountFromHost)
|
2007-05-05 14:09:54 -07:00
|
|
|
{
|
2010-11-22 13:50:25 -08:00
|
|
|
if (!mDBState) {
|
|
|
|
NS_WARNING("No DBState! Profile already closed?");
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2010-01-12 10:29:20 -08:00
|
|
|
// first, normalize the hostname, and fail if it contains illegal characters.
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString host(aHost);
|
2010-01-12 10:29:20 -08:00
|
|
|
nsresult rv = NormalizeHost(host);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString baseDomain;
|
2010-01-12 10:29:20 -08:00
|
|
|
rv = GetBaseDomainFromHost(host, baseDomain);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-11-18 10:52:33 -08:00
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieKey key = DEFAULT_APP_KEY(baseDomain);
|
|
|
|
EnsureReadDomain(key);
|
2010-10-19 17:24:53 -07:00
|
|
|
|
|
|
|
// Return a count of all cookies, including expired.
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieEntry *entry = mDBState->hostTable.GetEntry(key);
|
2010-10-19 17:24:53 -07:00
|
|
|
*aCountFromHost = entry ? entry->GetCookies().Length() : 0;
|
2007-05-05 14:09:54 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-07-15 17:57:29 -07:00
|
|
|
// get an enumerator of cookies stored by a particular host. this is provided by the
|
|
|
|
// nsICookieManager2 interface.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::GetCookiesFromHost(const nsACString &aHost,
|
|
|
|
nsISimpleEnumerator **aEnumerator)
|
|
|
|
{
|
2010-11-22 13:50:25 -08:00
|
|
|
if (!mDBState) {
|
|
|
|
NS_WARNING("No DBState! Profile already closed?");
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2010-01-12 10:29:20 -08:00
|
|
|
// first, normalize the hostname, and fail if it contains illegal characters.
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString host(aHost);
|
2010-01-12 10:29:20 -08:00
|
|
|
nsresult rv = NormalizeHost(host);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString baseDomain;
|
2010-01-12 10:29:20 -08:00
|
|
|
rv = GetBaseDomainFromHost(host, baseDomain);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-11-18 10:52:33 -08:00
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieKey key = DEFAULT_APP_KEY(baseDomain);
|
|
|
|
EnsureReadDomain(key);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieEntry *entry = mDBState->hostTable.GetEntry(key);
|
2009-12-19 15:55:56 -08:00
|
|
|
if (!entry)
|
|
|
|
return NS_NewEmptyEnumerator(aEnumerator);
|
2009-07-15 17:57:29 -07:00
|
|
|
|
2010-10-19 17:24:53 -07:00
|
|
|
nsCOMArray<nsICookie> cookieList(mMaxCookiesPerHost);
|
2009-12-19 15:55:56 -08:00
|
|
|
const nsCookieEntry::ArrayType &cookies = entry->GetCookies();
|
|
|
|
for (nsCookieEntry::IndexType i = 0; i < cookies.Length(); ++i) {
|
2010-10-19 17:24:53 -07:00
|
|
|
cookieList.AppendObject(cookies[i]);
|
2009-12-19 15:55:56 -08:00
|
|
|
}
|
2009-07-15 17:57:29 -07:00
|
|
|
|
|
|
|
return NS_NewArrayEnumerator(aEnumerator, cookieList);
|
|
|
|
}
|
|
|
|
|
2012-09-27 14:17:08 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This structure is used as a in/out parameter when enumerating the cookies
|
|
|
|
* for an app.
|
|
|
|
* It will contain the app id and onlyBrowserElement flag information as input
|
|
|
|
* and will contain the array of matching cookies as output.
|
|
|
|
*/
|
|
|
|
struct GetCookiesForAppStruct {
|
|
|
|
uint32_t appId;
|
|
|
|
bool onlyBrowserElement;
|
|
|
|
nsCOMArray<nsICookie> cookies;
|
|
|
|
|
|
|
|
GetCookiesForAppStruct() MOZ_DELETE;
|
|
|
|
GetCookiesForAppStruct(uint32_t aAppId, bool aOnlyBrowserElement)
|
|
|
|
: appId(aAppId)
|
|
|
|
, onlyBrowserElement(aOnlyBrowserElement)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
/* static */ PLDHashOperator
|
|
|
|
nsCookieService::GetCookiesForApp(nsCookieEntry* entry, void* arg)
|
|
|
|
{
|
|
|
|
GetCookiesForAppStruct* data = static_cast<GetCookiesForAppStruct*>(arg);
|
|
|
|
|
|
|
|
if (entry->mAppId != data->appId ||
|
|
|
|
(data->onlyBrowserElement && !entry->mInBrowserElement)) {
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsCookieEntry::ArrayType& cookies = entry->GetCookies();
|
|
|
|
|
|
|
|
for (nsCookieEntry::IndexType i = 0; i < cookies.Length(); ++i) {
|
|
|
|
data->cookies.AppendObject(cookies[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::GetCookiesForApp(uint32_t aAppId, bool aOnlyBrowserElement,
|
|
|
|
nsISimpleEnumerator** aEnumerator)
|
|
|
|
{
|
|
|
|
if (!mDBState) {
|
|
|
|
NS_WARNING("No DBState! Profile already closed?");
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2012-10-23 09:31:19 -07:00
|
|
|
NS_ENSURE_TRUE(aAppId != NECKO_UNKNOWN_APP_ID, NS_ERROR_INVALID_ARG);
|
2012-09-27 14:17:08 -07:00
|
|
|
|
|
|
|
GetCookiesForAppStruct data(aAppId, aOnlyBrowserElement);
|
|
|
|
mDBState->hostTable.EnumerateEntries(GetCookiesForApp, &data);
|
|
|
|
|
|
|
|
return NS_NewArrayEnumerator(aEnumerator, data.cookies);
|
|
|
|
}
|
|
|
|
|
2012-09-27 14:37:02 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::RemoveCookiesForApp(uint32_t aAppId, bool aOnlyBrowserElement)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
|
|
|
nsresult rv = GetCookiesForApp(aAppId, aOnlyBrowserElement,
|
|
|
|
getter_AddRefs(enumerator));
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool hasMore;
|
|
|
|
while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMore)) && hasMore) {
|
2014-03-15 12:00:17 -07:00
|
|
|
nsCOMPtr<nsISupports> supports;
|
2012-09-27 14:37:02 -07:00
|
|
|
nsCOMPtr<nsICookie> cookie;
|
2014-03-15 12:00:17 -07:00
|
|
|
rv = enumerator->GetNext(getter_AddRefs(supports));
|
2012-09-27 14:37:02 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-03-15 12:00:17 -07:00
|
|
|
cookie = do_QueryInterface(supports);
|
|
|
|
|
2012-09-27 14:37:02 -07:00
|
|
|
nsAutoCString host;
|
|
|
|
cookie->GetHost(host);
|
|
|
|
|
|
|
|
nsAutoCString name;
|
|
|
|
cookie->GetName(name);
|
|
|
|
|
|
|
|
nsAutoCString path;
|
|
|
|
cookie->GetPath(path);
|
|
|
|
|
|
|
|
// nsICookie do not carry the appId/inBrowserElement information.
|
|
|
|
// That means we have to guess. This is easy for appId but not for
|
|
|
|
// inBrowserElement flag.
|
|
|
|
// A simple solution is to always ask to remove the cookie with
|
|
|
|
// inBrowserElement = true and only ask for the other one to be removed if
|
|
|
|
// we happen to be in the case of !aOnlyBrowserElement.
|
|
|
|
// Anyway, with this solution, we will likely be looking for unexistant
|
|
|
|
// cookies.
|
|
|
|
//
|
|
|
|
// NOTE: we could make this better by getting nsCookieEntry objects instead
|
|
|
|
// of plain nsICookie.
|
|
|
|
Remove(host, aAppId, true, name, path, false);
|
|
|
|
if (!aOnlyBrowserElement) {
|
|
|
|
Remove(host, aAppId, false, name, path, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-04-08 12:24:58 -07:00
|
|
|
// find an exact cookie specified by host, name, and path that hasn't expired.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieService::FindCookie(const nsCookieKey &aKey,
|
2009-12-19 15:55:56 -08:00
|
|
|
const nsAFlatCString &aHost,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsAFlatCString &aName,
|
|
|
|
const nsAFlatCString &aPath,
|
2010-10-19 17:24:52 -07:00
|
|
|
nsListIter &aIter)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-18 09:04:04 -07:00
|
|
|
EnsureReadDomain(aKey);
|
2010-07-29 12:17:42 -07:00
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieEntry *entry = mDBState->hostTable.GetEntry(aKey);
|
2009-12-19 15:55:56 -08:00
|
|
|
if (!entry)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-11-18 10:52:33 -08:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
const nsCookieEntry::ArrayType &cookies = entry->GetCookies();
|
|
|
|
for (nsCookieEntry::IndexType i = 0; i < cookies.Length(); ++i) {
|
|
|
|
nsCookie *cookie = cookies[i];
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
if (aHost.Equals(cookie->Host()) &&
|
2009-12-19 15:55:56 -08:00
|
|
|
aPath.Equals(cookie->Path()) &&
|
|
|
|
aName.Equals(cookie->Name())) {
|
|
|
|
aIter = nsListIter(entry, i);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
// remove a cookie from the hashtable, and update the iterator state.
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2010-03-12 07:18:38 -08:00
|
|
|
nsCookieService::RemoveCookieFromList(const nsListIter &aIter,
|
|
|
|
mozIStorageBindingParamsArray *aParamsArray)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-06-17 14:52:22 -07:00
|
|
|
// if it's a non-session cookie, remove it from the db
|
2009-12-19 15:55:56 -08:00
|
|
|
if (!aIter.Cookie()->IsSession() && mDBState->dbConn) {
|
2010-03-12 07:18:38 -08:00
|
|
|
// Use the asynchronous binding methods to ensure that we do not acquire
|
|
|
|
// the database lock.
|
2010-11-12 09:32:35 -08:00
|
|
|
mozIStorageAsyncStatement *stmt = mDBState->stmtDelete;
|
2010-03-12 07:18:38 -08:00
|
|
|
nsCOMPtr<mozIStorageBindingParamsArray> paramsArray(aParamsArray);
|
|
|
|
if (!paramsArray) {
|
|
|
|
stmt->NewBindingParamsArray(getter_AddRefs(paramsArray));
|
2010-03-12 08:41:28 -08:00
|
|
|
}
|
2010-03-12 07:18:38 -08:00
|
|
|
|
2010-03-12 07:18:38 -08:00
|
|
|
nsCOMPtr<mozIStorageBindingParams> params;
|
|
|
|
paramsArray->NewBindingParams(getter_AddRefs(params));
|
|
|
|
|
2012-04-07 14:25:00 -07:00
|
|
|
DebugOnly<nsresult> rv =
|
|
|
|
params->BindUTF8StringByName(NS_LITERAL_CSTRING("name"),
|
|
|
|
aIter.Cookie()->Name());
|
2010-10-19 17:24:52 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
|
|
|
rv = params->BindUTF8StringByName(NS_LITERAL_CSTRING("host"),
|
|
|
|
aIter.Cookie()->Host());
|
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
|
|
|
rv = params->BindUTF8StringByName(NS_LITERAL_CSTRING("path"),
|
|
|
|
aIter.Cookie()->Path());
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
|
|
|
rv = paramsArray->AddParams(params);
|
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
|
|
|
// If we weren't given a params array, we'll need to remove it ourselves.
|
|
|
|
if (!aParamsArray) {
|
|
|
|
rv = stmt->BindParameters(paramsArray);
|
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
nsCOMPtr<mozIStoragePendingStatement> handle;
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = stmt->ExecuteAsync(mDBState->removeListener, getter_AddRefs(handle));
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2007-06-17 14:52:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
if (aIter.entry->GetCookies().Length() == 1) {
|
|
|
|
// we're removing the last element in the array - so just remove the entry
|
2007-03-22 10:30:00 -07:00
|
|
|
// from the hash. note that the entryclass' dtor will take care of
|
|
|
|
// releasing this last element for us!
|
2009-10-16 14:01:47 -07:00
|
|
|
mDBState->hostTable.RawRemoveEntry(aIter.entry);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
} else {
|
2009-12-19 15:55:56 -08:00
|
|
|
// just remove the element from the list
|
|
|
|
aIter.entry->GetCookies().RemoveElementAt(aIter.index);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-10-16 14:01:47 -07:00
|
|
|
--mDBState->cookieCount;
|
2007-06-17 14:52:22 -07:00
|
|
|
}
|
|
|
|
|
2010-11-12 09:32:35 -08:00
|
|
|
void
|
2010-03-12 07:18:38 -08:00
|
|
|
bindCookieParameters(mozIStorageBindingParamsArray *aParamsArray,
|
2012-09-18 09:04:04 -07:00
|
|
|
const nsCookieKey &aKey,
|
2010-03-12 07:18:38 -08:00
|
|
|
const nsCookie *aCookie)
|
2007-06-17 14:52:22 -07:00
|
|
|
{
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERTION(aParamsArray, "Null params array passed to bindCookieParameters!");
|
|
|
|
NS_ASSERTION(aCookie, "Null cookie passed to bindCookieParameters!");
|
2009-12-19 15:55:56 -08:00
|
|
|
|
2010-03-12 07:18:38 -08:00
|
|
|
// Use the asynchronous binding methods to ensure that we do not acquire the
|
|
|
|
// database lock.
|
|
|
|
nsCOMPtr<mozIStorageBindingParams> params;
|
2012-04-07 14:25:00 -07:00
|
|
|
DebugOnly<nsresult> rv =
|
|
|
|
aParamsArray->NewBindingParams(getter_AddRefs(params));
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2010-03-12 07:18:38 -08:00
|
|
|
|
2010-03-12 07:18:38 -08:00
|
|
|
// Bind our values to params
|
2010-10-19 17:24:52 -07:00
|
|
|
rv = params->BindUTF8StringByName(NS_LITERAL_CSTRING("baseDomain"),
|
2012-09-18 09:04:04 -07:00
|
|
|
aKey.mBaseDomain);
|
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
|
|
|
rv = params->BindInt32ByName(NS_LITERAL_CSTRING("appId"),
|
|
|
|
aKey.mAppId);
|
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
|
|
|
rv = params->BindInt32ByName(NS_LITERAL_CSTRING("inBrowserElement"),
|
|
|
|
aKey.mInBrowserElement ? 1 : 0);
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
rv = params->BindUTF8StringByName(NS_LITERAL_CSTRING("name"),
|
|
|
|
aCookie->Name());
|
2010-07-29 12:17:42 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
rv = params->BindUTF8StringByName(NS_LITERAL_CSTRING("value"),
|
|
|
|
aCookie->Value());
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
rv = params->BindUTF8StringByName(NS_LITERAL_CSTRING("host"),
|
|
|
|
aCookie->Host());
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
rv = params->BindUTF8StringByName(NS_LITERAL_CSTRING("path"),
|
|
|
|
aCookie->Path());
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
rv = params->BindInt64ByName(NS_LITERAL_CSTRING("expiry"),
|
|
|
|
aCookie->Expiry());
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
rv = params->BindInt64ByName(NS_LITERAL_CSTRING("lastAccessed"),
|
|
|
|
aCookie->LastAccessed());
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
rv = params->BindInt64ByName(NS_LITERAL_CSTRING("creationTime"),
|
|
|
|
aCookie->CreationTime());
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
rv = params->BindInt32ByName(NS_LITERAL_CSTRING("isSecure"),
|
|
|
|
aCookie->IsSecure());
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
2010-10-19 17:24:52 -07:00
|
|
|
rv = params->BindInt32ByName(NS_LITERAL_CSTRING("isHttpOnly"),
|
|
|
|
aCookie->IsHttpOnly());
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
|
|
|
// Bind the params to the array.
|
|
|
|
rv = aParamsArray->AddParams(params);
|
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-29 12:17:42 -07:00
|
|
|
void
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieService::AddCookieToList(const nsCookieKey &aKey,
|
2010-03-12 07:18:38 -08:00
|
|
|
nsCookie *aCookie,
|
2010-10-19 17:24:52 -07:00
|
|
|
DBState *aDBState,
|
2010-03-12 07:18:38 -08:00
|
|
|
mozIStorageBindingParamsArray *aParamsArray,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aWriteToDB)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-10-19 17:24:52 -07:00
|
|
|
NS_ASSERTION(!(aDBState->dbConn && !aWriteToDB && aParamsArray),
|
2010-03-12 07:18:38 -08:00
|
|
|
"Not writing to the DB but have a params array?");
|
2010-10-19 17:24:52 -07:00
|
|
|
NS_ASSERTION(!(!aDBState->dbConn && aParamsArray),
|
2010-03-12 07:18:38 -08:00
|
|
|
"Do not have a DB connection but have a params array?");
|
|
|
|
|
2012-09-18 09:04:04 -07:00
|
|
|
nsCookieEntry *entry = aDBState->hostTable.PutEntry(aKey);
|
2010-07-29 12:17:42 -07:00
|
|
|
NS_ASSERTION(entry, "can't insert element into a null entry!");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
entry->GetCookies().AppendElement(aCookie);
|
2010-10-19 17:24:52 -07:00
|
|
|
++aDBState->cookieCount;
|
2007-06-17 14:52:22 -07:00
|
|
|
|
2009-12-15 14:21:28 -08:00
|
|
|
// keep track of the oldest cookie, for when it comes time to purge
|
2010-10-19 17:24:52 -07:00
|
|
|
if (aCookie->LastAccessed() < aDBState->cookieOldestTime)
|
|
|
|
aDBState->cookieOldestTime = aCookie->LastAccessed();
|
2009-12-15 14:21:28 -08:00
|
|
|
|
2007-06-17 14:52:22 -07:00
|
|
|
// if it's a non-session cookie and hasn't just been read from the db, write it out.
|
2010-10-19 17:24:52 -07:00
|
|
|
if (aWriteToDB && !aCookie->IsSession() && aDBState->dbConn) {
|
2010-11-12 09:32:35 -08:00
|
|
|
mozIStorageAsyncStatement *stmt = aDBState->stmtInsert;
|
2010-05-24 18:09:34 -07:00
|
|
|
nsCOMPtr<mozIStorageBindingParamsArray> paramsArray(aParamsArray);
|
|
|
|
if (!paramsArray) {
|
|
|
|
stmt->NewBindingParamsArray(getter_AddRefs(paramsArray));
|
2007-11-01 15:34:16 -07:00
|
|
|
}
|
2012-09-18 09:04:04 -07:00
|
|
|
bindCookieParameters(paramsArray, aKey, aCookie);
|
2010-03-12 07:18:38 -08:00
|
|
|
|
|
|
|
// If we were supplied an array to store parameters, we shouldn't call
|
|
|
|
// executeAsync - someone up the stack will do this for us.
|
|
|
|
if (!aParamsArray) {
|
2012-10-25 12:47:55 -07:00
|
|
|
DebugOnly<nsresult> rv = stmt->BindParameters(paramsArray);
|
2010-05-24 18:09:34 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2010-03-12 07:18:38 -08:00
|
|
|
nsCOMPtr<mozIStoragePendingStatement> handle;
|
2010-11-12 09:32:35 -08:00
|
|
|
rv = stmt->ExecuteAsync(mDBState->insertListener, getter_AddRefs(handle));
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2007-11-01 15:34:16 -07:00
|
|
|
}
|
2007-06-17 14:52:22 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-11-18 00:08:34 -08:00
|
|
|
void
|
2010-03-12 07:18:38 -08:00
|
|
|
nsCookieService::UpdateCookieInList(nsCookie *aCookie,
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t aLastAccessed,
|
2010-03-12 07:18:38 -08:00
|
|
|
mozIStorageBindingParamsArray *aParamsArray)
|
2007-11-18 00:08:34 -08:00
|
|
|
{
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERTION(aCookie, "Passing a null cookie to UpdateCookieInList!");
|
|
|
|
|
|
|
|
// udpate the lastAccessed timestamp
|
2007-11-18 00:08:34 -08:00
|
|
|
aCookie->SetLastAccessed(aLastAccessed);
|
|
|
|
|
|
|
|
// if it's a non-session cookie, update it in the db too
|
2010-03-12 07:18:38 -08:00
|
|
|
if (!aCookie->IsSession() && aParamsArray) {
|
|
|
|
// Create our params holder.
|
|
|
|
nsCOMPtr<mozIStorageBindingParams> params;
|
|
|
|
aParamsArray->NewBindingParams(getter_AddRefs(params));
|
2007-11-18 00:08:34 -08:00
|
|
|
|
2010-03-12 07:18:38 -08:00
|
|
|
// Bind our parameters.
|
2012-10-25 12:47:55 -07:00
|
|
|
DebugOnly<nsresult> rv =
|
|
|
|
params->BindInt64ByName(NS_LITERAL_CSTRING("lastAccessed"),
|
|
|
|
aLastAccessed);
|
2010-10-19 17:24:52 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
|
|
|
rv = params->BindUTF8StringByName(NS_LITERAL_CSTRING("name"),
|
|
|
|
aCookie->Name());
|
2010-10-07 14:00:17 -07:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2010-10-19 17:24:52 -07:00
|
|
|
|
|
|
|
rv = params->BindUTF8StringByName(NS_LITERAL_CSTRING("host"),
|
|
|
|
aCookie->Host());
|
|
|
|
NS_ASSERT_SUCCESS(rv);
|
|
|
|
|
|
|
|
rv = params->BindUTF8StringByName(NS_LITERAL_CSTRING("path"),
|
|
|
|
aCookie->Path());
|
2010-03-12 07:18:38 -08:00
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2007-11-18 00:08:34 -08:00
|
|
|
|
2010-03-12 07:18:38 -08:00
|
|
|
// Add our bound parameters to the array.
|
|
|
|
rv = aParamsArray->AddParams(params);
|
|
|
|
NS_ASSERT_SUCCESS(rv);
|
2007-11-18 00:08:34 -08:00
|
|
|
}
|
|
|
|
}
|
2010-11-12 09:32:35 -08:00
|
|
|
|
2013-12-03 13:58:41 -08:00
|
|
|
size_t
|
|
|
|
nsCookieService::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
|
|
|
|
if (mDefaultDBState) {
|
|
|
|
n += mDefaultDBState->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
if (mPrivateDBState) {
|
|
|
|
n += mPrivateDBState->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2013-12-07 22:09:10 -08:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(CookieServiceMallocSizeOf)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::CollectReports(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData)
|
2013-12-03 13:58:41 -08:00
|
|
|
{
|
2013-12-07 22:09:10 -08:00
|
|
|
return MOZ_COLLECT_REPORT(
|
|
|
|
"explicit/cookie-service", KIND_HEAP, UNITS_BYTES,
|
|
|
|
SizeOfIncludingThis(CookieServiceMallocSizeOf),
|
|
|
|
"Memory used by the cookie service.");
|
2013-12-03 13:58:41 -08:00
|
|
|
}
|