
Components.utils.import("resource://avg/Preferences.js");var EXPORTED_SYMBOLS=["adapter_avgdnt","adapter_avgdnt_verdicts"];const PrefBranch="avg.userPreferences.DNT.";var _AvgDntPolicy=null;var adapter_avgdnt_verdicts={verdicts:{Disabled:-2,None:-1,Green:0,Yellow:1,Red:2}};var adapter_avgdnt={STARTUP_DISABLED_DNT:false,GetTrackerDetails:function(trackerId){if(_AvgDntPolicy==null){_AvgDntPolicy=Components.classes["@mozilla.org/content-policy;1"].getService().wrappedJSObject;}
var dbO=_AvgDntPolicy.dbO['items']||[];for(var j=0;j<dbO.length;j++){if(dbO[j]['aid']==trackerId){var trackerDetails={'id':dbO[j]['aid'],'category':dbO[j]['category'],'name':dbO[j]['name'],'blocked':_AvgDntPolicy.TrackerBlocked(trackerId)?1:0,'action':dbO[j]['action'],'score':dbO[j]['score'],'psnl_dc':dbO[j]['psnl_dc'],'anon_dc':dbO[j]['anon_dc'],'data_shr':dbO[j]['data_shr'],'data_ret':dbO[j]['data_ret'],'opt_out':dbO[j]['opt_out'],'prv_policy':dbO[j]['prv_policy']};return JSON.stringify(trackerDetails);}}
return null;},GetTrackersDB:function(){var avgDntPolicy=Components.classes["@mozilla.org/content-policy;1"].getService().wrappedJSObject;var dbO=avgDntPolicy.dbO['items']||[];return JSON.stringify(dbO);},UI_Elements:{DNT:""},TrackersStorage:{},TabID:0,StoreTrackers:function(aBrowser,TRACKER_ID){var URI=aBrowser.contentWindow.location.href;var data={};try{if(typeof aBrowser.dntID=="undefined"||aBrowser.dntID==null){aBrowser.dntID=adapter_avgdnt.TabID;aBrowser.dntURI=URI;adapter_avgdnt.TabID++;}else{if(aBrowser.dntURI!=URI){adapter_avgdnt.ResetTrackerForTab(aBrowser.dntID);aBrowser.dntURI=URI;}}
if(!adapter_avgdnt.TrackersStorage[aBrowser.dntID]){adapter_avgdnt.TrackersStorage[aBrowser.dntID]=[];}
if(adapter_avgdnt.TrackersStorage[aBrowser.dntID].indexOf(TRACKER_ID)>-1){return;}
adapter_avgdnt.TrackersStorage[aBrowser.dntID].push(TRACKER_ID);data["dntID"]=aBrowser.dntID;Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService).notifyObservers(null,"dntOnChange",JSON.stringify(data));}catch(ex){dump("\nStoreTrackers error: "+ex+"\n");}},ResetTrackerForTab:function(dntID){if(adapter_avgdnt.TrackersStorage[dntID]){adapter_avgdnt.TrackersStorage[dntID]=[];}},GetTrackersForTab:function(dntID){try{if(adapter_avgdnt.TrackersStorage[dntID]){return adapter_avgdnt.TrackersStorage[dntID];}
return null;}catch(ex){dump("\nGetTrackersForTab error"+ex+"\n");return null;}},GetDNTVerdictForTab:function(dntID){var tabTrackers=this.GetTrackersForTab(dntID);var verdict=adapter_avgdnt_verdicts.verdicts.None;var result={"verdict":verdict,"trackersFound":0,"trackersBlocked":0};if(tabTrackers==null){return result;}
var trackersFound=tabTrackers.length;var trackerObj=null;var trackersBlocked=0;for(var i in tabTrackers){trackerObj=this.GetTrackerDetails(tabTrackers[i]);trackerObj=JSON.parse(trackerObj);if(trackerObj["blocked"]==0&&trackerObj["action"]=="block"){verdict=adapter_avgdnt_verdicts.verdicts.Red;break;}
if(trackerObj["blocked"]==1){trackersBlocked++;}}
if(verdict!=adapter_avgdnt_verdicts.verdicts.Red&&trackersFound>trackersBlocked){verdict=adapter_avgdnt_verdicts.verdicts.Yellow;}else if(verdict!=adapter_avgdnt_verdicts.verdicts.Red&&trackersFound==trackersBlocked){verdict=adapter_avgdnt_verdicts.verdicts.Green}
result["verdict"]=verdict;result["trackersFound"]=trackersFound;result["trackersBlocked"]=trackersBlocked;return result;},DNTGetState:function(){return Preferences.get(PrefBranch+"Disabled",false);},DNTDBState:false,DisableDNT:function(state){Preferences.set(PrefBranch+"Disabled",state);if(_AvgDntPolicy==null){_AvgDntPolicy=Components.classes["@mozilla.org/content-policy;1"].getService().wrappedJSObject;}
_AvgDntPolicy.disableDNT(state);var wm=Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);var browserWindow=wm.getMostRecentWindow("navigator:browser");var data={};if(typeof browserWindow.gBrowser.selectedBrowser.dntID!="undefined"){data["dntID"]="-1";Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService).notifyObservers(null,"dntOnChange",JSON.stringify(data));}}};