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

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

Issue 29606600: Issue 5146 - Implement DownloadableSubscription parsing in C++ (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Nov. 13, 2017, 10:25 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <cwctype>
19
18 #include "DownloadableSubscription.h" 20 #include "DownloadableSubscription.h"
21 #include "../FilterNotifier.h"
22 #include "../StringScanner.h"
23 #include "../filter/CommentFilter.h"
24
25 namespace {
26 constexpr int MILLIS_IN_HOUR = 60 * 60 * 1000;
27 constexpr int MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR;
28 }
29
30 _DownloadableSubscription_Parser::_DownloadableSubscription_Parser(DownloadableS ubscription& sub)
31 : mSubscription(&sub)
32 {
33 }
34
35 void _DownloadableSubscription_Parser::Process(DependentString& line)
36 {
37 auto filter = FilterPtr(Filter::FromText(line), false);
38 if (filter->mType == Filter::Type::COMMENT)
39 {
40 auto comment = filter->As<CommentFilter>();
41 if (!comment->GetParam().empty())
42 {
43 if (comment->GetParam() == u"checksum"_str)
44 {
45 // XXX we'll have to calculate the checksum.
hub 2017/11/13 22:35:27 this part will require import a md5sum calculation
46 }
47 mParams[comment->GetParam()] = comment->GetValue();
48 }
49 }
50
51 if (filter)
52 mFilters.push_back(filter);
53 }
54
55 int _DownloadableSubscription_Parser::ParseExpires(const String& expires)
56 {
57 bool isHour = false;
58 StringScanner scanner(expires);
59 String::size_type numStart = 0;
60 String::size_type numLen = 0;
61 while(!scanner.done())
62 {
63 auto ch = scanner.next();
64 if (std::iswdigit(ch))
65 {
66 if (numLen == 0)
67 numStart = scanner.position();
68 numLen++;
69 }
70 else if (std::iswspace(ch))
71 {
72 if (numLen)
73 break;
74 }
75 else
76 {
77 if (numLen)
78 scanner.back();
79 break;
80 }
81 }
82
83 DependentString numStr(expires, numStart, numLen);
84 int num = numStr.toInt();
85 if (num == 0)
86 return 0;
87
88 while (!scanner.done())
89 {
90 auto ch = scanner.next();
91 if (std::iswspace(ch))
92 continue;
93 if (ch == u'h')
94 {
95 isHour = true;
96 // assume we are done here. The rest is ignored.
97 break;
98 }
99 }
100 // check for overflow.
101 if (!isHour && num > 24)
hub 2017/11/13 22:35:27 I should check the overflow for the expiration in
102 return 0;
103
104 num *= isHour ? MILLIS_IN_HOUR : MILLIS_IN_DAY;
105 return num;
106 }
107
108 int _DownloadableSubscription_Parser::Finalize()
109 {
110 auto entry = mParams.find(u"title"_str);
111 if (entry)
112 {
113 mSubscription->SetTitle(entry->second);
114 mSubscription->SetFixedTitle(true);
115 }
116 else
117 mSubscription->SetFixedTitle(false);
118
119 int version = 0;
120 entry = mParams.find(u"version"_str);
121 if (entry)
122 version = entry->second.toInt();
123 mSubscription->SetDataRevision(version);
124
125 int expires = 0;
126 entry = mParams.find(u"expires"_str);
127 if (entry)
128 expires = ParseExpires(entry->second);
129
130 FilterNotifier::SubscriptionChange(
131 FilterNotifier::Topic::SUBSCRIPTION_BEFORE_FILTERS_REPLACED,
132 *mSubscription);
hub 2017/11/13 22:35:28 I'm not if that's the right place to send this not
133 auto oldFilters = std::move(mSubscription->GetFilters());
134 mSubscription->GetFilters() = std::move(mFilters);
135 FilterNotifier::SubscriptionChange(
136 FilterNotifier::Topic::SUBSCRIPTION_FILTERS_REPLACED, *mSubscription);
137
138 return expires;
139 }
140
141 namespace {
142 DependentString emptyString = u""_str;
143 }
144
145 const String& _DownloadableSubscription_Parser::GetRedirect() const
146 {
147 auto entry = mParams.find(u"redirect"_str);
148 if (entry)
149 return entry->second;
150 return emptyString;
151 }
152
153 const String& _DownloadableSubscription_Parser::GetHomepage() const
154 {
155 auto entry = mParams.find(u"homepage"_str);
156 if (entry)
157 return entry->second;
158 return emptyString;
159 }
19 160
20 DownloadableSubscription::DownloadableSubscription(const String& id) 161 DownloadableSubscription::DownloadableSubscription(const String& id)
21 : Subscription(classType, id), mFixedTitle(false), mLastCheck(0), 162 : Subscription(classType, id), mFixedTitle(false), mLastCheck(0),
22 mHardExpiration(0), mSoftExpiration(0), mLastDownload(0), mLastSuccess(0), 163 mHardExpiration(0), mSoftExpiration(0), mLastDownload(0), mLastSuccess(0),
23 mErrorCount(0), mDataRevision(0), mDownloadCount(0) 164 mErrorCount(0), mDataRevision(0), mDownloadCount(0)
24 { 165 {
25 SetTitle(id); 166 SetTitle(id);
26 } 167 }
27 168
169 _DownloadableSubscription_Parser* DownloadableSubscription::ParseDownload()
170 {
171 return new _DownloadableSubscription_Parser(*this);
172 }
173
28 OwnedString DownloadableSubscription::Serialize() const 174 OwnedString DownloadableSubscription::Serialize() const
29 { 175 {
30 OwnedString result(Subscription::Serialize()); 176 OwnedString result(Subscription::Serialize());
31 if (mFixedTitle) 177 if (mFixedTitle)
32 result.append(u"fixedTitle=true\n"_str); 178 result.append(u"fixedTitle=true\n"_str);
33 if (!mHomepage.empty()) 179 if (!mHomepage.empty())
34 { 180 {
35 result.append(u"homepage="_str); 181 result.append(u"homepage="_str);
36 result.append(mHomepage); 182 result.append(mHomepage);
37 result.append(u'\n'); 183 result.append(u'\n');
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 result.append(u'\n'); 237 result.append(u'\n');
92 } 238 }
93 if (mDownloadCount) 239 if (mDownloadCount)
94 { 240 {
95 result.append(u"downloadCount="_str); 241 result.append(u"downloadCount="_str);
96 result.append(mDownloadCount); 242 result.append(mDownloadCount);
97 result.append(u'\n'); 243 result.append(u'\n');
98 } 244 }
99 return result; 245 return result;
100 } 246 }
OLDNEW

Powered by Google App Engine
This is Rietveld