LEFT | RIGHT |
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 20 matching lines...) Expand all Loading... |
31 function onLegacyAPISupported(msg, sender) | 31 function onLegacyAPISupported(msg, sender) |
32 { | 32 { |
33 port.off("safari.legacyAPISupported", onLegacyAPISupported); | 33 port.off("safari.legacyAPISupported", onLegacyAPISupported); |
34 resolve(msg.legacyAPISupported); | 34 resolve(msg.legacyAPISupported); |
35 } | 35 } |
36 port.on("safari.legacyAPISupported", onLegacyAPISupported); | 36 port.on("safari.legacyAPISupported", onLegacyAPISupported); |
37 }); | 37 }); |
38 let contentBlockingActive = false; | 38 let contentBlockingActive = false; |
39 let afterContentBlockingFinished = null; | 39 let afterContentBlockingFinished = null; |
40 let contentBlockListDirty = true; | 40 let contentBlockListDirty = true; |
41 let lastSetContentBlockerResult; | 41 let lastSetContentBlockerError; |
42 | 42 |
43 function clearBlockCounters() | 43 function clearBlockCounters() |
44 { | 44 { |
45 ext.pages.query({}, pages => | 45 ext.pages.query({}, pages => |
46 { | 46 { |
47 for (let page of pages) | 47 for (let page of pages) |
48 page.browserAction.setBadge(); | 48 page.browserAction.setBadge(); |
49 }); | 49 }); |
50 } | 50 } |
51 | 51 |
52 function setContentBlocker(callback) | 52 function setContentBlocker(callback) |
53 { | 53 { |
54 // setContentBlocker returns null if given the same blocklist as last time, | 54 // When given the same rules as last time setContentBlocker will always give |
55 // even when there was an error. (It's also wasteful to re-generate the | 55 // null (success) to the callback, even when there was actually an error. We |
56 // blocklist when nothing has changed!) | 56 // cache the last result therefore so that we can provide a consistent result |
| 57 // and also to avoid wastefully regenerating an identical blocklist. |
57 if (!contentBlockListDirty) | 58 if (!contentBlockListDirty) |
58 { | 59 { |
59 callback(lastSetContentBlockerResult); | 60 callback(lastSetContentBlockerError); |
60 return; | 61 return; |
61 } | 62 } |
62 | 63 |
63 let contentBlockerList = new ContentBlockerList(); | 64 let contentBlockerList = new ContentBlockerList(); |
64 for (let subscription of FilterStorage.subscriptions) | 65 for (let subscription of FilterStorage.subscriptions) |
65 if (!subscription.disabled) | 66 if (!subscription.disabled) |
66 for (let filter of subscription.filters) | 67 for (let filter of subscription.filters) |
67 contentBlockerList.addFilter(filter); | 68 contentBlockerList.addFilter(filter); |
68 | 69 |
69 contentBlockListDirty = false; | 70 contentBlockListDirty = false; |
70 safari.extension.setContentBlocker( | 71 safari.extension.setContentBlocker( |
71 // setContentBlocker seems to not work in Safari 9 unless the rules are | 72 // There is a strange bug in setContentBlocker for Safari 9 where if both |
72 // converted to JSON first. (An error is thrown: | 73 // the callback parameter is provided and the rules aren't converted to a |
73 // "Extension compilation failed: Failed to parse the JSON String.") | 74 // JSON string it fails. Worse still it actually performs the callback twice |
| 75 // too, firstly with an empty string and then with an Error: |
| 76 // "Extension compilation failed: Failed to parse the JSON String." |
| 77 // To mitigate this we convert the rules to JSON here and also ignore |
| 78 // callback values of "". (Usually the callback is performed with either |
| 79 // null for success or an Error on failure.) |
| 80 // Bug #26322821 filed on bugreport.apple.com |
74 JSON.stringify(contentBlockerList.generateRules()), | 81 JSON.stringify(contentBlockerList.generateRules()), |
75 function (result) | 82 function(error) |
76 { | 83 { |
77 // Safari 9 performs the callback twice under some conditions, first with | 84 if (error == "") |
78 // an empty string and then with an Error! | |
79 if (result == "") | |
80 return; | 85 return; |
81 | 86 |
82 lastSetContentBlockerResult = result; | 87 lastSetContentBlockerError = error; |
83 callback(result); | 88 callback(error); |
84 } | 89 } |
85 ); | 90 ); |
86 } | 91 } |
87 | 92 |
88 function updateContentBlocker(isStartup, legacyAPISupported) | 93 function updateContentBlocker(isStartup, legacyAPISupported) |
89 { | 94 { |
90 afterContentBlockingFinished = new Promise(resolve => | 95 afterContentBlockingFinished = new Promise(resolve => |
91 { | 96 { |
92 setContentBlocker(result => | 97 setContentBlocker(error => |
93 { | 98 { |
94 if (result instanceof Error) | 99 if (error instanceof Error) |
95 { | 100 { |
96 let suppressErrorMessage = false; | 101 let suppressErrorMessage = false; |
97 | 102 |
98 // If the content blocking API fails the first time it's used the | 103 // If the content blocking API fails the first time it's used the |
99 // legacy blocking API (if available) won't have been disabled. | 104 // legacy blocking API (if available) won't have been disabled. |
100 if (!contentBlockingActive && legacyAPISupported) | 105 if (!contentBlockingActive && legacyAPISupported) |
101 { | 106 { |
102 Prefs.safariContentBlocker = false; | 107 Prefs.safariContentBlocker = false; |
103 // If content blocking failed on startup and we're switching back to | 108 // If content blocking failed on startup and we're switching back to |
104 // the legacy API anyway we don't need to show an error message. | 109 // the legacy API anyway we don't need to show an error message. |
105 if (isStartup) | 110 if (isStartup) |
106 suppressErrorMessage = true; | 111 suppressErrorMessage = true; |
107 } | 112 } |
108 | 113 |
109 if (!suppressErrorMessage) | 114 if (!suppressErrorMessage) |
110 alert(result.message); | 115 alert(error.message); |
111 } | 116 } |
112 else if (!contentBlockingActive) | 117 else if (!contentBlockingActive) |
113 { | 118 { |
114 contentBlockingActive = true; | 119 contentBlockingActive = true; |
115 clearBlockCounters(); | 120 clearBlockCounters(); |
116 } | 121 } |
117 | 122 |
118 resolve(contentBlockingActive); | 123 resolve(contentBlockingActive); |
119 afterContentBlockingFinished = null; | 124 afterContentBlockingFinished = null; |
120 }); | 125 }); |
(...skipping 27 matching lines...) Expand all Loading... |
148 }); | 153 }); |
149 }); | 154 }); |
150 } | 155 } |
151 | 156 |
152 port.on("safari.contentBlockingActive", (msg, sender) => | 157 port.on("safari.contentBlockingActive", (msg, sender) => |
153 { | 158 { |
154 if (!contentBlockingActive && afterContentBlockingFinished) | 159 if (!contentBlockingActive && afterContentBlockingFinished) |
155 return afterContentBlockingFinished; | 160 return afterContentBlockingFinished; |
156 return contentBlockingActive; | 161 return contentBlockingActive; |
157 }); | 162 }); |
LEFT | RIGHT |