[Content Extensions] Split parsing and compiling of content extensions into their...
[WebKit-https.git] / Source / WebCore / contentextensions / ContentExtensionCompiler.cpp
1 /*
2  * Copyright (C) 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 "ContentExtensionCompiler.h"
28
29 #if ENABLE(CONTENT_EXTENSIONS)
30
31 #include "CompiledContentExtension.h"
32 #include "ContentExtensionActions.h"
33 #include "ContentExtensionParser.h"
34 #include "ContentExtensionRule.h"
35 #include "ContentExtensionsDebugging.h"
36 #include "DFABytecodeCompiler.h"
37 #include "NFA.h"
38 #include "NFAToDFA.h"
39 #include "URLFilterParser.h"
40 #include <wtf/CurrentTime.h>
41 #include <wtf/DataLog.h>
42 #include <wtf/text/CString.h>
43
44 namespace WebCore {
45 namespace ContentExtensions {
46
47 static Vector<unsigned> serializeActions(const Vector<ContentExtensionRule>& ruleList, Vector<SerializedActionByte>& actions)
48 {
49     ASSERT(!actions.size());
50     
51     Vector<unsigned> actionLocations;
52         
53     for (unsigned ruleIndex = 0; ruleIndex < ruleList.size(); ++ruleIndex) {
54         const ContentExtensionRule& rule = ruleList[ruleIndex];
55         actionLocations.append(actions.size());
56         
57         switch (rule.action().type()) {
58         case ActionType::InvalidAction:
59             RELEASE_ASSERT_NOT_REACHED();
60
61         case ActionType::BlockLoad:
62         case ActionType::BlockCookies:
63         case ActionType::IgnorePreviousRules:
64             actions.append(static_cast<SerializedActionByte>(rule.action().type()));
65             break;
66
67         case ActionType::CSSDisplayNone: {
68             const String& selector = rule.action().cssSelector();
69             // Append action type (1 byte).
70             actions.append(static_cast<SerializedActionByte>(ActionType::CSSDisplayNone));
71             // Append Selector length (4 bytes).
72             unsigned selectorLength = selector.length();
73             actions.resize(actions.size() + sizeof(unsigned));
74             *reinterpret_cast<unsigned*>(&actions[actions.size() - sizeof(unsigned)]) = selectorLength;
75             bool wideCharacters = !selector.is8Bit();
76             actions.append(wideCharacters);
77             // Append Selector.
78             if (wideCharacters) {
79                 for (unsigned i = 0; i < selectorLength; i++) {
80                     actions.resize(actions.size() + sizeof(UChar));
81                     *reinterpret_cast<UChar*>(&actions[actions.size() - sizeof(UChar)]) = selector[i];
82                 }
83             } else {
84                 for (unsigned i = 0; i < selectorLength; i++)
85                     actions.append(selector[i]);
86             }
87             break;
88         }
89         }
90     }
91     return actionLocations;
92 }
93
94
95 Ref<CompiledContentExtension> compileRuleList(const String& ruleList)
96 {
97     auto parsedRuleList = parseRuleList(ruleList);
98
99 #if CONTENT_EXTENSIONS_PERFORMANCE_REPORTING
100     double nfaBuildTimeStart = monotonicallyIncreasingTime();
101 #endif
102
103     Vector<SerializedActionByte> actions;
104     Vector<unsigned> actionLocations = serializeActions(parsedRuleList, actions);
105
106     NFA nfa;
107     URLFilterParser urlFilterParser(nfa);
108     for (unsigned ruleIndex = 0; ruleIndex < parsedRuleList.size(); ++ruleIndex) {
109         const ContentExtensionRule& contentExtensionRule = parsedRuleList[ruleIndex];
110         const Trigger& trigger = contentExtensionRule.trigger();
111         ASSERT(trigger.urlFilter.length());
112
113         String error = urlFilterParser.addPattern(trigger.urlFilter, trigger.urlFilterIsCaseSensitive, actionLocations[ruleIndex]);
114
115         if (!error.isNull()) {
116             dataLogF("Error while parsing %s: %s\n", trigger.urlFilter.utf8().data(), error.utf8().data());
117             continue;
118         }
119     }
120
121 #if CONTENT_EXTENSIONS_PERFORMANCE_REPORTING
122     double nfaBuildTimeEnd = monotonicallyIncreasingTime();
123     dataLogF("    Time spent building the NFA: %f\n", (nfaBuildTimeEnd - nfaBuildTimeStart));
124 #endif
125
126 #if CONTENT_EXTENSIONS_STATE_MACHINE_DEBUGGING
127     nfa.debugPrintDot();
128 #endif
129
130 #if CONTENT_EXTENSIONS_PERFORMANCE_REPORTING
131     double dfaBuildTimeStart = monotonicallyIncreasingTime();
132 #endif
133
134     const DFA dfa = NFAToDFA::convert(nfa);
135
136 #if CONTENT_EXTENSIONS_PERFORMANCE_REPORTING
137     double dfaBuildTimeEnd = monotonicallyIncreasingTime();
138     dataLogF("    Time spent building the DFA: %f\n", (dfaBuildTimeEnd - dfaBuildTimeStart));
139 #endif
140
141     // FIXME: never add a DFA that only matches the empty set.
142
143 #if CONTENT_EXTENSIONS_STATE_MACHINE_DEBUGGING
144     dfa.debugPrintDot();
145 #endif
146
147     Vector<DFABytecode> bytecode;
148     DFABytecodeCompiler compiler(dfa, bytecode);
149     compiler.compile();
150
151     return CompiledContentExtension::create(WTF::move(bytecode), WTF::move(actions));
152 }
153
154 } // namespace ContentExtensions
155 } // namespace WebCore
156
157 #endif // ENABLE(CONTENT_EXTENSIONS)