1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "BounceTrackingStateGlobal.h"
#include "BounceTrackingProtectionStorage.h"
#include "ErrorList.h"
#include "mozilla/Assertions.h"
#include "mozilla/Logging.h"
#include "mozilla/IntegerPrintfMacros.h"
#include "nsIBounceTrackingProtection.h"
#include "nsIPrincipal.h"
namespace mozilla {
NS_IMPL_CYCLE_COLLECTION(BounceTrackingStateGlobal);
extern LazyLogModule gBounceTrackingProtectionLog;
BounceTrackingStateGlobal::BounceTrackingStateGlobal(
BounceTrackingProtectionStorage* aStorage, const OriginAttributes& aAttrs)
: mStorage(aStorage), mOriginAttributes(aAttrs) {
MOZ_ASSERT(aStorage);
}
bool BounceTrackingStateGlobal::HasUserActivation(
const nsACString& aSiteHost) const {
return mUserActivation.Contains(aSiteHost);
}
nsresult BounceTrackingStateGlobal::RecordUserActivation(
const nsACString& aSiteHost, PRTime aTime, bool aSkipStorage) {
NS_ENSURE_TRUE(aSiteHost.Length(), NS_ERROR_INVALID_ARG);
NS_ENSURE_TRUE(aTime > 0, NS_ERROR_INVALID_ARG);
// A site must only be in one of the maps at a time.
bool hasRemoved = mBounceTrackers.Remove(aSiteHost);
if (hasRemoved) {
MOZ_LOG(gBounceTrackingProtectionLog, LogLevel::Debug,
("%s: Removed bounce tracking candidate due to user activation: %s",
__FUNCTION__, PromiseFlatCString(aSiteHost).get()));
}
// Make sure we don't overwrite an existing, more recent user activation. This
// is only relevant for callers that pass in a timestamp that isn't PR_Now(),
// e.g. when importing user activation data.
Maybe<PRTime> existingUserActivation = mUserActivation.MaybeGet(aSiteHost);
if (existingUserActivation.isSome() &&
existingUserActivation.value() >= aTime) {
MOZ_LOG(gBounceTrackingProtectionLog, LogLevel::Debug,
("%s: Skip: A more recent user activation "
"already exists for %s",
__FUNCTION__, PromiseFlatCString(aSiteHost).get()));
return NS_OK;
}
mUserActivation.InsertOrUpdate(aSiteHost, aTime);
if (aSkipStorage || !ShouldPersistToDisk()) {
return NS_OK;
}
// Write the change to storage.
NS_ENSURE_TRUE(mStorage, NS_ERROR_FAILURE);
return mStorage->UpdateDBEntry(
mOriginAttributes, aSiteHost,
BounceTrackingProtectionStorage::EntryType::UserActivation, aTime);
}
nsresult BounceTrackingStateGlobal::TestRemoveUserActivation(
const nsACString& aSiteHost) {
bool hasRemoved = mUserActivation.Remove(aSiteHost);
// Avoid potentially removing a bounce tracking entry if there is no user
// activation entry.
if (!hasRemoved) {
return NS_OK;
}
if (!ShouldPersistToDisk()) {
return NS_OK;
}
// Write the change to storage.
NS_ENSURE_TRUE(mStorage, NS_ERROR_FAILURE);
return mStorage->DeleteDBEntries(&mOriginAttributes, aSiteHost);
}
nsresult BounceTrackingStateGlobal::ClearUserActivationBefore(PRTime aTime) {
return ClearByTimeRange(
0, Some(aTime),
Some(BounceTrackingProtectionStorage::EntryType::UserActivation));
}
nsresult BounceTrackingStateGlobal::ClearSiteHost(const nsACString& aSiteHost,
bool aSkipStorage) {
NS_ENSURE_TRUE(aSiteHost.Length(), NS_ERROR_INVALID_ARG);
bool removedUserActivation = mUserActivation.Remove(aSiteHost);
bool removedBounceTracker = mBounceTrackers.Remove(aSiteHost);
if (removedUserActivation || removedBounceTracker) {
MOZ_ASSERT(removedUserActivation != removedBounceTracker,
"A site must only be in one of the maps at a time.");
}
mRecentPurges.Remove(aSiteHost);
if (aSkipStorage || !ShouldPersistToDisk()) {
return NS_OK;
}
NS_ENSURE_TRUE(mStorage, NS_ERROR_FAILURE);
return mStorage->DeleteDBEntries(&mOriginAttributes, aSiteHost);
}
nsresult BounceTrackingStateGlobal::ClearByTimeRange(
PRTime aFrom, Maybe<PRTime> aTo,
Maybe<BounceTrackingProtectionStorage::EntryType> aEntryType,
bool aSkipStorage) {
NS_ENSURE_ARG_MIN(aFrom, 0);
NS_ENSURE_TRUE(!aTo || aTo.value() > aFrom, NS_ERROR_INVALID_ARG);
MOZ_LOG(gBounceTrackingProtectionLog, LogLevel::Debug,
("%s: Clearing user activations by time range from %" PRIu64
" to %" PRIu64 " %s",
__FUNCTION__, aFrom, aTo.valueOr(0), Describe().get()));
// Clear in memory user activation data.
if (aEntryType.isNothing() ||
aEntryType.value() ==
BounceTrackingProtectionStorage::EntryType::UserActivation) {
for (auto iter = mUserActivation.Iter(); !iter.Done(); iter.Next()) {
if (iter.Data() >= aFrom &&
(aTo.isNothing() || iter.Data() <= aTo.value())) {
MOZ_LOG(gBounceTrackingProtectionLog, LogLevel::Debug,
("%s: Remove user activation for %s", __FUNCTION__,
PromiseFlatCString(iter.Key()).get()));
iter.Remove();
}
}
}
// Clear in memory bounce tracker data.
if (aEntryType.isNothing() ||
aEntryType.value() ==
BounceTrackingProtectionStorage::EntryType::BounceTracker) {
for (auto iter = mBounceTrackers.Iter(); !iter.Done(); iter.Next()) {
if (iter.Data() >= aFrom &&
(aTo.isNothing() || iter.Data() <= aTo.value())) {
MOZ_LOG(gBounceTrackingProtectionLog, LogLevel::Debug,
("%s: Remove bouncer tracker for %s", __FUNCTION__,
PromiseFlatCString(iter.Key()).get()));
iter.Remove();
}
}
}
// Clear purge tracker log if there is no type filter. Used for
// ClearDataService.
if (aEntryType.isNothing()) {
for (auto iter = mRecentPurges.Iter(); !iter.Done(); iter.Next()) {
auto& purgeArray = iter.Data();
// Use RemoveElementsBy to safely remove elements from nsTArray while
// iterating.
purgeArray.RemoveElementsBy([&](const auto& entry) {
const PRTime& purgeTime = entry->PurgeTimeRefConst();
bool shouldRemove =
purgeTime >= aFrom && (aTo.isNothing() || purgeTime <= aTo.value());
if (shouldRemove) {
MOZ_LOG(gBounceTrackingProtectionLog, LogLevel::Debug,
("%s: Remove purge log entry for site %s", __FUNCTION__,
PromiseFlatCString(iter.Key()).get()));
}
return shouldRemove;
});
// If the array is now empty, remove the entire entry from mRecentPurges
if (purgeArray.IsEmpty()) {
iter.Remove();
}
}
}
if (aSkipStorage || !ShouldPersistToDisk()) {
return NS_OK;
}
// Write the change to storage.
NS_ENSURE_TRUE(mStorage, NS_ERROR_FAILURE);
return mStorage->DeleteDBEntriesInTimeRange(&mOriginAttributes, aFrom, aTo,
aEntryType);
}
bool BounceTrackingStateGlobal::HasBounceTracker(
const nsACString& aSiteHost) const {
return mBounceTrackers.Contains(aSiteHost);
}
nsresult BounceTrackingStateGlobal::RecordBounceTracker(
const nsACString& aSiteHost, PRTime aTime, bool aSkipStorage) {
NS_ENSURE_TRUE(aSiteHost.Length(), NS_ERROR_INVALID_ARG);
NS_ENSURE_TRUE(aTime > 0, NS_ERROR_INVALID_ARG);
// Can not record a bounce tracker if the site has a user activation.
NS_ENSURE_TRUE(!mUserActivation.Contains(aSiteHost), NS_ERROR_FAILURE);
mBounceTrackers.InsertOrUpdate(aSiteHost, aTime);
if (aSkipStorage || !ShouldPersistToDisk()) {
return NS_OK;
}
// Write the change to storage.
NS_ENSURE_TRUE(mStorage, NS_ERROR_FAILURE);
return mStorage->UpdateDBEntry(
mOriginAttributes, aSiteHost,
BounceTrackingProtectionStorage::EntryType::BounceTracker, aTime);
}
nsresult BounceTrackingStateGlobal::RecordPurgedTracker(
const RefPtr<BounceTrackingPurgeEntry>& aEntry) {
MOZ_ASSERT(StaticPrefs::privacy_bounceTrackingProtection_mode() ==
nsIBounceTrackingProtection::MODE_ENABLED,
"Should only record purged trackers when the feature is enabled.");
NS_ENSURE_ARG_POINTER(aEntry);
// Ensure that entries unrelated to this state global can not be added.
bool entryOAMatchesStateGlobalOA =
aEntry->OriginAttributesRef() == mOriginAttributes;
MOZ_ASSERT(entryOAMatchesStateGlobalOA);
NS_ENSURE_TRUE(entryOAMatchesStateGlobalOA, NS_ERROR_INVALID_ARG);
nsTArray<RefPtr<BounceTrackingPurgeEntry>>& entriesForSite =
mRecentPurges.LookupOrInsert(aEntry->SiteHostRef());
entriesForSite.AppendElement(aEntry);
return NS_OK;
}
nsresult BounceTrackingStateGlobal::RemoveBounceTrackers(
const nsTArray<nsCString>& aSiteHosts) {
for (const nsCString& siteHost : aSiteHosts) {
mBounceTrackers.Remove(siteHost);
// TODO: Create a bulk delete query.
if (ShouldPersistToDisk()) {
NS_ENSURE_TRUE(mStorage, NS_ERROR_FAILURE);
nsresult rv = mStorage->DeleteDBEntries(&mOriginAttributes, siteHost);
NS_ENSURE_SUCCESS(rv, rv);
}
}
return NS_OK;
}
nsresult BounceTrackingStateGlobal::ClearByType(
BounceTrackingProtectionStorage::EntryType aType, bool aSkipStorage) {
if (aType == BounceTrackingProtectionStorage::EntryType::BounceTracker) {
mBounceTrackers.Clear();
} else {
MOZ_ASSERT(aType ==
BounceTrackingProtectionStorage::EntryType::UserActivation);
mUserActivation.Clear();
}
if (aSkipStorage || !ShouldPersistToDisk()) {
return NS_OK;
}
NS_ENSURE_TRUE(mStorage, NS_ERROR_FAILURE);
return mStorage->DeleteDBEntriesByType(
&mOriginAttributes,
BounceTrackingProtectionStorage::EntryType::BounceTracker);
}
// static
nsCString BounceTrackingStateGlobal::DescribeMap(
const nsTHashMap<nsCStringHashKey, PRTime>& aMap) {
nsAutoCString mapStr;
for (auto iter = aMap.ConstIter(); !iter.Done(); iter.Next()) {
mapStr.Append(nsPrintfCString("{ %s: %" PRIu64 " }, ",
PromiseFlatCString(iter.Key()).get(),
iter.Data()));
}
return std::move(mapStr);
}
nsCString BounceTrackingStateGlobal::Describe() {
nsAutoCString originAttributeSuffix;
mOriginAttributes.CreateSuffix(originAttributeSuffix);
return nsPrintfCString(
"{ mOriginAttributes: %s, mUserActivation: %s, mBounceTrackers: %s }",
originAttributeSuffix.get(), DescribeMap(mUserActivation).get(),
DescribeMap(mBounceTrackers).get());
}
} // namespace mozilla
|