home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / AIMP2 / aimp_2.61.583.exe / $TEMP / YandexPackSetup.msi / fil892922B099213BB43E3E5EE220ED8F90 < prev    next >
Text File  |  2010-07-12  |  14KB  |  432 lines

  1. var gYaDefence = {
  2.   _lastChangedTimestamp: 0,
  3.   
  4.   OVERRIDE_TIME_MONITORING: 5000,
  5.   
  6.   _init: function() {
  7.     OBSERVER_SERVICE.addObserver(this, "quit-application-granted", false);
  8.     OBSERVER_SERVICE.addObserver(this, "profile-after-change", false);
  9.     OBSERVER_SERVICE.addObserver(this, "profile-before-change", false);
  10.     OBSERVER_SERVICE.addObserver(this, "Ya-GUID-Response", false);
  11.   },
  12.   
  13.   _uninit: function() {
  14.     OBSERVER_SERVICE.removeObserver(this, "Ya-GUID-Response");
  15.     OBSERVER_SERVICE.removeObserver(this, "profile-before-change");
  16.     OBSERVER_SERVICE.removeObserver(this, "profile-after-change");
  17.     OBSERVER_SERVICE.removeObserver(this, "quit-application-granted");
  18.   },
  19.   
  20.   _profileInit: function() {
  21.     if (this.homepageDefenceEnabled && this.protectedHomepage === null && this.currentHomepage !== null) {
  22.         this.protectedHomepage = this.currentHomepage;
  23.     }
  24.     else if (/\.start2\.mozilla\.com/.test(this.currentHomepage) &&
  25.              /firefox\.yandex\./.test(this.protectedHomepage))
  26.     {
  27.       gYaSearchService.setBrowserHomePage(this.protectedHomepage);
  28.     }
  29.     else if (/\.start2\.mozilla\.com/.test(this.protectedHomepage) &&
  30.              /firefox\.yandex\./.test(this.currentHomepage))
  31.     {
  32.       this.protectedHomepage = this.currentHomepage;
  33.     }
  34.     
  35.     this.checkTimesChanged();
  36.     
  37.     var prefBranchInternal = Components.classes["@mozilla.org/preferences-service;1"]
  38.                                        .getService(Components.interfaces.nsIPrefBranchInternal);
  39.     prefBranchInternal.addObserver("yasearch.defence.homepage", this, false);
  40.     prefBranchInternal.addObserver("browser.startup.homepage", this, false);
  41.     prefBranchInternal.addObserver("yasearch.general.server.time", this, false);
  42.     
  43.     this.checkIsHomepageChanged();
  44.     
  45.     this.initFirstSearchHook();
  46.   },
  47.   
  48.   _profileUninit: function() {
  49.     var prefBranchInternal = Components.classes["@mozilla.org/preferences-service;1"]
  50.                                        .getService(Components.interfaces.nsIPrefBranchInternal);
  51.     prefBranchInternal.removeObserver("yasearch.defence.homepage", this);
  52.     prefBranchInternal.removeObserver("browser.startup.homepage", this);
  53.     prefBranchInternal.removeObserver("yasearch.general.server.time", this);
  54.   },
  55.   
  56.   observe: function(aSubject, aTopic, aData) {
  57.     switch (aTopic) {
  58.       case "quit-application-granted":
  59.         this._uninit();
  60.         break;
  61.       
  62.       case "profile-after-change":
  63.         OBSERVER_SERVICE.addObserver(this, "browser-ui-startup-complete", false);
  64.         break;
  65.       
  66.       case "browser-ui-startup-complete":
  67.         OBSERVER_SERVICE.removeObserver(this, "browser-ui-startup-complete");
  68.         this._profileInit();
  69.         break;
  70.       
  71.       case "profile-before-change":
  72.         this._profileUninit();
  73.         break;
  74.       
  75.       case "nsPref:changed":
  76.         switch (aData) {
  77.           case "yasearch.general.server.time":
  78.             var currentTimeValues = this.getChangesTimeValues();
  79.             if (currentTimeValues.start && currentTimeValues.nvalid) {
  80.               var serverTime = G_DateUtils.serverTime;
  81.               if (serverTime) {
  82.                 currentTimeValues.start = serverTime;
  83.                 currentTimeValues.nvalid = 0;
  84.                 this.setChangesTimeValues(currentTimeValues);
  85.               }
  86.             }
  87.             break;
  88.           
  89.           case "yasearch.defence.homepage.enabled":
  90.             if (this.homepageDefenceEnabled)
  91.               this.protectedHomepage = this.currentHomepage;
  92.             
  93.             break;
  94.           
  95.           case "browser.startup.homepage":
  96.             if (this.OVERRIDE_TIME_MONITORING > (Date.now() - this._lastChangedTimestamp) &&
  97.                 this.checkIsHomepageChanged(true))
  98.             { 
  99.               this.homepageDefenceEnabled = false;
  100.             } else {
  101.               this._lastChangedTimestamp = 0;
  102.               this.checkTimesChanged();
  103.             }
  104.             break;
  105.           
  106.           default:
  107.             break;
  108.         }//switch
  109.         break;//case "nsPref:changed"
  110.       
  111.       case "Ya-GUID-Response":
  112.         if (aData == "SENDED") {
  113.           this.changesTime = null;
  114.           this.fSearchValue = "SENDED";
  115.         }
  116.         break;
  117.       
  118.       default:
  119.         break;
  120.     }//switch
  121.   },
  122.   
  123.   get homepageDefenceEnabled() {
  124.     return gYaSearchService.getBoolPref("yasearch.defence.homepage.enabled");
  125.   },
  126.   
  127.   set homepageDefenceEnabled(val) {
  128.     gYaSearchService.setBoolPref("yasearch.defence.homepage.enabled", !!val);
  129.     return this.homepageDefenceEnabled;
  130.   },
  131.   
  132.   get currentHomepage() {
  133.     return gYaSearchService.getBrowserHomePage();
  134.   },
  135.   
  136.   get protectedHomepage() {
  137.     return gYaSearchService.getComplexValue("yasearch.defence.homepage.protected");
  138.   },
  139.   
  140.   set protectedHomepage(aValue) {
  141.     gYaSearchService.setComplexValue("yasearch.defence.homepage.protected", aValue);
  142.     return this.protectedHomepage;
  143.   },
  144.   
  145.   isYandexHomepage: function(aHomePageType) {
  146.     switch (aHomePageType) {
  147.       case "current":
  148.       case "protected":
  149.         return this._isYandexHomepage(this.getHostOrURLFromString(this[aHomePageType + "Homepage"]));
  150.     }
  151.     return null;
  152.   },
  153.   
  154.   _isYandexHomepage: function(aHost) {
  155.     return /(^|www\.)(yandex\.(ru|ua|kz|by|net|com)|ya\.ru)$/i.test(aHost) ||
  156.            "ru.start3.mozilla.com" === aHost;
  157.   },
  158.   
  159.   isYandexHost: function(aHost) {
  160.     return gYaSearchService.isYandexHost(aHost) || this._isYandexHomepage(aHost);
  161.   },
  162.   
  163.   getHostOrURLFromString: function(aString) {
  164.     var urlStr = (aString || "").toString().split("|")[0].replace(/^\s+|\s+$/g, "");
  165.     var domainStr = urlStr;
  166.     
  167.     try {
  168.       var uri = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService).newURI(urlStr, null, null);
  169.       if (/^(https?|file)$/.test(uri.scheme))
  170.         domainStr = uri.host;//without port
  171.     } catch(e) {}
  172.     
  173.     return domainStr;
  174.   },
  175.   
  176.   checkIsHomepageChanged: function(aIsChangedAgain) {
  177.     if (!this.homepageDefenceEnabled)
  178.       return null;
  179.     
  180.     let res = this._getProtectedDiff();
  181.     
  182.     if (res && this.isYandexHost(res.protectedHost) && this.isYandexHost(res.currentHost)) {
  183.       this.protectedHomepage = this.currentHomepage;
  184.       res = false;
  185.     }
  186.     
  187.     if (res)
  188.       this.showHomepageAlert(aIsChangedAgain);
  189.     
  190.     return res;
  191.   },
  192.   
  193.   _getProtectedDiff: function() {
  194.     let protectedHp = this.protectedHomepage;
  195.     if (protectedHp === null)
  196.       return null;
  197.     
  198.     let currentHp = this.currentHomepage;
  199.     
  200.     let protectedHost = this.getHostOrURLFromString(protectedHp);
  201.     let currentHost = this.getHostOrURLFromString(currentHp);
  202.     
  203.     if (protectedHost === currentHost)
  204.       return false;
  205.     
  206.     return {
  207.       protectedHost: protectedHost,
  208.       currentHost: currentHost
  209.     };
  210.   },
  211.   
  212.   __notifTimer: null,
  213.   
  214.   showHomepageAlert: function(aIsChangedAgain) {
  215.     this.__notifTimer = new G_Timer(function() {
  216.       if (gYaSearchService.isFirstDOMWinStuffDone) {
  217.         if (gYaDefence.__notifTimer)
  218.           gYaDefence.__notifTimer.cancel();
  219.         
  220.         new G_Timer(function() {
  221.           gYaDefence._showHomepageAlert(aIsChangedAgain);
  222.         }, 400);
  223.       
  224.       }
  225.     }, 1000, true, 15);
  226.   },
  227.   
  228.   _showHomepageAlert: function(aIsChangedAgain) {
  229.     const winType = "alert:YaHomepageDefenceAlert";
  230.     
  231.     var alertWindow = gYaSearchService.getWindow(winType);
  232.     
  233.     if (alertWindow) {
  234.       alertWindow.focus();
  235.     } else {
  236.       let flags = "chrome,popup=yes";
  237.       if (!gYaSearchService.AppInfo.browser.isGreaterThenFx35)
  238.         flags += ",titlebar=no";
  239.       
  240.       alertWindow = gYaSearchService.windowWatcher.openWindow(null, CHROME_CONTENT + "alerts/defence.alert.xul",
  241.                                                               "_blank", flags, null);
  242.       
  243.       alertWindow.arguments = [CHROME_IMAGES + "bar-logo.png", aIsChangedAgain];
  244.     }
  245.   },
  246.   
  247.   homepageAlertCallback: function(aAcceptCurrent, aProtectEnabled) {
  248.     if (!aProtectEnabled)
  249.       this.homepageDefenceEnabled = aProtectEnabled;
  250.     
  251.     if (aAcceptCurrent) {
  252.       this.protectedHomepage = this.currentHomepage;
  253.     } else {
  254.       if (this.isYandexHomepage("protected"))
  255.         this.setChangesTimeValues({prtwrk: 1});
  256.       
  257.       gYaSearchService.setBrowserHomePage(this.protectedHomepage);
  258.       
  259.       let URIFixup = Components.classes["@mozilla.org/docshell/urifixup;1"].getService(Components.interfaces.nsIURIFixup);
  260.       let protectedURL = (this.protectedHomepage || "").toString().split("|")[0].replace(/^\s+|\s+$/g, "");
  261.       try {
  262.         protectedURL = URIFixup.createFixupURI(protectedURL, URIFixup.FIXUP_FLAG_NONE);
  263.         if (protectedURL && protectedURL.spec) {
  264.           let browser = gYaSearchService.getWindow("navigator:browser");
  265.           if (browser && 'gBrowser' in browser) {
  266.             try {
  267.               browser.gBrowser.loadTabs([protectedURL.spec], false, false);
  268.             } catch(e) {}
  269.           }
  270.         }
  271.       } catch(e) {}
  272.       
  273.       this._lastChangedTimestamp = Date.now();
  274.     }
  275.   },
  276.   
  277.   _yaChangesPrefName: "yasearch.defence.homepage.changes",
  278.   
  279.   getChangesTimeValues: function() {
  280.     var res = {start: 0, total: 0, prtwrk: 0, nvalid: 0};
  281.     
  282.     var pref = (gYaSearchService.getCharPref(this._yaChangesPrefName) || "").split("|");
  283.     if (pref.length == 4) {
  284.       res.start  = gYaSearchService.parseIntFromStr(pref[0]);
  285.       res.total  = gYaSearchService.parseIntFromStr(pref[1]);
  286.       res.prtwrk = Math.min(1, gYaSearchService.parseIntFromStr(pref[2]));
  287.       res.nvalid = Math.min(1, gYaSearchService.parseIntFromStr(pref[3]));
  288.     }
  289.     
  290.     if (!res.start && res.nvalid)
  291.       res.nvalid = 0;
  292.     
  293.     if (res.total > (350 * DAY_SECS))
  294.       res.total = 0;
  295.     
  296.     return res;
  297.   },
  298.   
  299.   setChangesTimeValues: function(aTimeObject) {
  300.     var currentTime = this.getChangesTimeValues()
  301.         timePrefArray = [];
  302.     
  303.     ["start", "total", "prtwrk", "nvalid"]
  304.     .forEach(function(aType) {
  305.       if (aType in aTimeObject)
  306.         currentTime[aType] = aTimeObject[aType];
  307.       
  308.       timePrefArray.push(currentTime[aType]);
  309.     })
  310.     
  311.     gYaSearchService.setCharPref(this._yaChangesPrefName, timePrefArray.join("|"));
  312.   },
  313.   
  314.   _changesTimeWasCalculated: 0,
  315.   
  316.   TIME_CALCULATED_FLAG_TOTAL: 1,
  317.   TIME_CALCULATED_FLAG_PRTWRK: 2,
  318.   
  319.   get changesTime() {
  320.     var res = "";
  321.     this._changesTimeWasCalculated = 0;
  322.     
  323.     var currentTimeValues = this.getChangesTimeValues();
  324.     
  325.     if (currentTimeValues.start == 0 && currentTimeValues.total > 0) {
  326.       if (!this.isYandexHomepage("current") &&
  327.           (!this.homepageDefenceEnabled || !this.isYandexHomepage("protected")))
  328.       {
  329.         var time = Math.max(0, Math.ceil(currentTimeValues.total / DAY_SECS));
  330.         if (time) {
  331.           res += "&yahpchange=" + encodeURIComponent(time);
  332.           this._changesTimeWasCalculated += this.TIME_CALCULATED_FLAG_TOTAL;
  333.         }
  334.       }
  335.     }
  336.     
  337.     if (currentTimeValues.prtwrk == 1) {
  338.       res += "&prtwrk=1";
  339.       this._changesTimeWasCalculated += this.TIME_CALCULATED_FLAG_PRTWRK;
  340.     }
  341.     
  342.     return res;
  343.   },
  344.   
  345.   set changesTime(val) {
  346.     if (val === null && this._changesTimeWasCalculated) {
  347.       var newTimesData = {};
  348.       
  349.       if (this._changesTimeWasCalculated & this.TIME_CALCULATED_FLAG_TOTAL)
  350.         newTimesData.total = 0;
  351.       
  352.       if (this._changesTimeWasCalculated & this.TIME_CALCULATED_FLAG_PRTWRK)
  353.         newTimesData.prtwrk = 0;
  354.       
  355.       this.setChangesTimeValues(newTimesData);
  356.     }
  357.     
  358.     return null;
  359.   },
  360.   
  361.   checkTimesChanged: function() {
  362.     var currentTimeValues = this.getChangesTimeValues();
  363.     if (currentTimeValues.start == 0) {
  364.       if (this.isYandexHomepage("current")) {
  365.         currentTimeValues.start = Date.now();
  366.         currentTimeValues.nvalid = 1;
  367.       }
  368.     } else {
  369.       if (!this.isYandexHomepage("current")) {
  370.         if (currentTimeValues.start > 0 && !currentTimeValues.nvalid) {
  371.           if (G_DateUtils.serverTime > currentTimeValues.start)
  372.             currentTimeValues.total += (G_DateUtils.serverTime - currentTimeValues.start);
  373.         }
  374.         
  375.         currentTimeValues.start = 0;
  376.         currentTimeValues.nvalid = 0;
  377.       }
  378.     }
  379.     
  380.     this.setChangesTimeValues(currentTimeValues);
  381.   },
  382.   
  383.   FIRST_SEARCH_PREF_NAME: "yasearch.general.search.first",
  384.   
  385.   fSearchValue: null,
  386.   fSearchStatData: null,
  387.   
  388.   initFirstSearchHook: function() {
  389.     var fSearchPrefValue = gYaSearchService.getIntPref(this.FIRST_SEARCH_PREF_NAME);
  390.     if (fSearchPrefValue == 0 || fSearchPrefValue == 1) {
  391.       
  392.       delete this.fSearchStatData;
  393.       this.__defineGetter__("fSearchStatData", function() {
  394.         if (gYaSearchService.getIntPref(this.FIRST_SEARCH_PREF_NAME) == 1)
  395.           return "&action=firstsearch";
  396.         
  397.         return null;
  398.       });
  399.       
  400.       delete this.fSearchValue;
  401.       this.__defineSetter__("fSearchValue", function(aValue) {
  402.         switch (aValue) {
  403.           case "SENDING":
  404.             if (gYaSearchService.getIntPref(this.FIRST_SEARCH_PREF_NAME) == 0)
  405.               gYaSearchService.setIntPref(this.FIRST_SEARCH_PREF_NAME, 1);
  406.             break;
  407.  
  408.           case "SENDED":
  409.             if (gYaSearchService.getIntPref(this.FIRST_SEARCH_PREF_NAME) == 1)
  410.               gYaSearchService.setIntPref(this.FIRST_SEARCH_PREF_NAME, 2);
  411.             break;
  412.  
  413.           default:
  414.             break;
  415.         }
  416.       });
  417.       
  418.       var origGetSearchEngineUrl = gYaSearchService.getSearchEngineUrl;
  419.       
  420.       gYaSearchService.getSearchEngineUrl = function() {
  421.         var result = origGetSearchEngineUrl.apply(this, arguments);
  422.         
  423.         if (result && result.isYandexSearch)
  424.           gYaDefence.fSearchValue = "SENDING";
  425.         
  426.         return result;
  427.       }
  428.     }
  429.   }
  430. }
  431.  
  432. gYaDefence._init();