Bug 1403077 - add a test-only helper function to append the stylo blocklist. draft
authorJeremy Chen <jeremychen@mozilla.com>
Mon, 09 Oct 2017 12:07:04 +0800
changeset 676624 850383c866577eeb4a906cf71e11cb8dac6466bc
parent 676623 2fccddb43fedd6170b8a393b90ede01fbd4a9602
child 676625 ab9efb1ab04088325ae0c60c39a9fed196e5baa8
push id83551
push userbmo:jeremychen@mozilla.com
push dateMon, 09 Oct 2017 05:54:23 +0000
bugs1403077
milestone58.0a1
Bug 1403077 - add a test-only helper function to append the stylo blocklist. In the current blocklist impelemtation, we read the stylo blocklist from the user preferences very early and only once, which 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,30 @@ 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)
+{
+  nsLayoutUtils::AddToStyloBlocklist(aBlockedDomain);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsDOMWindowUtils::RemoveFromStyloBlocklist(const nsACString& aBlockedDomain)
+{
+  nsLayoutUtils::RemoveFromStyloBlocklist(aBlockedDomain);
+  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