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

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

Issue 29385742: Issue 4127 - [emscripten] Convert subscription classes to C++ - Part 2 (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Patch Set: Created March 16, 2017, 6:27 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 #include <cstdio> 1 #include <cstdio>
2 #include <cstdlib> 2 #include <cstdlib>
3 3
4 #include "Subscription.h" 4 #include "Subscription.h"
5 #include "DownloadableSubscription.h" 5 #include "DownloadableSubscription.h"
6 #include "UserDefinedSubscription.h" 6 #include "UserDefinedSubscription.h"
7 #include "../StringMap.h" 7 #include "../StringMap.h"
8 8
9 namespace 9 namespace
10 { 10 {
11 StringMap<Subscription*> knownSubscriptions(16); 11 StringMap<Subscription*> knownSubscriptions(16);
12 } 12 }
13 13
14 Subscription::Subscription(Type type, const String& id) 14 Subscription::Subscription(Type type, const String& id)
15 : mType(type), mID(id), mDisabled(false) 15 : mType(type), mID(id), mDisabled(false)
16 { 16 {
17 annotate_address(this, "Subscription"); 17 annotate_address(this, "Subscription");
18 } 18 }
19 19
20 Subscription::~Subscription() 20 Subscription::~Subscription()
21 { 21 {
22 knownSubscriptions.erase(mID); 22 knownSubscriptions.erase(mID);
23 } 23 }
24 24
25 Filter* Subscription::FilterAt(unsigned index)
26 {
27 if (index >= mFilters.size())
28 return nullptr;
29
30 FilterPtr result(mFilters[index]);
31 return result.release();
32 }
33
34 int Subscription::IndexOfFilter(Filter* filter)
35 {
36 for (unsigned i = 0; i < mFilters.size(); i++)
37 if (mFilters[i] == filter)
38 return i;
39 return -1;
40 }
41
25 OwnedString Subscription::Serialize() const 42 OwnedString Subscription::Serialize() const
26 { 43 {
27 OwnedString result(u"[Subscription]\nurl="_str); 44 OwnedString result(u"[Subscription]\nurl="_str);
28 result.append(mID); 45 result.append(mID);
29 result.append(u'\n'); 46 result.append(u'\n');
30 if (!mTitle.empty()) 47 if (!mTitle.empty())
31 { 48 {
32 result.append(u"title="_str); 49 result.append(u"title="_str);
33 result.append(mTitle); 50 result.append(mTitle);
34 result.append(u'\n'); 51 result.append(u'\n');
35 } 52 }
36 if (mDisabled) 53 if (mDisabled)
37 result.append(u"disabled=true\n"_str); 54 result.append(u"disabled=true\n"_str);
38 55
39 return result; 56 return result;
40 } 57 }
41 58
42 OwnedString Subscription::SerializeFilters() const 59 OwnedString Subscription::SerializeFilters() const
43 { 60 {
44 // TODO 61 if (!mFilters.size())
45 return OwnedString(); 62 return OwnedString();
63
64 OwnedString result(u"[Subscription filters]\n"_str);
65 for (unsigned i = 0; i < mFilters.size(); i++)
66 {
67 // TODO: Escape [ characters
Wladimir Palant 2017/03/16 18:31:23 Not fixing this at this point because the serializ
68 result.append(mFilters[i]->GetText());
69 result.append(u'\n');
70 }
71 return result;
46 } 72 }
47 73
48 Subscription* Subscription::FromID(const String& id) 74 Subscription* Subscription::FromID(const String& id)
49 { 75 {
50 if (id.empty()) 76 if (id.empty())
51 { 77 {
52 // Generate a new random ID 78 // Generate a new random ID
53 unsigned seed = knownSubscriptions.size(); 79 unsigned seed = knownSubscriptions.size();
54 OwnedString randomID(u"~user~000000"_str); 80 OwnedString randomID(u"~user~000000"_str);
55 do 81 do
(...skipping 10 matching lines...) Expand all
66 92
67 auto knownSubscription = knownSubscriptions.find(id); 93 auto knownSubscription = knownSubscriptions.find(id);
68 if (knownSubscription) 94 if (knownSubscription)
69 { 95 {
70 knownSubscription->second->AddRef(); 96 knownSubscription->second->AddRef();
71 return knownSubscription->second; 97 return knownSubscription->second;
72 } 98 }
73 99
74 SubscriptionPtr subscription; 100 SubscriptionPtr subscription;
75 if (!id.empty() && id[0] == '~') 101 if (!id.empty() && id[0] == '~')
76 subscription = new UserDefinedSubscription(id); 102 subscription = SubscriptionPtr(new UserDefinedSubscription(id), false);
77 else 103 else
78 subscription = new DownloadableSubscription(id); 104 subscription = SubscriptionPtr(new DownloadableSubscription(id), false);
79 105
80 // This is a hack: we looked up the entry using id but create it using 106 // This is a hack: we looked up the entry using id but create it using
81 // subscription->mID. This works because both are equal at this point. 107 // subscription->mID. This works because both are equal at this point.
82 // However, id refers to a temporary buffer which will go away. 108 // However, id refers to a temporary buffer which will go away.
83 enter_context("Adding to known subscriptions"); 109 enter_context("Adding to known subscriptions");
84 knownSubscription.assign(subscription->mID, subscription.get()); 110 knownSubscription.assign(subscription->mID, subscription.get());
85 exit_context(); 111 exit_context();
86 112
87 return subscription.release(); 113 return subscription.release();
88 } 114 }
OLDNEW

Powered by Google App Engine
This is Rietveld