Title: [222277] trunk/Source/WebCore
Revision
222277
Author
[email protected]
Date
2017-09-20 12:37:58 -0700 (Wed, 20 Sep 2017)

Log Message

Clean up content extensions code in preparation for more actions with string arguments
https://bugs.webkit.org/show_bug.cgi?id=177258

Reviewed by Tim Horton.

No change in behaviour.  Covered by existing tests.

* contentextensions/ContentExtensionCompiler.cpp:
(WebCore::ContentExtensions::serializeString):
(WebCore::ContentExtensions::resolvePendingDisplayNoneActions):
(WebCore::ContentExtensions::serializeActions):
(WebCore::ContentExtensions::serializeSelector): Deleted.
* contentextensions/ContentExtensionParser.cpp:
(WebCore::ContentExtensions::loadAction):
(WebCore::ContentExtensions::loadRule):
* contentextensions/ContentExtensionRule.cpp:
(WebCore::ContentExtensions::deserializeString):
(WebCore::ContentExtensions::Action::deserialize):
(WebCore::ContentExtensions::Action::deserializeType):
(WebCore::ContentExtensions::Action::serializedLength):
* contentextensions/ContentExtensionRule.h:
(WebCore::ContentExtensions::Action::Action):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (222276 => 222277)


--- trunk/Source/WebCore/ChangeLog	2017-09-20 19:30:15 UTC (rev 222276)
+++ trunk/Source/WebCore/ChangeLog	2017-09-20 19:37:58 UTC (rev 222277)
@@ -1,3 +1,28 @@
+2017-09-20  Alex Christensen  <[email protected]>
+
+        Clean up content extensions code in preparation for more actions with string arguments
+        https://bugs.webkit.org/show_bug.cgi?id=177258
+
+        Reviewed by Tim Horton.
+
+        No change in behaviour.  Covered by existing tests.
+
+        * contentextensions/ContentExtensionCompiler.cpp:
+        (WebCore::ContentExtensions::serializeString):
+        (WebCore::ContentExtensions::resolvePendingDisplayNoneActions):
+        (WebCore::ContentExtensions::serializeActions):
+        (WebCore::ContentExtensions::serializeSelector): Deleted.
+        * contentextensions/ContentExtensionParser.cpp:
+        (WebCore::ContentExtensions::loadAction):
+        (WebCore::ContentExtensions::loadRule):
+        * contentextensions/ContentExtensionRule.cpp:
+        (WebCore::ContentExtensions::deserializeString):
+        (WebCore::ContentExtensions::Action::deserialize):
+        (WebCore::ContentExtensions::Action::deserializeType):
+        (WebCore::ContentExtensions::Action::serializedLength):
+        * contentextensions/ContentExtensionRule.h:
+        (WebCore::ContentExtensions::Action::Action):
+
 2017-09-20  Joanmarie Diggs  <[email protected]>
 
         [ATK] atk_table_get_n_rows() and atk_table_get_n_columns() should return values of aria-rowcount and aria-colcount, if present

Modified: trunk/Source/WebCore/contentextensions/ContentExtensionCompiler.cpp (222276 => 222277)


--- trunk/Source/WebCore/contentextensions/ContentExtensionCompiler.cpp	2017-09-20 19:30:15 UTC (rev 222276)
+++ trunk/Source/WebCore/contentextensions/ContentExtensionCompiler.cpp	2017-09-20 19:37:58 UTC (rev 222277)
@@ -48,25 +48,23 @@
 namespace WebCore {
 namespace ContentExtensions {
 
-static void serializeSelector(Vector<SerializedActionByte>& actions, const String& selector)
+static void serializeString(Vector<SerializedActionByte>& actions, const String& string)
 {
-    // Append action type (1 byte).
-    actions.append(static_cast<SerializedActionByte>(ActionType::CSSDisplayNoneSelector));
     // Append Selector length (4 bytes).
-    unsigned selectorLength = selector.length();
-    actions.grow(actions.size() + sizeof(unsigned));
-    *reinterpret_cast<unsigned*>(&actions[actions.size() - sizeof(unsigned)]) = selectorLength;
-    bool wideCharacters = !selector.is8Bit();
+    uint32_t stringLength = string.length();
+    actions.grow(actions.size() + sizeof(uint32_t));
+    *reinterpret_cast<uint32_t*>(&actions[actions.size() - sizeof(uint32_t)]) = stringLength;
+    bool wideCharacters = !string.is8Bit();
     actions.append(wideCharacters);
     // Append Selector.
     if (wideCharacters) {
-        unsigned startIndex = actions.size();
-        actions.grow(actions.size() + sizeof(UChar) * selectorLength);
-        for (unsigned i = 0; i < selectorLength; ++i)
-            *reinterpret_cast<UChar*>(&actions[startIndex + i * sizeof(UChar)]) = selector[i];
+        uint32_t startIndex = actions.size();
+        actions.grow(actions.size() + sizeof(UChar) * stringLength);
+        for (uint32_t i = 0; i < stringLength; ++i)
+            *reinterpret_cast<UChar*>(&actions[startIndex + i * sizeof(UChar)]) = string[i];
     } else {
-        for (unsigned i = 0; i < selectorLength; ++i)
-            actions.append(selector[i]);
+        for (uint32_t i = 0; i < stringLength; ++i)
+            actions.append(string[i]);
     }
 }
 
@@ -74,7 +72,7 @@
     Vector<String> selectors;
     Vector<unsigned> clientLocations;
 };
-typedef HashMap<Trigger, PendingDisplayNoneActions, TriggerHash, TriggerHashTraits> PendingDisplayNoneActionsMap;
+using PendingDisplayNoneActionsMap = HashMap<Trigger, PendingDisplayNoneActions, TriggerHash, TriggerHashTraits>;
 
 static void resolvePendingDisplayNoneActions(Vector<SerializedActionByte>& actions, Vector<unsigned>& actionLocations, PendingDisplayNoneActionsMap& pendingDisplayNoneActionsMap)
 {
@@ -89,7 +87,8 @@
         }
 
         unsigned actionLocation = actions.size();
-        serializeSelector(actions, combinedSelectors.toString());
+        actions.append(static_cast<SerializedActionByte>(ActionType::CSSDisplayNoneSelector));
+        serializeString(actions, combinedSelectors.toString());
         for (unsigned clientLocation : pendingActions.clientLocations)
             actionLocations[clientLocation] = actionLocation;
     }
@@ -104,7 +103,8 @@
 
     // Order only matters because of IgnorePreviousRules. All other identical actions can be combined between each IgnorePreviousRules
     // and CSSDisplayNone strings can be combined if their triggers are identical.
-    typedef HashMap<uint32_t, uint32_t, DefaultHash<uint32_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<uint32_t>> ActionMap;
+    using ActionLocation = uint32_t;
+    using ActionMap = HashMap<ResourceFlags, ActionLocation, DefaultHash<ResourceFlags>::Hash, WTF::UnsignedWithZeroKeyHashTraits<ResourceFlags>>;
     ActionMap blockLoadActionsMap;
     ActionMap blockCookiesActionsMap;
     PendingDisplayNoneActionsMap cssDisplayNoneActionsMap;
@@ -130,10 +130,9 @@
         if (!rule.trigger().conditions.isEmpty()) {
             actionLocations.append(actions.size());
 
+            actions.append(static_cast<SerializedActionByte>(actionType));
             if (actionType == ActionType::CSSDisplayNoneSelector)
-                serializeSelector(actions, rule.action().stringArgument());
-            else
-                actions.append(static_cast<SerializedActionByte>(actionType));
+                serializeString(actions, rule.action().stringArgument());
             continue;
         }
 

Modified: trunk/Source/WebCore/contentextensions/ContentExtensionParser.cpp (222276 => 222277)


--- trunk/Source/WebCore/contentextensions/ContentExtensionParser.cpp	2017-09-20 19:30:15 UTC (rev 222276)
+++ trunk/Source/WebCore/contentextensions/ContentExtensionParser.cpp	2017-09-20 19:37:58 UTC (rev 222277)
@@ -258,11 +258,11 @@
     String actionType = asString(typeObject)->value(&exec);
 
     if (actionType == "block")
-        return {{ActionType::BlockLoad}};
+        return {{ ActionType::BlockLoad }};
     if (actionType == "ignore-previous-rules")
-        return {{ActionType::IgnorePreviousRules}};
+        return {{ ActionType::IgnorePreviousRules }};
     if (actionType == "block-cookies")
-        return {{ActionType::BlockCookies}};
+        return {{ ActionType::BlockCookies }};
     if (actionType == "css-display-none") {
         JSValue selector = actionObject.get(&exec, Identifier::fromString(&exec, "selector"));
         if (!selector || scope.exception() || !selector.isString())
@@ -271,12 +271,12 @@
         String selectorString = asString(selector)->value(&exec);
         if (!isValidCSSSelector(selectorString)) {
             // Skip rules with invalid selectors to be backwards-compatible.
-            return {std::nullopt};
+            return { std::nullopt };
         }
-        return {Action(ActionType::CSSDisplayNoneSelector, selectorString)};
+        return { Action(ActionType::CSSDisplayNoneSelector, selectorString) };
     }
     if (actionType == "make-https")
-        return {{ActionType::MakeHTTPS}};
+        return {{ ActionType::MakeHTTPS }};
     return makeUnexpected(ContentExtensionError::JSONInvalidActionType);
 }
 
@@ -291,9 +291,9 @@
         return makeUnexpected(action.error());
 
     if (action.value())
-        return {{{WTFMove(trigger.value()), WTFMove(action.value().value())}}};
+        return {{{ WTFMove(trigger.value()), WTFMove(action.value().value()) }}};
 
-    return {std::nullopt};
+    return { std::nullopt };
 }
 
 static Expected<Vector<ContentExtensionRule>, std::error_code> loadEncodedRules(ExecState& exec, String&& ruleJSON)

Modified: trunk/Source/WebCore/contentextensions/ContentExtensionRule.cpp (222276 => 222277)


--- trunk/Source/WebCore/contentextensions/ContentExtensionRule.cpp	2017-09-20 19:30:15 UTC (rev 222276)
+++ trunk/Source/WebCore/contentextensions/ContentExtensionRule.cpp	2017-09-20 19:37:58 UTC (rev 222277)
@@ -39,37 +39,39 @@
     ASSERT(!m_trigger.urlFilter.isEmpty());
 }
 
+static String deserializeString(const SerializedActionByte* actions, const uint32_t actionsLength, uint32_t beginIndex)
+{
+    uint32_t prefixLength = sizeof(uint32_t) + sizeof(bool);
+    uint32_t stringStartIndex = beginIndex + prefixLength;
+    RELEASE_ASSERT(actionsLength >= stringStartIndex);
+    uint32_t stringLength = *reinterpret_cast<const uint32_t*>(&actions[beginIndex]);
+    bool wideCharacters = actions[beginIndex + sizeof(uint32_t)];
+
+    if (wideCharacters) {
+        RELEASE_ASSERT(actionsLength >= stringStartIndex + stringLength * sizeof(UChar));
+        return String(reinterpret_cast<const UChar*>(&actions[stringStartIndex]), stringLength);
+    }
+    RELEASE_ASSERT(actionsLength >= stringStartIndex + stringLength * sizeof(LChar));
+    return String(reinterpret_cast<const LChar*>(&actions[stringStartIndex]), stringLength);
+}
+
 Action Action::deserialize(const SerializedActionByte* actions, const uint32_t actionsLength, uint32_t location)
 {
     RELEASE_ASSERT(location < actionsLength);
-    switch (static_cast<ActionType>(actions[location])) {
+    auto actionType = static_cast<ActionType>(actions[location]);
+    switch (actionType) {
     case ActionType::BlockCookies:
-        return Action(ActionType::BlockCookies, location);
     case ActionType::BlockLoad:
-        return Action(ActionType::BlockLoad, location);
     case ActionType::IgnorePreviousRules:
-        return Action(ActionType::IgnorePreviousRules, location);
     case ActionType::MakeHTTPS:
-        return Action(ActionType::MakeHTTPS, location);
-    case ActionType::CSSDisplayNoneSelector: {
-        uint32_t headerLength = sizeof(ActionType) + sizeof(uint32_t) + sizeof(bool);
-        uint32_t stringStartIndex = location + headerLength;
-        RELEASE_ASSERT(actionsLength >= stringStartIndex);
-        uint32_t selectorLength = *reinterpret_cast<const unsigned*>(&actions[location + sizeof(ActionType)]);
-        bool wideCharacters = actions[location + sizeof(ActionType) + sizeof(uint32_t)];
-        
-        if (wideCharacters) {
-            RELEASE_ASSERT(actionsLength >= stringStartIndex + selectorLength * sizeof(UChar));
-            return Action(ActionType::CSSDisplayNoneSelector, String(reinterpret_cast<const UChar*>(&actions[stringStartIndex]), selectorLength), location);
-        }
-        RELEASE_ASSERT(actionsLength >= stringStartIndex + selectorLength * sizeof(LChar));
-        return Action(ActionType::CSSDisplayNoneSelector, String(reinterpret_cast<const LChar*>(&actions[stringStartIndex]), selectorLength), location);
-    }
+        return Action(actionType, location);
+    case ActionType::CSSDisplayNoneSelector:
+        return Action(actionType, deserializeString(actions, actionsLength, location + sizeof(ActionType)), location);
     case ActionType::CSSDisplayNoneStyleSheet:
     case ActionType::InvalidAction:
-    default:
-        RELEASE_ASSERT_NOT_REACHED();
+        break;
     }
+    RELEASE_ASSERT_NOT_REACHED();
 }
     
 ActionType Action::deserializeType(const SerializedActionByte* actions, const uint32_t actionsLength, uint32_t location)
@@ -85,9 +87,9 @@
         return type;
     case ActionType::CSSDisplayNoneStyleSheet:
     case ActionType::InvalidAction:
-    default:
-        RELEASE_ASSERT_NOT_REACHED();
+        break;
     }
+    RELEASE_ASSERT_NOT_REACHED();
 }
     
 uint32_t Action::serializedLength(const SerializedActionByte* actions, const uint32_t actionsLength, uint32_t location)
@@ -100,21 +102,21 @@
     case ActionType::MakeHTTPS:
         return sizeof(ActionType);
     case ActionType::CSSDisplayNoneSelector: {
-        uint32_t headerLength = sizeof(ActionType) + sizeof(uint32_t) + sizeof(bool);
-        uint32_t stringStartIndex = location + headerLength;
+        uint32_t prefixLength = sizeof(ActionType) + sizeof(uint32_t) + sizeof(bool);
+        uint32_t stringStartIndex = location + prefixLength;
         RELEASE_ASSERT(actionsLength >= stringStartIndex);
-        uint32_t selectorLength = *reinterpret_cast<const unsigned*>(&actions[location + sizeof(ActionType)]);
+        uint32_t stringLength = *reinterpret_cast<const unsigned*>(&actions[location + sizeof(ActionType)]);
         bool wideCharacters = actions[location + sizeof(ActionType) + sizeof(uint32_t)];
         
         if (wideCharacters)
-            return headerLength + selectorLength * sizeof(UChar);
-        return headerLength + selectorLength * sizeof(LChar);
+            return prefixLength + stringLength * sizeof(UChar);
+        return prefixLength + stringLength * sizeof(LChar);
     }
     case ActionType::CSSDisplayNoneStyleSheet:
     case ActionType::InvalidAction:
-    default:
-        RELEASE_ASSERT_NOT_REACHED();
+        break;
     }
+    RELEASE_ASSERT_NOT_REACHED();
 }
 
 } // namespace ContentExtensions

Modified: trunk/Source/WebCore/contentextensions/ContentExtensionRule.h (222276 => 222277)


--- trunk/Source/WebCore/contentextensions/ContentExtensionRule.h	2017-09-20 19:30:15 UTC (rev 222276)
+++ trunk/Source/WebCore/contentextensions/ContentExtensionRule.h	2017-09-20 19:37:58 UTC (rev 222277)
@@ -148,7 +148,8 @@
         : m_type(type)
         , m_actionID(actionID)
     {
-        ASSERT(type != ActionType::CSSDisplayNoneSelector && type != ActionType::CSSDisplayNoneStyleSheet);
+        ASSERT(type != ActionType::CSSDisplayNoneSelector);
+        ASSERT(type != ActionType::CSSDisplayNoneStyleSheet);
     }
 
     bool operator==(const Action& other) const
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to