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 / fil3E935DBAFF54602646AC49CB1010FD89 < prev    next >
Text File  |  2010-07-12  |  12KB  |  354 lines

  1.  
  2. var gYaGeolocation;
  3.  
  4. // (!fx3.0 && !macos)
  5. if (Ci.nsIWifiMonitor &&
  6.     !/^darwin/i.test(Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo).QueryInterface(Ci.nsIXULRuntime).OS)) {
  7.  
  8. gYaGeolocation = {
  9.     GEO_API_VERSION: "1.2",
  10.     GEO_URL: "http://wi2geo.mobile.yandex.net/getlocation",
  11.     GEO_KEY: "AQIAAHBWEoNf/VB4O81cL2HOFJvtdNtP",
  12.     
  13.     TRUSTED_DOMAINS: ["ya.ru", ".yandex.ru", ".yandex.kz", ".yandex.ua",
  14.                       ".yandex.by", ".yandex.com", ".moikrug.ru"],
  15.     
  16.     UPDATE_INTERVAL: 10 * 60 * 1000,
  17.     
  18.     PREFS_PREFIX: "yasearch.geolocation.",
  19.     
  20.     _inited: false,
  21.     _updated: false,
  22.                                                            
  23.     wifi: "",
  24.     lastWifi: "",
  25.     lastIP: "",
  26.     
  27.     position: null,//{"latitude":null, "longitude":null, "precision":null},
  28.     
  29.     _watchingTimer: null,
  30.     
  31.     get enabled() {
  32.         return gYaSearchService.getBoolPref(this.PREFS_PREFIX + "enabled");
  33.     },
  34.     
  35.     get WifiMonitorService() {
  36.         return Cc["@mozilla.org/wifi/monitor;1"].getService(Ci.nsIWifiMonitor);
  37.     },
  38.     
  39.     get prefBranch() {
  40.         return Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch2);
  41.     },
  42.     
  43.     init: function YaGeoLoc_init() {
  44.         Cu.import("resource://yasearch/JSON.jsm", this);
  45.         
  46.         if (this._inited)
  47.             return;
  48.         
  49.         this._inited = true;
  50.         
  51.         OBSERVER_SERVICE.addObserver(this, "profile-before-change", false);
  52.         this.prefBranch.addObserver(this.PREFS_PREFIX, this, false);
  53.         
  54.         let dataStr = gYaSearchService.getCharPref(this.PREFS_PREFIX + "position") || "";
  55.         let savedPosition = null;
  56.         
  57.         try {
  58.             savedPosition = this.JSON.parse(dataStr);
  59.         } catch (e) {}
  60.         
  61.         if (savedPosition) {
  62.             this.position = savedPosition;
  63.             this.wifi = this.lastWifi = gYaSearchService.getCharPref(this.PREFS_PREFIX + "wifi.data") || "";
  64.         }
  65.         
  66.         this.lastIP = gYaSearchService.getCharPref(this.PREFS_PREFIX + "ip") || "";
  67.         
  68.         if (this.enabled)
  69.             this.startWatching();
  70.         
  71.         gYaSearchService.log("init\nsavedPosition: " + dataStr + "\nwifi: " + this.wifi);
  72.     },
  73.     
  74.     uninit: function YaGeoLoc_uninit() {
  75.         if (!this._inited)
  76.             return;
  77.         
  78.         this._inited = false;
  79.         
  80.         this.stopWatching();
  81.         
  82.         OBSERVER_SERVICE.removeObserver(this, "profile-before-change");
  83.         this.prefBranch.removeObserver(this.PREFS_PREFIX, this);
  84.         gYaSearchService.log("uninit");
  85.     },
  86.     
  87.     observe: function YaGeoLoc_observe(aSubject, aTopic, aData) {
  88.         switch (aTopic) {
  89.             case "profile-before-change":
  90.                 this.uninit();
  91.                 break;
  92.             
  93.             case "nsPref:changed":
  94.                 let prefName = aData.split(this.PREFS_PREFIX)[1];
  95.                 switch (prefName) {
  96.                     case "enabled":
  97.                         if (this.enabled) {
  98.                             this.startWatching();
  99.                         } else {
  100.                             this.stopWatching();
  101.                             this.removeGeoData();
  102.                         }
  103.                         break;
  104.                 }//switch
  105.                 break;//case "nsPref:changed"
  106.         
  107.         }//switch
  108.     },
  109.  
  110.     getLocalIP: function YaGeoLoc_getLocalIP() {
  111.         try {
  112.             let DNSService = Cc["@mozilla.org/network/dns-service;1"].getService(Ci.nsIDNSService);
  113.             let DNSRecord = DNSService.resolve(DNSService.myHostName, DNSService.RESOLVE_BYPASS_CACHE);
  114.             let ipAddress;
  115.             while (DNSRecord.hasMore()) {
  116.                 ipAddress = DNSRecord.getNextAddrAsString();
  117.  
  118.                 if (ipAddress.indexOf(":") >= 0)
  119.                     continue;
  120.  
  121.                 return ipAddress;
  122.             }
  123.         } catch(e) {
  124.             return "";
  125.         }
  126.     },
  127.  
  128.     removeGeoData: function YaGeoLogic_removeGeoData() {
  129.         //this.wifi = "";
  130.         this.lastWifi = "";
  131.         this.lastIP = "";
  132.         gYaSearchService.setCharPref(this.PREFS_PREFIX + "wifi.data", "");
  133.         gYaSearchService.setCharPref(this.PREFS_PREFIX + "position", "");
  134.         gYaSearchService.setCharPref(this.PREFS_PREFIX + "ip", "");
  135.     },
  136.     
  137.     startWatching: function YaGeoLoc_startWatching() {
  138.         if (!this._watchingTimer) {
  139.             this._watchingTimer = new G_Timer(this.updateOnTimer.bind(this), this.UPDATE_INTERVAL, true);
  140.             this.WifiMonitorService.startWatching(this);
  141.             this.updateOnTimer();
  142.             gYaSearchService.log("geo.StartWatching");
  143.         }
  144.     },
  145.     
  146.     stopWatching: function YaGeoLoc_stopWatching() {
  147.         if (this._watchingTimer) {
  148.             this._watchingTimer.cancel();
  149.             this._watchingTimer = null;
  150.             
  151.             this.WifiMonitorService.stopWatching(this);            
  152.             
  153.             this.removeCookies();
  154.         }
  155.     },
  156.     
  157.     updateWiFi: function YaGeoLoc_updateWiFi(aWifiString) {
  158.         this.wifi = aWifiString;
  159.         gYaSearchService.log("updateWiFi: " + aWifiString);
  160.         if (!this._updated)
  161.             this.updateOnTimer();
  162.     },
  163.     
  164.     updateOnTimer: function YaGeoLoc_updateOnTimer() {
  165.         gYaSearchService.log("updateOnTimer");
  166.         let ip = this.getLocalIP();
  167.         let ipChanged = (ip != this.lastIP);
  168.  
  169.         if (ipChanged) {
  170.             this.lastIP = ip;
  171.             gYaSearchService.setCharPref(this.PREFS_PREFIX + "ip", ip);
  172.         }
  173.  
  174.         if (!this.wifi || !this.equalWiFi(this.wifi, this.lastWifi) || !this.position) {
  175.             this.lastWifi = this.wifi;
  176.  
  177.             if (!this.wifi && !ipChanged && this.position) {
  178.                 this._updated = false;
  179.                 this.setCookies();
  180.                 return;
  181.             }
  182.             
  183.             this._updated = true;
  184.             this.getPositionByWiFi();
  185.         } else {
  186.             this._updated = false;
  187.             this.setCookies();
  188.         }
  189.     },
  190.     
  191.     equalWiFi: function YaGeoLoc_equalWiFi(wifi1, wifi2) {
  192.         let arr1 = wifi1.split(",");
  193.         let arr2 = wifi2.split(",");
  194.         
  195.         if (arr1.length != arr2.length)
  196.             return false;
  197.         
  198.         for (let i = 1, len = arr1.length; i < len; i += 3) {
  199.             if (arr1[i] != arr2[i])
  200.                 return false;
  201.         }
  202.  
  203.         return true;
  204.     },
  205.     
  206.     getPositionByWiFi: function YaGeoLoc_getPositionByWiFi() {
  207.         let params = [ "version=" + this.GEO_API_VERSION,
  208.                        "apikey=" + encodeURIComponent(this.GEO_KEY) ];
  209.         
  210.         if (this.wifi)
  211.             params.push("wifinetworks=" + encodeURIComponent(this.wifi));
  212.         
  213.         params.push("nogeocode");
  214.         
  215.         gYaSearchService.xmlHttpRequest(this.GEO_URL, {
  216.             callbackFunc: this.getPositionByWiFiCallback.bind(this),
  217.             data: params.join("&"),
  218.             background: true
  219.         });
  220.         
  221.         gYaSearchService.log("=== GetPositionByWiFi ===\nPost data:\n" + params.join("\n"));
  222.     },
  223.     
  224.     getPositionByWiFiCallback: function YaGeoLoc_getPositionByWiFiCallback(aRequest) {
  225.         try {
  226.             gYaSearchService.log("Wi2geo response: " + aRequest.target.responseText);
  227.         } catch (ex) {
  228.             gYaSearchService.log("Wi2geo response: [error]");
  229.         }
  230.         
  231.         if (gYaSearchService.isReqError(aRequest))
  232.             return;
  233.         
  234.         let responseXML = gYaSearchService.safeE4Xml(aRequest.target.responseXML, null, "wi2geo");
  235.         if (!(responseXML && responseXML.iamhere.length()))
  236.             return;
  237.         
  238.         responseXML = responseXML.iamhere[0];
  239.         
  240.         let position = { __proto__: null };
  241.         
  242.         for each (let aPositionProp in ["latitude", "longitude", "precision"]) {
  243.             let element = responseXML[aPositionProp][0];
  244.             let propValue = element ? element.toString() : null;
  245.             if (!propValue)
  246.                 return;
  247.             
  248.             position[aPositionProp] = propValue;
  249.         }
  250.         
  251.         position.timestamp = parseInt(Date.now() / 1000, 10);
  252.         
  253.         this.position = position;
  254.         gYaSearchService.setCharPref(this.PREFS_PREFIX + "position", this.JSON.stringify(position));
  255.         gYaSearchService.log("getPositionByWiFiCallback: position: " + this.JSON.stringify(position));
  256.         this.setCookies();
  257.     },
  258.     
  259.     setCookies: function YaGeoLoc_setCookies(aSubCookieValue) {
  260.         gYaSearchService.log("SetCookies");
  261.         const cookieSvc = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
  262.         
  263.         this.TRUSTED_DOMAINS.forEach(function(aDomain) {
  264.             let cookieURI = Cc["@mozilla.org/network/standard-url;1"].createInstance(Ci.nsIURI);
  265.             cookieURI.spec = "http://" + aDomain.replace(/^\./, "");
  266.             
  267.             let allCookiesStr = cookieSvc.getCookieString(cookieURI, null) || "";
  268.             let cookieMatch = allCookiesStr.match("(?:^|;)\\s*ys=([^;]*)");
  269.             let newCookieValue = this.parseCookie(cookieMatch ? decodeURIComponent(cookieMatch[1]) : "", aSubCookieValue);
  270.             
  271.             let cookieStr = "ys=" + newCookieValue//encodeURIComponent(newCookieValue);
  272.  
  273.             if(newCookieValue.length == 0) {
  274.                 let zeroDate = new Date(0);
  275.                 cookieStr += ";expires=" + zeroDate;
  276.             }
  277.  
  278.             if (aDomain.charAt(0) == ".")
  279.                 cookieStr += ";domain=" + aDomain;
  280.             
  281.             cookieSvc.setCookieString(cookieURI, null, cookieStr, null);
  282.             
  283.             gYaSearchService.log("setCookies: cookie for [" + cookieURI.spec + "]: " + cookieSvc.getCookieString(cookieURI, null));
  284.         }, this);
  285.     },
  286.  
  287.     removeCookies: function YaGeoLoc_removeCookies() {
  288.         this.setCookies("");
  289.         gYaSearchService.log("removeCookies");
  290.     },
  291.  
  292.     parseCookie: function YaGeoLoc_parseCookie(aCookieContainerStr, aSubCookieValue) {
  293.         let gpCookieText = typeof aSubCookieValue == "undefined" ? this.getCookieText() : aSubCookieValue;
  294.         
  295.         let ySubCookies = aCookieContainerStr.split("#");
  296.         let found = false;
  297.         
  298.         if (gpCookieText !== null) {
  299.             for (let i = ySubCookies.length; i--;) {
  300.                 if (ySubCookies[i].indexOf("gpauto.") == 0) {
  301.                     found = true;
  302.                     ySubCookies[i] = gpCookieText;
  303.                     break;
  304.                 }
  305.             }
  306.         }
  307.         
  308.         if (!found && gpCookieText)
  309.             ySubCookies.push(gpCookieText);
  310.         
  311.         return ySubCookies.filter(function(v) !!v).join("#");
  312.     },
  313.     
  314.     getCookieText: function YaGeoLoc_getCookieText() {
  315.         let position = this.position;
  316.         
  317.         if (position) {
  318.             try {
  319.                 return "gpauto." +
  320.                        [ position.latitude.replace(".", "_"),
  321.                          position.longitude.replace(".", "_"),
  322.                          position.precision,
  323.                          "1",
  324.                          parseInt(Date.now() / 1000, 10)
  325.                        ].join(":");
  326.             } catch(e) {}
  327.         }
  328.         
  329.         return null;
  330.     },
  331.     
  332.     onChange: function YaGeoLoc_onChange(aAccessPoints) {
  333.         let wifiData = aAccessPoints.map(function(aPoint, aIndex) {
  334.             return [ aIndex, aPoint.mac.replace(/\-/g,":"), aPoint.signal ].join(",");
  335.         }).join(",");
  336.  
  337.         let lastWifiData = gYaSearchService.getCharPref(this.PREFS_PREFIX + "wifi.data");
  338.         if (wifiData == lastWifiData)
  339.             return;
  340.  
  341.         gYaSearchService.setCharPref(this.PREFS_PREFIX + "wifi.data", wifiData);
  342.         
  343.         this.updateWiFi(wifiData);
  344.     },
  345.     
  346.     onError: function YaGeoLoc_onError(aValue) {
  347.         gYaSearchService.log("onError: Wifi scan error: " + aValue);
  348.     },
  349.     
  350.     QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIWifiListener, Ci.nsIObserver])
  351. }
  352.  
  353. }// (!fx3.0 && !macos)
  354.