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

Powered by Google App Engine
This is Rietveld