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"
18 #include "nsIObserverService.h"
19 #include "nsIPermission.h"
22 using namespace mozilla
;
24 NS_IMPL_ISUPPORTS(ContentBlockingTelemetryService
, nsIObserver
)
26 static StaticRefPtr
<ContentBlockingTelemetryService
>
27 sContentBlockingTelemetryService
;
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();
44 ContentBlockingTelemetryService::Observe(nsISupports
* aSubject
,
46 const char16_t
* aData
) {
47 if (strcmp(aTopic
, "idle-daily") == 0) {
48 ReportStoragePermissionExpire();
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"));
66 nsTArray
<RefPtr
<nsIPermission
>> permissions
;
68 permManager
->GetAllWithTypePrefix("3rdPartyStorage"_ns
, permissions
);
70 if (NS_WARN_IF(NS_FAILED(rv
))) {
71 LOG(("Fail to get all storage access permissions."));
74 nsTArray
<RefPtr
<nsIPermission
>> framePermissions
;
75 rv
= permManager
->GetAllWithTypePrefix("3rdPartyFrameStorage"_ns
,
77 if (NS_WARN_IF(NS_FAILED(rv
))) {
78 LOG(("Fail to get all frame storage access permissions."));
81 if (!permissions
.AppendElements(framePermissions
, fallible
)) {
82 LOG(("Fail to combine all storage access permissions."));
86 nsTArray
<uint32_t> records
;
88 for (const auto& permission
: permissions
) {
90 LOG(("Couldn't get the permission for unknown reasons"));
95 rv
= permission
->GetExpireType(&expireType
);
96 if (NS_WARN_IF(NS_FAILED(rv
))) {
97 LOG(("Couldn't get the expire type."));
101 // We only care about permissions that have a EXPIRE_TIME as the expire
103 if (expireType
!= nsIPermissionManager::EXPIRE_TIME
) {
107 // Collect how much longer the storage permission will be valid for, in
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."));
116 expirationTime
-= (PR_Now() / PR_USEC_PER_MSEC
);
118 // Skip expired permissions.
119 if (expirationTime
<= 0) {
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
);