Add CatchScope and force all exception checks to be via ThrowScope or CatchScope.
[WebKit-https.git] / Source / WebCore / contentextensions / ContentExtensionRule.cpp
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(const Trigger& trigger, const Action& action)
36     : m_trigger(trigger)
37     , m_action(action)
38 {
39     ASSERT(!m_trigger.urlFilter.isEmpty());
40 }
41
42 Action Action::deserialize(const SerializedActionByte* actions, const uint32_t actionsLength, uint32_t location)
43 {
44     RELEASE_ASSERT(location < actionsLength);
45     switch (static_cast<ActionType>(actions[location])) {
46     case ActionType::BlockCookies:
47         return Action(ActionType::BlockCookies, location);
48     case ActionType::BlockLoad:
49         return Action(ActionType::BlockLoad, location);
50     case ActionType::IgnorePreviousRules:
51         return Action(ActionType::IgnorePreviousRules, location);
52     case ActionType::MakeHTTPS:
53         return Action(ActionType::MakeHTTPS, location);
54     case ActionType::CSSDisplayNoneSelector: {
55         uint32_t headerLength = sizeof(ActionType) + sizeof(uint32_t) + sizeof(bool);
56         uint32_t stringStartIndex = location + headerLength;
57         RELEASE_ASSERT(actionsLength >= stringStartIndex);
58         uint32_t selectorLength = *reinterpret_cast<const unsigned*>(&actions[location + sizeof(ActionType)]);
59         bool wideCharacters = actions[location + sizeof(ActionType) + sizeof(uint32_t)];
60         
61         if (wideCharacters) {
62             RELEASE_ASSERT(actionsLength >= stringStartIndex + selectorLength * sizeof(UChar));
63             return Action(ActionType::CSSDisplayNoneSelector, String(reinterpret_cast<const UChar*>(&actions[stringStartIndex]), selectorLength), location);
64         }
65         RELEASE_ASSERT(actionsLength >= stringStartIndex + selectorLength * sizeof(LChar));
66         return Action(ActionType::CSSDisplayNoneSelector, String(reinterpret_cast<const LChar*>(&actions[stringStartIndex]), selectorLength), location);
67     }
68     case ActionType::CSSDisplayNoneStyleSheet:
69     case ActionType::InvalidAction:
70     default:
71         RELEASE_ASSERT_NOT_REACHED();
72     }
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::IgnorePreviousRules:
83     case ActionType::CSSDisplayNoneSelector:
84     case ActionType::MakeHTTPS:
85         return type;
86     case ActionType::CSSDisplayNoneStyleSheet:
87     case ActionType::InvalidAction:
88     default:
89         RELEASE_ASSERT_NOT_REACHED();
90     }
91 }
92     
93 uint32_t Action::serializedLength(const SerializedActionByte* actions, const uint32_t actionsLength, uint32_t location)
94 {
95     RELEASE_ASSERT(location < actionsLength);
96     switch (static_cast<ActionType>(actions[location])) {
97     case ActionType::BlockCookies:
98     case ActionType::BlockLoad:
99     case ActionType::IgnorePreviousRules:
100     case ActionType::MakeHTTPS:
101         return sizeof(ActionType);
102     case ActionType::CSSDisplayNoneSelector: {
103         uint32_t headerLength = sizeof(ActionType) + sizeof(uint32_t) + sizeof(bool);
104         uint32_t stringStartIndex = location + headerLength;
105         RELEASE_ASSERT(actionsLength >= stringStartIndex);
106         uint32_t selectorLength = *reinterpret_cast<const unsigned*>(&actions[location + sizeof(ActionType)]);
107         bool wideCharacters = actions[location + sizeof(ActionType) + sizeof(uint32_t)];
108         
109         if (wideCharacters)
110             return headerLength + selectorLength * sizeof(UChar);
111         return headerLength + selectorLength * sizeof(LChar);
112     }
113     case ActionType::CSSDisplayNoneStyleSheet:
114     case ActionType::InvalidAction:
115     default:
116         RELEASE_ASSERT_NOT_REACHED();
117     }
118 }
119
120 } // namespace ContentExtensions
121
122 } // namespace WebCore
123
124 #endif // ENABLE(CONTENT_EXTENSIONS)