<?xml version='1.0'?>
<!DOCTYPE bindings SYSTEM "/ts.dtd"><bindings xmlns="http://www.mozilla.org/xbl" xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

    <binding id="button">
        <implementation implements="nsIObserver">
            <field name="API" readonly="true">this.module.API</field>

            <field name="mWindowListener" readonly="true">
                this.API.Browser.getWindowListener(window)
            </field>

            <field name="NOTIFICATION_NAME" readonly="true">"typosquatting-notification"</field>

            <field name="NOTIFICATION_OBJECT_NAME" readonly="true">"typosquatting"</field>

            <property name="statDict">
                <getter><![CDATA[return {
"1": "Fx.showuri",
"2": "Fx.showuri.uri",
"3": "Fx.showuri.buttonuri",
"4": "Fx.showuri.closeuri",
"5": "Fx.whitelist",
"6": "Fx.show",
"7": "Fx.show.button",
"8": "Fx.show.close",
"9": "Fx.showuri.ignoreuri",
"10": "Fx.show.ignore",
"11": "Fx.showuri.icon",
"12": "Fx.show.icon"};]]></getter>
            </property>

            <constructor><![CDATA[this.wdgtxConstructor();]]></constructor>

            <destructor><![CDATA[this.wdgtxDestructor();]]></destructor>

            <method name="wdgtxConstructor">
                <body><![CDATA[if (this._wdgtxConstructed)
return;
this._wdgtxConstructed = true;
this._notifications = [];
this.mWindowListener.addListener("WindowLocationChange",this);
this._checkTab(gBrowser.mCurrentBrowser);]]></body>
            </method>

            <method name="wdgtxDestructor">
                <body><![CDATA[if (! this._wdgtxConstructed)
return;
this._wdgtxConstructed = false;
this._cleanNotifications();
this._notifications = null;
this.mWindowListener.removeListener("WindowLocationChange",this);
this.mWindowListener.removeAllTabData(this.NOTIFICATION_OBJECT_NAME);]]></body>
            </method>

            
            <method name="observe">
                <parameter name="aSubject"/>
                <parameter name="aTopic"/>
                <parameter name="aData"/>
                <body><![CDATA[switch (aTopic) {
case "WindowLocationChange":
this._checkTab(aData.tab);
break;
}
]]></body>
            </method>

            <method name="releaseNotification">
                <parameter name="aNotification"/>
                <body><![CDATA[var indx = this._notifications.indexOf(aNotification);
if (indx < 0)
{
return;
}

this._notifications.splice(indx,1);]]></body>
            </method>

            <method name="addNotification">
                <parameter name="aNotification"/>
                <body><![CDATA[this._notifications.push(aNotification);]]></body>
            </method>

            <method name="_cleanNotifications">
                <body><![CDATA[this._notifications.forEach(function (notification) {
notification && (typeof notification.wdgtxDestructor === "function" && notification.wdgtxDestructor());
}
);
this._notifications = [];]]></body>
            </method>

            <method name="_getLoweredURL">
                <parameter name="uri"/>
                <body><![CDATA[try {
return uri.spec.toLowerCase();
}
catch (e) {

}

return null;]]></body>
            </method>

            <method name="_checkTab">
                <parameter name="tab"/>
                <body><![CDATA[var tabData = this.mWindowListener.getTabData(tab,this.NOTIFICATION_OBJECT_NAME);
if (tabData.show)
{
if (tabData.action)
{
return;
}

this._showNotification(tab);
return;
}

var url = this._getLoweredURL(tab.currentURI);
var referringURI = tab.webNavigation.referringURI;
var referrer = referringURI && referringURI.spec || "";
this.module.promiseCheckURL(url).then((function (result) {
if (tab !== this.mWindowListener.currentTab)
{
return;
}

if (result.block === false)
{
this.sendStatistic(5,url,referrer);
}

if (result.block)
{
this._showNotification(tab,url,result.redirectURL);
}

}
).bind(this));]]></body>
            </method>

            <method name="_showNotification">
                <parameter name="tab"/>
                <parameter name="maliciousHost"/>
                <parameter name="targetHost"/>
                <body><![CDATA[var notificationBox = gBrowser.getNotificationBox(tab);
var notification = notificationBox.getNotificationWithValue(this.NOTIFICATION_NAME);
if (! notification)
{
notificationBox.appendNotification("",this.NOTIFICATION_NAME,"",notificationBox.PRIORITY_WARNING_HIGH,[]);
notification = notificationBox.getNotificationWithValue(this.NOTIFICATION_NAME);
notification.setAttribute("data-malicious-site",maliciousHost || "");
notification.setAttribute("data-targetURL",targetHost || "");
notification.browser = tab;
notification.typo = this;
notification.setAttribute("anonid",this.NOTIFICATION_NAME);
}
else
{
notificationBox._showNotification(notification,true);
}

return notification;]]></body>
            </method>

            <method name="sendStatistic">
                <parameter name="codeID"/>
                <parameter name="badurl"/>
                <parameter name="referrer"/>
                <parameter name="to"/>
                <body><![CDATA[var path = this.statDict[codeID];
var params = {
dtype: "stred",
pid: 12,
cid: 72200,
path: path};
if (badurl)
{
params.badurl = badurl;
}

if (referrer)
{
params.refbadurl = referrer;
}

if (to)
{
params.navbadurl = to;
}

var data = [];
for(let [key, val] in Iterator(params)) data.push(key + ("=" + encodeURIComponent(val)));
data.push("*data=" + encodeURIComponent("http://yandex.ru"));
this.API.Statistics.logCustomAction(codeID);
var url = "http://clck.yandex.ru/jclck/" + data.join("/");
var request = new XMLHttpRequest();
request.open("GET",url,true);
request.send(null);]]></body>
            </method>

        </implementation>
    </binding>
</bindings>
