@@ -319,27 +319,42 @@ HTTPSEverywhere.prototype = {
319319 // Also note some requests, like Safe Browsing requests, will have no
320320 // associated tab.
321321 getBrowserForChannel : function ( channel ) {
322- let loadContext ;
322+ let loadContext , topFrameElement , associatedWindow ;
323+ let spec = channel . URI . spec ;
323324 try {
324325 loadContext = channel . notificationCallbacks . getInterface ( CI . nsILoadContext ) ;
325326 } catch ( e ) {
327+ }
328+
329+ if ( ! loadContext ) {
326330 try {
327331 loadContext = channel . loadGroup . notificationCallbacks
328332 . getInterface ( CI . nsILoadContext ) ;
329333 } catch ( e ) {
330334 // Lots of requests have no notificationCallbacks, mostly background
331335 // ones like OCSP checks or smart browsing fetches.
332- this . log ( DBUG , "no loadGroup notificationCallbacks for "
333- + channel . URI . spec + ": " + e ) ;
336+ this . log ( DBUG , "getBrowserForChannel: no loadContext for " + spec ) ;
334337 return null ;
335338 }
336339 }
340+
341+ if ( loadContext ) {
342+ topFrameElement = loadContext . topFrameElement ;
343+ try {
344+ // If loadContext is an nsDocShell, associatedWindow is present.
345+ // Otherwise, if it's just a LoadContext, accessing it will throw
346+ // NS_ERROR_UNEXPECTED.
347+ associatedWindow = loadContext . associatedWindow ;
348+ } catch ( e ) {
349+ }
350+ }
351+
337352 // On e10s (multiprocess, aka electrolysis) Firefox,
338353 // loadContext.topFrameElement gives us a reference to the XUL <browser>
339354 // element we need. However, on non-e10s Firefox, topFrameElement is null.
340- if ( loadContext && loadContext . topFrameElement ) {
341- return loadContext . topFrameElement ;
342- } else if ( loadContext ) {
355+ if ( topFrameElement ) {
356+ return topFrameElement ;
357+ } else if ( associatedWindow ) {
343358 // For non-e10s Firefox, get the XUL <browser> element using this rather
344359 // magical / opaque code cribbed from
345360 // https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Tabbed_browser#Getting_the_browser_that_fires_the_http-on-modify-request_notification_(example_code_updated_for_loadContext)
@@ -355,33 +370,40 @@ HTTPSEverywhere.prototype = {
355370 . getInterface ( Ci . nsIDOMWindow ) ;
356371 // this is the gBrowser object of the firefox window this tab is in
357372 var gBrowser = aDOMWindow . gBrowser ;
358- // On Firefox for Android, gBrowser is not available. Instead use the
359- // BrowserApp API:
360- // https://developer.mozilla.org/en-US/Add-ons/Firefox_for_Android/API/BrowserApp
361373 if ( gBrowser ) {
362374 var aTab = gBrowser . _getTabForContentWindow ( contentWindow . top ) ;
363375 // this is the clickable tab xul element, the one found in the tab strip
364376 // of the firefox window, aTab.linkedBrowser is same as browser var above
365377 // this is the browser within the tab
366378 if ( aTab ) {
367- var browser = aTab . linkedBrowser ;
368- return browser ;
379+ return aTab . linkedBrowser ;
369380 } else {
370- this . log ( NOTE , "getBrowserForChannel: aTab was null." ) ;
381+ this . log ( NOTE , "getBrowserForChannel: aTab was null for " + spec ) ;
382+ return null ;
371383 }
372- } else {
373- // This code gets called on Android.
384+ } else if ( aDOMWindow . BrowserApp ) {
385+ // gBrowser is unavailable in Firefox for Android, and in some desktop
386+ // contexts, like the fetches for new tab tiles (which have an
387+ // associatedWindow, but no gBrowser)?
388+ // If available, try using the BrowserApp API:
389+ // https://developer.mozilla.org/en-US/Add-ons/Firefox_for_Android/API/BrowserApp
390+ // TODO: We don't get the toolbar icon on android. Probably need to fix
391+ // the gBrowser reference in toolbar_button.js.
392+ // Also TODO: Where are these log messages going? They don't show up in
393+ // remote debug console.
374394 var mTab = aDOMWindow . BrowserApp . getTabForWindow ( contentWindow . top ) ;
375395 if ( mTab ) {
376- var browser = mTab . browser ;
377- return browser ;
396+ return mTab . browser ;
378397 } else {
379- this . log ( NOTE , "getBrowserForChannel: mTab was null" ) ;
398+ this . log ( WARN , "getBrowserForChannel: mTab was null for " + spec ) ;
399+ return null ;
380400 }
401+ } else {
402+ this . log ( INFO , "getBrowserForChannel: No gBrowser and no BrowserApp for " + spec ) ;
403+ return null ;
381404 }
382405 } else {
383- this . log ( NOTE , "getBrowserForChannel: No loadContext for: " +
384- channel . URI . spec ) ;
406+ this . log ( NOTE , "getBrowserForChannel: No loadContext for " + spec ) ;
385407 return null ;
386408 }
387409 } ,
0 commit comments