Bug 1834274 - Part 1: Implement nsIRFPService.idl for nsRFPService. r=emilio

To allow remote settings client to set fingerprinting overrides from
Javascript, we need to implement nsIRFPService for nsRFPService.

This patch changes the return format of nsRFPService::GetOrCreate() to
match the singleton implementation of nsIRFPService. As a result of it,
we need to change the first init call in nsContentUtils because we
cannot forward a already_AddRefed<> to mozilla::Unused. It will cause a
memory leak.

Differential Revision: https://phabricator.services.mozilla.com/D182809
This commit is contained in:
Tim Huang 2023-09-26 14:07:47 +00:00
parent 9f496d89f4
commit d68f687593
7 changed files with 53 additions and 6 deletions

View File

@ -825,7 +825,8 @@ nsresult nsContentUtils::Init() {
Element::InitCCCallbacks(); Element::InitCCCallbacks();
Unused << nsRFPService::GetOrCreate(); RefPtr<nsRFPService> rfpService = nsRFPService::GetOrCreate();
MOZ_ASSERT(rfpService);
if (XRE_IsParentProcess()) { if (XRE_IsParentProcess()) {
AsyncPrecreateStringBundles(); AsyncPrecreateStringBundles();

View File

@ -0,0 +1,20 @@
# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# 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/.
Classes = [
{
'name': 'RFPService',
'cid': '{20093b2e-d5d5-4ce0-8355-96b8d2dc7ff5}',
'interfaces': ['nsIRFPService'],
'contract_ids': ['@mozilla.org/rfp-service;1'],
'type': 'mozilla::nsRFPService',
'headers': ['/toolkit/components/resistfingerprinting/nsRFPService.h'],
'singleton': True,
'constructor': 'mozilla::nsRFPService::GetOrCreate',
'js_name': 'rfp',
'processes': ProcessSelector.MAIN_PROCESS_ONLY,
},
]

View File

@ -26,3 +26,13 @@ EXPORTS.mozilla += [
EXTRA_JS_MODULES += [ EXTRA_JS_MODULES += [
"RFPHelper.sys.mjs", "RFPHelper.sys.mjs",
] ]
XPIDL_MODULE = "toolkit_resistfingerprinting"
XPCOM_MANIFESTS += [
"components.conf",
]
XPIDL_SOURCES += [
"nsIRFPService.idl",
]

View File

@ -0,0 +1,13 @@
/* 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 "nsISupports.idl"
/**
* The singleton serivce which handles fingerprinting protection stuffs.
*/
[scriptable, builtinclass, uuid(20093b2e-d5d5-4ce0-8355-96b8d2dc7ff5)]
interface nsIRFPService : nsISupports
{
};

View File

@ -112,7 +112,7 @@ const RFPTarget kDefaultFingerintingProtections =
// ============================================================================ // ============================================================================
// Structural Stuff & Pref Observing // Structural Stuff & Pref Observing
NS_IMPL_ISUPPORTS(nsRFPService, nsIObserver) NS_IMPL_ISUPPORTS(nsRFPService, nsIObserver, nsIRFPService)
static StaticRefPtr<nsRFPService> sRFPService; static StaticRefPtr<nsRFPService> sRFPService;
static bool sInitialized = false; static bool sInitialized = false;
@ -121,7 +121,7 @@ static bool sInitialized = false;
static Atomic<RFPTarget> sEnabledFingerintingProtections; static Atomic<RFPTarget> sEnabledFingerintingProtections;
/* static */ /* static */
nsRFPService* nsRFPService::GetOrCreate() { already_AddRefed<nsRFPService> nsRFPService::GetOrCreate() {
if (!sInitialized) { if (!sInitialized) {
sRFPService = new nsRFPService(); sRFPService = new nsRFPService();
nsresult rv = sRFPService->Init(); nsresult rv = sRFPService->Init();
@ -135,7 +135,7 @@ nsRFPService* nsRFPService::GetOrCreate() {
sInitialized = true; sInitialized = true;
} }
return sRFPService; return do_AddRef(sRFPService);
} }
static const char* gCallbackPrefs[] = { static const char* gCallbackPrefs[] = {

View File

@ -16,6 +16,7 @@
#include "nsICookieJarSettings.h" #include "nsICookieJarSettings.h"
#include "nsIObserver.h" #include "nsIObserver.h"
#include "nsISupports.h" #include "nsISupports.h"
#include "nsIRFPService.h"
#include "nsStringFwd.h" #include "nsStringFwd.h"
// Defines regarding spoofed values of Navigator object. These spoofed values // Defines regarding spoofed values of Navigator object. These spoofed values
@ -157,12 +158,13 @@ MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(RFPTarget);
// ============================================================================ // ============================================================================
class nsRFPService final : public nsIObserver { class nsRFPService final : public nsIObserver, public nsIRFPService {
public: public:
NS_DECL_ISUPPORTS NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER NS_DECL_NSIOBSERVER
NS_DECL_NSIRFPSERVICE
static nsRFPService* GetOrCreate(); static already_AddRefed<nsRFPService> GetOrCreate();
// _Rarely_ you will need to know if RFP is enabled, or if FPP is enabled. // _Rarely_ you will need to know if RFP is enabled, or if FPP is enabled.
// 98% of the time you should use nsContentUtils::ShouldResistFingerprinting // 98% of the time you should use nsContentUtils::ShouldResistFingerprinting

View File

@ -41,6 +41,7 @@
"nsIProperties": "dirsvc", "nsIProperties": "dirsvc",
"nsIPropertyBag2": "sysinfo", "nsIPropertyBag2": "sysinfo",
"nsIQuotaManagerService": "qms", "nsIQuotaManagerService": "qms",
"nsIRFPService": "rfp",
"nsIScriptSecurityManager": "scriptSecurityManager", "nsIScriptSecurityManager": "scriptSecurityManager",
"nsISearchService": "search", "nsISearchService": "search",
"nsISessionStorageService": "sessionStorage", "nsISessionStorageService": "sessionStorage",