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"
12 NS_IMPL_ISUPPORTS(DomainPolicy
, nsIDomainPolicy
)
14 DomainPolicy::DomainPolicy() : mBlacklist(new DomainSet())
15 , mSuperBlacklist(new DomainSet())
16 , mWhitelist(new DomainSet())
17 , mSuperWhitelist(new DomainSet())
20 DomainPolicy::~DomainPolicy()
22 // The SSM holds a strong ref to the DomainPolicy until Deactivate() is
23 // invoked, so we should never hit the destructor until that happens.
24 MOZ_ASSERT(!mBlacklist
&& !mSuperBlacklist
&&
25 !mWhitelist
&& !mSuperWhitelist
);
30 DomainPolicy::GetBlacklist(nsIDomainSet
** aSet
)
32 nsCOMPtr
<nsIDomainSet
> set
= mBlacklist
;
38 DomainPolicy::GetSuperBlacklist(nsIDomainSet
** aSet
)
40 nsCOMPtr
<nsIDomainSet
> set
= mSuperBlacklist
;
46 DomainPolicy::GetWhitelist(nsIDomainSet
** aSet
)
48 nsCOMPtr
<nsIDomainSet
> set
= mWhitelist
;
54 DomainPolicy::GetSuperWhitelist(nsIDomainSet
** aSet
)
56 nsCOMPtr
<nsIDomainSet
> set
= mSuperWhitelist
;
62 DomainPolicy::Deactivate()
64 // Clear the hashtables first to free up memory, since script might
65 // hold the doomed sets alive indefinitely.
67 mSuperBlacklist
->Clear();
69 mSuperWhitelist
->Clear();
73 mSuperBlacklist
= nullptr;
75 mSuperWhitelist
= nullptr;
78 nsScriptSecurityManager::GetScriptSecurityManager()->DeactivateDomainPolicy();
82 static already_AddRefed
<nsIURI
>
83 GetCanonicalClone(nsIURI
* aURI
)
85 nsCOMPtr
<nsIURI
> clone
;
86 nsresult rv
= aURI
->Clone(getter_AddRefs(clone
));
87 NS_ENSURE_SUCCESS(rv
, nullptr);
88 rv
= clone
->SetUserPass(EmptyCString());
89 NS_ENSURE_SUCCESS(rv
, nullptr);
90 rv
= clone
->SetPath(EmptyCString());
91 NS_ENSURE_SUCCESS(rv
, nullptr);
92 return clone
.forget();
95 NS_IMPL_ISUPPORTS(DomainSet
, nsIDomainSet
)
98 DomainSet::Add(nsIURI
* aDomain
)
100 nsCOMPtr
<nsIURI
> clone
= GetCanonicalClone(aDomain
);
101 NS_ENSURE_TRUE(clone
, NS_ERROR_FAILURE
);
102 mHashTable
.PutEntry(clone
);
107 DomainSet::Remove(nsIURI
* aDomain
)
109 nsCOMPtr
<nsIURI
> clone
= GetCanonicalClone(aDomain
);
110 NS_ENSURE_TRUE(clone
, NS_ERROR_FAILURE
);
111 mHashTable
.RemoveEntry(clone
);
123 DomainSet::Contains(nsIURI
* aDomain
, bool* aContains
)
126 nsCOMPtr
<nsIURI
> clone
= GetCanonicalClone(aDomain
);
127 NS_ENSURE_TRUE(clone
, NS_ERROR_FAILURE
);
128 *aContains
= mHashTable
.Contains(clone
);
133 DomainSet::ContainsSuperDomain(nsIURI
* aDomain
, bool* aContains
)
136 nsCOMPtr
<nsIURI
> clone
= GetCanonicalClone(aDomain
);
137 NS_ENSURE_TRUE(clone
, NS_ERROR_FAILURE
);
138 nsAutoCString domain
;
139 nsresult rv
= clone
->GetHost(domain
);
140 NS_ENSURE_SUCCESS(rv
, rv
);
142 // Check the current domain.
143 if (mHashTable
.Contains(clone
)) {
148 // Chop off everything before the first dot, or break if there are no
150 int32_t index
= domain
.Find(".");
151 if (index
== kNotFound
)
153 domain
.Assign(Substring(domain
, index
+ 1));
154 rv
= clone
->SetHost(domain
);
155 NS_ENSURE_SUCCESS(rv
, rv
);
163 } /* namespace mozilla */