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 */
57function 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 ) ;
1922for ( 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+ **/
5157var getStoredUserRules = function ( ) {
5258 var oldUserRuleString = localStorage . getItem ( USER_RULE_KEY ) ;
5359 var oldUserRules = [ ] ;
@@ -57,6 +63,10 @@ var getStoredUserRules = function() {
5763 return oldUserRules ;
5864} ;
5965var wr = chrome . webRequest ;
66+
67+ /**
68+ * Load all stored user rules
69+ */
6070var loadStoredUserRules = function ( ) {
6171 var rules = getStoredUserRules ( ) ;
6272 var i ;
@@ -68,7 +78,10 @@ var loadStoredUserRules = function() {
6878
6979loadStoredUserRules ( ) ;
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+ */
7285var 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+ * */
92109var 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+ * */
109129function 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
148168var 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+ * */
150175function 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.
255280var 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+ * */
259293function 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+ * */
289326function 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+ * */
300339function 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+ * */
319360function 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+ * */
345389function 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+ * */
354401function 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+ * */
358409function 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+ * */
369423function switchPlannerDetailsHtml ( tab_id ) {
370424 return switchPlannerRenderSections ( tab_id , switchPlannerDetailsHtmlSection ) ;
371425}
372426
427+ /**
428+ * Generate the detailed html fot the switch planner, by section
429+ * */
373430function 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+ * */
396457function 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+ * */
427492function 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
441507wr . 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.
448514chrome . cookies . onChanged . addListener ( onCookieChanged ) ;
449515
516+ /**
517+ * disable switch Planner
518+ * @param tabId the Tab to disable for
519+ */
450520function 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+ */
456530function enableSwitchPlannerFor ( tabId ) {
457531 switchPlannerEnabledFor [ tabId ] = true ;
458532}
0 commit comments