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

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

Issue 29384812: Issue 4127 - [emscripten] Convert subscription classes to C++ - Part 1 (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Patch Set: Created March 15, 2017, 3:05 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
« no previous file with comments | « compiled/subscription/Subscription.h ('k') | compiled/subscription/UserDefinedSubscription.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include <cstdio>
2 #include <cstdlib>
3
4 #include "Subscription.h"
5 #include "DownloadableSubscription.h"
6 #include "UserDefinedSubscription.h"
7 #include "../StringMap.h"
8
9 namespace
10 {
11 StringMap<Subscription*> knownSubscriptions(16);
12 }
13
14 Subscription::Subscription(Type type, const String& id)
15 : mType(type), mID(id), mDisabled(false)
16 {
17 annotate_address(this, "Subscription");
18 }
19
20 Subscription::~Subscription()
21 {
22 knownSubscriptions.erase(mID);
23 }
24
25 OwnedString Subscription::Serialize() const
26 {
27 OwnedString result(u"[Subscription]\nurl="_str);
28 result.append(mID);
29 result.append(u'\n');
30 if (!mTitle.empty())
31 {
32 result.append(u"title="_str);
33 result.append(mTitle);
34 result.append(u'\n');
35 }
36 if (mDisabled)
37 result.append(u"disabled=true\n"_str);
38
39 return result;
40 }
41
42 OwnedString Subscription::SerializeFilters() const
43 {
44 // TODO
45 return OwnedString();
46 }
47
48 Subscription* Subscription::FromID(const String& id)
49 {
50 if (id.empty())
51 {
52 // Generate a new random ID
53 unsigned seed = knownSubscriptions.length();
54 OwnedString randomID(u"~user~000000"_str);
55 do
56 {
57 int number = rand_r(&seed);
58 for (int i = randomID.length() - 6; i < randomID.length(); i++)
59 {
60 randomID[i] = '0' + (number % 10);
61 number /= 10;
62 }
63 } while (knownSubscriptions.find(randomID));
64 return FromID(randomID);
65 }
66
67 auto knownSubscription = knownSubscriptions.find(id);
68 if (knownSubscription)
69 {
70 knownSubscription->second->AddRef();
71 return knownSubscription->second;
72 }
73
74 SubscriptionPtr subscription;
75 if (!id.empty() && id[0] == '~')
76 subscription = new UserDefinedSubscription(id);
77 else
78 subscription = new DownloadableSubscription(id);
79
80 // 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.
82 // However, id refers to a temporary buffer which will go away.
83 enter_context("Adding to known subscriptions");
84 knownSubscription.assign(subscription->mID, subscription.get());
85 exit_context();
86
87 return subscription.release();
88 }
OLDNEW
« no previous file with comments | « compiled/subscription/Subscription.h ('k') | compiled/subscription/UserDefinedSubscription.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld