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

Unified Diff: lib/synchronizer.js

Issue 29606600: Issue 5146 - Implement DownloadableSubscription parsing in C++ (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Nov. 13, 2017, 10:25 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/synchronizer.js
===================================================================
--- a/lib/synchronizer.js
+++ b/lib/synchronizer.js
@@ -19,17 +19,16 @@
/**
* @fileOverview Manages synchronization of filter subscriptions.
*/
const {Downloader, Downloadable,
MILLIS_IN_SECOND, MILLIS_IN_MINUTE,
MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader");
-const {Filter} = require("filterClasses");
const {FilterStorage} = require("filterStorage");
const {FilterNotifier} = require("filterNotifier");
const {Prefs} = require("prefs");
const {Subscription, DownloadableSubscription} = require("subscriptionClasses");
const {Utils} = require("utils");
const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE;
const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR;
@@ -148,50 +147,44 @@
redirectCallback)
{
let lines = responseText.split(/[\r\n]+/);
let headerMatch = /\[Adblock(?:\s*Plus\s*([\d.]+)?)?\]/i.exec(lines[0]);
if (!headerMatch)
return errorCallback("synchronize_invalid_data");
let minVersion = headerMatch[1];
+ let parser = subscription.parseDownload();
+
// Don't remove parameter comments immediately but add them to a list first,
// they need to be considered in the checksum calculation.
- let remove = [];
- let params = {
- redirect: null,
- homepage: null,
- title: null,
- version: null,
- expires: null
- };
for (let i = 0; i < lines.length; i++)
{
let match = /^\s*!\s*(\w+)\s*:\s*(.*)/.exec(lines[i]);
if (match)
{
let keyword = match[1].toLowerCase();
let value = match[2];
- if (keyword in params)
- {
- params[keyword] = value;
- remove.push(i);
- }
- else if (keyword == "checksum")
+ if (keyword == "checksum")
{
lines.splice(i--, 1);
let checksum = Utils.generateChecksum(lines);
if (checksum && checksum != value.replace(/=+$/, ""))
return errorCallback("synchronize_checksum_mismatch");
}
}
}
- if (params.redirect)
- return redirectCallback(params.redirect);
+ // Process filters
+ lines.shift();
+ for (let line of lines)
+ parser.process(line);
hub 2017/11/13 22:35:28 I intend to parse the whole buffer, but for now we
+
+ if (parser.redirect)
+ return redirectCallback(parser.redirect);
hub 2017/11/13 22:35:28 This can be better as we should catch the redirect
// Handle redirects
let subscription = Subscription.fromURL(downloadable.redirectURL ||
downloadable.url);
if (downloadable.redirectURL &&
downloadable.redirectURL != downloadable.url)
{
let oldSubscription = Subscription.fromURL(downloadable.url);
@@ -212,84 +205,51 @@
// The download actually succeeded
subscription.lastSuccess = subscription.lastDownload = Math.round(
Date.now() / MILLIS_IN_SECOND
);
subscription.downloadStatus = "synchronize_ok";
subscription.downloadCount = downloadable.downloadCount;
subscription.errors = 0;
- // Remove lines containing parameters
- for (let i = remove.length - 1; i >= 0; i--)
- lines.splice(remove[i], 1);
-
// Process parameters
- if (params.homepage)
+ if (parser.homepage)
{
let url;
try
{
- url = new URL(params.homepage);
+ url = new URL(parser.homepage);
}
catch (e)
{
url = null;
}
if (url && (url.protocol == "http:" || url.protocol == "https:"))
subscription.homepage = url.href;
}
- if (params.title)
- {
- subscription.title = params.title;
- subscription.fixedTitle = true;
- }
+ if (minVersion)
+ subscription.requiredVersion = minVersion;
else
- subscription.fixedTitle = false;
-
- subscription.version = (params.version ? parseInt(params.version, 10) : 0);
+ delete subscription.requiredVersion;
let expirationInterval = DEFAULT_EXPIRATION_INTERVAL;
- if (params.expires)
- {
- let match = /^(\d+)\s*(h)?/.exec(params.expires);
- if (match)
- {
- let interval = parseInt(match[1], 10);
- if (match[2])
- expirationInterval = interval * MILLIS_IN_HOUR;
- else
- expirationInterval = interval * MILLIS_IN_DAY;
- }
- }
+ let expiration = parser.finalize();
+ if (expiration != 0)
+ expirationInterval = expiration;
let [
softExpiration,
hardExpiration
] = downloader.processExpirationInterval(expirationInterval);
subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND);
subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND);
- if (minVersion)
- subscription.requiredVersion = minVersion;
- else
- delete subscription.requiredVersion;
-
- // Process filters
- lines.shift();
- let filters = [];
- for (let line of lines)
- {
- line = Filter.normalize(line);
- if (line)
- filters.push(Filter.fromText(line));
- }
-
- FilterStorage.updateSubscriptionFilters(subscription, filters);
+ parser.delete();
return undefined;
},
_onDownloadError(downloadable, downloadURL, error, channelStatus,
responseStatus, redirectCallback)
{
let subscription = Subscription.fromURL(downloadable.url);
« compiled/subscription/DownloadableSubscription.cpp ('K') | « compiled/subscription/Subscription.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld