Skip to content

Commit 264efe6

Browse files
committed
Merge pull request EFForg#1884 from Thorsten-Sick/code_comments
Added code comments to js files (function headers)
2 parents 20f51ef + 4751e83 commit 264efe6

File tree

4 files changed

+202
-24
lines changed

4 files changed

+202
-24
lines changed

chromium/background.js

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22
/**
33
* Fetch and parse XML to be loaded as RuleSets.
4+
*
5+
* @param url: a relative URL to local XML
46
*/
57
function getRuleXml(url) {
68
var xhr = new XMLHttpRequest();
@@ -15,7 +17,8 @@ function getRuleXml(url) {
1517
return xhr.responseXML;
1618
}
1719

18-
var all_rules = new RuleSets(navigator.userAgent, LRUCache, localStorage);
20+
// Rules are loaded here
21+
var all_rules = new RuleSets(navigator.userAgent, LRUCache, localStorage);
1922
for (var i = 0; i < rule_list.length; i++) {
2023
all_rules.addFromXml(getRuleXml(rule_list[i]));
2124
}
@@ -48,6 +51,9 @@ chrome.storage.onChanged.addListener(function(changes, areaName) {
4851
}
4952
});
5053

54+
/**
55+
* Load stored user rules
56+
**/
5157
var getStoredUserRules = function() {
5258
var oldUserRuleString = localStorage.getItem(USER_RULE_KEY);
5359
var oldUserRules = [];
@@ -57,6 +63,10 @@ var getStoredUserRules = function() {
5763
return oldUserRules;
5864
};
5965
var wr = chrome.webRequest;
66+
67+
/**
68+
* Load all stored user rules
69+
*/
6070
var loadStoredUserRules = function() {
6171
var rules = getStoredUserRules();
6272
var i;
@@ -68,7 +78,10 @@ var loadStoredUserRules = function() {
6878

6979
loadStoredUserRules();
7080

71-
// Set the icon color correctly
81+
/**
82+
* Set the icon color correctly
83+
* Depending on http-nowhere it should be red/default
84+
*/
7285
var setIconColor = function() {
7386
var newIconPath = httpNowhereOn ? './icon38-red.png' : './icon38.png';
7487
chrome.browserAction.setIcon({
@@ -88,7 +101,11 @@ for (r in rs) {
88101
}
89102
*/
90103

91-
104+
/**
105+
* Adds a new user rule
106+
* @param params: params defining the rule
107+
* @param cb: Callback to call after success/fail
108+
* */
92109
var addNewRule = function(params, cb) {
93110
if (all_rules.addUserRule(params)) {
94111
// If we successfully added the user rule, save it in local
@@ -106,6 +123,9 @@ var addNewRule = function(params, cb) {
106123
}
107124
};
108125

126+
/**
127+
* Adds a listener for removed tabs
128+
* */
109129
function AppliedRulesets() {
110130
this.active_tab_rules = {};
111131

@@ -147,6 +167,11 @@ var domainBlacklist = {};
147167
// TODO: Remove this code if they ever give us a real counter
148168
var redirectCounter = {};
149169

170+
/**
171+
* Called before a HTTP(s) request. Does the heavy lifting
172+
* Cancels the request/redirects it to HTTPS. URL modification happens in here.
173+
* @param details of the handler, see Chrome doc
174+
* */
150175
function onBeforeRequest(details) {
151176
var uri = document.createElement('a');
152177
uri.href = details.url;
@@ -254,8 +279,17 @@ var activeTypes = { stylesheet: 1, script: 1, object: 1, other: 1};
254279
// UI treatment of insecure subframes.
255280
var passiveTypes = { main_frame: 1, sub_frame: 1, image: 1, xmlhttprequest: 1};
256281

257-
// Record a non-HTTPS URL loaded by a given hostname in the Switch Planner, for
258-
// use in determining which resources need to be ported to HTTPS.
282+
/**
283+
* Record a non-HTTPS URL loaded by a given hostname in the Switch Planner, for
284+
* use in determining which resources need to be ported to HTTPS.
285+
* (Reminder: Switch planner is the pro-tool enabled by switching into debug-mode)
286+
*
287+
* @param type: type of the resource (see activeTypes and passiveTypes arrays)
288+
* @param tab_id: The id of the tab
289+
* @param resource_host: The host of the original url
290+
* @param resource_url: the original url
291+
* @param rewritten_url: The url rewritten to
292+
* */
259293
function writeToSwitchPlanner(type, tab_id, resource_host, resource_url, rewritten_url) {
260294
var rw = "rw";
261295
if (rewritten_url == null)
@@ -284,8 +318,11 @@ function writeToSwitchPlanner(type, tab_id, resource_host, resource_url, rewritt
284318
switchPlannerInfo[tab_id][rw][resource_host][active_content][resource_url] = 1;
285319
}
286320

287-
// Return the number of properties in an object. For associative maps, this is
288-
// their size.
321+
/**
322+
* Return the number of properties in an object. For associative maps, this is
323+
* their size.
324+
* @param obj: object to calc the size for
325+
* */
289326
function objSize(obj) {
290327
if (typeof obj == 'undefined') return 0;
291328
var size = 0, key;
@@ -295,8 +332,10 @@ function objSize(obj) {
295332
return size;
296333
}
297334

298-
// Make an array of asset hosts by score so we can sort them,
299-
// presenting the most important ones first.
335+
/**
336+
* Make an array of asset hosts by score so we can sort them,
337+
* presenting the most important ones first.
338+
* */
300339
function sortSwitchPlanner(tab_id, rewritten) {
301340
var asset_host_list = [];
302341
if (typeof switchPlannerInfo[tab_id] === 'undefined' ||
@@ -315,7 +354,9 @@ function sortSwitchPlanner(tab_id, rewritten) {
315354
return asset_host_list;
316355
}
317356

318-
// Format the switch planner output for presentation to a user.
357+
/**
358+
* Format the switch planner output for presentation to a user.
359+
* */
319360
function switchPlannerSmallHtmlSection(tab_id, rewritten) {
320361
var asset_host_list = sortSwitchPlanner(tab_id, rewritten);
321362
if (asset_host_list.length == 0) {
@@ -342,6 +383,9 @@ function switchPlannerSmallHtmlSection(tab_id, rewritten) {
342383
return output;
343384
}
344385

386+
/**
387+
* Create switch planner sections
388+
* */
345389
function switchPlannerRenderSections(tab_id, f) {
346390
return "Unrewritten HTTP resources loaded from this tab (enable HTTPS on " +
347391
"these domains and add them to HTTPS Everywhere):<br/>" +
@@ -351,10 +395,17 @@ function switchPlannerRenderSections(tab_id, f) {
351395
f(tab_id, "rw");
352396
}
353397

398+
/**
399+
* Generate the small switch planner html content
400+
* */
354401
function switchPlannerSmallHtml(tab_id) {
355402
return switchPlannerRenderSections(tab_id, switchPlannerSmallHtmlSection);
356403
}
357404

405+
/**
406+
* Generate a HTML link from urls in map
407+
* map: the map containing the urls
408+
* */
358409
function linksFromKeys(map) {
359410
if (typeof map == 'undefined') return "";
360411
var output = "";
@@ -366,10 +417,16 @@ function linksFromKeys(map) {
366417
return output;
367418
}
368419

420+
/**
421+
* Generate the detailed html fot the switch planner
422+
* */
369423
function switchPlannerDetailsHtml(tab_id) {
370424
return switchPlannerRenderSections(tab_id, switchPlannerDetailsHtmlSection);
371425
}
372426

427+
/**
428+
* Generate the detailed html fot the switch planner, by section
429+
* */
373430
function switchPlannerDetailsHtmlSection(tab_id, rewritten) {
374431
var asset_host_list = sortSwitchPlanner(tab_id, rewritten);
375432
var output = "";
@@ -393,6 +450,10 @@ function switchPlannerDetailsHtmlSection(tab_id, rewritten) {
393450
return output;
394451
}
395452

453+
/**
454+
* monitor cookie changes. Automatically convert them to secure cookies
455+
* @param changeInfo Cookie changed info, see Chrome doc
456+
* */
396457
function onCookieChanged(changeInfo) {
397458
if (!changeInfo.removed && !changeInfo.cookie.secure) {
398459
if (all_rules.shouldSecureCookie(changeInfo.cookie, false)) {
@@ -424,6 +485,10 @@ function onCookieChanged(changeInfo) {
424485
}
425486
}
426487

488+
/**
489+
* handling redirects, breaking loops
490+
* @param details details for the redirect (see chrome doc)
491+
* */
427492
function onBeforeRedirect(details) {
428493
// Catch redirect loops (ignoring about:blank, etc. caused by other extensions)
429494
var prefix = details.redirectUrl.substring(0, 5);
@@ -438,6 +503,7 @@ function onBeforeRedirect(details) {
438503
}
439504
}
440505

506+
// Registers the handler for requests
441507
wr.onBeforeRequest.addListener(onBeforeRequest, {urls: ["https://*/*", "http://*/*"]}, ["blocking"]);
442508

443509
// Try to catch redirect loops on URLs we've redirected to HTTPS.
@@ -447,12 +513,20 @@ wr.onBeforeRedirect.addListener(onBeforeRedirect, {urls: ["https://*/*"]});
447513
// Listen for cookies set/updated and secure them if applicable. This function is async/nonblocking.
448514
chrome.cookies.onChanged.addListener(onCookieChanged);
449515

516+
/**
517+
* disable switch Planner
518+
* @param tabId the Tab to disable for
519+
*/
450520
function disableSwitchPlannerFor(tabId) {
451521
delete switchPlannerEnabledFor[tabId];
452522
// Clear stored URL info.
453523
delete switchPlannerInfo[tabId];
454524
}
455525

526+
/**
527+
* Enable switch planner for specific tab
528+
* @param tabId the tab to enable it for
529+
*/
456530
function enableSwitchPlannerFor(tabId) {
457531
switchPlannerEnabledFor[tabId] = true;
458532
}

chromium/devtools-panel.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ function e(id) {
22
return document.getElementById(id);
33
}
44

5+
/**
6+
* Send message to main extension for HTML to display
7+
* @param type: enable/disable
8+
*/
59
function sendMessage(type) {
610
chrome.runtime.sendMessage({
711
type: type,
812
tabId: chrome.devtools.inspectedWindow.tabId,
913
});
1014
}
1115

12-
// Turn on the Switch Planner recording mode, and hide the long description.
16+
/**
17+
* Turn on the Switch Planner recording mode, and hide the long description.
18+
*/
1319
function enableSwitchPlanner() {
1420
sendMessage("enable");
1521
e("SwitchPlannerDescription").style.display = "none";
@@ -20,15 +26,19 @@ function enableSwitchPlanner() {
2026
chrome.devtools.inspectedWindow.reload();
2127
}
2228

23-
// Disable the switch planner and reload, so any state is forgotten and
24-
// the long description is restored.
29+
/**
30+
* Disable the switch planner and reload, so any state is forgotten and
31+
* the long description is restored.
32+
*/
2533
function disableSwitchPlanner() {
2634
sendMessage("disable");
2735
document.location.reload();
2836
}
2937

30-
// Fetch summary HTML of the planner results from the background page for
31-
// display in the devtools panel.
38+
/**
39+
* Fetch summary HTML of the planner results from the background page for
40+
* display in the devtools panel.
41+
*/
3242
function display() {
3343
chrome.runtime.sendMessage({
3444
type: "getSmallHtml",

chromium/popup.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ function e(id) {
88
return document.getElementById(id);
99
}
1010

11+
/**
12+
* Handles rule (de)activation in the popup
13+
* @param checkbox checkbox being clicked
14+
* @param ruleset the ruleset tied tot he checkbox
15+
*/
1116
function toggleRuleLine(checkbox, ruleset) {
1217
ruleset.active = checkbox.checked;
1318

@@ -22,6 +27,11 @@ function toggleRuleLine(checkbox, ruleset) {
2227
chrome.tabs.reload();
2328
}
2429

30+
/**
31+
* Creates a rule line (including checkbox and icon) for the popup
32+
* @param ruleset the ruleset to build the line for
33+
* @returns {*}
34+
*/
2535
function createRuleLine(ruleset) {
2636

2737
// parent block for line
@@ -67,6 +77,10 @@ function createRuleLine(ruleset) {
6777
return line;
6878
}
6979

80+
/**
81+
* Create the list of rules for a specific tab
82+
* @param tab
83+
*/
7084
function gotTab(tab) {
7185
var rulesets = backgroundPage.activeRulesets.getRulesets(tab.id);
7286

@@ -85,6 +99,9 @@ function gotTab(tab) {
8599
}
86100
}
87101

102+
/**
103+
* Fill in content into the popup on load
104+
*/
88105
document.addEventListener("DOMContentLoaded", function () {
89106
stableRules = document.getElementById("StableRules");
90107
unstableRules = document.getElementById("UnstableRules");
@@ -129,6 +146,9 @@ function show(elem) {
129146
elem.style.display = "block";
130147
}
131148

149+
/**
150+
* Handles the manual addition of rules
151+
*/
132152
function addManualRule() {
133153
chrome.tabs.getSelected(null, function(tab) {
134154
hide(e("add-rule-link"));

0 commit comments

Comments
 (0)