1- // TODO: This keeps around history across "clear history" events. Fix that.
1+ // Records which tabId's are active in the HTTPS Switch Planner (see
2+ // devtools-panel.js).
23var switchPlannerEnabledFor = { } ;
4+ // Detailed information recorded when the HTTPS Switch Planner is active.
5+ // Structure is:
6+ // switchPlannerInfo[tabId][resource_host][active_content][url];
37var switchPlannerInfo = { } ;
4- console . log ( "XXX TESTING XXX" ) ;
58
69var all_rules = new RuleSets ( ) ;
710var wr = chrome . webRequest ;
@@ -147,21 +150,12 @@ function onBeforeRequest(details) {
147150
148151 // In Switch Planner Mode, record any non-rewriteable
149152 // HTTP URIs by parent hostname, along with the resource type.
150- if ( uri . protocol ( ) !== "https" ) {
151- // In order to figure out the document requesting this resource,
152- // have to get the tab. TODO: any cheaper way?
153- // XXX: Because this is async it's actually inaccurate during quick page
154- // switches. Maybe it only matters when you're switching domains though?
155- chrome . tabs . get ( details . tabId , function ( tab ) {
156- var tab_host = new URI ( tab . url ) . hostname ( ) ;
157- if ( tab_host !== canonical_host ) {
158- writeToSwitchPlanner ( details . type ,
159- tab_host ,
160- canonical_host ,
161- details . url ,
162- newuristr ) ;
163- }
164- } ) ;
153+ if ( switchPlannerEnabledFor [ details . tabId ] && uri . protocol ( ) !== "https" ) {
154+ writeToSwitchPlanner ( details . type ,
155+ details . tabId ,
156+ canonical_host ,
157+ details . url ,
158+ newuristr ) ;
165159 }
166160
167161 if ( newuristr ) {
@@ -188,7 +182,7 @@ var passiveTypes = { main_frame: 1, sub_frame: 1, image: 1, xmlhttprequest: 1};
188182// use in determining which resources need to be ported to HTTPS.
189183// TODO: Maybe unique by resource URL, so reloading a single page doesn't double
190184// the counts?
191- function writeToSwitchPlanner ( type , tab_host , resource_host , resource_url , rewritten_url ) {
185+ function writeToSwitchPlanner ( type , tab_id , resource_host , resource_url , rewritten_url ) {
192186 var rw = "rw" ;
193187 if ( rewritten_url == null )
194188 rw = "no" ;
@@ -207,14 +201,14 @@ function writeToSwitchPlanner(type, tab_host, resource_host, resource_url, rewri
207201 // TODO: Maybe also count rewritten URLs separately.
208202 if ( rewritten_url != null ) return ;
209203
210- if ( ! switchPlannerInfo [ tab_host ] )
211- switchPlannerInfo [ tab_host ] = { } ;
212- if ( ! switchPlannerInfo [ tab_host ] [ resource_host ] )
213- switchPlannerInfo [ tab_host ] [ resource_host ] = { } ;
214- if ( ! switchPlannerInfo [ tab_host ] [ resource_host ] [ active_content ] )
215- switchPlannerInfo [ tab_host ] [ resource_host ] [ active_content ] = { } ;
204+ if ( ! switchPlannerInfo [ tab_id ] )
205+ switchPlannerInfo [ tab_id ] = { } ;
206+ if ( ! switchPlannerInfo [ tab_id ] [ resource_host ] )
207+ switchPlannerInfo [ tab_id ] [ resource_host ] = { } ;
208+ if ( ! switchPlannerInfo [ tab_id ] [ resource_host ] [ active_content ] )
209+ switchPlannerInfo [ tab_id ] [ resource_host ] [ active_content ] = { } ;
216210
217- switchPlannerInfo [ tab_host ] [ resource_host ] [ active_content ] [ resource_url ] = 1 ;
211+ switchPlannerInfo [ tab_id ] [ resource_host ] [ active_content ] [ resource_url ] = 1 ;
218212}
219213
220214// Return the number of properties in an object. For associative maps, this is
@@ -230,11 +224,11 @@ function objSize(obj) {
230224
231225// Make an array of asset hosts by score so we can sort them,
232226// presenting the most important ones first.
233- function sortSwitchPlanner ( tab_host ) {
227+ function sortSwitchPlanner ( tab_id ) {
234228 var asset_host_list = [ ] ;
235- var parentInfo = switchPlannerInfo [ tab_host ] ;
236- for ( var asset_host in parentInfo ) {
237- var ah = parentInfo [ asset_host ] ;
229+ var tabInfo = switchPlannerInfo [ tab_id ] ;
230+ for ( var asset_host in tabInfo ) {
231+ var ah = tabInfo [ asset_host ] ;
238232 var activeCount = objSize ( ah [ 1 ] ) ;
239233 var passiveCount = objSize ( ah [ 0 ] ) ;
240234 var score = activeCount * 100 + passiveCount ;
@@ -245,8 +239,8 @@ function sortSwitchPlanner(tab_host) {
245239}
246240
247241// Format the switch planner output for presentation to a user.
248- function switchPlannerSmallHtml ( tab_host ) {
249- var asset_host_list = sortSwitchPlanner ( tab_host ) ;
242+ function switchPlannerSmallHtml ( tab_id ) {
243+ var asset_host_list = sortSwitchPlanner ( tab_id ) ;
250244 if ( asset_host_list . length == 0 ) {
251245 return "<b>none</b>" ;
252246 }
@@ -282,8 +276,8 @@ function linksFromKeys(map) {
282276 return output ;
283277}
284278
285- function switchPlannerDetailsHtml ( tab_host ) {
286- var asset_host_list = sortSwitchPlanner ( tab_host ) ;
279+ function switchPlannerDetailsHtml ( tab_id ) {
280+ var asset_host_list = sortSwitchPlanner ( tab_id ) ;
287281 var output = "" ;
288282
289283 for ( var i = asset_host_list . length - 1 ; i >= 0 ; i -- ) {
@@ -294,11 +288,11 @@ function switchPlannerDetailsHtml(tab_host) {
294288 output += "<b>" + host + "</b>: " ;
295289 if ( activeCount > 0 ) {
296290 output += activeCount + " active<br/>" ;
297- output += linksFromKeys ( switchPlannerInfo [ tab_host ] [ host ] [ 1 ] ) ;
291+ output += linksFromKeys ( switchPlannerInfo [ tab_id ] [ host ] [ 1 ] ) ;
298292 }
299293 if ( passiveCount > 0 ) {
300294 output += "<br/>" + passiveCount + " passive<br/>" ;
301- output += linksFromKeys ( switchPlannerInfo [ tab_host ] [ host ] [ 0 ] ) ;
295+ output += linksFromKeys ( switchPlannerInfo [ tab_id ] [ host ] [ 0 ] ) ;
302296 }
303297 output += "<br/>" ;
304298 }
@@ -407,13 +401,35 @@ chrome.tabs.onReplaced.addListener(function(addedTabId, removedTabId) {
407401// so we also use onBeforeSendHeaders to prevent a small window where cookies could be stolen.
408402chrome . cookies . onChanged . addListener ( onCookieChanged ) ;
409403
404+ function disableSwitchPlannerFor ( tabId ) {
405+ delete switchPlannerEnabledFor [ tabId ] ;
406+ // Clear stored URL info.
407+ delete switchPlannerInfo [ tabId ] ;
408+ }
409+
410+ function enableSwitchPlannerFor ( tabId ) {
411+ switchPlannerEnabledFor [ tabId ] = true ;
412+ }
413+
410414// Listen for connection from the DevTools panel so we can set up communication.
411- chrome . runtime . onMessage . addListener ( function ( message ) {
412- console . log ( message ) ;
413- if ( message . hasOwnProperty ( 'enable' ) ) {
414- var enable = message . enable ;
415- switchPlannerEnabledFor [ message . tabId ] = enable ;
416- if ( ! enable )
417- switchPlannerInfo = { } ;
415+ chrome . runtime . onConnect . addListener ( function ( port ) {
416+ if ( port . name == "devtools-page" ) {
417+ chrome . runtime . onMessage . addListener ( function ( message , sender , sendResponse ) {
418+ var tabId = message . tabId ;
419+
420+ var disableOnCloseCallback = function ( port ) {
421+ log ( DBUG , "Devtools window for tab " + tabId + " closed, clearing data." ) ;
422+ disableSwitchPlannerFor ( tabId ) ;
423+ } ;
424+
425+ if ( message . type === "enable" ) {
426+ enableSwitchPlannerFor ( tabId ) ;
427+ port . onDisconnect . addListener ( disableOnCloseCallback ) ;
428+ } else if ( message . type === "disable" ) {
429+ disableSwitchPlannerFor ( tabId ) ;
430+ } else if ( message . type === "getSmallHtml" ) {
431+ sendResponse ( { html : switchPlannerSmallHtml ( tabId ) } ) ;
432+ }
433+ } ) ;
418434 }
419435} ) ;
0 commit comments