Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: compiled/subscription/UserDefinedSubscription.cpp

Issue 29548581: Issue 4128, 5138 - Add Parser and Serializer implemented in C++ Base URL: https://github.com/adblockplus/adblockpluscore.git
Patch Set: Created Sept. 18, 2017, 5:23 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 29 matching lines...) Expand all
40 FilterCategory::ELEMHIDE, // ELEMHIDEEXCEPTION 40 FilterCategory::ELEMHIDE, // ELEMHIDEEXCEPTION
41 FilterCategory::ELEMHIDE, // ELEMHIDEEMULATION 41 FilterCategory::ELEMHIDE, // ELEMHIDEEMULATION
42 }; 42 };
43 43
44 static_assert( 44 static_assert(
45 sizeof(filterTypeToCategory) / sizeof(filterTypeToCategory[0]) == Filter::Ty pe::VALUE_COUNT, 45 sizeof(filterTypeToCategory) / sizeof(filterTypeToCategory[0]) == Filter::Ty pe::VALUE_COUNT,
46 "Unexpected number of filter types, was a new type added?" 46 "Unexpected number of filter types, was a new type added?"
47 ); 47 );
48 } 48 }
49 49
50 UserDefinedSubscription::UserDefinedSubscription(const String& id) 50 UserDefinedSubscription::UserDefinedSubscription(const String& id, const KeyValu es& properties)
51 : Subscription(Type::USERDEFINED, id), mDefaults(0) 51 : Subscription(Type::USERDEFINED, id, properties), mDefaults(FilterCategory: :NONE)
52 { 52 {
53 parseDefaultsProperty(properties, mDefaults);
53 } 54 }
54 55
55 bool UserDefinedSubscription::IsDefaultFor(const Filter& filter) const 56 bool UserDefinedSubscription::IsDefaultFor(const Filter& filter) const
56 { 57 {
57 if (filter.mType >= Filter::Type::VALUE_COUNT) 58 if (filter.mType >= Filter::Type::VALUE_COUNT)
58 { 59 {
59 assert(false, "Filter type exceeds valid range"); 60 assert(false, "Filter type exceeds valid range");
60 abort(); 61 abort();
61 } 62 }
62 return mDefaults & filterTypeToCategory[filter.mType]; 63 return mDefaults & filterTypeToCategory[filter.mType];
(...skipping 30 matching lines...) Expand all
93 FilterPtr filter(mFilters[pos]); 94 FilterPtr filter(mFilters[pos]);
94 mFilters.erase(mFilters.begin() + pos); 95 mFilters.erase(mFilters.begin() + pos);
95 if (GetListed()) 96 if (GetListed())
96 { 97 {
97 FilterNotifier::FilterChange(FilterNotifier::Topic::FILTER_REMOVED, 98 FilterNotifier::FilterChange(FilterNotifier::Topic::FILTER_REMOVED,
98 *filter.get(), this, pos); 99 *filter.get(), this, pos);
99 } 100 }
100 return true; 101 return true;
101 } 102 }
102 103
103 OwnedString UserDefinedSubscription::Serialize() const 104 OwnedString UserDefinedSubscription::SerializeProperties() const
104 { 105 {
105 OwnedString result(Subscription::Serialize()); 106 OwnedString result(Subscription::DoSerializeProperties());
106 if (!IsGeneric()) 107 if (!IsGeneric())
107 { 108 {
108 result.append(u"defaults="_str); 109 result.append(u"defaults="_str);
109 if (mDefaults & FilterCategory::BLOCKING) 110 if (mDefaults & FilterCategory::BLOCKING)
110 result.append(u" blocking"_str); 111 result.append(u" blocking"_str);
111 if (mDefaults & FilterCategory::WHITELIST) 112 if (mDefaults & FilterCategory::WHITELIST)
112 result.append(u" whitelist"_str); 113 result.append(u" whitelist"_str);
113 if (mDefaults & FilterCategory::ELEMHIDE) 114 if (mDefaults & FilterCategory::ELEMHIDE)
114 result.append(u" elemhide"_str); 115 result.append(u" elemhide"_str);
115 result.append(u'\n'); 116 result.append(u'\n');
116 } 117 }
117 return result; 118 return result;
118 } 119 }
120
121 void UserDefinedSubscription::parseDefaultsProperty(const KeyValues& properties, int& defaults)
122 {
123 auto stringDefaultFor = findPropertyValue(properties, u"defaults"_str);
124 if (stringDefaultFor == nullptr)
125 return;
126 String::size_type prevSpacePos = 0;
127 while (prevSpacePos != String::npos)
128 {
129 String::size_type spacePos = stringDefaultFor->find(u' ', prevSpacePos + 1);
130 auto values = SplitString(DependentString(*stringDefaultFor, prevSpacePos), spacePos);
131 if (values.first == u"blocking"_str)
132 defaults |= FilterCategory::BLOCKING;
133 else if (values.first == u"whitelist"_str)
134 defaults |= FilterCategory::WHITELIST;
135 else if (values.first == u"elemhide"_str)
136 defaults |= FilterCategory::ELEMHIDE;
137 prevSpacePos = spacePos;
138 }
139 }
OLDNEW

Powered by Google App Engine
This is Rietveld