Bug 1933479 - Add tab close button on hover to vertical tabs when sidebar is collapse...
[gecko.git] / toolkit / components / antitracking / ContentBlockingTelemetryService.cpp
blob3ea1e3f03d1055275e00b578958611b52031da85
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 "ContentBlockingTelemetryService.h"
9 #include "mozilla/ClearOnShutdown.h"
10 #include "mozilla/PermissionManager.h"
11 #include "mozilla/Services.h"
12 #include "mozilla/StaticPtr.h"
13 #include "mozilla/Telemetry.h"
15 #include "AntiTrackingLog.h"
16 #include "prtime.h"
18 #include "nsIObserverService.h"
19 #include "nsIPermission.h"
20 #include "nsTArray.h"
22 using namespace mozilla;
24 NS_IMPL_ISUPPORTS(ContentBlockingTelemetryService, nsIObserver)
26 static StaticRefPtr<ContentBlockingTelemetryService>
27 sContentBlockingTelemetryService;
29 /* static */
30 already_AddRefed<ContentBlockingTelemetryService>
31 ContentBlockingTelemetryService::GetSingleton() {
32 if (!sContentBlockingTelemetryService) {
33 sContentBlockingTelemetryService = new ContentBlockingTelemetryService();
34 ClearOnShutdown(&sContentBlockingTelemetryService);
37 RefPtr<ContentBlockingTelemetryService> service =
38 sContentBlockingTelemetryService;
40 return service.forget();
43 NS_IMETHODIMP
44 ContentBlockingTelemetryService::Observe(nsISupports* aSubject,
45 const char* aTopic,
46 const char16_t* aData) {
47 if (strcmp(aTopic, "idle-daily") == 0) {
48 ReportStoragePermissionExpire();
49 return NS_OK;
52 return NS_OK;
55 void ContentBlockingTelemetryService::ReportStoragePermissionExpire() {
56 MOZ_ASSERT(XRE_IsParentProcess());
58 LOG(("Start to report storage permission expire."));
60 PermissionManager* permManager = PermissionManager::GetInstance();
61 if (NS_WARN_IF(!permManager)) {
62 LOG(("Permission manager is null, bailing out early"));
63 return;
66 nsTArray<RefPtr<nsIPermission>> permissions;
67 nsresult rv =
68 permManager->GetAllWithTypePrefix("3rdPartyStorage"_ns, permissions);
70 if (NS_WARN_IF(NS_FAILED(rv))) {
71 LOG(("Fail to get all storage access permissions."));
72 return;
74 nsTArray<RefPtr<nsIPermission>> framePermissions;
75 rv = permManager->GetAllWithTypePrefix("3rdPartyFrameStorage"_ns,
76 framePermissions);
77 if (NS_WARN_IF(NS_FAILED(rv))) {
78 LOG(("Fail to get all frame storage access permissions."));
79 return;
81 if (!permissions.AppendElements(framePermissions, fallible)) {
82 LOG(("Fail to combine all storage access permissions."));
83 return;
86 nsTArray<uint32_t> records;
88 for (const auto& permission : permissions) {
89 if (!permission) {
90 LOG(("Couldn't get the permission for unknown reasons"));
91 continue;
94 uint32_t expireType;
95 rv = permission->GetExpireType(&expireType);
96 if (NS_WARN_IF(NS_FAILED(rv))) {
97 LOG(("Couldn't get the expire type."));
98 continue;
101 // We only care about permissions that have a EXPIRE_TIME as the expire
102 // type.
103 if (expireType != nsIPermissionManager::EXPIRE_TIME) {
104 continue;
107 // Collect how much longer the storage permission will be valid for, in
108 // days.
109 int64_t expirationTime = 0;
110 rv = permission->GetExpireTime(&expirationTime);
111 if (NS_WARN_IF(NS_FAILED(rv))) {
112 LOG(("Couldn't get the expire time."));
113 continue;
116 expirationTime -= (PR_Now() / PR_USEC_PER_MSEC);
118 // Skip expired permissions.
119 if (expirationTime <= 0) {
120 continue;
123 int64_t expireDays = expirationTime / 1000 / 60 / 60 / 24;
125 records.AppendElement(expireDays);
128 if (!records.IsEmpty()) {
129 Telemetry::Accumulate(Telemetry::STORAGE_ACCESS_REMAINING_DAYS, records);