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

Delta Between Two Patch Sets: options.js

Issue 29339314: Issue 3870 - Rewrite legacy options page to use async messages (Closed)
Left Patch Set: Removed redundant spaces and imports, fixed some typos Created April 5, 2016, 11:02 a.m.
Right Patch Set: Avoid another race condition Created April 7, 2016, 1:27 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/notificationHelper.js ('k') | safari/ext/background.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 var getPref = wrapper({type: "prefs.get"}, "key"); 65 var getPref = wrapper({type: "prefs.get"}, "key");
66 var togglePref = wrapper({type: "prefs.toggle"}, "key"); 66 var togglePref = wrapper({type: "prefs.toggle"}, "key");
67 var getSubscriptions = wrapper({type: "subscriptions.get"}, 67 var getSubscriptions = wrapper({type: "subscriptions.get"},
68 "downloadable", "special"); 68 "downloadable", "special");
69 var removeSubscription = wrapper({type: "subscriptions.remove"}, "url"); 69 var removeSubscription = wrapper({type: "subscriptions.remove"}, "url");
70 var addSubscription = wrapper({type: "subscriptions.add"}, 70 var addSubscription = wrapper({type: "subscriptions.add"},
71 "url", "title", "homepage"); 71 "url", "title", "homepage");
72 var toggleSubscription = wrapper({type: "subscriptions.toggle"}, 72 var toggleSubscription = wrapper({type: "subscriptions.toggle"},
73 "url", "keepInstalled"); 73 "url", "keepInstalled");
74 var updateSubscription = wrapper({type: "subscriptions.update"}, "url"); 74 var updateSubscription = wrapper({type: "subscriptions.update"}, "url");
75 var isSubscriptionDownloading = wrapper({type: "subscriptions.isDownloading"},
76 "url");
77 var importRawFilters = wrapper({type: "filters.importRaw"}, 75 var importRawFilters = wrapper({type: "filters.importRaw"},
78 "text", "removeExisting"); 76 "text", "removeExisting");
79 var addFilter = wrapper({type: "filters.add"}, "text"); 77 var addFilter = wrapper({type: "filters.add"}, "text");
80 var getFilters = wrapper({type: "filters.get"}, "subscriptionUrl"); 78 var getFilters = wrapper({type: "filters.get"}, "subscriptionUrl");
81 var removeFilter = wrapper({type: "filters.remove"}, "text"); 79 var removeFilter = wrapper({type: "filters.remove"}, "text");
82 80
83 var i18n = ext.i18n; 81 var i18n = ext.i18n;
84 var whitelistedDomainRegexp = /^@@\|\|([^\/:]+)\^\$document$/; 82 var whitelistedDomainRegexp = /^@@\|\|([^\/:]+)\^\$document$/;
85 var delayedSubscriptionSelection = null; 83 var delayedSubscriptionSelection = null;
86 84
85 var acceptableAdsUrl;
86
87 // Loads options and sets UI elements accordingly 87 // Loads options and sets UI elements accordingly
88 function loadOptions() 88 function loadOptions()
89 { 89 {
90 // Set page title to i18n version of "Adblock Plus Options" 90 // Set page title to i18n version of "Adblock Plus Options"
91 document.title = i18n.getMessage("options"); 91 document.title = i18n.getMessage("options");
92 92
93 // Set links 93 // Set links
94 getPref("subscriptions_exceptionsurl", function(url) 94 getPref("subscriptions_exceptionsurl", function(url)
95 { 95 {
96 $("#acceptableAdsLink").attr("href", url); 96 acceptableAdsUrl = url;
97 $("#acceptableAdsLink").attr("href", acceptableAdsUrl);
97 }); 98 });
98 getDocLink("acceptable_ads", function(url) 99 getDocLink("acceptable_ads", function(url)
99 { 100 {
100 $("#acceptableAdsDocs").attr("href", url); 101 $("#acceptableAdsDocs").attr("href", url);
101 }); 102 });
102 getDocLink("filterdoc", function(url) 103 getDocLink("filterdoc", function(url)
103 { 104 {
104 setLinks("filter-must-follow-syntax", url); 105 setLinks("filter-must-follow-syntax", url);
105 }); 106 });
106 getInfo("application", function(application) 107 getInfo("application", function(application)
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 }); 156 });
156 getPref("notifications_showui", function(notifications_showui) 157 getPref("notifications_showui", function(notifications_showui)
157 { 158 {
158 if (!notifications_showui) 159 if (!notifications_showui)
159 document.getElementById("shouldShowNotificationsContainer").hidden = true; 160 document.getElementById("shouldShowNotificationsContainer").hidden = true;
160 }); 161 });
161 162
162 // Register listeners in the background message responder 163 // Register listeners in the background message responder
163 ext.backgroundPage.sendMessage({ 164 ext.backgroundPage.sendMessage({
164 type: "app.listen", 165 type: "app.listen",
165 filter: ["addSubscription", "switchToOptionsSection"] 166 filter: ["addSubscription", "focusSection"]
166 }); 167 });
167 ext.backgroundPage.sendMessage( 168 ext.backgroundPage.sendMessage(
168 { 169 {
169 type: "filters.listen", 170 type: "filters.listen",
170 filter: ["added", "loaded", "removed"] 171 filter: ["added", "loaded", "removed"]
171 }); 172 });
172 ext.backgroundPage.sendMessage( 173 ext.backgroundPage.sendMessage(
173 { 174 {
174 type: "prefs.listen", 175 type: "prefs.listen",
175 filter: ["notifications_ignoredcategories", "notifications_showui", 176 filter: ["notifications_ignoredcategories", "notifications_showui",
176 "safari_contentblocker", "show_devtools_panel", 177 "safari_contentblocker", "show_devtools_panel",
177 "shouldShowBlockElementMenu"] 178 "shouldShowBlockElementMenu"]
178 }); 179 });
179 ext.backgroundPage.sendMessage( 180 ext.backgroundPage.sendMessage(
180 { 181 {
181 type: "subscriptions.listen", 182 type: "subscriptions.listen",
182 filter: ["added", "disabled", "homepage", "lastDownload", "removed", 183 filter: ["added", "disabled", "homepage", "lastDownload", "removed",
183 "title"] 184 "title", "downloadStatus", "downloading"]
184 }); 185 });
185 186
186 // Load recommended subscriptions 187 // Load recommended subscriptions
187 loadRecommendations(); 188 loadRecommendations();
188 189
189 // Show user's filters 190 // Show user's filters
190 reloadFilters(); 191 reloadFilters();
191 } 192 }
192 $(loadOptions); 193 $(loadOptions);
193 194
194 // Reloads the displayed subscriptions and filters 195 // Reloads the displayed subscriptions and filters
195 function reloadFilters() 196 function reloadFilters()
196 { 197 {
197 // Load user filter URLs 198 // Load user filter URLs
198 var container = document.getElementById("filterLists"); 199 var container = document.getElementById("filterLists");
199 while (container.lastChild) 200 while (container.lastChild)
200 container.removeChild(container.lastChild); 201 container.removeChild(container.lastChild);
201 202
202 getPref("subscriptions_exceptionsurl", function(acceptableAdsUrl) 203 getSubscriptions(true, false, function(subscriptions)
203 { 204 {
204 getSubscriptions(true, false, function(subscriptions) 205 for (var i = 0; i < subscriptions.length; i++)
205 { 206 {
206 for (var i = 0; i < subscriptions.length; i++) 207 var subscription = subscriptions[i];
207 { 208 if (subscription.url == acceptableAdsUrl)
208 var subscription = subscriptions[i]; 209 $("#acceptableAds").prop("checked", !subscription.disabled);
209 if (subscription.url == acceptableAdsUrl) 210 else
210 $("#acceptableAds").prop("checked", !subscription.disabled); 211 addSubscriptionEntry(subscription);
211 else 212 }
212 addSubscriptionEntry(subscription);
213 }
214 });
215 }); 213 });
216 214
217 // User-entered filters 215 // User-entered filters
218 getSubscriptions(false, true, function(subscriptions) 216 getSubscriptions(false, true, function(subscriptions)
219 { 217 {
220 clearListBox("userFiltersBox"); 218 clearListBox("userFiltersBox");
221 clearListBox("excludedDomainsBox"); 219 clearListBox("excludedDomainsBox");
222 220
223 for (var i = 0; i < subscriptions.length; i++) 221 for (var i = 0; i < subscriptions.length; i++)
224 getFilters(subscriptions[i].url, function(filters) 222 getFilters(subscriptions[i].url, function(filters)
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 addSubscription(url, title, null); 385 addSubscription(url, title, null);
388 } 386 }
389 387
390 $("#addSubscriptionContainer").hide(); 388 $("#addSubscriptionContainer").hide();
391 $("#customSubscriptionContainer").hide(); 389 $("#customSubscriptionContainer").hide();
392 $("#addSubscriptionButton").show(); 390 $("#addSubscriptionButton").show();
393 } 391 }
394 392
395 function toggleAcceptableAds() 393 function toggleAcceptableAds()
396 { 394 {
397 getPref("subscriptions_exceptionsurl", function(url) 395 toggleSubscription(acceptableAdsUrl, true);
398 {
399 toggleSubscription(url, true);
400 });
401 } 396 }
402 397
403 function findSubscriptionElement(subscription) 398 function findSubscriptionElement(subscription)
404 { 399 {
405 var children = document.getElementById("filterLists").childNodes; 400 var children = document.getElementById("filterLists").childNodes;
406 for (var i = 0; i < children.length; i++) 401 for (var i = 0; i < children.length; i++)
407 if (children[i]._subscription.url == subscription.url) 402 if (children[i]._subscription.url == subscription.url)
408 return children[i]; 403 return children[i];
409 return null; 404 return null;
410 } 405 }
(...skipping 12 matching lines...) Expand all
423 title.href = subscription.homepage; 418 title.href = subscription.homepage;
424 else 419 else
425 title.href = subscription.url; 420 title.href = subscription.url;
426 421
427 var enabled = element.getElementsByClassName("subscriptionEnabled")[0]; 422 var enabled = element.getElementsByClassName("subscriptionEnabled")[0];
428 enabled.checked = !subscription.disabled; 423 enabled.checked = !subscription.disabled;
429 424
430 var lastUpdate = element.getElementsByClassName("subscriptionUpdate")[0]; 425 var lastUpdate = element.getElementsByClassName("subscriptionUpdate")[0];
431 lastUpdate.classList.remove("error"); 426 lastUpdate.classList.remove("error");
432 427
433 if (subscription.downloadStatus && 428 var downloadStatus = subscription.downloadStatus;
434 subscription.downloadStatus != "synchronize_ok") 429 if (subscription.isDownloading)
430 {
431 lastUpdate.textContent = i18n.getMessage("filters_subscription_lastDownload_ inProgress");
432 }
433 else if (downloadStatus && downloadStatus != "synchronize_ok")
435 { 434 {
436 var map = 435 var map =
437 { 436 {
438 "synchronize_invalid_url": "filters_subscription_lastDownload_invalidURL" , 437 "synchronize_invalid_url": "filters_subscription_lastDownload_invalidURL" ,
439 "synchronize_connection_error": "filters_subscription_lastDownload_connec tionError", 438 "synchronize_connection_error": "filters_subscription_lastDownload_connec tionError",
440 "synchronize_invalid_data": "filters_subscription_lastDownload_invalidDat a", 439 "synchronize_invalid_data": "filters_subscription_lastDownload_invalidDat a",
441 "synchronize_checksum_mismatch": "filters_subscription_lastDownload_check sumMismatch" 440 "synchronize_checksum_mismatch": "filters_subscription_lastDownload_check sumMismatch"
442 }; 441 };
443 if (subscription.downloadStatus in map) 442 if (downloadStatus in map)
444 lastUpdate.textContent = i18n.getMessage(map[subscription.downloadStatus] ); 443 lastUpdate.textContent = i18n.getMessage(map[downloadStatus]);
445 else 444 else
446 lastUpdate.textContent = subscription.downloadStatus; 445 lastUpdate.textContent = downloadStatus;
447 lastUpdate.classList.add("error"); 446 lastUpdate.classList.add("error");
448 } 447 }
449 else if (subscription.lastDownload > 0) 448 else if (subscription.lastDownload > 0)
450 { 449 {
451 var timeDate = i18n_timeDateStrings(subscription.lastDownload * 1000); 450 var timeDate = i18n_timeDateStrings(subscription.lastDownload * 1000);
452 var messageID = (timeDate[1] ? "last_updated_at" : "last_updated_at_today"); 451 var messageID = (timeDate[1] ? "last_updated_at" : "last_updated_at_today");
453 lastUpdate.textContent = i18n.getMessage(messageID, timeDate); 452 lastUpdate.textContent = i18n.getMessage(messageID, timeDate);
454 } 453 }
455 } 454 }
456 455
457 function onSubscriptionMessage(action, subscription) 456 function onSubscriptionMessage(action, subscription)
458 { 457 {
458 var element = findSubscriptionElement(subscription);
459
459 switch (action) 460 switch (action)
460 { 461 {
461 case "title":
462 case "disabled": 462 case "disabled":
463 case "downloading":
464 case "downloadStatus":
463 case "homepage": 465 case "homepage":
464 case "lastDownload": 466 case "lastDownload":
465 case "downloadStatus": 467 case "title":
466 var element = findSubscriptionElement(subscription);
467 if (element) 468 if (element)
468 updateSubscriptionInfo(element, subscription); 469 updateSubscriptionInfo(element, subscription);
469 break; 470 break;
470 case "added": 471 case "added":
471 getPref("subscriptions_exceptionsurl", function(acceptableAdsUrl) 472 if (subscription.url == acceptableAdsUrl)
472 { 473 $("#acceptableAds").prop("checked", true);
473 if (subscription.url == acceptableAdsUrl) 474 else if (!element)
474 $("#acceptableAds").prop("checked", true); 475 addSubscriptionEntry(subscription);
475 else if (!findSubscriptionElement(subscription))
476 addSubscriptionEntry(subscription);
477 });
478 break; 476 break;
479 case "removed": 477 case "removed":
480 getPref("subscriptions_exceptionsurl", function(acceptableAdsUrl) 478 if (subscription.url == acceptableAdsUrl)
481 { 479 $("#acceptableAds").prop("checked", false);
482 if (subscription.url == acceptableAdsUrl) 480 else if (element)
483 { 481 element.parentNode.removeChild(element);
484 $("#acceptableAds").prop("checked", false);
485 }
486 else
487 {
488 var element = findSubscriptionElement(subscription);
489 if (element)
490 element.parentNode.removeChild(element);
491 }
492 });
493 break; 482 break;
494 } 483 }
495 } 484 }
496 485
497 function onPrefMessage(key, value) 486 function onPrefMessage(key, value)
498 { 487 {
499 switch (key) 488 switch (key)
500 { 489 {
501 case "notifications_showui": 490 case "notifications_showui":
502 document.getElementById("shouldShowNotificationsContainer").hidden = !valu e; 491 document.getElementById("shouldShowNotificationsContainer").hidden = !valu e;
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
648 else 637 else
649 $("#rawFilters").hide(); 638 $("#rawFilters").hide();
650 }); 639 });
651 } 640 }
652 641
653 // Called when user explicitly requests filter list updates 642 // Called when user explicitly requests filter list updates
654 function updateFilterLists() 643 function updateFilterLists()
655 { 644 {
656 // Without the URL parameter this will update all subscriptions 645 // Without the URL parameter this will update all subscriptions
657 updateSubscription(); 646 updateSubscription();
658
659 // Display a message for any subscriptions that are currently being downloaded
660 getSubscriptions(true, false, function(subscriptions)
661 {
662 var inProgressMessage = i18n.getMessage(
663 "filters_subscription_lastDownload_inProgress"
664 );
665
666 for (var i = 0; i < subscriptions.length; i += 1)
667 {
668 var element = findSubscriptionElement(subscriptions[i]);
669 if (element)
670 {
671 var label = element.getElementsByClassName("subscriptionUpdate")[0];
672 isSubscriptionDownloading(subscriptions[i].url,
673 function(label, isDownloading)
674 {
675 if (isDownloading)
676 label.textContent = inProgressMessage;
677 }.bind(undefined, label)
678 );
679 }
680 }
681 });
682 } 647 }
683 648
684 // Adds a subscription entry to the UI. 649 // Adds a subscription entry to the UI.
685 function addSubscriptionEntry(subscription) 650 function addSubscriptionEntry(subscription)
686 { 651 {
687 var template = document.getElementById("subscriptionTemplate"); 652 var template = document.getElementById("subscriptionTemplate");
688 var element = template.cloneNode(true); 653 var element = template.cloneNode(true);
689 element.removeAttribute("id"); 654 element.removeAttribute("id");
690 element._subscription = subscription; 655 element._subscription = subscription;
691 656
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 { 709 {
745 switch (message.type) 710 switch (message.type)
746 { 711 {
747 case "app.respond": 712 case "app.respond":
748 switch (message.action) 713 switch (message.action)
749 { 714 {
750 case "addSubscription": 715 case "addSubscription":
751 var subscription = message.args[0]; 716 var subscription = message.args[0];
752 startSubscriptionSelection(subscription.title, subscription.url); 717 startSubscriptionSelection(subscription.title, subscription.url);
753 break; 718 break;
754 case "switchToOptionsSection": 719 case "focusSection":
755 var tabs = document.getElementsByClassName("ui-tabs-panel"); 720 var tabs = document.getElementsByClassName("ui-tabs-panel");
756 for (var i = 0; i < tabs.length; i++) 721 for (var i = 0; i < tabs.length; i++)
757 { 722 {
758 var found = tabs[i].querySelector( 723 var found = tabs[i].querySelector(
759 "[data-section='" + message.args[0] + "']" 724 "[data-section='" + message.args[0] + "']"
760 ); 725 );
761 if (!found) 726 if (!found)
762 continue; 727 continue;
763 728
764 var previous = document.getElementsByClassName("focused"); 729 var previous = document.getElementsByClassName("focused");
(...skipping 11 matching lines...) Expand all
776 onFilterMessage(message.action, message.args[0]); 741 onFilterMessage(message.action, message.args[0]);
777 break; 742 break;
778 case "prefs.respond": 743 case "prefs.respond":
779 onPrefMessage(message.action, message.args[0]); 744 onPrefMessage(message.action, message.args[0]);
780 break; 745 break;
781 case "subscriptions.respond": 746 case "subscriptions.respond":
782 onSubscriptionMessage(message.action, message.args[0]); 747 onSubscriptionMessage(message.action, message.args[0]);
783 break; 748 break;
784 } 749 }
785 }); 750 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld