 Issue 29705719:
  Issue 6402 - Split filter hit / request logging out into own API  (Closed)
    
  
    Issue 29705719:
  Issue 6402 - Split filter hit / request logging out into own API  (Closed) 
  | Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| 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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 | 
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. | 
| 13 * | 13 * | 
| 14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 
| 16 */ | 16 */ | 
| 17 | 17 | 
| 18 /** @module devLogger */ | |
| 
Sebastian Noack
2018/03/20 01:28:26
I'm not too happy with the name "devLogger". It is
 
kzar
2018/03/21 15:18:45
I agree the name sucked but I'm also struggling to
 | |
| 19 | |
| 18 "use strict"; | 20 "use strict"; | 
| 19 | 21 | 
| 20 const {RegExpFilter, | 22 const {RegExpFilter, | 
| 21 WhitelistFilter, | 23 WhitelistFilter, | 
| 22 ElemHideFilter} = require("filterClasses"); | 24 ElemHideFilter} = require("filterClasses"); | 
| 23 const {SpecialSubscription} = require("subscriptionClasses"); | 25 const {SpecialSubscription} = require("subscriptionClasses"); | 
| 24 const {FilterStorage} = require("filterStorage"); | 26 const {FilterStorage} = require("filterStorage"); | 
| 25 const {defaultMatcher} = require("matcher"); | 27 const {defaultMatcher} = require("matcher"); | 
| 26 const {FilterNotifier} = require("filterNotifier"); | 28 const {FilterNotifier} = require("filterNotifier"); | 
| 27 const {extractHostFromFrame} = require("url"); | 29 const {extractHostFromFrame} = require("url"); | 
| 28 const {port} = require("messaging"); | 30 const {port} = require("messaging"); | 
| 29 | 31 | 
| 30 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"]; | 32 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"]; | 
| 31 | 33 | 
| 32 // Mapping of inspected tabs to their devpanel page | 34 // Mapping of inspected tabs to their listeners | 
| 33 // and recorded items. We can't use a PageMap here, | 35 // and recorded items. We can't use a PageMap here, | 
| 34 // because data must persist after navigation/reload. | 36 // because data must persist after navigation/reload. | 
| 35 let panels = new Map(); | 37 let pageLoggers = new Map(); | 
| 36 | 38 | 
| 37 function isActivePanel(panel) | 39 function isActivePageLogger(pageLogger) | 
| 38 { | 40 { | 
| 39 return panel && !panel.reload && !panel.reloading; | 41 return pageLogger && !pageLogger.reload && !pageLogger.reloading; | 
| 40 } | 42 } | 
| 41 | 43 | 
| 42 function getActivePanel(page) | 44 function getActivePageLogger(pageId) | 
| 43 { | 45 { | 
| 44 let panel = panels.get(page.id); | 46 let pageLogger = pageLoggers.get(pageId); | 
| 45 if (isActivePanel(panel)) | 47 if (isActivePageLogger(pageLogger)) | 
| 46 return panel; | 48 return pageLogger; | 
| 47 return null; | 49 return null; | 
| 48 } | 50 } | 
| 49 | 51 | 
| 50 function getFilterInfo(filter) | 52 function getFilterInfo(filter) | 
| 51 { | 53 { | 
| 52 if (!filter) | 54 if (!filter) | 
| 53 return null; | 55 return null; | 
| 54 | 56 | 
| 55 let userDefined = false; | 57 let userDefined = false; | 
| 56 let subscriptionTitle = null; | 58 let subscriptionTitle = null; | 
| (...skipping 10 matching lines...) Expand all Loading... | |
| 67 } | 69 } | 
| 68 | 70 | 
| 69 return { | 71 return { | 
| 70 text: filter.text, | 72 text: filter.text, | 
| 71 whitelisted: filter instanceof WhitelistFilter, | 73 whitelisted: filter instanceof WhitelistFilter, | 
| 72 userDefined, | 74 userDefined, | 
| 73 subscription: subscriptionTitle | 75 subscription: subscriptionTitle | 
| 74 }; | 76 }; | 
| 75 } | 77 } | 
| 76 | 78 | 
| 77 function hasRecord(panel, request, filter) | 79 function hasRecord(pageLogger, request, filter) | 
| 78 { | 80 { | 
| 79 return panel.records.some(record => | 81 return pageLogger.records.some(record => | 
| 80 record.request.url == request.url && | 82 record.request.url == request.url && | 
| 81 record.request.docDomain == request.docDomain && | 83 record.request.docDomain == request.docDomain && | 
| 82 | 84 | 
| 83 // Ignore partial (e.g. ELEMHIDE) whitelisting if there is already | 85 // Ignore partial (e.g. ELEMHIDE) whitelisting if there is already | 
| 84 // a DOCUMENT exception which disables all means of blocking. | 86 // a DOCUMENT exception which disables all means of blocking. | 
| 85 (record.request.type == "DOCUMENT" ? | 87 (record.request.type == "DOCUMENT" ? | 
| 86 nonRequestTypes.includes(request.type) : | 88 nonRequestTypes.includes(request.type) : | 
| 87 record.request.type == request.type) && | 89 record.request.type == request.type) && | 
| 88 | 90 | 
| 89 // Matched element hiding filters don't relate to a particular request, | 91 // Matched element hiding filters don't relate to a particular request, | 
| 90 // so we have to compare the selector in order to avoid duplicates. | 92 // so we have to compare the selector in order to avoid duplicates. | 
| 91 (record.filter && record.filter.selector) == (filter && filter.selector) | 93 (record.filter && record.filter.selector) == (filter && filter.selector) | 
| 92 ); | 94 ); | 
| 93 } | 95 } | 
| 94 | 96 | 
| 95 function addRecord(panel, request, filter) | 97 function emit(pageLogger, ...args) | 
| 96 { | 98 { | 
| 97 if (!hasRecord(panel, request, filter)) | 99 for (let listener of pageLogger.listeners.slice()) | 
| 100 listener(...args); | |
| 101 } | |
| 102 | |
| 103 function addRecord(pageLogger, request, filter) | |
| 104 { | |
| 105 if (!hasRecord(pageLogger, request, filter)) | |
| 98 { | 106 { | 
| 99 panel.port.postMessage({ | 107 emit(pageLogger, { | 
| 100 type: "add-record", | 108 type: "add-record", | 
| 
Sebastian Noack
2018/03/20 01:28:25
This abstraction is specific to the devtools panel
 | |
| 101 request, | 109 request, | 
| 102 filter: getFilterInfo(filter) | 110 filter: getFilterInfo(filter) | 
| 103 }); | 111 }); | 
| 104 | 112 | 
| 105 panel.records.push({request, filter}); | 113 pageLogger.records.push({request, filter}); | 
| 106 } | 114 } | 
| 107 } | 115 } | 
| 108 | 116 | 
| 109 function matchRequest(request) | 117 function matchRequest(request) | 
| 110 { | 118 { | 
| 111 return defaultMatcher.matchesAny( | 119 return defaultMatcher.matchesAny( | 
| 112 request.url, | 120 request.url, | 
| 113 RegExpFilter.typeMap[request.type], | 121 RegExpFilter.typeMap[request.type], | 
| 114 request.docDomain, | 122 request.docDomain, | 
| 115 request.thirdParty, | 123 request.thirdParty, | 
| 116 request.sitekey, | 124 request.sitekey, | 
| 117 request.specificOnly | 125 request.specificOnly | 
| 118 ); | 126 ); | 
| 119 } | 127 } | 
| 120 | 128 | 
| 121 /** | 129 /** | 
| 122 * Logs a request to the devtools panel. | 130 * Logs a request for a page if there's something (e.g. devtools panel) | 
| 131 * listening. | |
| 123 * | 132 * | 
| 124 * @param {?Page} page The page the request occured on or null if | 133 * @param {?Page} page The page the request occured on or null if | 
| 
Sebastian Noack
2018/03/20 01:28:25
It seems you turned all other "page" parameters in
 
kzar
2018/03/21 15:18:45
Done.
 | |
| 125 * the request isn't associated with a page | 134 * the request isn't associated with a page | 
| 126 * @param {string} url The URL of the request | 135 * @param {string} url The URL of the request | 
| 127 * @param {string} type The request type | 136 * @param {string} type The request type | 
| 128 * @param {string} docDomain The IDN-decoded hostname of the document | 137 * @param {string} docDomain The IDN-decoded hostname of the document | 
| 129 * @param {boolean} thirdParty Whether the origin of the request and | 138 * @param {boolean} thirdParty Whether the origin of the request and | 
| 130 * document differs | 139 * document differs | 
| 131 * @param {?string} sitekey The active sitekey if there is any | 140 * @param {?string} sitekey The active sitekey if there is any | 
| 132 * @param {?boolean} specificOnly Whether generic filters should be ignored | 141 * @param {?boolean} specificOnly Whether generic filters should be ignored | 
| 133 * @param {?BlockingFilter} filter The matched filter or null if there is no | 142 * @param {?BlockingFilter} filter The matched filter or null if there is no | 
| 134 * match | 143 * match | 
| 135 */ | 144 */ | 
| 136 exports.logRequest = function(page, url, type, docDomain, | 145 exports.logRequest = function(page, url, type, docDomain, thirdParty, sitekey, | 
| 137 thirdParty, sitekey, | |
| 138 specificOnly, filter) | 146 specificOnly, filter) | 
| 139 { | 147 { | 
| 140 if (panels.size == 0) | 148 if (pageLoggers.size == 0) | 
| 141 return; | 149 return; | 
| 142 | 150 | 
| 143 let request = {url, type, docDomain, thirdParty, sitekey, specificOnly}; | 151 let request = {url, type, docDomain, thirdParty, sitekey, specificOnly, | 
| 144 for (let [tabId, panel] of panels) | 152 pageId: page ? page.id : -1}; | 
| 145 if ((!page || page.id == tabId) && isActivePanel(panel)) | 153 for (let [pageId, pageLogger] of pageLoggers) | 
| 146 addRecord(panel, request, filter); | 154 if ((!page || page.id == pageId) && isActivePageLogger(pageLogger)) | 
| 155 addRecord(pageLogger, request, filter); | |
| 147 }; | 156 }; | 
| 148 | 157 | 
| 149 /** | 158 /** | 
| 150 * Logs active element hiding filters to the devtools panel. | 159 * Logs active element hiding filters for a page if there's something's | 
| 160 * (e.g. devtools panel) listening. | |
| 151 * | 161 * | 
| 152 * @param {Page} page The page the elements were hidden on | 162 * @param {int} pageId The page the elements were hidden on | 
| 153 * @param {string[]} selectors The selectors of applied ElemHideFilters | 163 * @param {string[]} selectors The selectors of applied ElemHideFilters | 
| 154 * @param {string[]} filters The text of applied ElemHideEmulationFilters | 164 * @param {string[]} filters The text of applied ElemHideEmulationFilters | 
| 155 * @param {string} docDomain The IDN-decoded hostname of the document | 165 * @param {string} docDomain The IDN-decoded hostname of the document | 
| 156 */ | 166 */ | 
| 157 function logHiddenElements(page, selectors, filters, docDomain) | 167 exports.logHiddenElements = function(pageId, selectors, filters, docDomain) | 
| 158 { | 168 { | 
| 159 let panel = getActivePanel(page); | 169 let pageLogger = getActivePageLogger(pageId); | 
| 160 if (panel) | 170 if (pageLogger) | 
| 161 { | 171 { | 
| 162 for (let subscription of FilterStorage.subscriptions) | 172 for (let subscription of FilterStorage.subscriptions) | 
| 163 { | 173 { | 
| 164 if (subscription.disabled) | 174 if (subscription.disabled) | 
| 165 continue; | 175 continue; | 
| 166 | 176 | 
| 167 for (let filter of subscription.filters) | 177 for (let filter of subscription.filters) | 
| 168 { | 178 { | 
| 169 // We only know the exact filter in case of element hiding emulation. | 179 // We only know the exact filter in case of element hiding emulation. | 
| 170 // For regular element hiding filters, the content script only knows | 180 // For regular element hiding filters, the content script only knows | 
| 171 // the selector, so we have to find a filter that has an identical | 181 // the selector, so we have to find a filter that has an identical | 
| 172 // selector and is active on the domain the match was reported from. | 182 // selector and is active on the domain the match was reported from. | 
| 173 let isActiveElemHideFilter = filter instanceof ElemHideFilter && | 183 let isActiveElemHideFilter = filter instanceof ElemHideFilter && | 
| 174 selectors.includes(filter.selector) && | 184 selectors.includes(filter.selector) && | 
| 175 filter.isActiveOnDomain(docDomain); | 185 filter.isActiveOnDomain(docDomain); | 
| 176 | 186 | 
| 177 if (isActiveElemHideFilter || filters.includes(filter.text)) | 187 if (isActiveElemHideFilter || filters.includes(filter.text)) | 
| 178 addRecord(panel, {type: "ELEMHIDE", docDomain}, filter); | 188 addRecord(pageLogger, {type: "ELEMHIDE", docDomain}, filter); | 
| 179 } | 189 } | 
| 180 } | 190 } | 
| 181 } | 191 } | 
| 182 } | 192 }; | 
| 183 | 193 | 
| 184 /** | 194 /** | 
| 185 * Logs a whitelisting filter, that disables (some kind of) | 195 * Logs a whitelisting filter, that disables (some kind of) blocking | 
| 186 * blocking for a particular document, to the devtools panel. | 196 * for a particular document if a devtools panel or similiar is listening. | 
| 187 * | 197 * | 
| 188 * @param {Page} page The page the whitelisting is active on | 198 * @param {number} pageId The page the whitelisting is active on | 
| 189 * @param {string} url The url of the whitelisted document | 199 * @param {string} url The url of the whitelisted document | 
| 190 * @param {number} typeMask The bit mask of whitelisting types checked | 200 * @param {number} typeMask The bit mask of whitelisting types checked | 
| 191 * for | 201 * for | 
| 192 * @param {string} docDomain The IDN-decoded hostname of the parent | 202 * @param {string} docDomain The IDN-decoded hostname of the parent | 
| 193 * document | 203 * document | 
| 194 * @param {WhitelistFilter} filter The matched whitelisting filter | 204 * @param {WhitelistFilter} filter The matched whitelisting filter | 
| 195 */ | 205 */ | 
| 196 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, | 206 exports.logWhitelistedDocument = function(pageId, url, typeMask, docDomain, | 
| 197 filter) | 207 filter) | 
| 198 { | 208 { | 
| 199 let panel = getActivePanel(page); | 209 let pageLogger = getActivePageLogger(pageId); | 
| 200 if (panel) | 210 if (pageLogger) | 
| 201 { | 211 { | 
| 202 for (let type of nonRequestTypes) | 212 for (let type of nonRequestTypes) | 
| 203 { | 213 { | 
| 204 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) | 214 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) | 
| 205 addRecord(panel, {url, type, docDomain}, filter); | 215 addRecord(pageLogger, {pageId, url, type, docDomain}, filter); | 
| 206 } | 216 } | 
| 207 } | 217 } | 
| 208 }; | 218 }; | 
| 209 | 219 | 
| 210 /** | |
| 211 * Checks whether a page is inspected by the devtools panel. | |
| 212 * | |
| 213 * @param {Page} page | |
| 214 * @return {boolean} | |
| 215 */ | |
| 216 exports.hasPanel = function(page) | |
| 217 { | |
| 218 return panels.has(page.id); | |
| 219 }; | |
| 220 | 220 | 
| 221 function onBeforeRequest(details) | 221 function onBeforeRequest(details) | 
| 222 { | 222 { | 
| 223 let panel = panels.get(details.tabId); | 223 let pageLogger = pageLoggers.get(details.tabId); | 
| 224 | 224 | 
| 225 // Clear the devtools panel and reload the inspected tab without caching | 225 // Clear the records and reload the inspected tab without caching | 
| 226 // when a new request is issued. However, make sure that we don't end up | 226 // when a new request is issued. However, make sure that we don't end up | 
| 227 // in an infinite recursion if we already triggered a reload. | 227 // in an infinite recursion if we already triggered a reload. | 
| 228 if (panel.reloading) | 228 if (pageLogger.reloading) | 
| 229 { | 229 { | 
| 230 panel.reloading = false; | 230 pageLogger.reloading = false; | 
| 231 } | 231 } | 
| 232 else | 232 else | 
| 233 { | 233 { | 
| 234 panel.records = []; | 234 pageLogger.records = []; | 
| 235 panel.port.postMessage({type: "reset"}); | 235 emit(pageLogger, {type: "reset"}); | 
| 236 | 236 | 
| 237 // We can't repeat the request if it isn't a GET request. Chrome would | 237 // We can't repeat the request if it isn't a GET request. Chrome would | 
| 238 // prompt the user to confirm reloading the page, and POST requests are | 238 // prompt the user to confirm reloading the page, and POST requests are | 
| 239 // known to cause issues on many websites if repeated. | 239 // known to cause issues on many websites if repeated. | 
| 240 if (details.method == "GET") | 240 if (details.method == "GET") | 
| 241 panel.reload = true; | 241 pageLogger.reload = true; | 
| 242 } | 242 } | 
| 243 } | 243 } | 
| 244 | 244 | 
| 245 function onLoading(page) | 245 function onLoading(page) | 
| 246 { | 246 { | 
| 247 let tabId = page.id; | 247 let tabId = page.id; | 
| 248 let panel = panels.get(tabId); | 248 let pageLogger = pageLoggers.get(tabId); | 
| 249 | 249 | 
| 250 // Reloading the tab is the only way that allows bypassing all caches, in | 250 // Reloading the tab is the only way that allows bypassing all caches, in | 
| 251 // order to see all requests in the devtools panel. Reloading must not be | 251 // order to see all requests in the devtools panel. Reloading must not be | 
| 252 // performed before the tab changes to "loading", otherwise it will load the | 252 // performed before the tab changes to "loading", otherwise it will load the | 
| 253 // previous URL. | 253 // previous URL. | 
| 254 if (panel && panel.reload) | 254 if (pageLogger && pageLogger.reload) | 
| 255 { | 255 { | 
| 256 browser.tabs.reload(tabId, {bypassCache: true}); | 256 browser.tabs.reload(tabId, {bypassCache: true}); | 
| 257 | 257 | 
| 258 panel.reload = false; | 258 pageLogger.reload = false; | 
| 259 panel.reloading = true; | 259 pageLogger.reloading = true; | 
| 260 } | 260 } | 
| 261 } | 261 } | 
| 262 | 262 | 
| 263 function updateFilters(filters, added) | 263 function updateFilters(filters, added) | 
| 264 { | 264 { | 
| 265 for (let panel of panels.values()) | 265 for (let pageLogger of pageLoggers.values()) | 
| 266 { | 266 { | 
| 267 for (let i = 0; i < panel.records.length; i++) | 267 for (let i = 0; i < pageLogger.records.length; i++) | 
| 268 { | 268 { | 
| 269 let record = panel.records[i]; | 269 let record = pageLogger.records[i]; | 
| 270 | 270 | 
| 271 // If an added filter matches a request shown in the devtools panel, | 271 // If an added filter matches a logged request,update that record to show | 
| 272 // update that record to show the new filter. Ignore filters that aren't | 272 // the new filter. Ignore filters that aren't associated with any | 
| 273 // associated with any sub-resource request. There is no record for these | 273 // sub-resource request. There is no record for these if they don't | 
| 274 // if they don't already match. In particular, in case of element hiding | 274 // already match. In particular, in case of element hiding filters, we | 
| 275 // filters, we also wouldn't know if any new element matches. | 275 // also wouldn't know if any new element matches. | 
| 276 if (added) | 276 if (added) | 
| 277 { | 277 { | 
| 278 if (nonRequestTypes.includes(record.request.type)) | 278 if (nonRequestTypes.includes(record.request.type)) | 
| 279 continue; | 279 continue; | 
| 280 | 280 | 
| 281 let filter = matchRequest(record.request); | 281 let filter = matchRequest(record.request); | 
| 282 if (!filters.includes(filter)) | 282 if (!filters.includes(filter)) | 
| 283 continue; | 283 continue; | 
| 284 | 284 | 
| 285 record.filter = filter; | 285 record.filter = filter; | 
| 286 } | 286 } | 
| 287 | 287 | 
| 288 // If a filter shown in the devtools panel got removed, update that | 288 // If a logged filter got removed, update that record to show the filter | 
| 289 // record to show the filter that matches now, or none, instead. | 289 // that matches now, or none, instead. | 
| 290 // For filters that aren't associated with any sub-resource request, | 290 // For filters that aren't associated with any sub-resource request, | 
| 291 // just remove the record. We wouldn't know whether another filter | 291 // just remove the record. We wouldn't know whether another filter | 
| 292 // matches instead until the page is reloaded. | 292 // matches instead until the page is reloaded. | 
| 293 else | 293 else | 
| 294 { | 294 { | 
| 295 if (!filters.includes(record.filter)) | 295 if (!filters.includes(record.filter)) | 
| 296 continue; | 296 continue; | 
| 297 | 297 | 
| 298 if (nonRequestTypes.includes(record.request.type)) | 298 if (nonRequestTypes.includes(record.request.type)) | 
| 299 { | 299 { | 
| 300 panel.port.postMessage({ | 300 emit(pageLogger, {type: "remove-record", index: i}); | 
| 301 type: "remove-record", | 301 pageLogger.records.splice(i--, 1); | 
| 302 index: i | |
| 303 }); | |
| 304 panel.records.splice(i--, 1); | |
| 305 continue; | 302 continue; | 
| 306 } | 303 } | 
| 307 | 304 | 
| 308 record.filter = matchRequest(record.request); | 305 record.filter = matchRequest(record.request); | 
| 309 } | 306 } | 
| 310 | 307 | 
| 311 panel.port.postMessage({ | 308 emit(pageLogger, { | 
| 312 type: "update-record", | 309 type: "update-record", | 
| 313 index: i, | 310 index: i, | 
| 314 request: record.request, | 311 request: record.request, | 
| 315 filter: getFilterInfo(record.filter) | 312 filter: getFilterInfo(record.filter) | 
| 316 }); | 313 }); | 
| 317 } | 314 } | 
| 318 } | 315 } | 
| 319 } | 316 } | 
| 320 | 317 | 
| 321 function onFilterAdded(filter) | 318 function onFilterAdded(filter) | 
| 322 { | 319 { | 
| 323 updateFilters([filter], true); | 320 updateFilters([filter], true); | 
| 324 } | 321 } | 
| 325 | 322 | 
| 326 function onFilterRemoved(filter) | 323 function onFilterRemoved(filter) | 
| 327 { | 324 { | 
| 328 updateFilters([filter], false); | 325 updateFilters([filter], false); | 
| 329 } | 326 } | 
| 330 | 327 | 
| 331 function onSubscriptionAdded(subscription) | 328 function onSubscriptionAdded(subscription) | 
| 332 { | 329 { | 
| 333 if (subscription instanceof SpecialSubscription) | 330 if (subscription instanceof SpecialSubscription) | 
| 334 updateFilters(subscription.filters, true); | 331 updateFilters(subscription.filters, true); | 
| 335 } | 332 } | 
| 336 | 333 | 
| 337 browser.runtime.onConnect.addListener(newPort => | 334 /** | 
| 338 { | 335 * @namespace | 
| 339 let match = newPort.name.match(/^devtools-(\d+)$/); | 336 * @static | 
| 340 if (!match) | 337 */ | 
| 341 return; | 338 let DevLogger = exports.DevLogger = { | 
| 339 /** | |
| 340 * Adds a callback that is called when a request | |
| 341 * or filter hit is logged for the page. | |
| 342 * | |
| 343 * @param {number} pageId | |
| 344 * @param {function} callback | |
| 345 */ | |
| 346 on(pageId, callback) | |
| 347 { | |
| 348 if (pageLoggers.size == 0) | |
| 349 { | |
| 350 ext.pages.onLoading.addListener(onLoading); | |
| 351 FilterNotifier.on("filter.added", onFilterAdded); | |
| 352 FilterNotifier.on("filter.removed", onFilterRemoved); | |
| 353 FilterNotifier.on("subscription.added", onSubscriptionAdded); | |
| 354 } | |
| 342 | 355 | 
| 343 let inspectedTabId = parseInt(match[1], 10); | 356 if (pageLoggers.has(pageId)) | 
| 344 let localOnBeforeRequest = onBeforeRequest.bind(); | 357 { | 
| 358 pageLoggers.get(pageId).listeners.push(callback); | |
| 359 } | |
| 360 else | |
| 361 { | |
| 362 let localOnBeforeRequest = onBeforeRequest.bind(); | |
| 363 pageLoggers.set(pageId, { | |
| 364 listeners: [callback], records: [], | |
| 365 reloading: false, reload: false, | |
| 366 localOnBeforeRequest | |
| 367 }); | |
| 368 browser.webRequest.onBeforeRequest.addListener( | |
| 369 localOnBeforeRequest, | |
| 370 { | |
| 371 urls: ["http://*/*", "https://*/*"], | |
| 372 types: ["main_frame"], | |
| 373 tabId: pageId | |
| 374 } | |
| 375 ); | |
| 376 } | |
| 377 }, | |
| 345 | 378 | 
| 346 browser.webRequest.onBeforeRequest.addListener( | 379 /** | 
| 347 localOnBeforeRequest, | 380 * Removes a callback for a page. | 
| 381 * | |
| 382 * @param {number} pageId | |
| 383 * @param {function} callback | |
| 384 */ | |
| 385 off(pageId, callback) | |
| 386 { | |
| 387 let pageLogger = pageLoggers.get(pageId); | |
| 388 if (pageLogger.listeners.length > 1) | |
| 348 { | 389 { | 
| 349 urls: ["http://*/*", "https://*/*"], | 390 let idx = pageLogger.listeners.indexOf(callback); | 
| 350 types: ["main_frame"], | 391 if (idx != -1) | 
| 351 tabId: inspectedTabId | 392 pageLogger.listeners.splice(idx, 1); | 
| 352 } | 393 } | 
| 353 ); | 394 else | 
| 395 { | |
| 396 browser.webRequest.onBeforeRequest.removeListener( | |
| 397 pageLogger.localOnBeforeRequest | |
| 398 ); | |
| 399 pageLoggers.delete(pageId); | |
| 
Sebastian Noack
2018/03/20 01:28:24
Is this logic correct? So if we have only one list
 | |
| 400 } | |
| 354 | 401 | 
| 355 if (panels.size == 0) | 402 if (pageLoggers.size == 0) | 
| 356 { | |
| 357 ext.pages.onLoading.addListener(onLoading); | |
| 358 FilterNotifier.on("filter.added", onFilterAdded); | |
| 359 FilterNotifier.on("filter.removed", onFilterRemoved); | |
| 360 FilterNotifier.on("subscription.added", onSubscriptionAdded); | |
| 361 } | |
| 362 | |
| 363 newPort.onDisconnect.addListener(() => | |
| 364 { | |
| 365 panels.delete(inspectedTabId); | |
| 366 browser.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); | |
| 367 | |
| 368 if (panels.size == 0) | |
| 369 { | 403 { | 
| 370 ext.pages.onLoading.removeListener(onLoading); | 404 ext.pages.onLoading.removeListener(onLoading); | 
| 371 FilterNotifier.off("filter.added", onFilterAdded); | 405 FilterNotifier.off("filter.added", onFilterAdded); | 
| 372 FilterNotifier.off("filter.removed", onFilterRemoved); | 406 FilterNotifier.off("filter.removed", onFilterRemoved); | 
| 373 FilterNotifier.off("subscription.added", onSubscriptionAdded); | 407 FilterNotifier.off("subscription.added", onSubscriptionAdded); | 
| 374 } | 408 } | 
| 375 }); | 409 }, | 
| 376 | 410 | 
| 377 panels.set(inspectedTabId, {port: newPort, records: []}); | 411 /** | 
| 378 }); | 412 * Checks whether a page has any devLogging listeners active. | 
| 413 * | |
| 414 * @param {number} pageId | |
| 415 * @return {boolean} | |
| 416 */ | |
| 417 listening(pageId) | |
| 418 { | |
| 419 return pageLoggers.has(pageId); | |
| 420 } | |
| 421 }; | |
| 379 | 422 | 
| 380 port.on("devtools.traceElemHide", (message, sender) => | 423 port.on("devlogger.traceElemHide", (message, sender) => | 
| 381 { | 424 { | 
| 382 logHiddenElements( | 425 exports.logHiddenElements( | 
| 383 sender.page, message.selectors, message.filters, | 426 sender.page, message.selectors, message.filters, | 
| 384 extractHostFromFrame(sender.frame) | 427 extractHostFromFrame(sender.frame) | 
| 385 ); | 428 ); | 
| 386 }); | 429 }); | 
| OLD | NEW |