Bug 1403077 - add two test-only helper functions to access the stylo blocklist. r=heycam
authorJeremy Chen <jeremychen@mozilla.com>
Mon, 09 Oct 2017 12:07:04 +0800
changeset 427797 abf7029dbcd02a59ab4f13f168bb2d89adabe89e
parent 427796 08363e433669fe7e6eae4a5776728ed4864e4d35
child 427798 13255dcbdaf60b83edb23e3b4a2dd63e4d59d723
push id97
push userfmarier@mozilla.com
push dateSat, 14 Oct 2017 01:12:59 +0000
reviewersheycam
bugs1403077
milestone58.0a1
Bug 1403077 - add two test-only helper functions to access the stylo blocklist. r=heycam In the current blocklist implementation, we read the stylo blocklist from the user preferences very early and only once, even earlier than the test preferences updating happens. So, to be able to test the functionality of the stylo blocklist, we add these two nsIDOMWindowUtils APIs to be able to add/remove a mock domain to the existing blocklist. MozReview-Commit-ID: K48ejLBcNbn
dom/base/nsDOMWindowUtils.cpp
dom/interfaces/base/nsIDOMWindowUtils.idl
layout/base/nsLayoutUtils.cpp
layout/base/nsLayoutUtils.h
--- a/dom/base/nsDOMWindowUtils.cpp
+++ b/dom/base/nsDOMWindowUtils.cpp
@@ -4499,16 +4499,34 @@ nsDOMWindowUtils::EnsureDirtyRootFrame()
 NS_IMETHODIMP
 nsDOMWindowUtils::GetIsStyledByServo(bool* aStyledByServo)
 {
   nsIDocument* doc = GetDocument();
   *aStyledByServo = doc && doc->IsStyledByServo();
   return NS_OK;
 }
 
+NS_IMETHODIMP
+nsDOMWindowUtils::AddToStyloBlocklist(const nsACString& aBlockedDomain)
+{
+#ifdef MOZ_STYLO
+  nsLayoutUtils::AddToStyloBlocklist(aBlockedDomain);
+#endif
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsDOMWindowUtils::RemoveFromStyloBlocklist(const nsACString& aBlockedDomain)
+{
+#ifdef MOZ_STYLO
+  nsLayoutUtils::RemoveFromStyloBlocklist(aBlockedDomain);
+#endif
+  return NS_OK;
+}
+
 NS_INTERFACE_MAP_BEGIN(nsTranslationNodeList)
   NS_INTERFACE_MAP_ENTRY(nsISupports)
   NS_INTERFACE_MAP_ENTRY(nsITranslationNodeList)
 NS_INTERFACE_MAP_END
 
 NS_IMPL_ADDREF(nsTranslationNodeList)
 NS_IMPL_RELEASE(nsTranslationNodeList)
 
--- a/dom/interfaces/base/nsIDOMWindowUtils.idl
+++ b/dom/interfaces/base/nsIDOMWindowUtils.idl
@@ -2041,16 +2041,30 @@ interface nsIDOMWindowUtils : nsISupport
 
   /**
    * Whether the current document is styled by Servo's style engine.
    *
    * This calls nsIDocument::IsStyledByServo().
    */
   readonly attribute boolean isStyledByServo;
 
+  /**
+   * Add a domain to the existing stylo blocklist.
+   *
+   * This calls nsLayoutUtils::AddToStyloBlocklist().
+   */
+  void addToStyloBlocklist(in ACString aBlockedDomain);
+
+  /**
+   * Remove a domain from the existing stylo blocklist.
+   *
+   * This calls nsLayoutUtils::RemoveFromStyloBlocklist().
+   */
+  void removeFromStyloBlocklist(in ACString aBlockedDomain);
+
   // These consts are only for testing purposes.
   const long DEFAULT_MOUSE_POINTER_ID = 0;
   const long DEFAULT_PEN_POINTER_ID   = 1;
   const long DEFAULT_TOUCH_POINTER_ID = 2;
 
   // Match WidgetMouseEventBase::buttonType.
   const long MOUSE_BUTTON_LEFT_BUTTON   = 0;
   const long MOUSE_BUTTON_MIDDLE_BUTTON = 1;
--- a/layout/base/nsLayoutUtils.cpp
+++ b/layout/base/nsLayoutUtils.cpp
@@ -8056,16 +8056,42 @@ nsLayoutUtils::IsInStyloBlocklist(nsIPri
   NS_SUCCEEDED(tldService->GetBaseDomain(codebaseURI, 0, baseDomain));
   for (const nsCString& domains : *sStyloBlocklist) {
     if (baseDomain.Equals(domains)) {
       return true;
     }
   }
   return false;
 }
+
+/* static */
+void
+nsLayoutUtils::AddToStyloBlocklist(const nsACString& aBlockedDomain)
+{
+  if (!sStyloBlocklist) {
+    sStyloBlocklist = new nsTArray<nsCString>;
+  }
+  sStyloBlocklist->AppendElement(aBlockedDomain);
+}
+
+/* static */
+void
+nsLayoutUtils::RemoveFromStyloBlocklist(const nsACString& aBlockedDomain)
+{
+  if (!sStyloBlocklist) {
+    return;
+  }
+
+  sStyloBlocklist->RemoveElement(aBlockedDomain);
+
+  if (sStyloBlocklist->IsEmpty()) {
+    delete sStyloBlocklist;
+    sStyloBlocklist = nullptr;
+  }
+}
 #endif
 
 /* static */
 void
 nsLayoutUtils::RegisterImageRequest(nsPresContext* aPresContext,
                                     imgIRequest* aRequest,
                                     bool* aRequestRegistered)
 {
--- a/layout/base/nsLayoutUtils.h
+++ b/layout/base/nsLayoutUtils.h
@@ -2562,16 +2562,30 @@ public:
   /**
    * Principal-based blocklist for stylo.
    * Check if aPrincipal is blocked by stylo's blocklist and should fallback to
    * use Gecko's style backend. Note that using a document's principal rather
    * than the document URI will let us piggy-back off the existing principal
    * relationships and symmetries.
    */
   static bool IsInStyloBlocklist(nsIPrincipal* aPrincipal);
+
+  /**
+   * Add aBlockedDomain to the existing stylo blocklist, i.e., sStyloBlocklist.
+   * This function is exposed to nsDOMWindowUtils and only for testing purpose.
+   * So, NEVER use this in any other cases.
+   */
+  static void AddToStyloBlocklist(const nsACString& aBlockedDomain);
+
+  /**
+   * Remove aBlockedDomain from the existing stylo blocklist, i.e., sStyloBlocklist.
+   * This function is exposed to nsDOMWindowUtils and only for testing purpose.
+   * So, NEVER use this in any other cases.
+   */
+  static void RemoveFromStyloBlocklist(const nsACString& aBlockedDomain);
 #endif
 
   /**
    * Register an imgIRequest object with a refresh driver.
    *
    * @param aPresContext The nsPresContext whose refresh driver we want to
    *        register with.
    * @param aRequest A pointer to the imgIRequest object which the client wants