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

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

Issue 29548581: Issue 4128, 5138 - Add Parser and Serializer implemented in C++ Base URL: https://github.com/adblockplus/adblockpluscore.git
Patch Set: rebase Created March 7, 2018, 12:01 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 13 matching lines...) Expand all
24 #include "UserDefinedSubscription.h" 24 #include "UserDefinedSubscription.h"
25 #include "../StringMap.h" 25 #include "../StringMap.h"
26 26
27 ABP_NS_USING 27 ABP_NS_USING
28 28
29 namespace 29 namespace
30 { 30 {
31 StringMap<Subscription*> knownSubscriptions(16); 31 StringMap<Subscription*> knownSubscriptions(16);
32 } 32 }
33 33
34 Subscription::Subscription(Type type, const String& id) 34 Subscription::Subscription(Type type, const String& id, const KeyValues& propert ies)
35 : mID(id), mType(type), mDisabled(false), mListed(false) 35 : mID(id), mType(type), mDisabled(false), mListed(false)
36 { 36 {
37 annotate_address(this, "Subscription"); 37 annotate_address(this, "Subscription");
38 parseProperty(properties, mTitle, u"title"_str);
39 parseProperty(properties, mDisabled, u"disabled"_str);
38 } 40 }
39 41
40 Subscription::~Subscription() 42 Subscription::~Subscription()
41 { 43 {
42 knownSubscriptions.erase(mID); 44 knownSubscriptions.erase(mID);
43 } 45 }
44 46
45 Filter* Subscription::FilterAt(Subscription::Filters::size_type index) 47 Filter* Subscription::FilterAt(Subscription::Filters::size_type index)
46 { 48 {
47 if (index >= mFilters.size()) 49 if (index >= mFilters.size())
48 return nullptr; 50 return nullptr;
49 51
50 FilterPtr result(mFilters[index]); 52 FilterPtr result(mFilters[index]);
51 return result.release(); 53 return result.release();
52 } 54 }
53 55
54 int Subscription::IndexOfFilter(const Filter& filter) 56 int Subscription::IndexOfFilter(const Filter& filter)
55 { 57 {
56 for (Filters::size_type i = 0; i < mFilters.size(); i++) 58 for (Filters::size_type i = 0; i < mFilters.size(); i++)
57 if (mFilters[i] == &filter) 59 if (mFilters[i] == &filter)
58 return i; 60 return i;
59 return -1; 61 return -1;
60 } 62 }
61 63
62 OwnedString Subscription::Serialize() const 64 void Subscription::AddFilter(Filter& filter)
63 { 65 {
64 OwnedString result(u"[Subscription]\nurl="_str); 66 mFilters.emplace_back(&filter);
67 }
68
69 OwnedString Subscription::SerializeProperties() const
70 {
71 if (const auto* downcastedSubscription = As<DownloadableSubscription>())
72 return downcastedSubscription->SerializeProperties();
73 else if (const auto* downcastedSubscription = As<UserDefinedSubscription>())
74 return downcastedSubscription->SerializeProperties();
75
76 return DoSerializeProperties();
77 }
78
79
80 OwnedString Subscription::DoSerializeProperties() const
81 {
82 OwnedString result(u"url="_str);
65 result.append(mID); 83 result.append(mID);
66 result.append(u'\n'); 84 result.append(u'\n');
67 if (!mTitle.empty()) 85 if (!mTitle.empty())
68 { 86 {
69 result.append(u"title="_str); 87 result.append(u"title="_str);
70 result.append(mTitle); 88 result.append(mTitle);
71 result.append(u'\n'); 89 result.append(u'\n');
72 } 90 }
73 if (mDisabled) 91 if (mDisabled)
74 result.append(u"disabled=true\n"_str); 92 result.append(u"disabled=true\n"_str);
75 93
76 return result; 94 return result;
77 } 95 }
78 96
79 OwnedString Subscription::SerializeFilters() const 97 SubscriptionPtr Subscription::FromProperties(const String& id, const KeyValues& keyValues)
80 {
81 if (!mFilters.size())
82 return OwnedString();
83
84 OwnedString result(u"[Subscription filters]\n"_str);
85 for (const auto& filter : mFilters)
86 {
87 // TODO: Escape [ characters
88 result.append(filter->GetText());
89 result.append(u'\n');
90 }
91 return result;
92 }
93
94 Subscription* Subscription::FromID(const String& id)
95 { 98 {
96 if (id.empty()) 99 if (id.empty())
97 { 100 {
98 // Generate a new random ID 101 // Generate a new random ID
99 std::mt19937 gen(knownSubscriptions.size()); 102 std::mt19937 gen(knownSubscriptions.size());
100 OwnedString randomID(u"~user~000000"_str); 103 OwnedString randomID(u"~user~000000"_str);
101 do 104 do
102 { 105 {
103 int number = gen(); 106 int number = gen();
104 for (String::size_type i = randomID.length() - 6; i < randomID.length(); i ++) 107 for (String::size_type i = randomID.length() - 6; i < randomID.length(); i ++)
105 { 108 {
106 randomID[i] = '0' + (number % 10); 109 randomID[i] = '0' + (number % 10);
107 number /= 10; 110 number /= 10;
108 } 111 }
109 } while (knownSubscriptions.find(randomID)); 112 } while (knownSubscriptions.find(randomID));
110 return FromID(randomID); 113 return FromProperties(randomID, keyValues);
111 } 114 }
112 115
113 auto knownSubscription = knownSubscriptions.find(id); 116 auto knownSubscription = knownSubscriptions.find(id);
114 if (knownSubscription) 117 if (knownSubscription)
115 { 118 return SubscriptionPtr(knownSubscription->second);
116 knownSubscription->second->AddRef();
117 return knownSubscription->second;
118 }
119 119
120 SubscriptionPtr subscription; 120 SubscriptionPtr subscription;
121 if (!id.empty() && id[0] == '~') 121 if (id[0] == '~')
122 subscription = SubscriptionPtr(new UserDefinedSubscription(id), false); 122 subscription.reset(new UserDefinedSubscription(id, keyValues), false);
123 else 123 else
124 subscription = SubscriptionPtr(new DownloadableSubscription(id), false); 124 subscription.reset(new DownloadableSubscription(id, keyValues), false);
125 125
126 // This is a hack: we looked up the entry using id but create it using 126 // This is a hack: we looked up the entry using id but create it using
127 // subscription->mID. This works because both are equal at this point. 127 // subscription->mID. This works because both are equal at this point.
128 // However, id refers to a temporary buffer which will go away. 128 // However, id refers to a temporary buffer which will go away.
129 enter_context("Adding to known subscriptions"); 129 enter_context("Adding to known subscriptions");
130 knownSubscription.assign(subscription->mID, subscription.get()); 130 knownSubscription.assign(subscription->mID, subscription.get());
131 exit_context(); 131 exit_context();
132 132
133 return subscription.release(); 133 return subscription;
134 } 134 }
135
136 SubscriptionPtr Subscription::FromProperties(const KeyValues& properties)
137 {
138 const String* id = findPropertyValue(properties, u"url"_str);
139 if (id == nullptr || id->empty())
140 return SubscriptionPtr();
141 return FromProperties(*id, properties);
142 }
143
144 const String* Subscription::findPropertyValue(const Subscription::KeyValues& pro perties, const String& propertyName)
145 {
146 for (const auto& property : properties)
147 if (property.first == propertyName)
148 return &property.second;
149 return nullptr;
150 }
OLDNEW

Powered by Google App Engine
This is Rietveld