/* See license.txt for terms of usage */

FBL.ns(function() { with (FBL) {

// ************************************************************************************************
// Model implementation

Firebug.JSONViewerModel = extend(Firebug.Module,
{
    initialize: function() 
    {
        Firebug.NetMonitor.NetInfoBody.addListener(this);

        // Used by Firebug.DOMPanel.DirTable domplate.
        this.toggles = {};
    },

    shutdown: function() 
    {
        Firebug.NetMonitor.NetInfoBody.removeListener(this);
    },

    initTabBody: function(infoBox, file)
    {
        if (FBTrace.DBG_JSONVIEWER)
            FBTrace.sysout("jsonviewer.JSONViewerModel.initTabBody", infoBox);

        const maybeHarmful = /[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/;
        const jsonStrings = /"(\\.|[^"\\\n\r])*"/g;

        // xxxadatta02: not every JSON response is going to have this header...
        // need some way to override this
        var contentType = new String(file.request.contentType).toLowerCase();
        if ((contentType != "application/json" &&
            contentType != "text/plain" &&
            contentType != "text/x-json" &&
            contentType != "text/javascript"))
            return;

        file.jsonObject = this.parseJSON(file);

        // Check the file.request content-type and display
        // the tab only when appropriate.
        if (file.jsonObject)
            Firebug.NetMonitor.NetInfoBody.appendTab(infoBox, "JSON",
                $STR("jsonviewer.tab.JSON"));
    },

    // Update listener for TabView
    updateTabBody: function(infoBox, file, context)
    {
        var tab = infoBox.selectedTab;
        var tabBody = getElementByClass(infoBox, "netInfoJSONText");
        if (!hasClass(tab, "netInfoJSONTab") || tabBody.updated)
            return;

        tabBody.updated = true;

        if (file.jsonObject) {
            Firebug.DOMPanel.DirTable.tag.replace(
                 {object: file.jsonObject, toggles: this.toggles}, tabBody);
        }
    },
    
    parseJSON: function(file)
    {
        var jsonString = new String(file.responseText);
        return parseJSONString(jsonString, "http://" + file.request.originalURI.host);
    },
    
}); 

// ************************************************************************************************
// Registration

Firebug.registerModule(Firebug.JSONViewerModel);
}});

@@ -1036,20 +1036,25 @@
 tentType
- != 
+.indexOf(
 %22applica
@@ -1059,24 +1059,31 @@
 cation/json%22
+) != 0)
  &&%0A        
@@ -1078,32 +1078,33 @@
  &&%0A            
+(
 contentType != %22
@@ -1094,28 +1094,33 @@
 (contentType
- != 
+.indexOf(
 %22text/plain%22
@@ -1119,16 +1119,23 @@
 t/plain%22
+) != 0)
  &&%0A    
@@ -1134,32 +1134,33 @@
  &&%0A            
+(
 contentType != %22
@@ -1150,28 +1150,33 @@
 (contentType
- != 
+.indexOf(
 %22text/x-json
@@ -1176,16 +1176,23 @@
 /x-json%22
+) != 0)
  &&%0A    
@@ -1199,16 +1199,17 @@
         
+(
 contentT
@@ -1211,20 +1211,25 @@
 tentType
- != 
+.indexOf(
 %22text/ja
@@ -1238,16 +1238,22 @@
 script%22)
+ != 0)
 )%0A      

Fix for filtering JSON responses.