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

Unified Diff: messageResponder.js

Issue 29321198: Issue 2376 - Implement custom filters in new options page (Closed)
Patch Set: Error handling Created July 13, 2015, 2:03 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
« no previous file with comments | « locale/en-US/options.json ('k') | options.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: messageResponder.js
===================================================================
--- a/messageResponder.js
+++ b/messageResponder.js
@@ -28,8 +28,11 @@
var filterClasses = require("filterClasses");
var Filter = filterClasses.Filter;
+ var WhitelistFilter = filterClasses.WhitelistFilter
var BlockingFilter = filterClasses.BlockingFilter;
var Synchronizer = require("synchronizer").Synchronizer;
+ var parseFilters = require("filterValidation").parseFilters;
+ var parseFilter = require("filterValidation").parseFilter;
var subscriptionClasses = require("subscriptionClasses");
var Subscription = subscriptionClasses.Subscription;
@@ -46,6 +49,8 @@
var convertSubscription = convertObject.bind(null, ["disabled",
"downloadStatus", "homepage", "lastSuccess", "title", "url"]);
+ var convertFilterParsingError = convertObject.bind(null, ["type",
+ "reason", "selector"]);
var convertFilter = convertObject.bind(null, ["text"]);
var changeListeners = null;
@@ -55,6 +60,15 @@
"subscription": "subscriptions.listen"
};
+ function sendMessage(page, type, action, args)
+ {
+ page.sendMessage({
+ type: type,
+ action: action,
+ args: args
+ });
+ }
+
function onFilterChange(action)
{
if (action == "load")
@@ -91,16 +105,26 @@
{
var filters = changeListeners.get(pages[i]);
if (filters[type] && filters[type].indexOf(action) >= 0)
- {
- pages[i].sendMessage({
- type: messageTypes[type],
- action: action,
- args: args
- });
- }
+ sendMessage(pages[i], messageTypes[type], action, args);
}
};
+ function sendError(error, page)
+ {
+ var type = "app";
+ var action = "error";
saroyanm 2015/07/13 14:09:52 @Thomas for Error handling, is it something that u
Thomas Greiner 2015/07/13 15:54:21 The user is subscribing to it with the "app.listen
+ console.log(error);
+ error = convertFilterParsingError(error);
+ if (!page)
+ {
+ var pages = changeListeners.keys();
+ for (var i = 0; i < pages.length; i++)
+ sendMessage(pages[i], messageTypes[type], action, error);
+ }
+ else
+ sendMessage(page, messageTypes[type], action, [error]);
+ }
+
global.ext.onMessage.addListener(function(message, sender, callback)
{
var listenerFilters = null;
@@ -195,12 +219,58 @@
callback(subscription.filters.map(convertFilter));
break;
+ case "filters.importRaw":
+ var result = parseFilters(message.text);
+ var errors = result.errors.filter(function(error)
+ {
+ return error.type != "unexpected-filter-list-header";
+ });
+
+ if (errors.length > 0)
+ {
+ sendError(errors.join("\n"), sender.page);
+ return;
+ }
+
+ var seenFilter = Object.create(null);
+ for (var i = 0; i < result.filters.length; i++)
+ {
+ var filter = result.filters[i];
+ FilterStorage.addFilter(filter);
+ seenFilter[filter.text] = null;
+ }
+
+ for (var i = 0; i < FilterStorage.subscriptions.length; i++)
+ {
+ var subscription = FilterStorage.subscriptions[i];
+ if (!(subscription instanceof SpecialSubscription))
+ continue;
+
+ var filters = subscription.filters.slice();
+ for (var j = 0; j < filters.length; j++)
+ {
+ var filter = filters[j];
+ if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text))
+ continue;
+
+ if (!(filter.text in seenFilter))
+ FilterStorage.removeFilter(filter);
+ }
+ }
+ break;
case "filters.listen":
if (message.filter)
listenerFilters.filter = message.filter;
else
delete listenerFilters.filter;
break;
+ case "filters.parse":
+ var result = parseFilter(message.text);
+ if (result.error)
+ sendError(result.error, sender.page);
+ else
+ FilterStorage.addFilter(result.filter);
+ break;
case "filters.remove":
var filter = Filter.fromText(message.text);
var subscription = null;
« no previous file with comments | « locale/en-US/options.json ('k') | options.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld