/*
 * Copyright 2014, Red Hat, Inc. and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.zanata.action;

import lombok.extern.slf4j.Slf4j;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
 */
@AutoCreate
@Name("ajaxCounter")
@Slf4j
public class AjaxCounterBean {
    // NB this doesn't handle setInterval or setTimeout
    // TODO handle XHR.abort (NB: readystatechange listener triggers on Chrome 45, despite MDN docs)
    // http://stackoverflow.com/questions/4410218/trying-to-keep-track-of-number-of-outstanding-ajax-requests-in-firefox
    private static final String AJAX_COUNTER_SCRIPT = "<script type=\"application/javascript\">\n" +
            "(function(xhr) {\n" +
            "    if (xhr.active === undefined) {\n" +
            "        xhr.active = 0;\n" +
            "        var pt = xhr.prototype;\n" +
            "        var _send = pt.send;\n" +
            "        pt.send = function() {\n" +
            "            xhr.active++;\n" +
            "            this.addEventListener('readystatechange', function(e) {\n" +
            "                if ( this.readyState == XMLHttpRequest.DONE ) {\n" +
            "                    setTimeout(function() {\n" +
            "                        xhr.active--;\n" +
            "                    }, 1);\n" +
            "                }\n" +
            "            });\n" +
            "            _send.apply(this, arguments);\n" +
            "        }\n" +
            "    }\n" +
            "})(XMLHttpRequest);\n" +
            "</script>\n";

    @In
    private HttpServletRequest httpRequest;

    public String getAjaxCounterScript() {
        String propName = "zanata.countAjax";
        if (Boolean.getBoolean(propName)) {
            return AJAX_COUNTER_SCRIPT;
        }
        return "";
    }

    public String getJavascriptFinishedScript() {
        String propName = "zanata.countAjax";
        String scriptUrl = httpRequest.getContextPath() +
                "/javax.faces.resource/test/finished.js.seam?ln=script";
        if (Boolean.getBoolean(propName)) {
            return "<script defer=\"defer\" type=\"application/javascript\" " +
                    "src=\"" + scriptUrl + "\"></script>";
        }
        return "";
    }
}

@@ -1527,16 +1527,83 @@
 N docs)%0A
+    // TODO handle send() being called again while request is open%0A
     // h
@@ -1715,16 +1715,16 @@
 firefox%0A
-
     priv
@@ -2122,21 +2122,56 @@
     
-xhr.active++;
+if (this.hasActiveEventListener === undefined) %7B
 %5Cn%22 
@@ -2177,32 +2177,36 @@
  +%0A            %22
+    
             this
@@ -2252,17 +2252,16 @@
 unction(
-e
 ) %7B%5Cn%22 +
@@ -2266,32 +2266,36 @@
  +%0A            %22
+    
                 
@@ -2380,16 +2380,20 @@
         
+ 
+   
 setTimeo
@@ -2418,32 +2418,36 @@
  +%0A            %22
+    
                 
@@ -2506,16 +2506,20 @@
         
+    
 %7D, 1);%5Cn
@@ -2543,32 +2543,36 @@
                 
+   
+ 
 %7D%5Cn%22 +%0A         
@@ -2587,18 +2587,166 @@
         
-%7D)
+    %7D);%5Cn%22 +%0A            %22                this.hasActiveEventListener = true;%5Cn%22 +%0A            %22            %7D%22 +%0A            %22            xhr.active++
 ;%5Cn%22 +%0A 

Handle reused XMLHttpRequest objects