Bug 1357107 - Move the handling of the permissions.default.image pref to ImageBlocker.cpp. Original patch by ehsan. r=bzbarsky
authorpbz <pbz@mozilla.com>
Wed, 22 Jan 2020 10:45:15 +0000
changeset 511088 dea8bc3b320acdd689a27596a0a54eb794941333
parent 511087 74493854a1b6478d5a9c3674df1369788783eeee
child 511089 798234088fd904c05e9312a0f703980e284d3bf7
push id37045
push usercsabou@mozilla.com
push dateWed, 22 Jan 2020 21:48:55 +0000
treeherdermozilla-central@a80ee2878a79 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs1357107
milestone74.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1357107 - Move the handling of the permissions.default.image pref to ImageBlocker.cpp. Original patch by ehsan. r=bzbarsky Differential Revision: https://phabricator.services.mozilla.com/D59695
image/ImageBlocker.cpp
image/ImageBlocker.h
image/build/components.conf
image/imgLoader.cpp
image/moz.build
modules/libpref/init/StaticPrefList.yaml
modules/libpref/init/all.js
new file mode 100644
--- /dev/null
+++ b/image/ImageBlocker.cpp
@@ -0,0 +1,63 @@
+/* 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 "ImageBlocker.h"
+#include "nsIPermissionManager.h"
+#include "nsContentUtils.h"
+#include "mozilla/StaticPrefs_permissions.h"
+#include "nsNetUtil.h"
+
+using namespace mozilla;
+using namespace mozilla::image;
+
+NS_IMPL_ISUPPORTS(ImageBlocker, nsIContentPolicy)
+
+NS_IMETHODIMP
+ImageBlocker::ShouldLoad(nsIURI* aContentLocation, nsILoadInfo* aLoadInfo,
+                         const nsACString& aMimeGuess, int16_t* aShouldLoad) {
+  uint32_t contentType = aLoadInfo->GetExternalContentPolicyType();
+  MOZ_ASSERT(contentType == nsContentUtils::InternalContentPolicyTypeToExternal(
+                                contentType),
+             "We should only see external content policy types here.");
+
+  *aShouldLoad = nsIContentPolicy::ACCEPT;
+
+  // we only want to check http, https, ftp
+  // for chrome:// and resources and others, no need to check.
+  nsAutoCString scheme;
+  aContentLocation->GetScheme(scheme);
+  if (!scheme.LowerCaseEqualsLiteral("ftp") &&
+      !scheme.LowerCaseEqualsLiteral("http") &&
+      !scheme.LowerCaseEqualsLiteral("https")) {
+    return NS_OK;
+  }
+
+  // Block loading images depending on the permissions.default.image pref.
+  if ((contentType == nsIContentPolicy::TYPE_IMAGE ||
+       contentType == nsIContentPolicy::TYPE_IMAGESET) &&
+      StaticPrefs::permissions_default_image() ==
+          nsIPermissionManager::DENY_ACTION) {
+    NS_SetRequestBlockingReason(
+        aLoadInfo, nsILoadInfo::BLOCKING_REASON_CONTENT_POLICY_CONTENT_BLOCKED);
+    *aShouldLoad = nsIContentPolicy::REJECT_TYPE;
+  }
+
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+ImageBlocker::ShouldProcess(nsIURI* aContentLocation, nsILoadInfo* aLoadInfo,
+                            const nsACString& aMimeGuess,
+                            int16_t* aShouldProcess) {
+#ifdef DEBUG
+  uint32_t contentType = aLoadInfo->GetExternalContentPolicyType();
+  MOZ_ASSERT(contentType == nsContentUtils::InternalContentPolicyTypeToExternal(
+                                contentType),
+             "We should only see external content policy types here.");
+#endif
+
+  // We block images at load level already, so those should not end up here.
+  *aShouldProcess = nsIContentPolicy::ACCEPT;
+  return NS_OK;
+}
new file mode 100644
--- /dev/null
+++ b/image/ImageBlocker.h
@@ -0,0 +1,33 @@
+/* -*- 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 mozilla_image_ImageBlocker_h
+#define mozilla_image_ImageBlocker_h
+
+#include "nsIContentPolicy.h"
+
+namespace mozilla {
+namespace image {
+
+#define IMAGEBLOCKER_CONTRACTID "@mozilla.org/image-blocker-content-policy;1"
+#define IMAGEBLOCKER_CID                             \
+  { /* f6fcd651-164b-4416-b001-9c8c393fd93b */       \
+    0xf6fcd651, 0x164b, 0x4416, {                    \
+      0xb0, 0x01, 0x9c, 0x8c, 0x39, 0x3f, 0xd9, 0x3b \
+    }                                                \
+  }
+
+class ImageBlocker final : public nsIContentPolicy {
+  ~ImageBlocker() = default;
+
+ public:
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSICONTENTPOLICY
+};
+
+}  // namespace image
+}  // namespace mozilla
+
+#endif  // mozilla_image_ImageBlocker_h
--- a/image/build/components.conf
+++ b/image/build/components.conf
@@ -23,16 +23,25 @@ Classes = [
             '@mozilla.org/image/loader;1',
         ],
         'type': 'imgLoader',
         'headers': ['imgLoader.h'],
         'init_method': 'Init',
         'categories': {'content-sniffing-services': '@mozilla.org/image/loader;1'},
     },
     {
+        'cid': '{f6fcd651-164b-4416-b001-9c8c393fd93b}',
+        'contract_ids': [
+            '@mozilla.org/image-blocker-content-policy;1',
+        ],
+        'type': 'mozilla::image::ImageBlocker',
+        'headers': ['ImageBlocker.h'],
+        'categories': {'content-policy': '@mozilla.org/image-blocker-content-policy;1'},
+    },
+    {
         'cid': '{20557898-1dd2-11b2-8f65-9c462ee2bc95}',
         'contract_ids': ['@mozilla.org/image/request;1'],
         'type': 'imgRequestProxy',
         'headers': ['imgRequestProxy.h'],
     },
     {
         'cid': '{3d8fa16d-c9e1-4b50-bdef-2c7ae249967a}',
         'contract_ids': ['@mozilla.org/image/tools;1'],
--- a/image/imgLoader.cpp
+++ b/image/imgLoader.cpp
@@ -77,17 +77,17 @@ class imgMemoryReporter final : public n
  public:
   NS_DECL_ISUPPORTS
 
   NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
                             nsISupports* aData, bool aAnonymize) override {
     MOZ_ASSERT(NS_IsMainThread());
 
     layers::CompositorManagerChild* manager =
-        CompositorManagerChild::GetInstance();
+        mozilla::layers::CompositorManagerChild::GetInstance();
     if (!manager || !StaticPrefs::image_mem_debug_reporting()) {
       layers::SharedSurfacesMemoryReport sharedSurfaces;
       FinishCollectReports(aHandleReport, aData, aAnonymize, sharedSurfaces);
       return NS_OK;
     }
 
     RefPtr<imgMemoryReporter> self(this);
     nsCOMPtr<nsIHandleReportCallback> handleReport(aHandleReport);
--- a/image/moz.build
+++ b/image/moz.build
@@ -34,16 +34,17 @@ XPIDL_SOURCES += [
     'imgITools.idl',
     'nsIIconURI.idl',
 ]
 
 XPIDL_MODULE = 'imglib2'
 
 EXPORTS += [
     'FrameTimeout.h',
+    'ImageBlocker.h',
     'ImageCacheKey.h',
     'ImageLogging.h',
     'ImageMetadata.h',
     'ImageOps.h',
     'ImageRegion.h',
     'ImgDrawResult.h',
     'imgLoader.h',
     'imgRequest.h',
@@ -70,16 +71,17 @@ UNIFIED_SOURCES += [
     'DecodedSurfaceProvider.cpp',
     'Decoder.cpp',
     'DecoderFactory.cpp',
     'DynamicImage.cpp',
     'FrameAnimator.cpp',
     'FrozenImage.cpp',
     'IDecodingTask.cpp',
     'Image.cpp',
+    'ImageBlocker.cpp',
     'ImageCacheKey.cpp',
     'ImageFactory.cpp',
     'ImageMemoryReporter.cpp',
     'ImageOps.cpp',
     'ImageWrapper.cpp',
     'imgFrame.cpp',
     'imgLoader.cpp',
     'imgRequest.cpp',
--- a/modules/libpref/init/StaticPrefList.yaml
+++ b/modules/libpref/init/StaticPrefList.yaml
@@ -7255,16 +7255,23 @@
 - name: page_load.deprioritization_period
   type: RelaxedAtomicUint32
   value: 5000
   mirror: always
 
 #---------------------------------------------------------------------------
 # Prefs starting with "permissions."
 #---------------------------------------------------------------------------
+
+# 1-Accept, 2-Deny, Any other value: Accept
+- name: permissions.default.image
+  type: RelaxedAtomicUint32
+  value: 1
+  mirror: always
+
 - name: permissions.delegation.enabled
   type: bool
   value: true
   mirror: always
 
 - name: permissions.isolateBy.userContext
   type: bool
   value: false
--- a/modules/libpref/init/all.js
+++ b/modules/libpref/init/all.js
@@ -1951,18 +1951,16 @@ pref("network.http.tailing.delay-max", 6
 // Total limit we delay tailed requests since a page load beginning.
 pref("network.http.tailing.total-max", 45000);
 
 // Enable or disable the whole fix from bug 1563538
 pref("network.http.spdy.bug1563538", true);
 pref("network.http.spdy.bug1563695", true);
 pref("network.http.spdy.bug1556491", true);
 
-pref("permissions.default.image",           1); // 1-Accept, 2-Deny, 3-dontAcceptForeign
-
 pref("network.proxy.type",                  5);
 pref("network.proxy.ftp",                   "");
 pref("network.proxy.ftp_port",              0);
 pref("network.proxy.http",                  "");
 pref("network.proxy.http_port",             0);
 pref("network.proxy.ssl",                   "");
 pref("network.proxy.ssl_port",              0);
 pref("network.proxy.socks",                 "");