2011-01-28 Pavel Feldman <pfeldman@chromium.org>
[WebKit-https.git] / Source / WebCore / inspector / front-end / NetworkPanel.js
index 7b6df8b..485856e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright (C) 2007, 2008 Apple Inc.  All rights reserved.
  * Copyright (C) 2008, 2009 Anthony Ricaud <rik@webkit.org>
- * Copyright (C) 2010 Google Inc. All rights reserved.
+ * Copyright (C) 2011 Google Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -38,7 +38,6 @@ WebInspector.NetworkPanel = function()
     this._resources = [];
     this._resourcesById = {};
     this._resourcesByURL = {};
-    this._lastIdentifier = 0;
     this._staleResources = [];
     this._resourceGridNodes = {};
     this._mainResourceLoadTime = -1;
@@ -79,6 +78,11 @@ WebInspector.NetworkPanel = function()
     this._filter(this._filterAllElement, false);
 
     this._toggleGridMode();
+
+    WebInspector.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.ResourceStarted, this._onResourceStarted, this);
+    WebInspector.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.ResourceUpdated, this._onResourceUpdated, this);
+    WebInspector.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.ResourceFinished, this._onResourceUpdated, this);
+    WebInspector.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.MainResourceCommitLoad, this._onMainResourceCommitLoad, this);
 }
 
 WebInspector.NetworkPanel.prototype = {
@@ -99,7 +103,7 @@ WebInspector.NetworkPanel.prototype = {
 
     elementsToRestoreScrollPositionsFor: function()
     {
-        return [this.containerElement];
+        return [this.containerElement, this._dataGrid.scrollContainer];
     },
 
     resize: function()
@@ -114,8 +118,6 @@ WebInspector.NetworkPanel.prototype = {
         if (!this._viewingResourceMode)
             return;
         WebInspector.Panel.prototype.updateSidebarWidth.call(this, width);
-        if (this._summaryBarElement.parentElement === this.element)
-            this._summaryBarElement.style.width = width + "px";
     },
 
     updateMainViewWidth: function(width)
@@ -143,7 +145,6 @@ WebInspector.NetworkPanel.prototype = {
                 delete this._summaryBarRowNode;
             }
             this._summaryBarElement.addStyleClass("network-summary-bar-bottom");
-            this._summaryBarElement.style.setProperty("width", this.sidebarElement.offsetWidth + "px");
             this.element.appendChild(this._summaryBarElement);
             this._dataGrid.element.style.bottom = "20px";
             return;
@@ -153,11 +154,11 @@ WebInspector.NetworkPanel.prototype = {
             // Glue status to table.
             this._summaryBarRowNode = new WebInspector.NetworkTotalGridNode(this._summaryBarElement);
             this._summaryBarElement.removeStyleClass("network-summary-bar-bottom");
-            this._summaryBarElement.style.removeProperty("width");
             this._dataGrid.appendChild(this._summaryBarRowNode);
             this._dataGrid.element.style.bottom = 0;
             this._sortItems();
         }
+        this._updateOffscreenRows();
     },
 
     _resetSummaryBar: function()
@@ -214,6 +215,7 @@ WebInspector.NetworkPanel.prototype = {
         this.containerElement.appendChild(this._dataGrid.element);
         this._dataGrid.addEventListener("sorting changed", this._sortItems, this);
         this._dataGrid.addEventListener("width changed", this._updateDividersIfNeeded, this);
+        this._dataGrid.scrollContainer.addEventListener("scroll", this._updateOffscreenRows.bind(this));
 
         this._patchTimelineHeader();
     },
@@ -733,12 +735,6 @@ WebInspector.NetworkPanel.prototype = {
         this._preserveLogToggle.toggled = !this._preserveLogToggle.toggled;
     },
 
-    reset: function()
-    {
-        if (!this._preserveLogToggle.toggled)
-            this._reset();
-    },
-
     _reset: function()
     {
         this._popoverHelper.hidePopup();
@@ -771,32 +767,44 @@ WebInspector.NetworkPanel.prototype = {
 
     get resources()
     {
-        return this._resourcesById;
+        return this._resources;
     },
 
-    refreshResource: function(resource)
+    resourceById: function(id)
     {
-        if (!resource.identifier)
-            resource.identifier = "network:" + this._lastIdentifier++;
+        return this._resourcesById[id];
+    },
 
-        if (!this._resourcesById[resource.identifier]) {
-            this._resources.push(resource);
-            this._resourcesById[resource.identifier] = resource;
-            this._resourcesByURL[resource.url] = resource;
+    _onResourceStarted: function(event)
+    {
+        this._appendResource(event.data);
+    },
 
-            // Pull all the redirects of the main resource upon commit load.
-            if (resource.redirects) {
-                for (var i = 0; i < resource.redirects.length; ++i)
-                    this.refreshResource(resource.redirects[i]);
-            }
+    _appendResource: function(resource)
+    {
+        this._resources.push(resource);
+        this._resourcesById[resource.identifier] = resource;
+        this._resourcesByURL[resource.url] = resource;
+
+        // Pull all the redirects of the main resource upon commit load.
+        if (resource.redirects) {
+            for (var i = 0; i < resource.redirects.length; ++i)
+                this._refreshResource(resource.redirects[i]);
         }
 
+        this._refreshResource(resource);
+    },
+
+    _onResourceUpdated: function(event)
+    {
+        this._refreshResource(event.data);
+    },
+
+    _refreshResource: function(resource)
+    {
         this._staleResources.push(resource);
         this._scheduleRefresh();
 
-        if (!resource)
-            return;
-
         var oldView = WebInspector.ResourceView.existingResourceViewForResource(resource);
         if (!oldView)
             return;
@@ -809,6 +817,24 @@ WebInspector.NetworkPanel.prototype = {
             this.visibleView = newView;
     },
 
+    clear: function()
+    {
+        if (this._preserveLogToggle.toggled)
+            return;
+        this._reset();
+    },
+
+    _onMainResourceCommitLoad: function()
+    {
+        if (this._preserveLogToggle.toggled)
+            return;
+
+        this._reset();
+        // Now resurrect the main resource along with all redirects that lead to it.
+        var resourcesToAppend = (WebInspector.mainResource.redirects || []).concat(WebInspector.mainResource);
+        resourcesToAppend.forEach(this._appendResource, this);
+    },
+
     canShowSourceLine: function(url, line)
     {
         return !!this._resourcesByURL[url];
@@ -905,7 +931,6 @@ WebInspector.NetworkPanel.prototype = {
             this._viewsContainerElement.addStyleClass("hidden");
             this.sidebarElement.style.right = 0;
             this.sidebarElement.style.removeProperty("width");
-            this._summaryBarElement.style.removeProperty("width");
             if (this._dataGrid.selectedNode)
                 this._dataGrid.selectedNode.selected = false;
         }
@@ -978,7 +1003,7 @@ WebInspector.NetworkPanel.prototype = {
     _contextMenu: function(event)
     {
         // createBlobURL is enabled conditionally, do not expose resource export if it's not available.
-        if (typeof window.createObjectURL !== "function" || !Preferences.resourceExportEnabled)
+        if (typeof window.webkitURL.createObjectURL !== "function" || !Preferences.resourceExportEnabled)
             return;
 
         var contextMenu = new WebInspector.ContextMenu();
@@ -995,13 +1020,43 @@ WebInspector.NetworkPanel.prototype = {
         var harArchive = {
             log: (new WebInspector.HARLog()).build()
         }
-        offerFileForDownload(JSON.stringify(harArchive));
+        InspectorFrontendHost.copyText(JSON.stringify(harArchive));
     },
 
     _exportResource: function(resource)
     {
         var har = (new WebInspector.HAREntry(resource)).build();
-        offerFileForDownload(JSON.stringify(har));
+        InspectorFrontendHost.copyText(JSON.stringify(har));
+    },
+
+    _updateOffscreenRows: function(e)
+    {
+        var dataTableBody = this._dataGrid.dataTableBody;
+        var rows = dataTableBody.children;
+        var recordsCount = rows.length;
+        if (recordsCount < 2)
+            return;  // Filler row only.
+
+        var visibleTop = this._dataGrid.scrollContainer.scrollTop;
+        var visibleBottom = visibleTop + this._dataGrid.scrollContainer.offsetHeight;
+
+        var rowHeight = rows[0].offsetHeight;
+
+        // Filler is at recordsCount - 1.
+        for (var i = 0; i < recordsCount - 1; ++i) {
+            var row = rows[i];
+            // Don't touch summaty - quit instead.
+            if (row === this._summaryBarRowNode)
+                break;
+            var rowIsVisible = i * rowHeight < visibleBottom && (i + 1) * rowHeight > visibleTop;
+            if (rowIsVisible !== row.rowIsVisible) {
+                if (rowIsVisible)
+                    row.removeStyleClass("offscreen");
+                else
+                    row.addStyleClass("offscreen");
+                row.rowIsVisible = rowIsVisible;
+            }
+        }
     }
 }
 
@@ -1230,7 +1285,7 @@ WebInspector.NetworkTimeCalculator.prototype = {
 
     formatValue: function(value)
     {
-        return Number.secondsToString(value, WebInspector.UIString);
+        return Number.secondsToString(value);
     },
 
     _lowerBound: function(resource)
@@ -1254,7 +1309,7 @@ WebInspector.NetworkTransferTimeCalculator = function()
 WebInspector.NetworkTransferTimeCalculator.prototype = {
     formatValue: function(value)
     {
-        return Number.secondsToString(value, WebInspector.UIString);
+        return Number.secondsToString(value);
     },
 
     _lowerBound: function(resource)
@@ -1278,7 +1333,7 @@ WebInspector.NetworkTransferDurationCalculator = function()
 WebInspector.NetworkTransferDurationCalculator.prototype = {
     formatValue: function(value)
     {
-        return Number.secondsToString(value, WebInspector.UIString);
+        return Number.secondsToString(value);
     },
 
     _upperBound: function(resource)
@@ -1408,15 +1463,7 @@ WebInspector.NetworkDataGridNode.prototype = {
         if (this._resource.category === WebInspector.resourceCategories.images) {
             var previewImage = document.createElement("img");
             previewImage.className = "image-network-icon-preview";
-
-            function onResourceContent()
-            {
-                previewImage.src = this._resource.contentURL;
-            }
-            if (Preferences.useDataURLForResourceImageIcons)
-                this._resource.requestContent(onResourceContent.bind(this));
-            else
-                previewImage.src = this._resource.url;
+            this._resource.populateImageSource(previewImage);
 
             var iconElement = document.createElement("div");
             iconElement.className = "icon";