Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / bindings / NetworkMapping.js
blobb008811c9871807f0174c4957ffbf721086f8976
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 /**
6  * @constructor
7  * @param {!WebInspector.Workspace} workspace
8  * @param {!WebInspector.FileSystemMapping} fileSystemMapping
9  */
10 WebInspector.NetworkMapping = function(workspace, fileSystemMapping)
12     this._workspace = workspace;
13     this._fileSystemMapping = fileSystemMapping;
14     InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.RevealSourceLine, this._revealSourceLine, this);
17 WebInspector.NetworkMapping.prototype = {
18     /**
19      * @param {!WebInspector.UISourceCode} uiSourceCode
20      * @return {string}
21      */
22     networkURL: function(uiSourceCode)
23     {
24         // FIXME: This should use fileSystemMapping to determine url.
25         return uiSourceCode.networkURL();
26     },
28     /**
29      * @param {string} url
30      * @return {boolean}
31      */
32     hasMappingForURL: function(url)
33     {
34         return this._fileSystemMapping.hasMappingForURL(url);
35     },
37     /**
38      * @param {string} url
39      * @param {!WebInspector.Target} target
40      * @return {?WebInspector.UISourceCode}
41      */
42     _networkUISourceCodeForURL: function(url, target)
43     {
44         var splitURL = WebInspector.ParsedURL.splitURLIntoPathComponents(url);
45         var projectId = WebInspector.NetworkProject.projectId(target, splitURL[0], false);
46         var project = this._workspace.project(projectId);
47         return project ? project.uiSourceCode(splitURL.slice(1).join("/")) : null;
48     },
50     /**
51      * @param {string} url
52      * @param {!WebInspector.Target} target
53      * @return {?WebInspector.UISourceCode}
54      */
55     _contentScriptUISourceCodeForURL: function(url, target)
56     {
57         var splitURL = WebInspector.ParsedURL.splitURLIntoPathComponents(url);
58         var projectId = WebInspector.NetworkProject.projectId(target, splitURL[0], true);
59         var project = this._workspace.project(projectId);
60         return project ? project.uiSourceCode(splitURL.slice(1).join("/")) : null;
61     },
63     /**
64      * @param {string} url
65      * @param {!WebInspector.Target} target
66      * @return {?WebInspector.UISourceCode}
67      */
68     uiSourceCodeForURL: function(url, target)
69     {
70         var file = this._fileSystemMapping.fileForURL(url);
71         if (file) {
72             var projectId = WebInspector.FileSystemWorkspaceBinding.projectId(file.fileSystemPath);
73             var project = this._workspace.project(projectId);
74             return project ? project.uiSourceCode(file.filePath) : null;
75         }
77         return this._networkUISourceCodeForURL(url, target) || this._contentScriptUISourceCodeForURL(url, target);
78     },
80     /**
81      * @param {string} url
82      * @return {?WebInspector.UISourceCode}
83      */
84     uiSourceCodeForURLForAnyTarget: function(url)
85     {
86         for (var target of WebInspector.targetManager.targets()) {
87             var result = this.uiSourceCodeForURL(url, target);
88             if (result)
89                 return result;
90         }
91         return null;
92     },
94     /**
95      * @param {string} fileSystemPath
96      * @param {string} filePath
97      * @return {string}
98      */
99     urlForPath: function(fileSystemPath, filePath)
100     {
101         return this._fileSystemMapping.urlForPath(fileSystemPath, filePath);
102     },
104     /**
105      * @param {!WebInspector.UISourceCode} networkUISourceCode
106      * @param {!WebInspector.UISourceCode} uiSourceCode
107      * @param {!WebInspector.FileSystemWorkspaceBinding} fileSystemWorkspaceBinding
108      */
109     addMapping: function(networkUISourceCode, uiSourceCode, fileSystemWorkspaceBinding)
110     {
111         var url = this.networkURL(networkUISourceCode);
112         var path = uiSourceCode.path();
113         var fileSystemPath = fileSystemWorkspaceBinding.fileSystemPath(uiSourceCode.project().id());
114         this._fileSystemMapping.addMappingForResource(url, fileSystemPath, path);
115     },
117     /**
118      * @param {!WebInspector.UISourceCode} uiSourceCode
119      */
120     removeMapping: function(uiSourceCode)
121     {
122         var networkURL = this.networkURL(uiSourceCode);
123         this._fileSystemMapping.removeMappingForURL(networkURL);
124     },
126     /**
127      * @param {!WebInspector.Event} event
128      */
129     _revealSourceLine: function(event)
130     {
131         var url = /** @type {string} */ (event.data["url"]);
132         var lineNumber = /** @type {number} */ (event.data["lineNumber"]);
133         var columnNumber = /** @type {number} */ (event.data["columnNumber"]);
135         var uiSourceCode = this.uiSourceCodeForURLForAnyTarget(url);
136         if (uiSourceCode) {
137             WebInspector.Revealer.reveal(uiSourceCode.uiLocation(lineNumber, columnNumber));
138             return;
139         }
141         /**
142          * @param {!WebInspector.Event} event
143          * @this {WebInspector.NetworkMapping}
144          */
145         function listener(event)
146         {
147             var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
148             if (this.networkURL(uiSourceCode) === url) {
149                 WebInspector.Revealer.reveal(uiSourceCode.uiLocation(lineNumber, columnNumber));
150                 this._workspace.removeEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, listener, this);
151             }
152         }
154         this._workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, listener, this);
155     },
159  * @type {!WebInspector.NetworkMapping}
160  */
161 WebInspector.networkMapping;