2003-10-11 01:23:44 +04:00
|
|
|
// vim:ts=2:sw=2:et:
|
2003-03-22 04:24:51 +03:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is cookie manager code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Michiel van Leeuwen (mvl@exedo.nl).
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2003
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2003-10-11 01:23:44 +04:00
|
|
|
* Darin Fisher <darin@meer.net>
|
2003-03-22 04:24:51 +03:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
|
|
|
|
#include "nsCookiePermission.h"
|
2003-10-11 04:06:13 +04:00
|
|
|
#include "nsICookie2.h"
|
2003-03-22 04:24:51 +03:00
|
|
|
#include "nsIServiceManager.h"
|
2003-08-02 21:20:52 +04:00
|
|
|
#include "nsICookiePromptService.h"
|
2003-10-11 04:06:13 +04:00
|
|
|
#include "nsICookieManager2.h"
|
2004-11-15 23:16:52 +03:00
|
|
|
#include "nsNetUtil.h"
|
2003-08-02 21:20:52 +04:00
|
|
|
#include "nsIURI.h"
|
2003-10-11 01:23:44 +04:00
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
2005-02-25 23:46:35 +03:00
|
|
|
#include "nsIPrefBranch2.h"
|
2003-10-11 01:23:44 +04:00
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIDocShellTreeItem.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsILoadGroup.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2003-03-22 04:24:51 +03:00
|
|
|
#include "nsString.h"
|
2003-11-11 08:28:12 +03:00
|
|
|
#include "nsCRT.h"
|
2003-03-22 04:24:51 +03:00
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
************************ nsCookiePermission ********************
|
|
|
|
****************************************************************/
|
|
|
|
|
2004-03-10 09:47:49 +03:00
|
|
|
// values for mCookiesLifetimePolicy
|
|
|
|
// 0 == accept normally
|
|
|
|
// 1 == ask before accepting
|
|
|
|
// 2 == downgrade to session
|
|
|
|
// 3 == limit lifetime to N days
|
|
|
|
static const PRUint32 ACCEPT_NORMALLY = 0;
|
|
|
|
static const PRUint32 ASK_BEFORE_ACCEPT = 1;
|
|
|
|
static const PRUint32 ACCEPT_SESSION = 2;
|
|
|
|
static const PRUint32 ACCEPT_FOR_N_DAYS = 3;
|
|
|
|
|
2003-08-02 21:20:52 +04:00
|
|
|
static const PRBool kDefaultPolicy = PR_TRUE;
|
2004-03-10 09:47:49 +03:00
|
|
|
static const char kCookiesLifetimePolicy[] = "network.cookie.lifetimePolicy";
|
2003-10-16 17:17:12 +04:00
|
|
|
static const char kCookiesLifetimeDays[] = "network.cookie.lifetime.days";
|
2004-03-10 09:47:49 +03:00
|
|
|
static const char kCookiesAlwaysAcceptSession[] = "network.cookie.alwaysAcceptSessionCookies";
|
|
|
|
#ifdef MOZ_MAIL_NEWS
|
2003-10-11 01:23:44 +04:00
|
|
|
static const char kCookiesDisabledForMailNews[] = "network.cookie.disableCookieForMailNews";
|
2003-10-16 17:17:12 +04:00
|
|
|
#endif
|
2004-03-10 09:47:49 +03:00
|
|
|
|
|
|
|
static const char kCookiesPrefsMigrated[] = "network.cookie.prefsMigrated";
|
|
|
|
// obsolete pref names for migration
|
|
|
|
static const char kCookiesLifetimeEnabled[] = "network.cookie.lifetime.enabled";
|
|
|
|
static const char kCookiesLifetimeBehavior[] = "network.cookie.lifetime.behavior";
|
|
|
|
static const char kCookiesAskPermission[] = "network.cookie.warnAboutCookies";
|
|
|
|
|
2003-10-11 01:23:44 +04:00
|
|
|
static const char kPermissionType[] = "cookie";
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2003-10-11 04:06:13 +04:00
|
|
|
// XXX these casts and constructs are horrible, but our nsInt64/nsTime
|
|
|
|
// classes are lacking so we need them for now. see bug 198694.
|
|
|
|
#define USEC_PER_SEC (nsInt64(1000000))
|
|
|
|
#define NOW_IN_SECONDS (nsInt64(PR_Now()) / USEC_PER_SEC)
|
|
|
|
|
2004-03-10 09:47:49 +03:00
|
|
|
#ifdef MOZ_MAIL_NEWS
|
2003-10-11 01:23:44 +04:00
|
|
|
// returns PR_TRUE if URI appears to be the URI of a mailnews protocol
|
|
|
|
static PRBool
|
|
|
|
IsFromMailNews(nsIURI *aURI)
|
|
|
|
{
|
|
|
|
static const char *kMailNewsProtocols[] =
|
|
|
|
{ "imap", "news", "snews", "mailbox", nsnull };
|
|
|
|
PRBool result;
|
|
|
|
for (const char **p = kMailNewsProtocols; *p; ++p) {
|
|
|
|
if (NS_SUCCEEDED(aURI->SchemeIs(*p, &result)) && result)
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2003-10-11 01:23:44 +04:00
|
|
|
NS_IMPL_ISUPPORTS2(nsCookiePermission,
|
|
|
|
nsICookiePermission,
|
|
|
|
nsIObserver)
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCookiePermission::Init()
|
|
|
|
{
|
2003-08-16 01:04:54 +04:00
|
|
|
nsresult rv;
|
2003-10-11 01:23:44 +04:00
|
|
|
mPermMgr = do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// failure to access the pref service is non-fatal...
|
2005-02-25 23:46:35 +03:00
|
|
|
nsCOMPtr<nsIPrefBranch2> prefBranch =
|
2003-10-11 01:23:44 +04:00
|
|
|
do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
if (prefBranch) {
|
2004-03-10 09:47:49 +03:00
|
|
|
prefBranch->AddObserver(kCookiesLifetimePolicy, this, PR_FALSE);
|
2004-01-29 02:45:17 +03:00
|
|
|
prefBranch->AddObserver(kCookiesLifetimeDays, this, PR_FALSE);
|
2004-03-10 09:47:49 +03:00
|
|
|
prefBranch->AddObserver(kCookiesAlwaysAcceptSession, this, PR_FALSE);
|
|
|
|
#ifdef MOZ_MAIL_NEWS
|
2004-01-29 02:45:17 +03:00
|
|
|
prefBranch->AddObserver(kCookiesDisabledForMailNews, this, PR_FALSE);
|
2003-10-16 17:17:12 +04:00
|
|
|
#endif
|
2003-10-11 01:23:44 +04:00
|
|
|
PrefChanged(prefBranch, nsnull);
|
2004-03-10 09:47:49 +03:00
|
|
|
|
|
|
|
// migration code for original cookie prefs
|
|
|
|
PRBool migrated;
|
|
|
|
rv = prefBranch->GetBoolPref(kCookiesPrefsMigrated, &migrated);
|
|
|
|
if (NS_FAILED(rv) || !migrated) {
|
|
|
|
PRBool warnAboutCookies = PR_FALSE;
|
|
|
|
prefBranch->GetBoolPref(kCookiesAskPermission, &warnAboutCookies);
|
|
|
|
|
|
|
|
// if the user is using ask before accepting, we'll use that
|
|
|
|
if (warnAboutCookies)
|
|
|
|
prefBranch->SetIntPref(kCookiesLifetimePolicy, ASK_BEFORE_ACCEPT);
|
|
|
|
|
|
|
|
PRBool lifetimeEnabled = PR_FALSE;
|
|
|
|
prefBranch->GetBoolPref(kCookiesLifetimeEnabled, &lifetimeEnabled);
|
|
|
|
|
|
|
|
// if they're limiting lifetime and not using the prompts, use the
|
|
|
|
// appropriate limited lifetime pref
|
|
|
|
if (lifetimeEnabled && !warnAboutCookies) {
|
|
|
|
PRInt32 lifetimeBehavior;
|
|
|
|
prefBranch->GetIntPref(kCookiesLifetimeBehavior, &lifetimeBehavior);
|
|
|
|
if (lifetimeBehavior)
|
|
|
|
prefBranch->SetIntPref(kCookiesLifetimePolicy, ACCEPT_FOR_N_DAYS);
|
|
|
|
else
|
|
|
|
prefBranch->SetIntPref(kCookiesLifetimePolicy, ACCEPT_SESSION);
|
|
|
|
}
|
|
|
|
prefBranch->SetBoolPref(kCookiesPrefsMigrated, PR_TRUE);
|
|
|
|
}
|
2003-08-16 01:04:54 +04:00
|
|
|
}
|
|
|
|
|
2003-10-11 01:23:44 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-11-11 05:46:06 +03:00
|
|
|
nsCookiePermission::PrefChanged(nsIPrefBranch *aPrefBranch,
|
|
|
|
const char *aPref)
|
2003-10-11 01:23:44 +04:00
|
|
|
{
|
|
|
|
PRBool val;
|
|
|
|
|
2003-11-11 05:46:06 +03:00
|
|
|
#define PREF_CHANGED(_P) (!aPref || !strcmp(aPref, _P))
|
2003-10-11 01:23:44 +04:00
|
|
|
|
2004-03-10 09:47:49 +03:00
|
|
|
if (PREF_CHANGED(kCookiesLifetimePolicy) &&
|
|
|
|
NS_SUCCEEDED(aPrefBranch->GetIntPref(kCookiesLifetimePolicy, &val)))
|
|
|
|
mCookiesLifetimePolicy = val;
|
2003-10-16 17:17:12 +04:00
|
|
|
|
|
|
|
if (PREF_CHANGED(kCookiesLifetimeDays) &&
|
2003-11-11 05:46:06 +03:00
|
|
|
NS_SUCCEEDED(aPrefBranch->GetIntPref(kCookiesLifetimeDays, &val)))
|
2003-10-16 17:17:12 +04:00
|
|
|
// save cookie lifetime in seconds instead of days
|
|
|
|
mCookiesLifetimeSec = val * 24 * 60 * 60;
|
|
|
|
|
2004-03-10 09:47:49 +03:00
|
|
|
if (PREF_CHANGED(kCookiesAlwaysAcceptSession) &&
|
|
|
|
NS_SUCCEEDED(aPrefBranch->GetBoolPref(kCookiesAlwaysAcceptSession, &val)))
|
|
|
|
mCookiesAlwaysAcceptSession = val;
|
|
|
|
|
|
|
|
#ifdef MOZ_MAIL_NEWS
|
2003-10-11 01:23:44 +04:00
|
|
|
if (PREF_CHANGED(kCookiesDisabledForMailNews) &&
|
2003-11-11 05:46:06 +03:00
|
|
|
NS_SUCCEEDED(aPrefBranch->GetBoolPref(kCookiesDisabledForMailNews, &val)))
|
2003-10-11 01:23:44 +04:00
|
|
|
mCookiesDisabledForMailNews = val;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookiePermission::SetAccess(nsIURI *aURI,
|
|
|
|
nsCookieAccess aAccess)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// NOTE: nsCookieAccess values conveniently match up with
|
|
|
|
// the permission codes used by nsIPermissionManager.
|
|
|
|
// this is nice because it avoids conversion code.
|
|
|
|
//
|
|
|
|
return mPermMgr->Add(aURI, kPermissionType, aAccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookiePermission::CanAccess(nsIURI *aURI,
|
|
|
|
nsIURI *aFirstURI,
|
|
|
|
nsIChannel *aChannel,
|
|
|
|
nsCookieAccess *aResult)
|
|
|
|
{
|
2004-03-10 09:47:49 +03:00
|
|
|
#ifdef MOZ_MAIL_NEWS
|
2003-10-11 01:23:44 +04:00
|
|
|
// disable cookies in mailnews if user's prefs say so
|
|
|
|
if (mCookiesDisabledForMailNews) {
|
|
|
|
//
|
|
|
|
// try to examine the "app type" of the docshell owning this request. if
|
|
|
|
// we find a docshell in the heirarchy of type APP_TYPE_MAIL, then assume
|
|
|
|
// this URI is being loaded from within mailnews.
|
|
|
|
//
|
|
|
|
// XXX this is a pretty ugly hack at the moment since cookies really
|
|
|
|
// shouldn't have to talk to the docshell directly. ultimately, we want
|
|
|
|
// to talk to some more generic interface, which the docshell would also
|
|
|
|
// implement. but, the basic mechanism here of leveraging the channel's
|
|
|
|
// (or loadgroup's) notification callbacks attribute seems ideal as it
|
|
|
|
// avoids the problem of having to modify all places in the code which
|
|
|
|
// kick off network requests.
|
|
|
|
//
|
|
|
|
PRUint32 appType = nsIDocShell::APP_TYPE_UNKNOWN;
|
|
|
|
if (aChannel) {
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> item, parent;
|
2004-11-15 23:16:52 +03:00
|
|
|
NS_QueryNotificationCallbacks(aChannel, parent);
|
2003-10-11 01:23:44 +04:00
|
|
|
if (parent) {
|
|
|
|
do {
|
|
|
|
item = parent;
|
|
|
|
nsCOMPtr<nsIDocShell> docshell = do_QueryInterface(item);
|
|
|
|
if (docshell)
|
|
|
|
docshell->GetAppType(&appType);
|
|
|
|
} while (appType != nsIDocShell::APP_TYPE_MAIL &&
|
|
|
|
NS_SUCCEEDED(item->GetParent(getter_AddRefs(parent))) &&
|
|
|
|
parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((appType == nsIDocShell::APP_TYPE_MAIL) ||
|
|
|
|
(aFirstURI && IsFromMailNews(aFirstURI)) ||
|
|
|
|
IsFromMailNews(aURI)) {
|
|
|
|
*aResult = ACCESS_DENY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2004-03-10 09:47:49 +03:00
|
|
|
#endif // MOZ_MAIL_NEWS
|
2003-10-11 01:23:44 +04:00
|
|
|
|
|
|
|
// finally, check with permission manager...
|
2003-10-16 17:17:12 +04:00
|
|
|
nsresult rv = mPermMgr->TestPermission(aURI, kPermissionType, (PRUint32 *) aResult);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
switch (*aResult) {
|
|
|
|
// if we have one of the publicly-available values, just return it
|
|
|
|
case nsIPermissionManager::UNKNOWN_ACTION: // ACCESS_DEFAULT
|
|
|
|
case nsIPermissionManager::ALLOW_ACTION: // ACCESS_ALLOW
|
|
|
|
case nsIPermissionManager::DENY_ACTION: // ACCESS_DENY
|
|
|
|
break;
|
|
|
|
|
2004-04-08 03:27:02 +04:00
|
|
|
// ACCESS_SESSION means the cookie can be accepted; the session
|
|
|
|
// downgrade will occur in CanSetCookie().
|
|
|
|
case nsICookiePermission::ACCESS_SESSION:
|
2003-10-16 17:17:12 +04:00
|
|
|
*aResult = ACCESS_ALLOW;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// ack, an unknown type! just use the defaults.
|
|
|
|
default:
|
|
|
|
*aResult = ACCESS_DEFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2003-10-11 01:23:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookiePermission::CanSetCookie(nsIURI *aURI,
|
|
|
|
nsIChannel *aChannel,
|
2003-10-11 04:06:13 +04:00
|
|
|
nsICookie2 *aCookie,
|
2003-10-16 17:17:12 +04:00
|
|
|
PRBool *aIsSession,
|
|
|
|
PRInt64 *aExpiry,
|
|
|
|
PRBool *aResult)
|
2003-10-11 01:23:44 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aURI, "null uri");
|
|
|
|
|
|
|
|
*aResult = kDefaultPolicy;
|
|
|
|
|
|
|
|
PRUint32 perm;
|
|
|
|
mPermMgr->TestPermission(aURI, kPermissionType, &perm);
|
2003-10-16 17:17:12 +04:00
|
|
|
switch (perm) {
|
2004-04-08 03:27:02 +04:00
|
|
|
case nsICookiePermission::ACCESS_SESSION:
|
2003-10-16 17:17:12 +04:00
|
|
|
*aIsSession = PR_TRUE;
|
|
|
|
|
|
|
|
case nsIPermissionManager::ALLOW_ACTION: // ACCESS_ALLOW
|
2003-10-11 01:23:44 +04:00
|
|
|
*aResult = PR_TRUE;
|
2003-10-16 17:17:12 +04:00
|
|
|
break;
|
2003-08-16 01:04:54 +04:00
|
|
|
|
2003-10-16 17:17:12 +04:00
|
|
|
case nsIPermissionManager::DENY_ACTION: // ACCESS_DENY
|
2003-10-11 01:23:44 +04:00
|
|
|
*aResult = PR_FALSE;
|
2003-10-16 17:17:12 +04:00
|
|
|
break;
|
2003-08-02 21:20:52 +04:00
|
|
|
|
2003-10-16 17:17:12 +04:00
|
|
|
default:
|
|
|
|
// the permission manager has nothing to say about this cookie -
|
|
|
|
// so, we apply the default prefs to it.
|
|
|
|
NS_ASSERTION(perm == nsIPermissionManager::UNKNOWN_ACTION, "unknown permission");
|
2004-03-10 09:47:49 +03:00
|
|
|
|
|
|
|
// now we need to figure out what type of accept policy we're dealing with
|
|
|
|
// if we accept cookies normally, just bail and return
|
|
|
|
if (mCookiesLifetimePolicy == ACCEPT_NORMALLY) {
|
|
|
|
*aResult = PR_TRUE;
|
|
|
|
return NS_OK;
|
2003-08-02 21:20:52 +04:00
|
|
|
}
|
2004-03-10 09:47:49 +03:00
|
|
|
|
|
|
|
// declare this here since it'll be used in all of the remaining cases
|
|
|
|
nsInt64 currentTime = NOW_IN_SECONDS;
|
|
|
|
nsInt64 delta = nsInt64(*aExpiry) - currentTime;
|
|
|
|
|
2003-10-16 17:17:12 +04:00
|
|
|
// check whether the user wants to be prompted
|
2004-03-10 09:47:49 +03:00
|
|
|
if (mCookiesLifetimePolicy == ASK_BEFORE_ACCEPT) {
|
|
|
|
// if it's a session cookie and the user wants to accept these
|
|
|
|
// without asking, just accept the cookie and return
|
|
|
|
if (*aIsSession && mCookiesAlwaysAcceptSession) {
|
|
|
|
*aResult = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-10-16 17:17:12 +04:00
|
|
|
// default to rejecting, in case the prompting process fails
|
|
|
|
*aResult = PR_FALSE;
|
|
|
|
|
|
|
|
nsCAutoString hostPort;
|
|
|
|
aURI->GetHostPort(hostPort);
|
|
|
|
|
|
|
|
if (!aCookie) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
// If there is no host, use the scheme, and append "://",
|
|
|
|
// to make sure it isn't a host or something.
|
|
|
|
// This is done to make the dialog appear for javascript cookies from
|
|
|
|
// file:// urls, and make the text on it not too weird. (bug 209689)
|
|
|
|
if (hostPort.IsEmpty()) {
|
|
|
|
aURI->GetScheme(hostPort);
|
|
|
|
if (hostPort.IsEmpty()) {
|
|
|
|
// still empty. Just return the default.
|
|
|
|
return NS_OK;
|
2003-10-11 04:06:13 +04:00
|
|
|
}
|
2003-10-16 17:17:12 +04:00
|
|
|
hostPort = hostPort + NS_LITERAL_CSTRING("://");
|
2003-10-11 04:06:13 +04:00
|
|
|
}
|
|
|
|
|
2003-10-16 17:17:12 +04:00
|
|
|
// we don't cache the cookiePromptService - it's not used often, so not
|
|
|
|
// worth the memory.
|
2004-03-10 09:47:49 +03:00
|
|
|
nsresult rv;
|
2003-10-16 17:17:12 +04:00
|
|
|
nsCOMPtr<nsICookiePromptService> cookiePromptService =
|
|
|
|
do_GetService(NS_COOKIEPROMPTSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// try to get a nsIDOMWindow from the channel...
|
|
|
|
nsCOMPtr<nsIDOMWindow> parent;
|
2004-11-15 23:16:52 +03:00
|
|
|
if (aChannel)
|
|
|
|
NS_QueryNotificationCallbacks(aChannel, parent);
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2003-10-16 17:17:12 +04:00
|
|
|
// get some useful information to present to the user:
|
|
|
|
// whether a previous cookie already exists, and how many cookies this host
|
|
|
|
// has set
|
|
|
|
PRBool foundCookie;
|
|
|
|
PRUint32 countFromHost;
|
|
|
|
nsCOMPtr<nsICookieManager2> cookieManager = do_GetService(NS_COOKIEMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = cookieManager->FindMatchingCookie(aCookie, &countFromHost, &foundCookie);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// check if the cookie we're trying to set is already expired, and return;
|
|
|
|
// but only if there's no previous cookie, because then we need to delete the previous
|
|
|
|
// cookie. we need this check to avoid prompting the user for already-expired cookies.
|
|
|
|
if (!foundCookie && !*aIsSession && delta <= nsInt64(0)) {
|
|
|
|
// the cookie has already expired. accept it, and let the backend figure
|
|
|
|
// out it's expired, so that we get correct logging & notifications.
|
|
|
|
*aResult = PR_TRUE;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool rememberDecision = PR_FALSE;
|
|
|
|
rv = cookiePromptService->CookieDialog(parent, aCookie, hostPort,
|
|
|
|
countFromHost, foundCookie,
|
|
|
|
&rememberDecision, aResult);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2004-01-29 06:34:58 +03:00
|
|
|
|
|
|
|
if (*aResult == nsICookiePromptService::ACCEPT_SESSION_COOKIE)
|
|
|
|
*aIsSession = PR_TRUE;
|
2003-10-16 17:17:12 +04:00
|
|
|
|
|
|
|
if (rememberDecision) {
|
2004-01-29 06:34:58 +03:00
|
|
|
switch (*aResult) {
|
|
|
|
case nsICookiePromptService::DENY_COOKIE:
|
|
|
|
mPermMgr->Add(aURI, kPermissionType, (PRUint32) nsIPermissionManager::DENY_ACTION);
|
|
|
|
break;
|
|
|
|
case nsICookiePromptService::ACCEPT_COOKIE:
|
|
|
|
mPermMgr->Add(aURI, kPermissionType, (PRUint32) nsIPermissionManager::ALLOW_ACTION);
|
|
|
|
break;
|
|
|
|
case nsICookiePromptService::ACCEPT_SESSION_COOKIE:
|
2004-04-08 03:27:02 +04:00
|
|
|
mPermMgr->Add(aURI, kPermissionType, nsICookiePermission::ACCESS_SESSION);
|
2004-01-29 06:34:58 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2003-10-16 17:17:12 +04:00
|
|
|
}
|
2004-03-10 09:47:49 +03:00
|
|
|
} else {
|
|
|
|
// we're not prompting, so we must be limiting the lifetime somehow
|
|
|
|
// if it's a session cookie, we do nothing
|
|
|
|
if (!*aIsSession && delta > nsInt64(0)) {
|
|
|
|
if (mCookiesLifetimePolicy == ACCEPT_SESSION) {
|
|
|
|
// limit lifetime to session
|
|
|
|
*aIsSession = PR_TRUE;
|
|
|
|
} else if (delta > mCookiesLifetimeSec) {
|
|
|
|
// limit lifetime to specified time
|
|
|
|
*aExpiry = currentTime + mCookiesLifetimeSec;
|
|
|
|
}
|
|
|
|
}
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-10-11 01:23:44 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookiePermission::Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject);
|
2003-11-11 05:46:06 +03:00
|
|
|
NS_ASSERTION(!nsCRT::strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic),
|
|
|
|
"unexpected topic - we only deal with pref changes!");
|
|
|
|
|
2003-10-11 01:23:44 +04:00
|
|
|
if (prefBranch)
|
2003-11-11 05:46:06 +03:00
|
|
|
PrefChanged(prefBranch, NS_LossyConvertUTF16toASCII(aData).get());
|
2003-10-11 01:23:44 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|