Bumping manifests a=b2g-bump
[gecko.git] / caps / DomainPolicy.cpp
blobec0230cbe3b93740fd91d02e9edbe6067a2ca0d1
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"
10 namespace mozilla {
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);
29 NS_IMETHODIMP
30 DomainPolicy::GetBlacklist(nsIDomainSet** aSet)
32 nsCOMPtr<nsIDomainSet> set = mBlacklist;
33 set.forget(aSet);
34 return NS_OK;
37 NS_IMETHODIMP
38 DomainPolicy::GetSuperBlacklist(nsIDomainSet** aSet)
40 nsCOMPtr<nsIDomainSet> set = mSuperBlacklist;
41 set.forget(aSet);
42 return NS_OK;
45 NS_IMETHODIMP
46 DomainPolicy::GetWhitelist(nsIDomainSet** aSet)
48 nsCOMPtr<nsIDomainSet> set = mWhitelist;
49 set.forget(aSet);
50 return NS_OK;
53 NS_IMETHODIMP
54 DomainPolicy::GetSuperWhitelist(nsIDomainSet** aSet)
56 nsCOMPtr<nsIDomainSet> set = mSuperWhitelist;
57 set.forget(aSet);
58 return NS_OK;
61 NS_IMETHODIMP
62 DomainPolicy::Deactivate()
64 // Clear the hashtables first to free up memory, since script might
65 // hold the doomed sets alive indefinitely.
66 mBlacklist->Clear();
67 mSuperBlacklist->Clear();
68 mWhitelist->Clear();
69 mSuperWhitelist->Clear();
71 // Null them out.
72 mBlacklist = nullptr;
73 mSuperBlacklist = nullptr;
74 mWhitelist = nullptr;
75 mSuperWhitelist = nullptr;
77 // Inform the SSM.
78 nsScriptSecurityManager::GetScriptSecurityManager()->DeactivateDomainPolicy();
79 return NS_OK;
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)
97 NS_IMETHODIMP
98 DomainSet::Add(nsIURI* aDomain)
100 nsCOMPtr<nsIURI> clone = GetCanonicalClone(aDomain);
101 NS_ENSURE_TRUE(clone, NS_ERROR_FAILURE);
102 mHashTable.PutEntry(clone);
103 return NS_OK;
106 NS_IMETHODIMP
107 DomainSet::Remove(nsIURI* aDomain)
109 nsCOMPtr<nsIURI> clone = GetCanonicalClone(aDomain);
110 NS_ENSURE_TRUE(clone, NS_ERROR_FAILURE);
111 mHashTable.RemoveEntry(clone);
112 return NS_OK;
115 NS_IMETHODIMP
116 DomainSet::Clear()
118 mHashTable.Clear();
119 return NS_OK;
122 NS_IMETHODIMP
123 DomainSet::Contains(nsIURI* aDomain, bool* aContains)
125 *aContains = false;
126 nsCOMPtr<nsIURI> clone = GetCanonicalClone(aDomain);
127 NS_ENSURE_TRUE(clone, NS_ERROR_FAILURE);
128 *aContains = mHashTable.Contains(clone);
129 return NS_OK;
132 NS_IMETHODIMP
133 DomainSet::ContainsSuperDomain(nsIURI* aDomain, bool* aContains)
135 *aContains = false;
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);
141 while (true) {
142 // Check the current domain.
143 if (mHashTable.Contains(clone)) {
144 *aContains = true;
145 return NS_OK;
148 // Chop off everything before the first dot, or break if there are no
149 // dots left.
150 int32_t index = domain.Find(".");
151 if (index == kNotFound)
152 break;
153 domain.Assign(Substring(domain, index + 1));
154 rv = clone->SetHost(domain);
155 NS_ENSURE_SUCCESS(rv, rv);
158 // No match.
159 return NS_OK;
163 } /* namespace mozilla */