| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 /** | 4 /** |
| 5 * @unrestricted | 5 * @unrestricted |
| 6 */ | 6 */ |
| 7 WebInspector.Persistence = class extends WebInspector.Object { | 7 Persistence.Persistence = class extends Common.Object { |
| 8 /** | 8 /** |
| 9 * @param {!WebInspector.Workspace} workspace | 9 * @param {!Workspace.Workspace} workspace |
| 10 * @param {!WebInspector.BreakpointManager} breakpointManager | 10 * @param {!Bindings.BreakpointManager} breakpointManager |
| 11 * @param {!WebInspector.FileSystemMapping} fileSystemMapping | 11 * @param {!Workspace.FileSystemMapping} fileSystemMapping |
| 12 */ | 12 */ |
| 13 constructor(workspace, breakpointManager, fileSystemMapping) { | 13 constructor(workspace, breakpointManager, fileSystemMapping) { |
| 14 super(); | 14 super(); |
| 15 this._workspace = workspace; | 15 this._workspace = workspace; |
| 16 this._breakpointManager = breakpointManager; | 16 this._breakpointManager = breakpointManager; |
| 17 /** @type {!Map<string, number>} */ | 17 /** @type {!Map<string, number>} */ |
| 18 this._filePathPrefixesToBindingCount = new Map(); | 18 this._filePathPrefixesToBindingCount = new Map(); |
| 19 | 19 |
| 20 if (Runtime.experiments.isEnabled('persistence2')) { | 20 if (Runtime.experiments.isEnabled('persistence2')) { |
| 21 var linkDecorator = new WebInspector.PersistenceUtils.LinkDecorator(this); | 21 var linkDecorator = new Persistence.PersistenceUtils.LinkDecorator(this); |
| 22 WebInspector.Linkifier.setLinkDecorator(linkDecorator); | 22 Components.Linkifier.setLinkDecorator(linkDecorator); |
| 23 this._mapping = | 23 this._mapping = |
| 24 new WebInspector.Automapping(workspace, this._onBindingCreated.bind(th
is), this._onBindingRemoved.bind(this)); | 24 new Persistence.Automapping(workspace, this._onBindingCreated.bind(thi
s), this._onBindingRemoved.bind(this)); |
| 25 } else { | 25 } else { |
| 26 this._mapping = new WebInspector.DefaultMapping( | 26 this._mapping = new Persistence.DefaultMapping( |
| 27 workspace, fileSystemMapping, this._onBindingCreated.bind(this), this.
_onBindingRemoved.bind(this)); | 27 workspace, fileSystemMapping, this._onBindingCreated.bind(this), this.
_onBindingRemoved.bind(this)); |
| 28 } | 28 } |
| 29 } | 29 } |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * @param {!WebInspector.PersistenceBinding} binding | 32 * @param {!Persistence.PersistenceBinding} binding |
| 33 */ | 33 */ |
| 34 _onBindingCreated(binding) { | 34 _onBindingCreated(binding) { |
| 35 if (binding.network.isDirty()) { | 35 if (binding.network.isDirty()) { |
| 36 WebInspector.console.log(WebInspector.UIString( | 36 Common.console.log(Common.UIString( |
| 37 '%s can not be persisted to file system due to unsaved changes.', bind
ing.network.name())); | 37 '%s can not be persisted to file system due to unsaved changes.', bind
ing.network.name())); |
| 38 return; | 38 return; |
| 39 } | 39 } |
| 40 if (binding.fileSystem.isDirty()) | 40 if (binding.fileSystem.isDirty()) |
| 41 binding.network.setWorkingCopy(binding.fileSystem.workingCopy()); | 41 binding.network.setWorkingCopy(binding.fileSystem.workingCopy()); |
| 42 | 42 |
| 43 binding.network[WebInspector.Persistence._binding] = binding; | 43 binding.network[Persistence.Persistence._binding] = binding; |
| 44 binding.fileSystem[WebInspector.Persistence._binding] = binding; | 44 binding.fileSystem[Persistence.Persistence._binding] = binding; |
| 45 | 45 |
| 46 binding.fileSystem.forceLoadOnCheckContent(); | 46 binding.fileSystem.forceLoadOnCheckContent(); |
| 47 | 47 |
| 48 binding.network.addEventListener( | 48 binding.network.addEventListener( |
| 49 WebInspector.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCo
pyCommitted, this); | 49 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC
ommitted, this); |
| 50 binding.fileSystem.addEventListener( | 50 binding.fileSystem.addEventListener( |
| 51 WebInspector.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCo
pyCommitted, this); | 51 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC
ommitted, this); |
| 52 | 52 |
| 53 this._addFilePathBindingPrefixes(binding.fileSystem.url()); | 53 this._addFilePathBindingPrefixes(binding.fileSystem.url()); |
| 54 | 54 |
| 55 this._moveBreakpoints(binding.fileSystem, binding.network); | 55 this._moveBreakpoints(binding.fileSystem, binding.network); |
| 56 this.dispatchEventToListeners(WebInspector.Persistence.Events.BindingCreated
, binding); | 56 this.dispatchEventToListeners(Persistence.Persistence.Events.BindingCreated,
binding); |
| 57 } | 57 } |
| 58 | 58 |
| 59 /** | 59 /** |
| 60 * @param {!WebInspector.PersistenceBinding} binding | 60 * @param {!Persistence.PersistenceBinding} binding |
| 61 */ | 61 */ |
| 62 _onBindingRemoved(binding) { | 62 _onBindingRemoved(binding) { |
| 63 if (binding.network.isDirty()) | 63 if (binding.network.isDirty()) |
| 64 binding.fileSystem.setWorkingCopy(binding.network.workingCopy()); | 64 binding.fileSystem.setWorkingCopy(binding.network.workingCopy()); |
| 65 | 65 |
| 66 binding.network[WebInspector.Persistence._binding] = null; | 66 binding.network[Persistence.Persistence._binding] = null; |
| 67 binding.fileSystem[WebInspector.Persistence._binding] = null; | 67 binding.fileSystem[Persistence.Persistence._binding] = null; |
| 68 | 68 |
| 69 binding.network.removeEventListener( | 69 binding.network.removeEventListener( |
| 70 WebInspector.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCo
pyCommitted, this); | 70 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC
ommitted, this); |
| 71 binding.fileSystem.removeEventListener( | 71 binding.fileSystem.removeEventListener( |
| 72 WebInspector.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCo
pyCommitted, this); | 72 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC
ommitted, this); |
| 73 | 73 |
| 74 this._removeFilePathBindingPrefixes(binding.fileSystem.url()); | 74 this._removeFilePathBindingPrefixes(binding.fileSystem.url()); |
| 75 | 75 |
| 76 this._copyBreakpoints(binding.network, binding.fileSystem); | 76 this._copyBreakpoints(binding.network, binding.fileSystem); |
| 77 this.dispatchEventToListeners(WebInspector.Persistence.Events.BindingRemoved
, binding); | 77 this.dispatchEventToListeners(Persistence.Persistence.Events.BindingRemoved,
binding); |
| 78 } | 78 } |
| 79 | 79 |
| 80 /** | 80 /** |
| 81 * @param {!WebInspector.Event} event | 81 * @param {!Common.Event} event |
| 82 */ | 82 */ |
| 83 _onWorkingCopyCommitted(event) { | 83 _onWorkingCopyCommitted(event) { |
| 84 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.target); | 84 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.target); |
| 85 var binding = uiSourceCode[WebInspector.Persistence._binding]; | 85 var binding = uiSourceCode[Persistence.Persistence._binding]; |
| 86 if (!binding || binding[WebInspector.Persistence._muteCommit]) | 86 if (!binding || binding[Persistence.Persistence._muteCommit]) |
| 87 return; | 87 return; |
| 88 var newContent = /** @type {string} */ (event.data.content); | 88 var newContent = /** @type {string} */ (event.data.content); |
| 89 var other = binding.network === uiSourceCode ? binding.fileSystem : binding.
network; | 89 var other = binding.network === uiSourceCode ? binding.fileSystem : binding.
network; |
| 90 var target = WebInspector.NetworkProject.targetForUISourceCode(binding.netwo
rk); | 90 var target = Bindings.NetworkProject.targetForUISourceCode(binding.network); |
| 91 if (target.isNodeJS()) { | 91 if (target.isNodeJS()) { |
| 92 other.requestContent().then( | 92 other.requestContent().then( |
| 93 currentContent => this._syncNodeJSContent(binding, other, currentConte
nt, newContent)); | 93 currentContent => this._syncNodeJSContent(binding, other, currentConte
nt, newContent)); |
| 94 return; | 94 return; |
| 95 } | 95 } |
| 96 binding[WebInspector.Persistence._muteCommit] = true; | 96 binding[Persistence.Persistence._muteCommit] = true; |
| 97 other.addRevision(newContent); | 97 other.addRevision(newContent); |
| 98 binding[WebInspector.Persistence._muteCommit] = false; | 98 binding[Persistence.Persistence._muteCommit] = false; |
| 99 this._contentSyncedForTest(); | 99 this._contentSyncedForTest(); |
| 100 } | 100 } |
| 101 | 101 |
| 102 /** | 102 /** |
| 103 * @param {!WebInspector.PersistenceBinding} binding | 103 * @param {!Persistence.PersistenceBinding} binding |
| 104 * @param {!WebInspector.UISourceCode} uiSourceCode | 104 * @param {!Workspace.UISourceCode} uiSourceCode |
| 105 * @param {string} currentContent | 105 * @param {string} currentContent |
| 106 * @param {string} newContent | 106 * @param {string} newContent |
| 107 */ | 107 */ |
| 108 _syncNodeJSContent(binding, uiSourceCode, currentContent, newContent) { | 108 _syncNodeJSContent(binding, uiSourceCode, currentContent, newContent) { |
| 109 if (uiSourceCode === binding.fileSystem) { | 109 if (uiSourceCode === binding.fileSystem) { |
| 110 if (newContent.startsWith(WebInspector.Persistence._NodePrefix) && | 110 if (newContent.startsWith(Persistence.Persistence._NodePrefix) && |
| 111 newContent.endsWith(WebInspector.Persistence._NodeSuffix)) | 111 newContent.endsWith(Persistence.Persistence._NodeSuffix)) |
| 112 newContent = newContent.substring( | 112 newContent = newContent.substring( |
| 113 WebInspector.Persistence._NodePrefix.length, | 113 Persistence.Persistence._NodePrefix.length, |
| 114 newContent.length - WebInspector.Persistence._NodeSuffix.length); | 114 newContent.length - Persistence.Persistence._NodeSuffix.length); |
| 115 if (currentContent.startsWith(WebInspector.Persistence._NodeShebang)) | 115 if (currentContent.startsWith(Persistence.Persistence._NodeShebang)) |
| 116 newContent = WebInspector.Persistence._NodeShebang + newContent; | 116 newContent = Persistence.Persistence._NodeShebang + newContent; |
| 117 } else { | 117 } else { |
| 118 if (newContent.startsWith(WebInspector.Persistence._NodeShebang)) | 118 if (newContent.startsWith(Persistence.Persistence._NodeShebang)) |
| 119 newContent = newContent.substring(WebInspector.Persistence._NodeShebang.
length); | 119 newContent = newContent.substring(Persistence.Persistence._NodeShebang.l
ength); |
| 120 if (currentContent.startsWith(WebInspector.Persistence._NodePrefix) && | 120 if (currentContent.startsWith(Persistence.Persistence._NodePrefix) && |
| 121 currentContent.endsWith(WebInspector.Persistence._NodeSuffix)) | 121 currentContent.endsWith(Persistence.Persistence._NodeSuffix)) |
| 122 newContent = WebInspector.Persistence._NodePrefix + newContent + WebInsp
ector.Persistence._NodeSuffix; | 122 newContent = Persistence.Persistence._NodePrefix + newContent + Persiste
nce.Persistence._NodeSuffix; |
| 123 } | 123 } |
| 124 binding[WebInspector.Persistence._muteCommit] = true; | 124 binding[Persistence.Persistence._muteCommit] = true; |
| 125 uiSourceCode.addRevision(newContent); | 125 uiSourceCode.addRevision(newContent); |
| 126 binding[WebInspector.Persistence._muteCommit] = false; | 126 binding[Persistence.Persistence._muteCommit] = false; |
| 127 this._contentSyncedForTest(); | 127 this._contentSyncedForTest(); |
| 128 } | 128 } |
| 129 | 129 |
| 130 _contentSyncedForTest() { | 130 _contentSyncedForTest() { |
| 131 } | 131 } |
| 132 | 132 |
| 133 /** | 133 /** |
| 134 * @param {!WebInspector.UISourceCode} from | 134 * @param {!Workspace.UISourceCode} from |
| 135 * @param {!WebInspector.UISourceCode} to | 135 * @param {!Workspace.UISourceCode} to |
| 136 */ | 136 */ |
| 137 _moveBreakpoints(from, to) { | 137 _moveBreakpoints(from, to) { |
| 138 var breakpoints = this._breakpointManager.breakpointsForUISourceCode(from); | 138 var breakpoints = this._breakpointManager.breakpointsForUISourceCode(from); |
| 139 for (var breakpoint of breakpoints) { | 139 for (var breakpoint of breakpoints) { |
| 140 breakpoint.remove(); | 140 breakpoint.remove(); |
| 141 this._breakpointManager.setBreakpoint( | 141 this._breakpointManager.setBreakpoint( |
| 142 to, breakpoint.lineNumber(), breakpoint.columnNumber(), breakpoint.con
dition(), breakpoint.enabled()); | 142 to, breakpoint.lineNumber(), breakpoint.columnNumber(), breakpoint.con
dition(), breakpoint.enabled()); |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | 145 |
| 146 /** | 146 /** |
| 147 * @param {!WebInspector.UISourceCode} from | 147 * @param {!Workspace.UISourceCode} from |
| 148 * @param {!WebInspector.UISourceCode} to | 148 * @param {!Workspace.UISourceCode} to |
| 149 */ | 149 */ |
| 150 _copyBreakpoints(from, to) { | 150 _copyBreakpoints(from, to) { |
| 151 var breakpoints = this._breakpointManager.breakpointsForUISourceCode(from); | 151 var breakpoints = this._breakpointManager.breakpointsForUISourceCode(from); |
| 152 for (var breakpoint of breakpoints) | 152 for (var breakpoint of breakpoints) |
| 153 this._breakpointManager.setBreakpoint( | 153 this._breakpointManager.setBreakpoint( |
| 154 to, breakpoint.lineNumber(), breakpoint.columnNumber(), breakpoint.con
dition(), breakpoint.enabled()); | 154 to, breakpoint.lineNumber(), breakpoint.columnNumber(), breakpoint.con
dition(), breakpoint.enabled()); |
| 155 } | 155 } |
| 156 | 156 |
| 157 /** | 157 /** |
| 158 * @param {!WebInspector.UISourceCode} uiSourceCode | 158 * @param {!Workspace.UISourceCode} uiSourceCode |
| 159 * @return {boolean} | 159 * @return {boolean} |
| 160 */ | 160 */ |
| 161 hasUnsavedCommittedChanges(uiSourceCode) { | 161 hasUnsavedCommittedChanges(uiSourceCode) { |
| 162 if (this._workspace.hasResourceContentTrackingExtensions()) | 162 if (this._workspace.hasResourceContentTrackingExtensions()) |
| 163 return false; | 163 return false; |
| 164 if (uiSourceCode.url() && WebInspector.fileManager.isURLSaved(uiSourceCode.u
rl())) | 164 if (uiSourceCode.url() && Workspace.fileManager.isURLSaved(uiSourceCode.url(
))) |
| 165 return false; | 165 return false; |
| 166 if (uiSourceCode.project().canSetFileContent()) | 166 if (uiSourceCode.project().canSetFileContent()) |
| 167 return false; | 167 return false; |
| 168 if (uiSourceCode[WebInspector.Persistence._binding]) | 168 if (uiSourceCode[Persistence.Persistence._binding]) |
| 169 return false; | 169 return false; |
| 170 return !!uiSourceCode.history.length; | 170 return !!uiSourceCode.history.length; |
| 171 } | 171 } |
| 172 | 172 |
| 173 /** | 173 /** |
| 174 * @param {!WebInspector.UISourceCode} uiSourceCode | 174 * @param {!Workspace.UISourceCode} uiSourceCode |
| 175 * @return {?WebInspector.PersistenceBinding} | 175 * @return {?Persistence.PersistenceBinding} |
| 176 */ | 176 */ |
| 177 binding(uiSourceCode) { | 177 binding(uiSourceCode) { |
| 178 return uiSourceCode[WebInspector.Persistence._binding] || null; | 178 return uiSourceCode[Persistence.Persistence._binding] || null; |
| 179 } | 179 } |
| 180 | 180 |
| 181 /** | 181 /** |
| 182 * @param {!WebInspector.UISourceCode} uiSourceCode | 182 * @param {!Workspace.UISourceCode} uiSourceCode |
| 183 * @return {?WebInspector.UISourceCode} | 183 * @return {?Workspace.UISourceCode} |
| 184 */ | 184 */ |
| 185 fileSystem(uiSourceCode) { | 185 fileSystem(uiSourceCode) { |
| 186 var binding = this.binding(uiSourceCode); | 186 var binding = this.binding(uiSourceCode); |
| 187 return binding ? binding.fileSystem : null; | 187 return binding ? binding.fileSystem : null; |
| 188 } | 188 } |
| 189 | 189 |
| 190 /** | 190 /** |
| 191 * @param {string} filePath | 191 * @param {string} filePath |
| 192 */ | 192 */ |
| 193 _addFilePathBindingPrefixes(filePath) { | 193 _addFilePathBindingPrefixes(filePath) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 222 if (!filePath.endsWith('/')) | 222 if (!filePath.endsWith('/')) |
| 223 filePath += '/'; | 223 filePath += '/'; |
| 224 return this._filePathPrefixesToBindingCount.has(filePath); | 224 return this._filePathPrefixesToBindingCount.has(filePath); |
| 225 } | 225 } |
| 226 | 226 |
| 227 dispose() { | 227 dispose() { |
| 228 this._mapping.dispose(); | 228 this._mapping.dispose(); |
| 229 } | 229 } |
| 230 }; | 230 }; |
| 231 | 231 |
| 232 WebInspector.Persistence._binding = Symbol('Persistence.Binding'); | 232 Persistence.Persistence._binding = Symbol('Persistence.Binding'); |
| 233 WebInspector.Persistence._muteCommit = Symbol('Persistence.MuteCommit'); | 233 Persistence.Persistence._muteCommit = Symbol('Persistence.MuteCommit'); |
| 234 | 234 |
| 235 WebInspector.Persistence._NodePrefix = '(function (exports, require, module, __f
ilename, __dirname) { '; | 235 Persistence.Persistence._NodePrefix = '(function (exports, require, module, __fi
lename, __dirname) { '; |
| 236 WebInspector.Persistence._NodeSuffix = '\n});'; | 236 Persistence.Persistence._NodeSuffix = '\n});'; |
| 237 WebInspector.Persistence._NodeShebang = '#!/usr/bin/env node\n'; | 237 Persistence.Persistence._NodeShebang = '#!/usr/bin/env node\n'; |
| 238 | 238 |
| 239 WebInspector.Persistence.Events = { | 239 Persistence.Persistence.Events = { |
| 240 BindingCreated: Symbol('BindingCreated'), | 240 BindingCreated: Symbol('BindingCreated'), |
| 241 BindingRemoved: Symbol('BindingRemoved') | 241 BindingRemoved: Symbol('BindingRemoved') |
| 242 }; | 242 }; |
| 243 | 243 |
| 244 /** | 244 /** |
| 245 * @unrestricted | 245 * @unrestricted |
| 246 */ | 246 */ |
| 247 WebInspector.PersistenceBinding = class { | 247 Persistence.PersistenceBinding = class { |
| 248 /** | 248 /** |
| 249 * @param {!WebInspector.UISourceCode} network | 249 * @param {!Workspace.UISourceCode} network |
| 250 * @param {!WebInspector.UISourceCode} fileSystem | 250 * @param {!Workspace.UISourceCode} fileSystem |
| 251 * @param {boolean} exactMatch | 251 * @param {boolean} exactMatch |
| 252 */ | 252 */ |
| 253 constructor(network, fileSystem, exactMatch) { | 253 constructor(network, fileSystem, exactMatch) { |
| 254 this.network = network; | 254 this.network = network; |
| 255 this.fileSystem = fileSystem; | 255 this.fileSystem = fileSystem; |
| 256 this.exactMatch = exactMatch; | 256 this.exactMatch = exactMatch; |
| 257 } | 257 } |
| 258 }; | 258 }; |
| 259 | 259 |
| 260 /** @type {!WebInspector.Persistence} */ | 260 /** @type {!Persistence.Persistence} */ |
| 261 WebInspector.persistence; | 261 Persistence.persistence; |
| OLD | NEW |