| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 /** |
| 6 * @constructor |
| 7 * @param {!WebInspector.Workspace} workspace |
| 8 * @param {!WebInspector.FileSystemMapping} fileSystemMapping |
| 9 * @param {function(!WebInspector.PersistenceBinding)} onBindingCreated |
| 10 * @param {function(!WebInspector.PersistenceBinding)} onBindingRemoved |
| 11 */ |
| 12 WebInspector.DefaultMapping = function(workspace, fileSystemMapping, onBindingCr
eated, onBindingRemoved) |
| 13 { |
| 14 this._workspace = workspace; |
| 15 this._fileSystemMapping = fileSystemMapping; |
| 16 /** @type {!Set<!WebInspector.PersistenceBinding>} */ |
| 17 this._bindings = new Set(); |
| 18 this._onBindingCreated = onBindingCreated; |
| 19 this._onBindingRemoved = onBindingRemoved; |
| 20 |
| 21 this._eventListeners = [ |
| 22 workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeAdd
ed, this._onUISourceCodeAdded, this), |
| 23 workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeRem
oved, this._onUISourceCodeRemoved, this), |
| 24 workspace.addEventListener(WebInspector.Workspace.Events.ProjectRemoved,
this._onProjectRemoved, this), |
| 25 this._fileSystemMapping.addEventListener(WebInspector.FileSystemMapping.
Events.FileMappingAdded, this._remap, this), |
| 26 this._fileSystemMapping.addEventListener(WebInspector.FileSystemMapping.
Events.FileMappingRemoved, this._remap, this) |
| 27 ]; |
| 28 this._remap(); |
| 29 } |
| 30 |
| 31 WebInspector.DefaultMapping._binding = Symbol("DefaultMapping.Binding"); |
| 32 |
| 33 WebInspector.DefaultMapping.prototype = { |
| 34 _remap: function() |
| 35 { |
| 36 for (var binding of this._bindings.valuesArray()) |
| 37 this._unbind(binding.network); |
| 38 var networkProjects = this._workspace.projectsForType(WebInspector.proje
ctTypes.Network); |
| 39 for (var networkProject of networkProjects) { |
| 40 for (var uiSourceCode of networkProject.uiSourceCodes()) |
| 41 this._bind(uiSourceCode); |
| 42 } |
| 43 }, |
| 44 |
| 45 /** |
| 46 * @param {!WebInspector.Event} event |
| 47 */ |
| 48 _onUISourceCodeAdded: function(event) |
| 49 { |
| 50 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */(event.data)
; |
| 51 this._bind(uiSourceCode); |
| 52 }, |
| 53 |
| 54 /** |
| 55 * @param {!WebInspector.Event} event |
| 56 */ |
| 57 _onUISourceCodeRemoved: function(event) |
| 58 { |
| 59 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */(event.data)
; |
| 60 this._unbind(uiSourceCode); |
| 61 }, |
| 62 |
| 63 /** |
| 64 * @param {!WebInspector.Event} event |
| 65 */ |
| 66 _onProjectRemoved: function(event) |
| 67 { |
| 68 var project = /** @type {!WebInspector.Project} */(event.data); |
| 69 for (var uiSourceCode of project.uiSourceCodes()) |
| 70 this._unbind(uiSourceCode); |
| 71 }, |
| 72 |
| 73 /** |
| 74 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 75 * @return {?WebInspector.PersistenceBinding} |
| 76 */ |
| 77 _createBinding: function(uiSourceCode) |
| 78 { |
| 79 if (uiSourceCode.project().type() === WebInspector.projectTypes.FileSyst
em) { |
| 80 var fileSystemPath = WebInspector.FileSystemWorkspaceBinding.fileSys
temPath(uiSourceCode.project().id()); |
| 81 var networkURL = this._fileSystemMapping.networkURLForFileSystemURL(
fileSystemPath, uiSourceCode.url()); |
| 82 var networkSourceCode = networkURL ? this._workspace.uiSourceCodeFor
URL(networkURL) : null; |
| 83 return networkSourceCode ? new WebInspector.PersistenceBinding(netwo
rkSourceCode, uiSourceCode) : null; |
| 84 } |
| 85 if (uiSourceCode.project().type() === WebInspector.projectTypes.Network)
{ |
| 86 var file = this._fileSystemMapping.fileForURL(uiSourceCode.url()); |
| 87 var projectId = file ? WebInspector.FileSystemWorkspaceBinding.proje
ctId(file.fileSystemPath) : null; |
| 88 var fileSourceCode = file && projectId ? this._workspace.uiSourceCod
e(projectId, file.fileURL) : null; |
| 89 return fileSourceCode ? new WebInspector.PersistenceBinding(uiSource
Code, fileSourceCode) : null; |
| 90 } |
| 91 return null; |
| 92 }, |
| 93 |
| 94 /** |
| 95 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 96 */ |
| 97 _bind: function(uiSourceCode) |
| 98 { |
| 99 console.assert(!uiSourceCode[WebInspector.DefaultMapping._binding], "Can
not bind already bound UISourceCode!"); |
| 100 var binding = this._createBinding(uiSourceCode); |
| 101 if (!binding) |
| 102 return; |
| 103 this._bindings.add(binding); |
| 104 binding.network[WebInspector.DefaultMapping._binding] = binding; |
| 105 binding.fileSystem[WebInspector.DefaultMapping._binding] = binding; |
| 106 |
| 107 binding.fileSystem.addEventListener(WebInspector.UISourceCode.Events.Tit
leChanged, this._onFileSystemUISourceCodeRenamed, this); |
| 108 |
| 109 this._onBindingCreated.call(null, binding); |
| 110 }, |
| 111 |
| 112 /** |
| 113 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 114 */ |
| 115 _unbind: function(uiSourceCode) |
| 116 { |
| 117 var binding = uiSourceCode[WebInspector.DefaultMapping._binding]; |
| 118 if (!binding) |
| 119 return; |
| 120 this._bindings.delete(binding); |
| 121 binding.network[WebInspector.DefaultMapping._binding] = null; |
| 122 binding.fileSystem[WebInspector.DefaultMapping._binding] = null; |
| 123 |
| 124 binding.fileSystem.removeEventListener(WebInspector.UISourceCode.Events.
TitleChanged, this._onFileSystemUISourceCodeRenamed, this); |
| 125 |
| 126 this._onBindingRemoved.call(null, binding); |
| 127 }, |
| 128 |
| 129 /** |
| 130 * @param {!WebInspector.Event} event |
| 131 */ |
| 132 _onFileSystemUISourceCodeRenamed: function(event) |
| 133 { |
| 134 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */(event.targe
t); |
| 135 var binding = uiSourceCode[WebInspector.DefaultMapping._binding]; |
| 136 this._unbind(binding.network); |
| 137 this._bind(binding.network); |
| 138 }, |
| 139 |
| 140 dispose: function() |
| 141 { |
| 142 WebInspector.EventTarget.removeEventListeners(this._eventListeners); |
| 143 } |
| 144 } |
| OLD | NEW |