Web Inspector: merge SourceMapManager into NetworkManager
authordrousso@apple.com <drousso@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 3 Oct 2018 01:49:05 +0000 (01:49 +0000)
committerdrousso@apple.com <drousso@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 3 Oct 2018 01:49:05 +0000 (01:49 +0000)
https://bugs.webkit.org/show_bug.cgi?id=190224

Reviewed by Joseph Pecoraro.

* UserInterface/Main.html:
* UserInterface/Base/Main.js:
(WI.loaded):
* UserInterface/Test.html:
* UserInterface/Test/Test.js:
(WI.loaded):

* UserInterface/Controllers/NetworkManager.js:
(WI.NetworkManager):
(WI.NetworkManager.prototype.downloadSourceMap): Added.
(WI.NetworkManager.prototype.resourceRequestWasServedFromMemoryCache):
(WI.NetworkManager.prototype.resourceRequestDidFinishLoading):
(WI.NetworkManager.prototype._createResource):
(WI.NetworkManager.prototype._loadAndParseSourceMap): Added.
(WI.NetworkManager.prototype._sourceMapLoadAndParseFailed): Added.
(WI.NetworkManager.prototype._sourceMapLoadAndParseSucceeded): Added.
(WI.NetworkManager.prototype._handleFrameMainResourceDidChange): Added.
* UserInterface/Controllers/SourceMapManager.js: Removed.

* UserInterface/Models/Script.js:
(WI.Script):

* WebInspectorUI.vcxproj/WebInspectorUI.vcxproj:
* WebInspectorUI.vcxproj/WebInspectorUI.vcxproj.filters:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@236783 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebInspectorUI/ChangeLog
Source/WebInspectorUI/UserInterface/Base/Main.js
Source/WebInspectorUI/UserInterface/Controllers/NetworkManager.js
Source/WebInspectorUI/UserInterface/Controllers/SourceMapManager.js [deleted file]
Source/WebInspectorUI/UserInterface/Main.html
Source/WebInspectorUI/UserInterface/Models/Script.js
Source/WebInspectorUI/UserInterface/Test.html
Source/WebInspectorUI/UserInterface/Test/Test.js
Source/WebInspectorUI/WebInspectorUI.vcxproj/WebInspectorUI.vcxproj
Source/WebInspectorUI/WebInspectorUI.vcxproj/WebInspectorUI.vcxproj.filters

index e0574ad..6b7f7cb 100644 (file)
@@ -1,3 +1,35 @@
+2018-10-02  Devin Rousso  <drousso@apple.com>
+
+        Web Inspector: merge SourceMapManager into NetworkManager
+        https://bugs.webkit.org/show_bug.cgi?id=190224
+
+        Reviewed by Joseph Pecoraro.
+
+        * UserInterface/Main.html:
+        * UserInterface/Base/Main.js:
+        (WI.loaded):
+        * UserInterface/Test.html:
+        * UserInterface/Test/Test.js:
+        (WI.loaded):
+
+        * UserInterface/Controllers/NetworkManager.js:
+        (WI.NetworkManager):
+        (WI.NetworkManager.prototype.downloadSourceMap): Added.
+        (WI.NetworkManager.prototype.resourceRequestWasServedFromMemoryCache):
+        (WI.NetworkManager.prototype.resourceRequestDidFinishLoading):
+        (WI.NetworkManager.prototype._createResource):
+        (WI.NetworkManager.prototype._loadAndParseSourceMap): Added.
+        (WI.NetworkManager.prototype._sourceMapLoadAndParseFailed): Added.
+        (WI.NetworkManager.prototype._sourceMapLoadAndParseSucceeded): Added.
+        (WI.NetworkManager.prototype._handleFrameMainResourceDidChange): Added.
+        * UserInterface/Controllers/SourceMapManager.js: Removed.
+
+        * UserInterface/Models/Script.js:
+        (WI.Script):
+
+        * WebInspectorUI.vcxproj/WebInspectorUI.vcxproj:
+        * WebInspectorUI.vcxproj/WebInspectorUI.vcxproj.filters:
+
 2018-10-02  Nikita Vasilyev  <nvasilyev@apple.com>
 
         Web Inspector: Styles: start editing property name/value on mouseup instead of mousedown
index 3ff34d6..c8f0ceb 100644 (file)
@@ -123,7 +123,6 @@ WI.loaded = function()
     this.applicationCacheManager = new WI.ApplicationCacheManager;
     this.timelineManager = new WI.TimelineManager;
     this.debuggerManager = new WI.DebuggerManager;
-    this.sourceMapManager = new WI.SourceMapManager;
     this.layerTreeManager = new WI.LayerTreeManager;
     this.probeManager = new WI.ProbeManager;
     this.workerManager = new WI.WorkerManager;
index 0541bfb..674f5d2 100644 (file)
@@ -37,6 +37,9 @@ WI.NetworkManager = class NetworkManager extends WI.Object
 
         this._waitingForMainFrameResourceTreePayload = true;
 
+        this._sourceMapURLMap = new Map;
+        this._downloadingSourceMaps = new Set;
+
         if (window.PageAgent) {
             PageAgent.enable();
             PageAgent.getResourceTree(this._processMainFrameResourceTreePayload.bind(this));
@@ -49,6 +52,7 @@ WI.NetworkManager = class NetworkManager extends WI.Object
             NetworkAgent.enable();
 
         WI.notifications.addEventListener(WI.Notification.ExtraDomainsActivated, this._extraDomainsActivated, this);
+        WI.Frame.addEventListener(WI.Frame.Event.MainResourceDidChange, this._handleFrameMainResourceDidChange, this);
     }
 
     // Public
@@ -73,6 +77,45 @@ WI.NetworkManager = class NetworkManager extends WI.Object
         return this._resourceRequestIdentifierMap.get(requestIdentifier) || null;
     }
 
+    downloadSourceMap(sourceMapURL, baseURL, originalSourceCode)
+    {
+        // The baseURL could have come from a "//# sourceURL". Attempt to get a
+        // reasonable absolute URL for the base by using the main resource's URL.
+        if (WI.networkManager.mainFrame)
+            baseURL = absoluteURL(baseURL, WI.networkManager.mainFrame.url);
+
+        if (sourceMapURL.startsWith("data:")) {
+            this._loadAndParseSourceMap(sourceMapURL, baseURL, originalSourceCode);
+            return;
+        }
+
+        sourceMapURL = absoluteURL(sourceMapURL, baseURL);
+        if (!sourceMapURL)
+            return;
+
+        console.assert(originalSourceCode.url);
+        if (!originalSourceCode.url)
+            return;
+
+        // FIXME: <rdar://problem/13265694> Source Maps: Better handle when multiple resources reference the same SourceMap
+
+        if (this._sourceMapURLMap.has(sourceMapURL) || this._downloadingSourceMaps.has(sourceMapURL))
+            return;
+
+        let loadAndParseSourceMap = () => {
+            this._loadAndParseSourceMap(sourceMapURL, baseURL, originalSourceCode);
+        };
+
+        if (!WI.networkManager.mainFrame) {
+            // If we don't have a main frame, then we are likely in the middle of building the resource tree.
+            // Delaying until the next runloop is enough in this case to then start loading the source map.
+            setTimeout(loadAndParseSourceMap, 0);
+            return;
+        }
+
+        loadAndParseSourceMap();
+    }
+
     frameDidNavigate(framePayload)
     {
         // Called from WI.PageObserver.
@@ -340,7 +383,7 @@ WI.NetworkManager = class NetworkManager extends WI.Object
         console.assert(resource.cached, "This resource should be classified as cached since it was served from the MemoryCache", resource);
 
         if (cachedResourcePayload.sourceMapURL)
-            WI.sourceMapManager.downloadSourceMap(cachedResourcePayload.sourceMapURL, resource.url, resource);
+            this.downloadSourceMap(cachedResourcePayload.sourceMapURL, resource.url, resource);
 
         // No need to associate the resource with the requestIdentifier, since this is the only event
         // sent for memory cache resource loads.
@@ -435,7 +478,7 @@ WI.NetworkManager = class NetworkManager extends WI.Object
         resource.markAsFinished(elapsedTime);
 
         if (sourceMapURL)
-            WI.sourceMapManager.downloadSourceMap(sourceMapURL, resource.url, resource);
+            this.downloadSourceMap(sourceMapURL, resource.url, resource);
 
         this._resourceRequestIdentifierMap.delete(requestIdentifier);
     }
@@ -701,7 +744,7 @@ WI.NetworkManager = class NetworkManager extends WI.Object
         var resource = new WI.Resource(payload.url, payload.mimeType, payload.type, framePayload.loaderId, payload.targetId);
 
         if (payload.sourceMapURL)
-            WI.sourceMapManager.downloadSourceMap(payload.sourceMapURL, resource.url, resource);
+            this.downloadSourceMap(payload.sourceMapURL, resource.url, resource);
 
         return resource;
     }
@@ -769,11 +812,102 @@ WI.NetworkManager = class NetworkManager extends WI.Object
         this.dispatchEventToListeners(WI.NetworkManager.Event.MainFrameDidChange, {oldMainFrame});
     }
 
+    _loadAndParseSourceMap(sourceMapURL, baseURL, originalSourceCode)
+    {
+        this._downloadingSourceMaps.add(sourceMapURL);
+
+        let sourceMapLoaded = (error, content, mimeType, statusCode) => {
+            if (error || statusCode >= 400) {
+                this._sourceMapLoadAndParseFailed(sourceMapURL);
+                return;
+            }
+
+            if (content.slice(0, 3) === ")]}") {
+                let firstNewlineIndex = content.indexOf("\n");
+                if (firstNewlineIndex === -1) {
+                    this._sourceMapLoadAndParseFailed(sourceMapURL);
+                    return;
+                }
+
+                content = content.substring(firstNewlineIndex);
+            }
+
+            try {
+                let payload = JSON.parse(content);
+                let baseURL = sourceMapURL.startsWith("data:") ? originalSourceCode.url : sourceMapURL;
+                let sourceMap = new WI.SourceMap(baseURL, payload, originalSourceCode);
+                this._sourceMapLoadAndParseSucceeded(sourceMapURL, sourceMap);
+            } catch {
+                this._sourceMapLoadAndParseFailed(sourceMapURL);
+            }
+        };
+
+        if (sourceMapURL.startsWith("data:")) {
+            let {mimeType, base64, data} = parseDataURL(sourceMapURL);
+            let content = base64 ? atob(data) : data;
+            sourceMapLoaded(null, content, mimeType, 0);
+            return;
+        }
+
+        if (!window.NetworkAgent) {
+            this._sourceMapLoadAndParseFailed(sourceMapURL);
+            return;
+        }
+
+        let frameIdentifier = null;
+        if (originalSourceCode instanceof WI.Resource && originalSourceCode.parentFrame)
+            frameIdentifier = originalSourceCode.parentFrame.id;
+
+        if (!frameIdentifier)
+            frameIdentifier = WI.networkManager.mainFrame ? WI.networkManager.mainFrame.id : "";
+
+        NetworkAgent.loadResource(frameIdentifier, sourceMapURL, sourceMapLoaded);
+    }
+
+    _sourceMapLoadAndParseFailed(ssourceMapLurceMapURL)
+    {
+        this._downloadingSourceMaps.delete(sourceMapURL);
+    }
+
+    _sourceMapLoadAndParseSucceeded(sourceMapURL, sourceMap)
+    {
+        if (!this._downloadingSourceMaps.has(sourceMapURL))
+            return;
+
+        this._downloadingSourceMaps.delete(sourceMapURL);
+
+        this._sourceMapURLMap.set(sourceMapURL, sourceMap);
+
+        for (let source of sourceMap.sources())
+            sourceMap.addResource(new WI.SourceMapResource(source, sourceMap));
+
+        // Associate the SourceMap with the originalSourceCode.
+        sourceMap.originalSourceCode.addSourceMap(sourceMap);
+
+        // If the originalSourceCode was not a Resource, be sure to also associate with the Resource if one exists.
+        // FIXME: We should try to use the right frame instead of a global lookup by URL.
+        if (!(sourceMap.originalSourceCode instanceof WI.Resource)) {
+            console.assert(sourceMap.originalSourceCode instanceof WI.Script);
+            let resource = sourceMap.originalSourceCode.resource;
+            if (resource)
+                resource.addSourceMap(sourceMap);
+        }
+    }
+
     _extraDomainsActivated(event)
     {
         if (event.data.domains.includes("Page") && window.PageAgent)
             PageAgent.getResourceTree(this._processMainFrameResourceTreePayload.bind(this));
     }
+
+    _handleFrameMainResourceDidChange(event)
+    {
+        if (!event.target.isMainFrame())
+            return;
+
+        this._sourceMapURLMap.clear();
+        this._downloadingSourceMaps.clear();
+    }
 };
 
 WI.NetworkManager.Event = {
diff --git a/Source/WebInspectorUI/UserInterface/Controllers/SourceMapManager.js b/Source/WebInspectorUI/UserInterface/Controllers/SourceMapManager.js
deleted file mode 100644 (file)
index f594ffe..0000000
+++ /dev/null
@@ -1,184 +0,0 @@
-/*
- * Copyright (C) 2013 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-WI.SourceMapManager = class SourceMapManager extends WI.Object
-{
-    constructor()
-    {
-        super();
-
-        this._sourceMapURLMap = {};
-        this._downloadingSourceMaps = {};
-
-        WI.Frame.addEventListener(WI.Frame.Event.MainResourceDidChange, this._mainResourceDidChange, this);
-    }
-
-    // Public
-
-    sourceMapForURL(sourceMapURL)
-    {
-        return this._sourceMapURLMap[sourceMapURL];
-    }
-
-    downloadSourceMap(sourceMapURL, baseURL, originalSourceCode)
-    {
-        // The baseURL could have come from a "//# sourceURL". Attempt to get a
-        // reasonable absolute URL for the base by using the main resource's URL.
-        if (WI.networkManager.mainFrame)
-            baseURL = absoluteURL(baseURL, WI.networkManager.mainFrame.url);
-
-        if (sourceMapURL.startsWith("data:")) {
-            this._loadAndParseSourceMap(sourceMapURL, baseURL, originalSourceCode);
-            return;
-        }
-
-        sourceMapURL = absoluteURL(sourceMapURL, baseURL);
-        if (!sourceMapURL)
-            return;
-
-        console.assert(originalSourceCode.url);
-        if (!originalSourceCode.url)
-            return;
-
-        // FIXME: <rdar://problem/13265694> Source Maps: Better handle when multiple resources reference the same SourceMap
-
-        if (sourceMapURL in this._sourceMapURLMap)
-            return;
-
-        if (sourceMapURL in this._downloadingSourceMaps)
-            return;
-
-        function loadAndParseSourceMap()
-        {
-            this._loadAndParseSourceMap(sourceMapURL, baseURL, originalSourceCode);
-        }
-
-        if (!WI.networkManager.mainFrame) {
-            // If we don't have a main frame, then we are likely in the middle of building the resource tree.
-            // Delaying until the next runloop is enough in this case to then start loading the source map.
-            setTimeout(loadAndParseSourceMap.bind(this), 0);
-            return;
-        }
-
-        loadAndParseSourceMap.call(this);
-    }
-
-    // Private
-
-    _loadAndParseSourceMap(sourceMapURL, baseURL, originalSourceCode)
-    {
-        this._downloadingSourceMaps[sourceMapURL] = true;
-
-        function sourceMapLoaded(error, content, mimeType, statusCode)
-        {
-            if (error || statusCode >= 400) {
-                this._loadAndParseFailed(sourceMapURL);
-                return;
-            }
-
-            if (content.slice(0, 3) === ")]}") {
-                var firstNewlineIndex = content.indexOf("\n");
-                if (firstNewlineIndex === -1) {
-                    this._loadAndParseFailed(sourceMapURL);
-                    return;
-                }
-
-                content = content.substring(firstNewlineIndex);
-            }
-
-            try {
-                var payload = JSON.parse(content);
-                var baseURL = sourceMapURL.startsWith("data:") ? originalSourceCode.url : sourceMapURL;
-                var sourceMap = new WI.SourceMap(baseURL, payload, originalSourceCode);
-                this._loadAndParseSucceeded(sourceMapURL, sourceMap);
-            } catch {
-                this._loadAndParseFailed(sourceMapURL);
-            }
-        }
-
-        if (sourceMapURL.startsWith("data:")) {
-            let {mimeType, base64, data} = parseDataURL(sourceMapURL);
-            let content = base64 ? atob(data) : data;
-            sourceMapLoaded.call(this, null, content, mimeType, 0);
-            return;
-        }
-
-        if (!window.NetworkAgent) {
-            this._loadAndParseFailed(sourceMapURL);
-            return;
-        }
-
-        var frameIdentifier = null;
-        if (originalSourceCode instanceof WI.Resource && originalSourceCode.parentFrame)
-            frameIdentifier = originalSourceCode.parentFrame.id;
-
-        if (!frameIdentifier)
-            frameIdentifier = WI.networkManager.mainFrame ? WI.networkManager.mainFrame.id : "";
-
-        NetworkAgent.loadResource(frameIdentifier, sourceMapURL, sourceMapLoaded.bind(this));
-    }
-
-    _loadAndParseFailed(sourceMapURL)
-    {
-        delete this._downloadingSourceMaps[sourceMapURL];
-    }
-
-    _loadAndParseSucceeded(sourceMapURL, sourceMap)
-    {
-        if (!(sourceMapURL in this._downloadingSourceMaps))
-            return;
-
-        delete this._downloadingSourceMaps[sourceMapURL];
-
-        this._sourceMapURLMap[sourceMapURL] = sourceMap;
-
-        var sources = sourceMap.sources();
-        for (var i = 0; i < sources.length; ++i) {
-            var sourceMapResource = new WI.SourceMapResource(sources[i], sourceMap);
-            sourceMap.addResource(sourceMapResource);
-        }
-
-        // Associate the SourceMap with the originalSourceCode.
-        sourceMap.originalSourceCode.addSourceMap(sourceMap);
-
-        // If the originalSourceCode was not a Resource, be sure to also associate with the Resource if one exists.
-        // FIXME: We should try to use the right frame instead of a global lookup by URL.
-        if (!(sourceMap.originalSourceCode instanceof WI.Resource)) {
-            console.assert(sourceMap.originalSourceCode instanceof WI.Script);
-            var resource = sourceMap.originalSourceCode.resource;
-            if (resource)
-                resource.addSourceMap(sourceMap);
-        }
-    }
-
-    _mainResourceDidChange(event)
-    {
-        if (!event.target.isMainFrame())
-            return;
-
-        this._sourceMapURLMap = {};
-        this._downloadingSourceMaps = {};
-    }
-};
index a122c68..609ad71 100644 (file)
     <script src="Controllers/ProbeManager.js"></script>
     <script src="Controllers/ResourceQueryController.js"></script>
     <script src="Controllers/RuntimeManager.js"></script>
-    <script src="Controllers/SourceMapManager.js"></script>
     <script src="Controllers/TargetManager.js"></script>
     <script src="Controllers/TimelineManager.js"></script>
     <script src="Controllers/TypeTokenAnnotator.js"></script>
index 31897ed..fb42769 100644 (file)
@@ -65,7 +65,7 @@ WI.Script = class Script extends WI.SourceCode
         }
 
         if (this._sourceMappingURL)
-            WI.sourceMapManager.downloadSourceMap(this._sourceMappingURL, this._url, this);
+            WI.networkManager.downloadSourceMap(this._sourceMappingURL, this._url, this);
     }
 
     // Static
index c571c7a..42fdd58 100644 (file)
     <script src="Controllers/NetworkManager.js"></script>
     <script src="Controllers/ProbeManager.js"></script>
     <script src="Controllers/RuntimeManager.js"></script>
-    <script src="Controllers/SourceMapManager.js"></script>
     <script src="Controllers/TargetManager.js"></script>
     <script src="Controllers/TimelineManager.js"></script>
     <script src="Controllers/WorkerManager.js"></script>
index 9e76671..b04c77e 100644 (file)
@@ -59,7 +59,6 @@ WI.loaded = function()
     this.memoryManager = new WI.MemoryManager;
     this.timelineManager = new WI.TimelineManager;
     this.debuggerManager = new WI.DebuggerManager;
-    this.sourceMapManager = new WI.SourceMapManager;
     this.layerTreeManager = new WI.LayerTreeManager;
     this.probeManager = new WI.ProbeManager;
     this.workerManager = new WI.WorkerManager;
index 3930b31..893b354 100644 (file)
     <None Include="..\UserInterface\SourceCodeTextRange.js" />
     <None Include="..\UserInterface\SourceCodeTreeElement.js" />
     <None Include="..\UserInterface\SourceMap.js" />
-    <None Include="..\UserInterface\SourceMapManager.js" />
     <None Include="..\UserInterface\SourceMapResource.js" />
     <None Include="..\UserInterface\SourceMapResourceTreeElement.js" />
     <None Include="..\UserInterface\StorageTreeElement.js" />
index 6b4d108..0e5152f 100644 (file)
     <None Include="..\UserInterface\SourceMap.js">
       <Filter>UserInterface</Filter>
     </None>
-    <None Include="..\UserInterface\SourceMapManager.js">
-      <Filter>UserInterface</Filter>
-    </None>
     <None Include="..\UserInterface\SourceMapResource.js">
       <Filter>UserInterface</Filter>
     </None>