ServiceWorker Inspector: Various issues inspecting service worker on mobile.twitter.com
[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.frameResourceManager.mainFrame)
50             baseURL = absoluteURL(baseURL, WI.frameResourceManager.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.frameResourceManager.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         // COMPATIBILITY (iOS 7): Network.loadResource did not exist.
129         // Also, JavaScript Debuggable may reach this.
130         if (!window.NetworkAgent || !NetworkAgent.loadResource) {
131             this._loadAndParseFailed(sourceMapURL);
132             return;
133         }
134
135         var frameIdentifier = null;
136         if (originalSourceCode instanceof WI.Resource && originalSourceCode.parentFrame)
137             frameIdentifier = originalSourceCode.parentFrame.id;
138
139         if (!frameIdentifier && WI.frameResourceManager.mainFrame)
140             frameIdentifier = WI.frameResourceManager.mainFrame.id;
141         else
142             frameIdentifier = "";
143
144         NetworkAgent.loadResource(frameIdentifier, sourceMapURL, sourceMapLoaded.bind(this));
145     }
146
147     _loadAndParseFailed(sourceMapURL)
148     {
149         delete this._downloadingSourceMaps[sourceMapURL];
150     }
151
152     _loadAndParseSucceeded(sourceMapURL, sourceMap)
153     {
154         if (!(sourceMapURL in this._downloadingSourceMaps))
155             return;
156
157         delete this._downloadingSourceMaps[sourceMapURL];
158
159         this._sourceMapURLMap[sourceMapURL] = sourceMap;
160
161         var sources = sourceMap.sources();
162         for (var i = 0; i < sources.length; ++i) {
163             var sourceMapResource = new WI.SourceMapResource(sources[i], sourceMap);
164             sourceMap.addResource(sourceMapResource);
165         }
166
167         // Associate the SourceMap with the originalSourceCode.
168         sourceMap.originalSourceCode.addSourceMap(sourceMap);
169
170         // If the originalSourceCode was not a Resource, be sure to also associate with the Resource if one exists.
171         // FIXME: We should try to use the right frame instead of a global lookup by URL.
172         if (!(sourceMap.originalSourceCode instanceof WI.Resource)) {
173             console.assert(sourceMap.originalSourceCode instanceof WI.Script);
174             var resource = sourceMap.originalSourceCode.resource;
175             if (resource)
176                 resource.addSourceMap(sourceMap);
177         }
178     }
179
180     _mainResourceDidChange(event)
181     {
182         if (!event.target.isMainFrame())
183             return;
184
185         this._sourceMapURLMap = {};
186         this._downloadingSourceMaps = {};
187     }
188 };