Browse Source

more refactoring, code review

pull/2/head
gorhill 10 years ago
parent
commit
1f4ff9c809
  1. 7
      platform/chromium/vapi-background.js
  2. 1
      platform/firefox/vapi-background.js
  3. 4
      src/info.html
  4. 4
      src/js/background.js
  5. 125
      src/js/traffic.js

7
platform/chromium/vapi-background.js

@ -791,13 +791,6 @@ vAPI.net.registerListeners = function() {
}, },
this.onHeadersReceived.extra this.onHeadersReceived.extra
); );
chrome.webRequest.onErrorOccurred.addListener(
this.onErrorOccurred.callback,
{
'urls': this.onErrorOccurred.urls || ['<all_urls>']
}
);
// <<<<<<<< // <<<<<<<<
// End of: Network event handlers // End of: Network event handlers
}; };

1
platform/firefox/vapi-background.js

@ -1202,7 +1202,6 @@ var httpObserver = {
hostname: URI.asciiHost, hostname: URI.asciiHost,
parentFrameId: channelData[1], parentFrameId: channelData[1],
responseHeaders: result ? [{name: topic, value: result}] : [], responseHeaders: result ? [{name: topic, value: result}] : [],
statusLine: channel.responseStatus.toString(),
tabId: channelData[3], tabId: channelData[3],
type: type, type: type,
url: URI.asciiSpec url: URI.asciiSpec

4
src/info.html

@ -25,8 +25,8 @@
<h2 data-i18n="statsPageDetailedStats" id="detailed-stats"></h2> <h2 data-i18n="statsPageDetailedStats" id="detailed-stats"></h2>
<div> <div>
<select id="selectPageUrlsTemplate" style="display: none;"> <select id="selectPageUrlsTemplate" style="display: none;">
<option value="all" data-i18n="statsPageDetailedAllPages"></option>
<option value="http://chromium-behind-the-scene/" data-i18n="statsPageDetailedBehindTheScenePage"></option>
<option value="" data-i18n="statsPageDetailedAllPages"></option>
<option value="http://behind-the-scene/" data-i18n="statsPageDetailedBehindTheScenePage"></option>
</select> </select>
<select id="selectPageUrls"> <select id="selectPageUrls">
</select> </select>

4
src/js/background.js

@ -101,10 +101,6 @@ return {
pageStores: {}, pageStores: {},
pageStoreCemetery: {}, pageStoreCemetery: {},
// A map of redirects, to allow reverse lookup of redirects from landing
// page, so that redirection can be reported to the user.
redirectRequests: {},
// page url => permission scope // page url => permission scope
tMatrix: null, tMatrix: null,
pMatrix: null, pMatrix: null,

125
src/js/traffic.js

@ -206,6 +206,8 @@ var onBeforeRootFrameRequestHandler = function(details) {
// console.debug('onBeforeRequestHandler()> block=%s "%s": %o', block, details.url, details); // console.debug('onBeforeRequestHandler()> block=%s "%s": %o', block, details.url, details);
pageStore.recordRequest('doc', requestURL, block);
pageStore.updateBadgeAsync();
// Not blocked // Not blocked
if ( !block ) { if ( !block ) {
@ -217,13 +219,6 @@ var onBeforeRootFrameRequestHandler = function(details) {
// Blocked // Blocked
// rhill 2014-01-15: Delay logging of non-blocked top `main_frame`
// requests, in order to ensure any potential redirects is reported
// in proper chronological order.
// https://github.com/gorhill/httpswitchboard/issues/112
pageStore.recordRequest('doc', requestURL, block);
pageStore.updateBadgeAsync();
// If it's a blacklisted frame, redirect to frame.html // If it's a blacklisted frame, redirect to frame.html
// rhill 2013-11-05: The root frame contains a link to noop.css, this // rhill 2013-11-05: The root frame contains a link to noop.css, this
// allows to later check whether the root frame has been unblocked by the // allows to later check whether the root frame has been unblocked by the
@ -282,8 +277,6 @@ var onBeforeRequestHandler = function(details) {
return; return;
} }
var requestHostname = µmuri.hostname;
// Re-classify orphan HTTP requests as behind-the-scene requests. There is // Re-classify orphan HTTP requests as behind-the-scene requests. There is
// not much else which can be done, because there are URLs // not much else which can be done, because there are URLs
// which cannot be handled by µMatrix, i.e. `opera://startpage`, // which cannot be handled by µMatrix, i.e. `opera://startpage`,
@ -291,11 +284,12 @@ var onBeforeRequestHandler = function(details) {
// to scope on unknown scheme? Etc. // to scope on unknown scheme? Etc.
// https://github.com/gorhill/httpswitchboard/issues/191 // https://github.com/gorhill/httpswitchboard/issues/191
// https://github.com/gorhill/httpswitchboard/issues/91#issuecomment-37180275 // https://github.com/gorhill/httpswitchboard/issues/91#issuecomment-37180275
var pageStore = µm.mustPageStoreFromTabId(details.tabId);
var tabId = pageStore.tabId;
var tabContext = µm.tabContextManager.mustLookup(details.tabId);
var tabId = tabContext.tabId;
var requestHostname = µmuri.hostname;
// Disallow request as per temporary matrix? // Disallow request as per temporary matrix?
var block = µm.mustBlock(pageStore.pageHostname, requestHostname, requestType);
var block = µm.mustBlock(tabContext.rootHostname, requestHostname, requestType);
// Record request. // Record request.
// https://github.com/gorhill/httpswitchboard/issues/342 // https://github.com/gorhill/httpswitchboard/issues/342
@ -303,6 +297,7 @@ var onBeforeRequestHandler = function(details) {
// processing has already been performed, and that a synthetic URL has // processing has already been performed, and that a synthetic URL has
// been constructed for logging purpose. Use this synthetic URL if // been constructed for logging purpose. Use this synthetic URL if
// it is available. // it is available.
var pageStore = µm.mustPageStoreFromTabId(details.tabId);
pageStore.recordRequest(requestType, requestURL, block); pageStore.recordRequest(requestType, requestURL, block);
pageStore.updateBadgeAsync(); pageStore.updateBadgeAsync();
@ -457,16 +452,20 @@ var onHeadersReceived = function(details) {
/******************************************************************************/ /******************************************************************************/
var onMainDocHeadersReceived = function(details) { var onMainDocHeadersReceived = function(details) {
var µm = µMatrix;
// https://github.com/gorhill/uMatrix/issues/145 // https://github.com/gorhill/uMatrix/issues/145
// Check if the main_frame is a download // Check if the main_frame is a download
if ( headerValue(details.responseHeaders, 'content-disposition').lastIndexOf('attachment', 0) === 0 ) { if ( headerValue(details.responseHeaders, 'content-disposition').lastIndexOf('attachment', 0) === 0 ) {
µb.tabContextManager.unpush(details.tabId, details.url);
µm.tabContextManager.unpush(details.tabId, details.url);
}
var tabContext = µm.tabContextManager.lookup(details.tabId);
if ( tabContext === null ) {
return;
} }
// console.debug('onMainDocHeadersReceived()> "%s": %o', details.url, details); // console.debug('onMainDocHeadersReceived()> "%s": %o', details.url, details);
var µm = µMatrix;
// rhill 2013-12-07: // rhill 2013-12-07:
// Apparently in Opera, onBeforeRequest() is triggered while the // Apparently in Opera, onBeforeRequest() is triggered while the
// URL is not yet bound to a tab (-1), which caused the code here // URL is not yet bound to a tab (-1), which caused the code here
@ -484,61 +483,19 @@ var onMainDocHeadersReceived = function(details) {
// https://github.com/gorhill/httpswitchboard/issues/191 // https://github.com/gorhill/httpswitchboard/issues/191
// https://github.com/gorhill/httpswitchboard/issues/91#issuecomment-37180275 // https://github.com/gorhill/httpswitchboard/issues/91#issuecomment-37180275
var pageStore = µm.mustPageStoreFromTabId(details.tabId); var pageStore = µm.mustPageStoreFromTabId(details.tabId);
var tabId = pageStore.tabId;
var µmuri = µm.URI.set(details.url);
var requestURL = µmuri.normalizedURI();
var requestScheme = µmuri.scheme;
var requestHostname = µmuri.hostname;
var headers = details.responseHeaders; var headers = details.responseHeaders;
// Simplify code paths by splitting func in two different handlers, one
// for main docs, one for sub docs.
// rhill 2014-01-15: Report redirects.
// https://github.com/gorhill/httpswitchboard/issues/112
// rhill 2014-02-10: Handle all redirects.
// https://github.com/gorhill/httpswitchboard/issues/188
if ( reStatusRedirect.test(details.statusLine) ) {
var i = headerIndexFromName('location', headers);
if ( i >= 0 ) {
// rhill 2014-01-20: Be ready to handle relative URLs.
// https://github.com/gorhill/httpswitchboard/issues/162
var locationURL = µmuri.set(headers[i].value.trim()).normalizedURI();
if ( µmuri.authority === '' ) {
locationURL = requestScheme + '://' + requestHostname + µmuri.path;
}
µm.redirectRequests[locationURL] = requestURL;
}
// console.debug('onMainDocHeadersReceived()> redirect "%s" to "%s"', requestURL, headers[i].value);
}
// rhill 2014-01-15: Report redirects if any.
// https://github.com/gorhill/httpswitchboard/issues/112
if ( reStatusOK.test(details.statusLine) ) {
var mainFrameStack = [requestURL];
var destinationURL = requestURL;
var sourceURL;
while ( sourceURL = µm.redirectRequests[destinationURL] ) {
mainFrameStack.push(sourceURL);
delete µm.redirectRequests[destinationURL];
destinationURL = sourceURL;
}
while ( destinationURL = mainFrameStack.pop() ) {
pageStore.recordRequest('doc', destinationURL, false);
}
pageStore.updateBadgeAsync();
}
// Maybe modify inbound headers // Maybe modify inbound headers
var csp = ''; var csp = '';
// Enforce strict HTTPS? // Enforce strict HTTPS?
if ( requestScheme === 'https' && µm.tMatrix.evaluateSwitchZ('https-strict', pageStore.pageHostname) ) {
var requestScheme = µm.URI.schemeFromURI(details.url);
if ( requestScheme === 'https' && µm.tMatrix.evaluateSwitchZ('https-strict', tabContext.rootHostname) ) {
csp += "default-src chrome-search: data: https: wss: 'unsafe-eval' 'unsafe-inline';"; csp += "default-src chrome-search: data: https: wss: 'unsafe-eval' 'unsafe-inline';";
} }
// https://github.com/gorhill/httpswitchboard/issues/181 // https://github.com/gorhill/httpswitchboard/issues/181
pageStore.pageScriptBlocked = µm.mustBlock(pageStore.pageHostname, requestHostname, 'script');
pageStore.pageScriptBlocked = µm.mustBlock(tabContext.rootHostname, tabContext.rootHostname, 'script');
if ( pageStore.pageScriptBlocked ) { if ( pageStore.pageScriptBlocked ) {
// If javascript not allowed, say so through a `Content-Security-Policy` directive. // If javascript not allowed, say so through a `Content-Security-Policy` directive.
// console.debug('onMainDocHeadersReceived()> PAGE CSP "%s": %o', details.url, details); // console.debug('onMainDocHeadersReceived()> PAGE CSP "%s": %o', details.url, details);
@ -555,9 +512,6 @@ var onMainDocHeadersReceived = function(details) {
} }
}; };
var reStatusOK = /\b(?:200|304)\b/;
var reStatusRedirect = /\b30[12378]\b/;
/******************************************************************************/ /******************************************************************************/
var onSubDocHeadersReceived = function(details) { var onSubDocHeadersReceived = function(details) {
@ -622,43 +576,6 @@ var headerValue = function(headers, name) {
/******************************************************************************/ /******************************************************************************/
var onErrorOccurredHandler = function(details) {
// console.debug('onErrorOccurred()> "%s": %o', details.url, details);
var requestType = requestTypeNormalizer[details.type] || 'other';
// Ignore all that is not a main document
if ( requestType !== 'doc'|| details.parentFrameId >= 0 ) {
return;
}
var µm = µMatrix;
var pageStats = µm.pageStoreFromTabId(details.tabId);
if ( !pageStats ) {
return;
}
// rhill 2014-01-28: Unwind the stack of redirects if any. Chromium will
// emit an error when a web page redirects apparently endlessly, so
// we need to unravel and report all these redirects upon error.
// https://github.com/gorhill/httpswitchboard/issues/171
var requestURL = µm.URI.set(details.url).normalizedURI();
var mainFrameStack = [requestURL];
var destinationURL = requestURL;
var sourceURL;
while ( sourceURL = µm.redirectRequests[destinationURL] ) {
mainFrameStack.push(sourceURL);
delete µm.redirectRequests[destinationURL];
destinationURL = sourceURL;
}
while ( destinationURL = mainFrameStack.pop() ) {
pageStats.recordRequest('doc', destinationURL, false);
}
pageStats.updateBadgeAsync();
};
/******************************************************************************/
// Caller must ensure headerName is normalized to lower case. // Caller must ensure headerName is normalized to lower case.
var headerIndexFromName = function(headerName, headers) { var headerIndexFromName = function(headerName, headers) {
@ -720,14 +637,6 @@ vAPI.net.onHeadersReceived = {
callback: onHeadersReceived callback: onHeadersReceived
}; };
vAPI.net.onErrorOccurred = {
urls: [
"http://*/*",
"https://*/*"
],
callback: onErrorOccurredHandler
};
/******************************************************************************/ /******************************************************************************/
var start = function() { var start = function() {

Loading…
Cancel
Save