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: Addressed comments Created April 13, 2017, 1:02 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 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 #include <cstdio>
19 #include <cstdlib>
20
21 #include "Subscription.h"
22 #include "DownloadableSubscription.h"
23 #include "UserDefinedSubscription.h"
24 #include "../StringMap.h"
25
26 namespace
27 {
28 StringMap<Subscription*> knownSubscriptions(16);
29 }
30
31 Subscription::Subscription(Type type, const String& id)
32 : mType(type), mID(id), mDisabled(false)
33 {
34 annotate_address(this, "Subscription");
35 }
36
37 Subscription::~Subscription()
38 {
39 knownSubscriptions.erase(mID);
40 }
41
42 OwnedString Subscription::Serialize() const
43 {
44 OwnedString result(u"[Subscription]\nurl="_str);
45 result.append(mID);
46 result.append(u'\n');
47 if (!mTitle.empty())
48 {
49 result.append(u"title="_str);
50 result.append(mTitle);
51 result.append(u'\n');
52 }
53 if (mDisabled)
54 result.append(u"disabled=true\n"_str);
55
56 return result;
57 }
58
59 OwnedString Subscription::SerializeFilters() const
60 {
61 // TODO
62 return OwnedString();
63 }
64
65 Subscription* Subscription::FromID(const String& id)
66 {
67 if (id.empty())
68 {
69 // Generate a new random ID
70 unsigned int seed = knownSubscriptions.size();
71 OwnedString randomID(u"~user~000000"_str);
72 do
73 {
74 int number = rand_r(&seed);
75 for (int i = randomID.length() - 6; i < randomID.length(); i++)
76 {
77 randomID[i] = '0' + (number % 10);
78 number /= 10;
79 }
80 } while (knownSubscriptions.find(randomID));
81 return FromID(randomID);
82 }
83
84 auto knownSubscription = knownSubscriptions.find(id);
85 if (knownSubscription)
86 {
87 knownSubscription->second->AddRef();
88 return knownSubscription->second;
89 }
90
91 SubscriptionPtr subscription;
92 if (!id.empty() && id[0] == '~')
93 subscription = new UserDefinedSubscription(id);
94 else
95 subscription = new DownloadableSubscription(id);
96
97 // This is a hack: we looked up the entry using id but create it using
98 // subscription->mID. This works because both are equal at this point.
99 // However, id refers to a temporary buffer which will go away.
100 enter_context("Adding to known subscriptions");
101 knownSubscription.assign(subscription->mID, subscription.get());
102 exit_context();
103
104 return subscription.release();
105 }
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