| 1 | /* |
| 2 | * Copyright (C) 2014, 2015 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "ContentExtensionRule.h" |
| 28 | |
| 29 | #if ENABLE(CONTENT_EXTENSIONS) |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | namespace ContentExtensions { |
| 34 | |
| 35 | ContentExtensionRule::ContentExtensionRule(Trigger&& trigger, Action&& action) |
| 36 | : m_trigger(WTFMove(trigger)) |
| 37 | , m_action(WTFMove(action)) |
| 38 | { |
| 39 | ASSERT(!m_trigger.urlFilter.isEmpty()); |
| 40 | } |
| 41 | |
| 42 | static String deserializeString(const SerializedActionByte* actions, const uint32_t actionsLength, uint32_t beginIndex) |
| 43 | { |
| 44 | uint32_t prefixLength = sizeof(uint32_t) + sizeof(bool); |
| 45 | uint32_t stringStartIndex = beginIndex + prefixLength; |
| 46 | RELEASE_ASSERT(actionsLength >= stringStartIndex); |
| 47 | uint32_t stringLength = *reinterpret_cast<const uint32_t*>(&actions[beginIndex]); |
| 48 | bool wideCharacters = actions[beginIndex + sizeof(uint32_t)]; |
| 49 | |
| 50 | if (wideCharacters) { |
| 51 | RELEASE_ASSERT(actionsLength >= stringStartIndex + stringLength * sizeof(UChar)); |
| 52 | return String(reinterpret_cast<const UChar*>(&actions[stringStartIndex]), stringLength); |
| 53 | } |
| 54 | RELEASE_ASSERT(actionsLength >= stringStartIndex + stringLength * sizeof(LChar)); |
| 55 | return String(reinterpret_cast<const LChar*>(&actions[stringStartIndex]), stringLength); |
| 56 | } |
| 57 | |
| 58 | Action Action::deserialize(const SerializedActionByte* actions, const uint32_t actionsLength, uint32_t location) |
| 59 | { |
| 60 | RELEASE_ASSERT(location < actionsLength); |
| 61 | auto actionType = static_cast<ActionType>(actions[location]); |
| 62 | switch (actionType) { |
| 63 | case ActionType::BlockCookies: |
| 64 | case ActionType::BlockLoad: |
| 65 | case ActionType::IgnorePreviousRules: |
| 66 | case ActionType::MakeHTTPS: |
| 67 | return Action(actionType, location); |
| 68 | case ActionType::CSSDisplayNoneSelector: |
| 69 | case ActionType::Notify: |
| 70 | return Action(actionType, deserializeString(actions, actionsLength, location + sizeof(ActionType)), location); |
| 71 | } |
| 72 | RELEASE_ASSERT_NOT_REACHED(); |
| 73 | } |
| 74 | |
| 75 | ActionType Action::deserializeType(const SerializedActionByte* actions, const uint32_t actionsLength, uint32_t location) |
| 76 | { |
| 77 | RELEASE_ASSERT(location < actionsLength); |
| 78 | ActionType type = static_cast<ActionType>(actions[location]); |
| 79 | switch (type) { |
| 80 | case ActionType::BlockCookies: |
| 81 | case ActionType::BlockLoad: |
| 82 | case ActionType::Notify: |
| 83 | case ActionType::IgnorePreviousRules: |
| 84 | case ActionType::CSSDisplayNoneSelector: |
| 85 | case ActionType::MakeHTTPS: |
| 86 | return type; |
| 87 | } |
| 88 | RELEASE_ASSERT_NOT_REACHED(); |
| 89 | } |
| 90 | |
| 91 | uint32_t Action::serializedLength(const SerializedActionByte* actions, const uint32_t actionsLength, uint32_t location) |
| 92 | { |
| 93 | RELEASE_ASSERT(location < actionsLength); |
| 94 | switch (static_cast<ActionType>(actions[location])) { |
| 95 | case ActionType::BlockCookies: |
| 96 | case ActionType::BlockLoad: |
| 97 | case ActionType::IgnorePreviousRules: |
| 98 | case ActionType::MakeHTTPS: |
| 99 | return sizeof(ActionType); |
| 100 | case ActionType::Notify: |
| 101 | case ActionType::CSSDisplayNoneSelector: { |
| 102 | uint32_t prefixLength = sizeof(ActionType) + sizeof(uint32_t) + sizeof(bool); |
| 103 | uint32_t stringStartIndex = location + prefixLength; |
| 104 | RELEASE_ASSERT(actionsLength >= stringStartIndex); |
| 105 | uint32_t stringLength = *reinterpret_cast<const unsigned*>(&actions[location + sizeof(ActionType)]); |
| 106 | bool wideCharacters = actions[location + sizeof(ActionType) + sizeof(uint32_t)]; |
| 107 | |
| 108 | if (wideCharacters) |
| 109 | return prefixLength + stringLength * sizeof(UChar); |
| 110 | return prefixLength + stringLength * sizeof(LChar); |
| 111 | } |
| 112 | } |
| 113 | RELEASE_ASSERT_NOT_REACHED(); |
| 114 | } |
| 115 | |
| 116 | Trigger Trigger::isolatedCopy() const |
| 117 | { |
| 118 | return { |
| 119 | urlFilter.isolatedCopy(), |
| 120 | urlFilterIsCaseSensitive, |
| 121 | topURLConditionIsCaseSensitive, |
| 122 | flags, |
| 123 | conditions.isolatedCopy(), |
| 124 | conditionType |
| 125 | }; |
| 126 | } |
| 127 | |
| 128 | Action Action::isolatedCopy() const |
| 129 | { |
| 130 | if (hasStringArgument(m_type)) { |
| 131 | return { |
| 132 | m_type, |
| 133 | m_stringArgument.isolatedCopy(), |
| 134 | m_actionID |
| 135 | }; |
| 136 | } else { |
| 137 | return { |
| 138 | m_type, |
| 139 | m_actionID |
| 140 | }; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | } // namespace ContentExtensions |
| 145 | |
| 146 | } // namespace WebCore |
| 147 | |
| 148 | #endif // ENABLE(CONTENT_EXTENSIONS) |
| 149 | |