Bug 1322235: Part 7 - DeCOMtaminate moz-extension protocol handler. r?billm draft
authorKris Maglione <maglione.k@gmail.com>
Tue, 23 May 2017 14:40:38 -0700
changeset 585209 eef3c88bc0ce8a59dc3edefa38d16b93d897be21
parent 585208 7f6913f036073d3870c3919a64cf4c6ad7bd4be3
child 585210 ac206ed4f9b16994d71466968ea6e0dcad854e47
push id61052
push usermaglione.k@gmail.com
push dateFri, 26 May 2017 17:14:32 +0000
reviewersbillm
bugs1322235
milestone55.0a1
Bug 1322235: Part 7 - DeCOMtaminate moz-extension protocol handler. r?billm This removes unnecessary COM overhead from the extension protocol service, particularly from the flag lookup code, which is called often, and from hot paths. The devirtualized lookups should have virtually no overhead for extensions without web-accessible resources, and very little overhead except when resources are specified as non-prefix globs. MozReview-Commit-ID: 4hQ7GuQSjvW
netwerk/protocol/res/ExtensionProtocolHandler.cpp
--- a/netwerk/protocol/res/ExtensionProtocolHandler.cpp
+++ b/netwerk/protocol/res/ExtensionProtocolHandler.cpp
@@ -1,47 +1,55 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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 "ExtensionProtocolHandler.h"
 
-#include "nsIAddonPolicyService.h"
+#include "mozilla/ExtensionPolicyService.h"
 #include "nsServiceManagerUtils.h"
 #include "nsIURL.h"
 #include "nsIChannel.h"
 #include "nsIStreamListener.h"
 #include "nsIInputStream.h"
 #include "nsIOutputStream.h"
 #include "nsIStreamConverterService.h"
 #include "nsNetUtil.h"
 #include "LoadInfo.h"
 #include "SimpleChannel.h"
 
 namespace mozilla {
 namespace net {
 
+using extensions::URLInfo;
+
 NS_IMPL_QUERY_INTERFACE(ExtensionProtocolHandler, nsISubstitutingProtocolHandler,
                         nsIProtocolHandler, nsIProtocolHandlerWithDynamicFlags,
                         nsISupportsWeakReference)
 NS_IMPL_ADDREF_INHERITED(ExtensionProtocolHandler, SubstitutingProtocolHandler)
 NS_IMPL_RELEASE_INHERITED(ExtensionProtocolHandler, SubstitutingProtocolHandler)
 
+static inline ExtensionPolicyService&
+EPS()
+{
+  return ExtensionPolicyService::GetSingleton();
+}
+
 nsresult
 ExtensionProtocolHandler::GetFlagsForURI(nsIURI* aURI, uint32_t* aFlags)
 {
   // In general a moz-extension URI is only loadable by chrome, but a whitelisted
   // subset are web-accessible (and cross-origin fetchable). Check that whitelist.
-  nsCOMPtr<nsIAddonPolicyService> aps = do_GetService("@mozilla.org/addons/policy-service;1");
   bool loadableByAnyone = false;
-  if (aps) {
-    nsresult rv = aps->ExtensionURILoadableByAnyone(aURI, &loadableByAnyone);
-    NS_ENSURE_SUCCESS(rv, rv);
+
+  URLInfo url(aURI);
+  if (auto* policy = EPS().GetByURL(url)) {
+    loadableByAnyone = policy->IsPathWebAccessible(url.Path());
   }
 
   *aFlags = URI_STD | URI_IS_LOCAL_RESOURCE | (loadableByAnyone ? (URI_LOADABLE_BY_ANYONE | URI_FETCHABLE_BY_ANYONE) : URI_DANGEROUS_TO_LOAD);
   return NS_OK;
 }
 
 bool
 ExtensionProtocolHandler::ResolveSpecialCases(const nsACString& aHost,
@@ -50,32 +58,25 @@ ExtensionProtocolHandler::ResolveSpecial
                                               nsACString& aResult)
 {
   // Create special moz-extension:-pages such as moz-extension://foo/_blank.html
   // for all registered extensions. We can't just do this as a substitution
   // because substitutions can only match on host.
   if (!SubstitutingProtocolHandler::HasSubstitution(aHost)) {
     return false;
   }
+
   if (aPathname.EqualsLiteral("/_blank.html")) {
     aResult.AssignLiteral("about:blank");
     return true;
   }
+
   if (aPathname.EqualsLiteral("/_generated_background_page.html")) {
-    nsCOMPtr<nsIAddonPolicyService> aps =
-      do_GetService("@mozilla.org/addons/policy-service;1");
-    if (!aps) {
-      return false;
-    }
-    nsresult rv = aps->GetGeneratedBackgroundPageUrl(aHost, aResult);
-    NS_ENSURE_SUCCESS(rv, false);
-    if (!aResult.IsEmpty()) {
-      MOZ_RELEASE_ASSERT(Substring(aResult, 0, 5).Equals("data:"));
-      return true;
-    }
+    Unused << EPS().GetGeneratedBackgroundPageUrl(aHost, aResult);
+    return !aResult.IsEmpty();
   }
 
   return false;
 }
 
 static inline Result<Ok, nsresult>
 WrapNSResult(nsresult aRv)
 {