1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim: set ts=4 et sw=4 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "DomainPolicy.h"
8 #include "nsScriptSecurityManager.h"
13 NS_IMPL_ISUPPORTS1(DomainPolicy
, nsIDomainPolicy
)
15 DomainPolicy::DomainPolicy() : mBlacklist(new DomainSet())
16 , mSuperBlacklist(new DomainSet())
17 , mWhitelist(new DomainSet())
18 , mSuperWhitelist(new DomainSet())
21 DomainPolicy::~DomainPolicy()
23 // The SSM holds a strong ref to the DomainPolicy until Deactivate() is
24 // invoked, so we should never hit the destructor until that happens.
25 MOZ_ASSERT(!mBlacklist
&& !mSuperBlacklist
&&
26 !mWhitelist
&& !mSuperWhitelist
);
31 DomainPolicy::GetBlacklist(nsIDomainSet
** aSet
)
33 nsCOMPtr
<nsIDomainSet
> set
= mBlacklist
;
39 DomainPolicy::GetSuperBlacklist(nsIDomainSet
** aSet
)
41 nsCOMPtr
<nsIDomainSet
> set
= mSuperBlacklist
;
47 DomainPolicy::GetWhitelist(nsIDomainSet
** aSet
)
49 nsCOMPtr
<nsIDomainSet
> set
= mWhitelist
;
55 DomainPolicy::GetSuperWhitelist(nsIDomainSet
** aSet
)
57 nsCOMPtr
<nsIDomainSet
> set
= mSuperWhitelist
;
63 DomainPolicy::Deactivate()
65 // Clear the hashtables first to free up memory, since script might
66 // hold the doomed sets alive indefinitely.
68 mSuperBlacklist
->Clear();
70 mSuperWhitelist
->Clear();
74 mSuperBlacklist
= nullptr;
76 mSuperWhitelist
= nullptr;
79 nsScriptSecurityManager::GetScriptSecurityManager()->DeactivateDomainPolicy();
83 static already_AddRefed
<nsIURI
>
84 GetCanonicalClone(nsIURI
* aURI
)
86 nsCOMPtr
<nsIURI
> clone
;
87 nsresult rv
= aURI
->Clone(getter_AddRefs(clone
));
88 NS_ENSURE_SUCCESS(rv
, nullptr);
89 rv
= clone
->SetUserPass(EmptyCString());
90 NS_ENSURE_SUCCESS(rv
, nullptr);
91 rv
= clone
->SetPath(EmptyCString());
92 NS_ENSURE_SUCCESS(rv
, nullptr);
93 return clone
.forget();
96 NS_IMPL_ISUPPORTS1(DomainSet
, nsIDomainSet
)
99 DomainSet::Add(nsIURI
* aDomain
)
101 nsCOMPtr
<nsIURI
> clone
= GetCanonicalClone(aDomain
);
102 NS_ENSURE_TRUE(clone
, NS_ERROR_FAILURE
);
103 mHashTable
.PutEntry(clone
);
108 DomainSet::Remove(nsIURI
* aDomain
)
110 nsCOMPtr
<nsIURI
> clone
= GetCanonicalClone(aDomain
);
111 NS_ENSURE_TRUE(clone
, NS_ERROR_FAILURE
);
112 mHashTable
.RemoveEntry(clone
);
124 DomainSet::Contains(nsIURI
* aDomain
, bool* aContains
)
127 nsCOMPtr
<nsIURI
> clone
= GetCanonicalClone(aDomain
);
128 NS_ENSURE_TRUE(clone
, NS_ERROR_FAILURE
);
129 *aContains
= mHashTable
.Contains(clone
);
134 DomainSet::ContainsSuperDomain(nsIURI
* aDomain
, bool* aContains
)
137 nsCOMPtr
<nsIURI
> clone
= GetCanonicalClone(aDomain
);
138 NS_ENSURE_TRUE(clone
, NS_ERROR_FAILURE
);
139 nsAutoCString domain
;
140 nsresult rv
= clone
->GetHost(domain
);
141 NS_ENSURE_SUCCESS(rv
, rv
);
143 // Check the current domain.
144 if (mHashTable
.Contains(clone
)) {
149 // Chop off everything before the first dot, or break if there are no
151 int32_t index
= domain
.Find(".");
152 if (index
== kNotFound
)
154 domain
.Assign(Substring(domain
, index
+ 1));
155 rv
= clone
->SetHost(domain
);
156 NS_ENSURE_SUCCESS(rv
, rv
);
164 } /* namespace hotness */
165 } /* namespace mozilla */