/*
 * Copyright (C) 2010 APN, LLC. All rights reserved
 * References to external sources embedded in the code.
 * 
 * Author: Alexander J. Vincent
 * Description: HTTP header injection for certain partners.
 */
var ATB_HTTP_Headers=function(){this._initProgressListeners()};ATB_HTTP_Headers._headerName="X-toolbar-ask.com";ATB_HTTP_Headers.prototype._acceptedDomains={};ATB_HTTP_Headers.prototype._headerValue="hello world";ATB_HTTP_Headers.prototype._isAttached=false;ATB_HTTP_Headers.prototype._pageLoadedEventID=null;ATB_HTTP_Headers.prototype.attach=function(){if(this._isAttached){return}Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService).addObserver(this,"http-on-modify-request",false);ATB.Logger.info("HTTP header injection has begun.");this._isAttached=true};ATB_HTTP_Headers.prototype.detach=function(){if(!this._isAttached){return}ATB.Logger.info("Stopped listening for page-loaded events from web progress listener.");Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService).removeObserver(this,"http-on-modify-request");ATB.Logger.info("HTTP header injection has ended.");this._isAttached=false};ATB_HTTP_Headers.prototype._initProgressListeners=function(){try{this._headerValue=ATB.Prefs.getCharPref(ATB.Constants.ATB_USER_AGENT_APPEND,ATB.Prefs.PREFS_SERVICE);this._initURLList();if(!this._progressInitialized){var a=gBrowser.browsers;var c=Components.interfaces.nsIWebProgress;for(var b=0;b<a.length;b++){progress=a[b].webProgress;progress.addProgressListener(this,c.NOTIFY_STATE_DOCUMENT)}gBrowser.addEventListener("TabOpen",this,true);ATB.Logger.info("Listening for page-loaded events from web progress listener.");this._progressInitialized=true}}catch(d){ATB.Logger.error("handlePageLoad:  failed to init progress listeners.  "+d)}};ATB_HTTP_Headers.prototype._initURLList=function(){var a=ATB.Prefs.getCharPref(ATB.Constants.ATB_HTTP_HEADER_WHITELIST_HOSTS);ATB.Logger.info("http headers._initURLList: "+a);if(a==""){this.checkForRefresh();ATB.Logger.info("http headers._initURLList abort, urlList is an empty string.  Are we going through an update?");return}a=ATB.Utils.jsonEval(a);var b;for(b=0;b<a.length;b++){this._acceptedDomains[a[b].toString().toLowerCase()]=true}};ATB_HTTP_Headers.prototype._setToolbarNotificationURLList=function(a){ATB.Logger.info("headers.json response:",a);a=ATB.Utils.trim(a);if(ATB.Logger.isDebugEnabled()){ATB.Logger.debug("_setToolbarNotificationURLList: Setting to "+a)}ATB.Prefs.setCharPref(ATB.Constants.ATB_HTTP_HEADER_WHITELIST_HOSTS,a);this._acceptedDomains={};this._initURLList()};ATB_HTTP_Headers.prototype.checkForRefresh=function(){var a=ATB.Prefs.getCharPref(ATB.Constants.ATB_HTTP_HEADER_WHITELIST_URI);if(!a){ATB.Logger.info("HTTP headers checkForRefresh:  No URL to load, no whitelist in preferences... uh-oh");return}var b=this;ATB.Net.getBinaryResponseTextFromUrlAsynchronously(a,function(e,c,d){b._setToolbarNotificationURLList(c);ATB.Prefs.setCharPref(ATB.Constants.ATB_HTTP_HEADER_WHITELIST_URI,"")},null,null)};ATB_HTTP_Headers.prototype.observe=function(b,a,d){b.QueryInterface(Components.interfaces.nsIHttpChannel);var c=b.URI.host.toLowerCase();if(c in this._acceptedDomains){b.setRequestHeader(ATB_HTTP_Headers._headerName,this._headerValue,true)}};ATB_HTTP_Headers.prototype.handlePageLoad=function(d,h){ATB.Logger.info("handlePageLoad: entering");var b,a;if(!(d instanceof Components.interfaces.nsIChannel)){ATB.Logger.warn("handlePageLoad: not a nsIChannel");return}try{var f=d.URI.host.toLowerCase()}catch(g){ATB.Logger.warn("handlePageLoad: no host");return}if(!(f in this._acceptedDomains)){ATB.Logger.info("handlePageLoad: domain not found: "+f);return}var c=h.document.getElementById("ask-toolbar-details");if(!c){ATB.Logger.info("handlePageLoad: target element not found");return}c.setAttribute(ATB_HTTP_Headers._headerName,this._headerValue);ATB.Logger.info("handlePageLoad: finished, "+ATB_HTTP_Headers._headerName+" = "+this._headerValue)};ATB_HTTP_Headers.prototype.onStateChange=function(b,a,c,f){try{if(c&Components.interfaces.nsIWebProgressListener.STATE_STOP){this.handlePageLoad(a,b.DOMWindow)}}catch(d){ATB.Logger.error("handlePageLoad failure: "+d)}};ATB_HTTP_Headers.prototype.onProgressChange=function(){};ATB_HTTP_Headers.prototype.onStatusChange=function(){};ATB_HTTP_Headers.prototype.onSecurityChange=function(){};ATB_HTTP_Headers.prototype.onLocationChange=function(){};ATB_HTTP_Headers.prototype.handleEvent=function(a){var c=Components.interfaces.nsIWebProgress;var b=gBrowser.getBrowserForTab(a.originalTarget).webProgress;b.addProgressListener(this,c.NOTIFY_STATE_DOCUMENT)};ATB_HTTP_Headers.prototype.QueryInterface=function(a){if(a.equals(Components.interfaces.nsIObserver)||a.equals(Components.interfaces.nsIDOMEventListener)||a.equals(Components.interfaces.nsIWebProgressListener)||a.equals(Components.interfaces.nsISupportsWeakReference)||a.equals(Components.interfaces.nsISupports)){return this}throw Components.results.NS_ERROR_NO_INTERFACE};