Bug 1871991 - Required arguments after optional are not supported r=jesup
[gecko.git] / toolkit / components / antitracking / ContentBlockingUserInteraction.cpp
blob452b4dd409eb82a05aef458a085131b671e611a1
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "AntiTrackingLog.h"
8 #include "ContentBlockingUserInteraction.h"
9 #include "AntiTrackingUtils.h"
11 #include "mozilla/BounceTrackingProtection.h"
12 #include "mozilla/dom/ContentChild.h"
13 #include "mozilla/PermissionManager.h"
14 #include "nsIPrincipal.h"
15 #include "nsXULAppAPI.h"
16 #include "prtime.h"
18 namespace mozilla {
20 /* static */
21 void ContentBlockingUserInteraction::Observe(nsIPrincipal* aPrincipal) {
22 if (!aPrincipal || aPrincipal->IsSystemPrincipal()) {
23 // The content process may have sent us garbage data.
24 return;
27 if (XRE_IsParentProcess()) {
28 LOG_PRIN(("Saving the userInteraction for %s", _spec), aPrincipal);
30 // The bounce tracking protection has its own interaction store.
31 RefPtr<BounceTrackingProtection> bounceTrackingProtection =
32 BounceTrackingProtection::GetSingleton();
33 // May be nullptr if the feature is disabled.
34 if (bounceTrackingProtection) {
35 nsresult rv = bounceTrackingProtection->RecordUserActivation(aPrincipal);
36 if (NS_WARN_IF(NS_FAILED(rv))) {
37 LOG(("BounceTrackingProtection::RecordUserActivation failed."));
41 PermissionManager* permManager = PermissionManager::GetInstance();
42 if (NS_WARN_IF(!permManager)) {
43 LOG(("Permission manager is null, bailing out early"));
44 return;
47 // Remember that this pref is stored in seconds!
48 uint32_t expirationType = nsIPermissionManager::EXPIRE_TIME;
49 uint32_t expirationTime =
50 StaticPrefs::privacy_userInteraction_expiration() * 1000;
51 int64_t when = (PR_Now() / PR_USEC_PER_MSEC) + expirationTime;
53 uint32_t privateBrowsingId = 0;
54 nsresult rv = aPrincipal->GetPrivateBrowsingId(&privateBrowsingId);
55 if (!NS_WARN_IF(NS_FAILED(rv)) && privateBrowsingId > 0) {
56 // If we are coming from a private window, make sure to store a
57 // session-only permission which won't get persisted to disk.
58 expirationType = nsIPermissionManager::EXPIRE_SESSION;
59 when = 0;
62 rv = permManager->AddFromPrincipal(aPrincipal, USER_INTERACTION_PERM,
63 nsIPermissionManager::ALLOW_ACTION,
64 expirationType, when);
65 Unused << NS_WARN_IF(NS_FAILED(rv));
67 if (StaticPrefs::privacy_antitracking_testing()) {
68 nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
69 obs->NotifyObservers(
70 nullptr, "antitracking-test-user-interaction-perm-added", nullptr);
72 return;
75 dom::ContentChild* cc = dom::ContentChild::GetSingleton();
76 MOZ_ASSERT(cc);
78 LOG_PRIN(("Asking the parent process to save the user-interaction for us: %s",
79 _spec),
80 aPrincipal);
81 cc->SendStoreUserInteractionAsPermission(aPrincipal);
84 /* static */
85 bool ContentBlockingUserInteraction::Exists(nsIPrincipal* aPrincipal) {
86 PermissionManager* permManager = PermissionManager::GetInstance();
87 if (NS_WARN_IF(!permManager)) {
88 return false;
91 uint32_t result = 0;
92 nsresult rv = permManager->TestPermissionWithoutDefaultsFromPrincipal(
93 aPrincipal, USER_INTERACTION_PERM, &result);
94 if (NS_WARN_IF(NS_FAILED(rv))) {
95 return false;
98 return result == nsIPermissionManager::ALLOW_ACTION;
101 } // namespace mozilla