Bug 1357107 - Part 1: Move the handling of the permissions.default.image pref to imgLoader.cpp; r=bzbarsky
authorEhsan Akhgari <ehsan.akhgari@gmail.com>
Tue, 25 Apr 2017 02:03:12 -0400
changeset 355506 9ed481306bc0c478d56c9b270c0f19772f15a245
parent 355505 e1ceacb7312d50a989e5ad19b46ae1cb8c79e2b5
child 355507 d9618183e8825531cd7b5e4247a57f0d1009f7d0
push id31727
push usercbook@mozilla.com
push dateFri, 28 Apr 2017 08:36:40 +0000
treeherdermozilla-central@8f2b930cd028 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs1357107
milestone55.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 - Part 1: Move the handling of the permissions.default.image pref to imgLoader.cpp; r=bzbarsky
image/ImageBlocker.cpp
image/ImageBlocker.h
image/build/nsImageModule.cpp
image/imgRequestProxy.cpp
image/moz.build
new file mode 100644
--- /dev/null
+++ b/image/ImageBlocker.cpp
@@ -0,0 +1,82 @@
+/* -*- 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 "ImageBlocker.h"
+#include "nsIPermissionManager.h"
+#include "nsContentUtils.h"
+#include "mozilla/Unused.h"
+
+using namespace mozilla;
+using namespace mozilla::image;
+
+bool ImageBlocker::sInitialized = false;
+int32_t ImageBlocker::sImagePermission = nsIPermissionManager::ALLOW_ACTION;
+
+NS_IMPL_ISUPPORTS(ImageBlocker, nsIContentPolicy)
+
+ImageBlocker::ImageBlocker()
+{
+  if (!sInitialized) {
+    Preferences::AddIntVarCache(&sImagePermission,
+                                "permissions.default.image");
+    sInitialized = true;
+  }
+}
+
+NS_IMETHODIMP
+ImageBlocker::ShouldLoad(uint32_t aContentType,
+                         nsIURI* aContentLocation,
+                         nsIURI* aRequestOrigin,
+                         nsISupports* aContext,
+                         const nsACString& aMimeTypeGuess,
+                         nsISupports* aExtra,
+                         nsIPrincipal* aRequestPrincipal,
+                         int16_t* aShouldLoad)
+{
+  MOZ_ASSERT(aContentType == nsContentUtils::InternalContentPolicyTypeToExternal(aContentType),
+             "We should only see external content policy types here.");
+
+  *aShouldLoad = nsIContentPolicy::ACCEPT;
+  if (!aContentLocation) {
+    return NS_OK;
+  }
+
+  nsAutoCString scheme;
+  Unused << 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 (aContentType == nsIContentPolicy::TYPE_IMAGE ||
+      aContentType == nsIContentPolicy::TYPE_IMAGESET) {
+    *aShouldLoad = (sImagePermission == nsIPermissionManager::ALLOW_ACTION) ?
+                     nsIContentPolicy::ACCEPT :
+                     nsIContentPolicy::REJECT_TYPE;
+  }
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+ImageBlocker::ShouldProcess(uint32_t aContentType,
+                            nsIURI* aContentLocation,
+                            nsIURI* aRequestOrigin,
+                            nsISupports* aRequestingContext,
+                            const nsACString& aMimeTypeGuess,
+                            nsISupports* aExtra,
+                            nsIPrincipal* aRequestPrincipal,
+                            int16_t* aShouldProcess)
+{
+  MOZ_ASSERT(aContentType == nsContentUtils::InternalContentPolicyTypeToExternal(aContentType),
+             "We should only see external content policy types here.");
+
+  return ShouldLoad(aContentType, aContentLocation, aRequestOrigin,
+                    aRequestingContext, aMimeTypeGuess, aExtra,
+		    aRequestPrincipal, aShouldProcess);
+}
new file mode 100644
--- /dev/null
+++ b/image/ImageBlocker.h
@@ -0,0 +1,42 @@
+/* -*- 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:
+  ImageBlocker();
+
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSICONTENTPOLICY
+
+private:
+  static bool sInitialized;
+  static int32_t sImagePermission;
+};
+
+}
+}
+
+#endif // mozilla_image_ImageBlocker_h
--- a/image/build/nsImageModule.cpp
+++ b/image/build/nsImageModule.cpp
@@ -5,16 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsImageModule.h"
 
 #include "mozilla/ModuleUtils.h"
 #include "nsMimeTypes.h"
 
 #include "DecodePool.h"
+#include "ImageBlocker.h"
 #include "ImageFactory.h"
 #include "ShutdownTracker.h"
 #include "SurfaceCache.h"
 #include "SurfacePipe.h"
 
 #include "gfxPrefs.h"
 #include "imgLoader.h"
 #include "imgRequest.h"
@@ -33,44 +34,48 @@ using namespace mozilla::image;
 // comment documenting the imgLoader constructor.
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(imgLoader, Init)
 NS_GENERIC_FACTORY_CONSTRUCTOR(imgRequestProxy)
 NS_GENERIC_FACTORY_CONSTRUCTOR(imgTools)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsICOEncoder)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsJPEGEncoder)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsPNGEncoder)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsBMPEncoder)
+NS_GENERIC_FACTORY_CONSTRUCTOR(ImageBlocker)
 NS_DEFINE_NAMED_CID(NS_IMGLOADER_CID);
 NS_DEFINE_NAMED_CID(NS_IMGREQUESTPROXY_CID);
 NS_DEFINE_NAMED_CID(NS_IMGTOOLS_CID);
 NS_DEFINE_NAMED_CID(NS_ICOENCODER_CID);
 NS_DEFINE_NAMED_CID(NS_JPEGENCODER_CID);
 NS_DEFINE_NAMED_CID(NS_PNGENCODER_CID);
 NS_DEFINE_NAMED_CID(NS_BMPENCODER_CID);
+NS_DEFINE_NAMED_CID(IMAGEBLOCKER_CID);
 
 static const mozilla::Module::CIDEntry kImageCIDs[] = {
   { &kNS_IMGLOADER_CID, false, nullptr, imgLoaderConstructor, },
   { &kNS_IMGREQUESTPROXY_CID, false, nullptr, imgRequestProxyConstructor, },
   { &kNS_IMGTOOLS_CID, false, nullptr, imgToolsConstructor, },
   { &kNS_ICOENCODER_CID, false, nullptr, nsICOEncoderConstructor, },
   { &kNS_JPEGENCODER_CID, false, nullptr, nsJPEGEncoderConstructor, },
   { &kNS_PNGENCODER_CID, false, nullptr, nsPNGEncoderConstructor, },
   { &kNS_BMPENCODER_CID, false, nullptr, nsBMPEncoderConstructor, },
+  { &kIMAGEBLOCKER_CID, false, nullptr, ImageBlockerConstructor, },
   { nullptr }
 };
 
 static const mozilla::Module::ContractIDEntry kImageContracts[] = {
   { "@mozilla.org/image/cache;1", &kNS_IMGLOADER_CID },
   { "@mozilla.org/image/loader;1", &kNS_IMGLOADER_CID },
   { "@mozilla.org/image/request;1", &kNS_IMGREQUESTPROXY_CID },
   { "@mozilla.org/image/tools;1", &kNS_IMGTOOLS_CID },
   { "@mozilla.org/image/encoder;2?type=" IMAGE_ICO_MS, &kNS_ICOENCODER_CID },
   { "@mozilla.org/image/encoder;2?type=" IMAGE_JPEG, &kNS_JPEGENCODER_CID },
   { "@mozilla.org/image/encoder;2?type=" IMAGE_PNG, &kNS_PNGENCODER_CID },
   { "@mozilla.org/image/encoder;2?type=" IMAGE_BMP, &kNS_BMPENCODER_CID },
+  { IMAGEBLOCKER_CONTRACTID, &kIMAGEBLOCKER_CID },
   { nullptr }
 };
 
 static const mozilla::Module::CategoryEntry kImageCategories[] = {
   { "Gecko-Content-Viewers", IMAGE_GIF, "@mozilla.org/content/document-loader-factory;1" },
   { "Gecko-Content-Viewers", IMAGE_JPEG, "@mozilla.org/content/document-loader-factory;1" },
   { "Gecko-Content-Viewers", IMAGE_PJPEG, "@mozilla.org/content/document-loader-factory;1" },
   { "Gecko-Content-Viewers", IMAGE_JPG, "@mozilla.org/content/document-loader-factory;1" },
@@ -78,16 +83,17 @@ static const mozilla::Module::CategoryEn
   { "Gecko-Content-Viewers", IMAGE_ICO_MS, "@mozilla.org/content/document-loader-factory;1" },
   { "Gecko-Content-Viewers", IMAGE_BMP, "@mozilla.org/content/document-loader-factory;1" },
   { "Gecko-Content-Viewers", IMAGE_BMP_MS, "@mozilla.org/content/document-loader-factory;1" },
   { "Gecko-Content-Viewers", IMAGE_ICON_MS, "@mozilla.org/content/document-loader-factory;1" },
   { "Gecko-Content-Viewers", IMAGE_PNG, "@mozilla.org/content/document-loader-factory;1" },
   { "Gecko-Content-Viewers", IMAGE_APNG, "@mozilla.org/content/document-loader-factory;1" },
   { "Gecko-Content-Viewers", IMAGE_X_PNG, "@mozilla.org/content/document-loader-factory;1" },
   { "content-sniffing-services", "@mozilla.org/image/loader;1", "@mozilla.org/image/loader;1" },
+  { "content-policy", IMAGEBLOCKER_CONTRACTID, IMAGEBLOCKER_CONTRACTID },
   { nullptr }
 };
 
 static bool sInitialized = false;
 nsresult
 mozilla::image::EnsureModuleInitialized()
 {
   MOZ_ASSERT(NS_IsMainThread());
--- a/image/imgRequestProxy.cpp
+++ b/image/imgRequestProxy.cpp
@@ -612,17 +612,17 @@ static imgRequestProxy* NewProxy(imgRequ
 {
   return new imgRequestProxy();
 }
 
 imgRequestProxy* NewStaticProxy(imgRequestProxy* aThis)
 {
   nsCOMPtr<nsIPrincipal> currentPrincipal;
   aThis->GetImagePrincipal(getter_AddRefs(currentPrincipal));
-  RefPtr<image::Image> image = aThis->GetImage();
+  RefPtr<Image> image = aThis->GetImage();
   return new imgRequestProxyStatic(image, currentPrincipal);
 }
 
 NS_IMETHODIMP
 imgRequestProxy::Clone(imgINotificationObserver* aObserver,
                        imgIRequest** aClone)
 {
   nsresult result;
--- a/image/moz.build
+++ b/image/moz.build
@@ -58,16 +58,17 @@ UNIFIED_SOURCES += [
     'DecodePool.cpp',
     'Decoder.cpp',
     'DecoderFactory.cpp',
     'DynamicImage.cpp',
     'FrameAnimator.cpp',
     'FrozenImage.cpp',
     'IDecodingTask.cpp',
     'Image.cpp',
+    'ImageBlocker.cpp',
     'ImageCacheKey.cpp',
     'ImageFactory.cpp',
     'ImageOps.cpp',
     'ImageWrapper.cpp',
     'imgFrame.cpp',
     'imgLoader.cpp',
     'imgRequest.cpp',
     'imgRequestProxy.cpp',