GC can collect JS wrappers of nodes in the mutation records waiting to be delivered
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Controllers / SourceMapManager.js
1 /*
2  * Copyright (C) 2013 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WI.SourceMapManager = class SourceMapManager extends WI.Object
27 {
28     constructor()
29     {
30         super();
31
32         this._sourceMapURLMap = {};
33         this._downloadingSourceMaps = {};
34
35         WI.Frame.addEventListener(WI.Frame.Event.MainResourceDidChange, this._mainResourceDidChange, this);
36     }
37
38     // Public
39
40     sourceMapForURL(sourceMapURL)
41     {
42         return this._sourceMapURLMap[sourceMapURL];
43     }
44
45     downloadSourceMap(sourceMapURL, baseURL, originalSourceCode)
46     {
47         // The baseURL could have come from a "//# sourceURL". Attempt to get a
48         // reasonable absolute URL for the base by using the main resource's URL.
49         if (WI.networkManager.mainFrame)
50             baseURL = absoluteURL(baseURL, WI.networkManager.mainFrame.url);
51
52         if (sourceMapURL.startsWith("data:")) {
53             this._loadAndParseSourceMap(sourceMapURL, baseURL, originalSourceCode);
54             return;
55         }
56
57         sourceMapURL = absoluteURL(sourceMapURL, baseURL);
58         if (!sourceMapURL)
59             return;
60
61         console.assert(originalSourceCode.url);
62         if (!originalSourceCode.url)
63             return;
64
65         // FIXME: <rdar://problem/13265694> Source Maps: Better handle when multiple resources reference the same SourceMap
66
67         if (sourceMapURL in this._sourceMapURLMap)
68             return;
69
70         if (sourceMapURL in this._downloadingSourceMaps)
71             return;
72
73         function loadAndParseSourceMap()
74         {
75             this._loadAndParseSourceMap(sourceMapURL, baseURL, originalSourceCode);
76         }
77
78         if (!WI.networkManager.mainFrame) {
79             // If we don't have a main frame, then we are likely in the middle of building the resource tree.
80             // Delaying until the next runloop is enough in this case to then start loading the source map.
81             setTimeout(loadAndParseSourceMap.bind(this), 0);
82             return;
83         }
84
85         loadAndParseSourceMap.call(this);
86     }
87
88     // Private
89
90     _loadAndParseSourceMap(sourceMapURL, baseURL, originalSourceCode)
91     {
92         this._downloadingSourceMaps[sourceMapURL] = true;
93
94         function sourceMapLoaded(error, content, mimeType, statusCode)
95         {
96             if (error || statusCode >= 400) {
97                 this._loadAndParseFailed(sourceMapURL);
98                 return;
99             }
100
101             if (content.slice(0, 3) === ")]}") {
102                 var firstNewlineIndex = content.indexOf("\n");
103                 if (firstNewlineIndex === -1) {
104                     this._loadAndParseFailed(sourceMapURL);
105                     return;
106                 }
107
108                 content = content.substring(firstNewlineIndex);
109             }
110
111             try {
112                 var payload = JSON.parse(content);
113                 var baseURL = sourceMapURL.startsWith("data:") ? originalSourceCode.url : sourceMapURL;
114                 var sourceMap = new WI.SourceMap(baseURL, payload, originalSourceCode);
115                 this._loadAndParseSucceeded(sourceMapURL, sourceMap);
116             } catch {
117                 this._loadAndParseFailed(sourceMapURL);
118             }
119         }
120
121         if (sourceMapURL.startsWith("data:")) {
122             let {mimeType, base64, data} = parseDataURL(sourceMapURL);
123             let content = base64 ? atob(data) : data;
124             sourceMapLoaded.call(this, null, content, mimeType, 0);
125             return;
126         }
127
128         if (!window.NetworkAgent) {
129             this._loadAndParseFailed(sourceMapURL);
130             return;
131         }
132
133         var frameIdentifier = null;
134         if (originalSourceCode instanceof WI.Resource && originalSourceCode.parentFrame)
135             frameIdentifier = originalSourceCode.parentFrame.id;
136
137         if (!frameIdentifier)
138             frameIdentifier = WI.networkManager.mainFrame ? WI.networkManager.mainFrame.id : "";
139
140         NetworkAgent.loadResource(frameIdentifier, sourceMapURL, sourceMapLoaded.bind(this));
141     }
142
143     _loadAndParseFailed(sourceMapURL)
144     {
145         delete this._downloadingSourceMaps[sourceMapURL];
146     }
147
148     _loadAndParseSucceeded(sourceMapURL, sourceMap)
149     {
150         if (!(sourceMapURL in this._downloadingSourceMaps))
151             return;
152
153         delete this._downloadingSourceMaps[sourceMapURL];
154
155         this._sourceMapURLMap[sourceMapURL] = sourceMap;
156
157         var sources = sourceMap.sources();
158         for (var i = 0; i < sources.length; ++i) {
159             var sourceMapResource = new WI.SourceMapResource(sources[i], sourceMap);
160             sourceMap.addResource(sourceMapResource);
161         }
162
163         // Associate the SourceMap with the originalSourceCode.
164         sourceMap.originalSourceCode.addSourceMap(sourceMap);
165
166         // If the originalSourceCode was not a Resource, be sure to also associate with the Resource if one exists.
167         // FIXME: We should try to use the right frame instead of a global lookup by URL.
168         if (!(sourceMap.originalSourceCode instanceof WI.Resource)) {
169             console.assert(sourceMap.originalSourceCode instanceof WI.Script);
170             var resource = sourceMap.originalSourceCode.resource;
171             if (resource)
172                 resource.addSourceMap(sourceMap);
173         }
174     }
175
176     _mainResourceDidChange(event)
177     {
178         if (!event.target.isMainFrame())
179             return;
180
181         this._sourceMapURLMap = {};
182         this._downloadingSourceMaps = {};
183     }
184 };