Bug 1330890 - Part 2: Add nsRFPService.cpp, which is a service of fingerprinting resistance. r?ehsan draft
authorTim Huang <tihuang@mozilla.com>
Wed, 29 Mar 2017 17:52:00 +0800
changeset 557812 cd9e9a1307f2bb7a79e62cc5c3c83342d6f67ca8
parent 557811 52c684b8e8432fefac5fc20924e62a5dd67b0e70
child 557813 c5462872893bff8fa452a702a577aa880454f971
push id52842
push userbmo:tihuang@mozilla.com
push dateFri, 07 Apr 2017 10:14:09 +0000
reviewersehsan
bugs1330890
milestone55.0a1
Bug 1330890 - Part 2: Add nsRFPService.cpp, which is a service of fingerprinting resistance. r?ehsan This patch adds a new service for fingerprinting resistance, which is called nsRFPService. This service will be put in /toolkit/components/resistfingerprinting. This service is responsible for observing the change of pref 'privacy.resistfingerprinting' and doing underlying jobs. This service changes environment value 'TZ' and calls nsJSUtils::ResetTimeZone() in response to the change of the pref. This service will be started and initiated during the first creation of nsGlobalWindow. This service is only a nsIObserver for now. In the future, however, it will be responsible for more fingerprinting resistance jobs, like changing prefs after 'privacy.resistfingerprinting' is changed. MozReview-Commit-ID: Gc0A2OqwRzU
dom/base/nsGlobalWindow.cpp
toolkit/components/moz.build
toolkit/components/resistfingerprinting/moz.build
toolkit/components/resistfingerprinting/nsRFPService.cpp
toolkit/components/resistfingerprinting/nsRFPService.h
--- a/dom/base/nsGlobalWindow.cpp
+++ b/dom/base/nsGlobalWindow.cpp
@@ -86,16 +86,17 @@
 #include "mozilla/ProcessHangMonitor.h"
 #include "mozilla/ThrottledEventQueue.h"
 #include "AudioChannelService.h"
 #include "nsAboutProtocolUtils.h"
 #include "nsCharTraits.h" // NS_IS_HIGH/LOW_SURROGATE
 #include "PostMessageEvent.h"
 #include "mozilla/dom/DocGroup.h"
 #include "mozilla/dom/TabGroup.h"
+#include "nsRFPService.h"
 
 // Interfaces Needed
 #include "nsIFrame.h"
 #include "nsCanvasFrame.h"
 #include "nsIWidget.h"
 #include "nsIWidgetListener.h"
 #include "nsIBaseWindow.h"
 #include "nsIDeviceSensors.h"
@@ -1618,16 +1619,18 @@ nsGlobalWindow::nsGlobalWindow(nsGlobalW
     TimeoutManager::Initialize();
     Preferences::AddBoolVarCache(&sIdleObserversAPIFuzzTimeDisabled,
                                  "dom.idle-observers-api.fuzz_time.disabled",
                                  false);
 
     Preferences::AddUintVarCache(&gThrottledIdlePeriodLength,
                                  "dom.idle_period.throttled_length",
                                  DEFAULT_THROTTLED_IDLE_PERIOD_LENGTH);
+
+    nsRFPService::GetOrCreate();
     sFirstTime = false;
   }
 
   if (gDumpFile == nullptr) {
     const nsAdoptingCString& fname =
       Preferences::GetCString("browser.dom.window.dump.file");
     if (!fname.IsEmpty()) {
       // if this fails to open, Dump() knows to just go to stdout
--- a/toolkit/components/moz.build
+++ b/toolkit/components/moz.build
@@ -44,16 +44,17 @@ DIRS += [
     'privatebrowsing',
     'processsingleton',
     'promiseworker',
     'prompts',
     'protobuf',
     'reader',
     'remotebrowserutils',
     'reflect',
+    'resistfingerprinting',
     'securityreporter',
     'startup',
     'statusfilter',
     'telemetry',
     'thumbnails',
     'timermanager',
     'tooltiptext',
     'typeaheadfind',
new file mode 100644
--- /dev/null
+++ b/toolkit/components/resistfingerprinting/moz.build
@@ -0,0 +1,15 @@
+# -*- 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/.
+
+UNIFIED_SOURCES += [
+    'nsRFPService.cpp',
+]
+
+FINAL_LIBRARY = 'xul'
+
+EXPORTS += [
+    'nsRFPService.h',
+]
new file mode 100644
--- /dev/null
+++ b/toolkit/components/resistfingerprinting/nsRFPService.cpp
@@ -0,0 +1,74 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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 "nsRFPService.h"
+
+#include "mozilla/ClearOnShutdown.h"
+#include "mozilla/Preferences.h"
+#include "mozilla/StaticPtr.h"
+
+#include "nsCOMPtr.h"
+#include "nsServiceManagerUtils.h"
+#include "nsString.h"
+
+#include "nsIPrefBranch.h"
+#include "nsIPrefService.h"
+#include "nsJSUtils.h"
+
+#include "prenv.h"
+
+using namespace mozilla;
+
+#define RESIST_FINGERPRINTING_PREF "privacy.resistFingerprinting"
+
+NS_IMPL_ISUPPORTS(nsRFPService, nsIObserver)
+
+static StaticRefPtr<nsRFPService> gRFPService;
+
+/* static */
+nsRFPService*
+nsRFPService::GetOrCreate()
+{
+  if (!gRFPService) {
+      gRFPService = new nsRFPService();
+      gRFPService->Init();
+      ClearOnShutdown(&gRFPService);
+  }
+
+  return gRFPService;
+}
+
+nsresult
+nsRFPService::Init()
+{
+  nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
+
+  prefs->AddObserver(RESIST_FINGERPRINTING_PREF, this, false);
+
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsRFPService::Observe(nsISupports* aObject, const char* aTopic,
+                      const char16_t* aMessage)
+{
+  if (!strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic)) {
+    NS_ConvertUTF16toUTF8 pref(aMessage);
+
+    if (pref.EqualsLiteral(RESIST_FINGERPRINTING_PREF)) {
+      bool isRPFEnabled = Preferences::GetBool(RESIST_FINGERPRINTING_PREF);
+
+      if (isRPFEnabled) {
+        PR_SetEnv("TZ=UTC");
+      } else {
+        unsetenv("TZ");
+      }
+
+      nsJSUtils::ResetTimeZone();
+    }
+  }
+
+  return NS_OK;
+}
new file mode 100644
--- /dev/null
+++ b/toolkit/components/resistfingerprinting/nsRFPService.h
@@ -0,0 +1,31 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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/. */
+
+#ifndef __nsRFPService_h__
+#define __nsRFPService_h__
+
+#include "nsIObserver.h"
+
+namespace mozilla {
+
+class nsRFPService final : public nsIObserver
+{
+public:
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIOBSERVER
+
+  static nsRFPService* GetOrCreate();
+
+private:
+  nsresult Init();
+
+  nsRFPService() {}
+
+  ~nsRFPService() {}
+};
+
+} // mozilla namespace
+
+#endif /* __nsRFPService_h__ */