Chromium Code Reviews| 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 Persistence.Persistence = class extends Common.Object { | 7 Persistence.Persistence = class extends Common.Object { |
| 8 /** | 8 /** |
| 9 * @param {!Workspace.Workspace} workspace | 9 * @param {!Workspace.Workspace} workspace |
| 10 * @param {!Bindings.BreakpointManager} breakpointManager | 10 * @param {!Bindings.BreakpointManager} breakpointManager |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 } else { | 25 } else { |
| 26 this._mapping = new Persistence.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 {!Persistence.PersistenceBinding} binding | 32 * @param {!Persistence.PersistenceBinding} binding |
| 33 */ | 33 */ |
| 34 _onBindingCreated(binding) { | 34 _onBindingCreated(binding) { |
| 35 if (binding.network.isDirty()) { | 35 if (!binding.network.contentLoaded() && !binding.fileSystem.contentLoaded() && !binding.network.isDirty() && |
| 36 Common.console.log( | 36 !binding.fileSystem.isDirty()) { |
| 37 Common.UIString('%s can not be persisted to file system due to unsaved changes.', binding.network.name())); | 37 this._establishBinding(binding); |
| 38 return; | 38 return; |
| 39 } | 39 } |
| 40 if (binding.fileSystem.isDirty()) | |
| 41 binding.network.setWorkingCopy(binding.fileSystem.workingCopy()); | |
| 42 | 40 |
| 41 if (binding[Persistence.Persistence._prevalidatePromise]) | |
| 42 return; | |
| 43 var promise = this._validateBinding(binding).then(onBindingValidated.bind(th is)); | |
| 44 binding[Persistence.Persistence._prevalidatePromise] = promise; | |
| 45 | |
| 46 /** | |
| 47 * @this {Persistence.Persistence} | |
| 48 */ | |
| 49 function onBindingValidated(isValid) { | |
|
dgozman
2016/12/07 01:45:41
@param
| |
| 50 if (binding[Persistence.Persistence._prevalidatePromise] !== promise) | |
| 51 return; | |
| 52 binding[Persistence.Persistence._prevalidatePromise] = null; | |
| 53 | |
| 54 if (isValid) | |
| 55 this._establishBinding(binding); | |
| 56 else | |
| 57 this._prevalidationFailedForTest(binding); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 /** | |
| 62 * @param {!Persistence.PersistenceBinding} binding | |
| 63 */ | |
| 64 _prevalidationFailedForTest(binding) { | |
| 65 } | |
| 66 | |
| 67 /** | |
| 68 * @param {!Persistence.PersistenceBinding} binding | |
|
dgozman
2016/12/07 01:45:41
@return promise.....
| |
| 69 */ | |
| 70 _validateBinding(binding) { | |
| 71 return Promise.all([binding.network.requestContent(), binding.fileSystem.req uestContent()]).then(onContentsLoaded); | |
| 72 | |
| 73 function onContentsLoaded() { | |
|
dgozman
2016/12/07 01:45:41
@return boolean
| |
| 74 var target = Bindings.NetworkProject.targetForUISourceCode(binding.network ); | |
| 75 if (target.isNodeJS()) { | |
| 76 var fileSystemContent = binding.fileSystem.workingCopy(); | |
| 77 var networkContent = binding.network.workingCopy(); | |
| 78 var rewrappedNetworkContent = Persistence.Persistence._rewrapNodeJSConte nt( | |
| 79 binding, binding.fileSystem, fileSystemContent, networkContent); | |
| 80 return fileSystemContent === rewrappedNetworkContent; | |
| 81 } | |
| 82 // Trim trailing whitespaces because V8 adds trailing newline. | |
| 83 var fileSystemContent = binding.fileSystem.workingCopy().replace(/\s*$/, ' '); | |
|
dgozman
2016/12/07 01:45:41
.trimRight()
| |
| 84 var networkContent = binding.network.workingCopy().replace(/\s*$/, ''); | |
| 85 return fileSystemContent === networkContent; | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 /** | |
| 90 * @param {!Persistence.PersistenceBinding} binding | |
| 91 */ | |
| 92 _establishBinding(binding) { | |
| 43 binding.network[Persistence.Persistence._binding] = binding; | 93 binding.network[Persistence.Persistence._binding] = binding; |
| 44 binding.fileSystem[Persistence.Persistence._binding] = binding; | 94 binding.fileSystem[Persistence.Persistence._binding] = binding; |
| 45 | 95 |
| 46 binding.fileSystem.forceLoadOnCheckContent(); | 96 binding.fileSystem.forceLoadOnCheckContent(); |
| 47 | 97 |
| 98 binding.network.addEventListener(Workspace.UISourceCode.Events.ContentLoaded , this._onContentLoaded, this); | |
| 99 binding.fileSystem.addEventListener(Workspace.UISourceCode.Events.ContentLoa ded, this._onContentLoaded, this); | |
| 48 binding.network.addEventListener( | 100 binding.network.addEventListener( |
| 49 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this); | 101 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this); |
| 50 binding.fileSystem.addEventListener( | 102 binding.fileSystem.addEventListener( |
| 51 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this); | 103 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this); |
| 52 binding.network.addEventListener( | 104 binding.network.addEventListener( |
| 53 Workspace.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyCha nged, this); | 105 Workspace.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyCha nged, this); |
| 54 binding.fileSystem.addEventListener( | 106 binding.fileSystem.addEventListener( |
| 55 Workspace.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyCha nged, this); | 107 Workspace.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyCha nged, this); |
| 56 | 108 |
| 57 this._addFilePathBindingPrefixes(binding.fileSystem.url()); | 109 this._addFilePathBindingPrefixes(binding.fileSystem.url()); |
| 58 | 110 |
| 59 this._moveBreakpoints(binding.fileSystem, binding.network); | 111 this._moveBreakpoints(binding.fileSystem, binding.network); |
| 60 this.dispatchEventToListeners(Persistence.Persistence.Events.BindingCreated, binding); | 112 this.dispatchEventToListeners(Persistence.Persistence.Events.BindingCreated, binding); |
| 61 } | 113 } |
| 62 | 114 |
| 63 /** | 115 /** |
| 64 * @param {!Persistence.PersistenceBinding} binding | 116 * @param {!Persistence.PersistenceBinding} binding |
| 65 */ | 117 */ |
| 66 _onBindingRemoved(binding) { | 118 _onBindingRemoved(binding) { |
| 119 binding[Persistence.Persistence._postvalidatePromise] = null; | |
| 120 if (binding[Persistence.Persistence._prevalidatePromise]) { | |
| 121 binding[Persistence.Persistence._prevalidatePromise] = null; | |
| 122 return; | |
|
dgozman
2016/12/07 01:45:41
Drop this return, and the if.
| |
| 123 } | |
| 124 if (!binding.network[Persistence.Persistence._binding] || !binding.fileSyste m[Persistence.Persistence._binding]) | |
| 125 return; | |
| 126 | |
| 67 binding.network[Persistence.Persistence._binding] = null; | 127 binding.network[Persistence.Persistence._binding] = null; |
| 68 binding.fileSystem[Persistence.Persistence._binding] = null; | 128 binding.fileSystem[Persistence.Persistence._binding] = null; |
| 69 | 129 |
| 130 binding.network.removeEventListener(Workspace.UISourceCode.Events.ContentLoa ded, this._onContentLoaded, this); | |
| 131 binding.fileSystem.removeEventListener(Workspace.UISourceCode.Events.Content Loaded, this._onContentLoaded, this); | |
| 70 binding.network.removeEventListener( | 132 binding.network.removeEventListener( |
| 71 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this); | 133 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this); |
| 72 binding.fileSystem.removeEventListener( | 134 binding.fileSystem.removeEventListener( |
| 73 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this); | 135 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this); |
| 74 binding.network.removeEventListener( | 136 binding.network.removeEventListener( |
| 75 Workspace.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyCha nged, this); | 137 Workspace.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyCha nged, this); |
| 76 binding.fileSystem.removeEventListener( | 138 binding.fileSystem.removeEventListener( |
| 77 Workspace.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyCha nged, this); | 139 Workspace.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyCha nged, this); |
| 78 | 140 |
| 79 this._removeFilePathBindingPrefixes(binding.fileSystem.url()); | 141 this._removeFilePathBindingPrefixes(binding.fileSystem.url()); |
| 80 | 142 |
| 81 this._breakpointManager.copyBreakpoints(binding.network.url(), binding.fileS ystem); | 143 this._breakpointManager.copyBreakpoints(binding.network.url(), binding.fileS ystem); |
| 82 this.dispatchEventToListeners(Persistence.Persistence.Events.BindingRemoved, binding); | 144 this.dispatchEventToListeners(Persistence.Persistence.Events.BindingRemoved, binding); |
| 83 } | 145 } |
| 84 | 146 |
| 85 /** | 147 /** |
| 86 * @param {!Common.Event} event | 148 * @param {!Common.Event} event |
| 87 */ | 149 */ |
| 150 _onContentLoaded(event) { | |
| 151 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.target); | |
| 152 var binding = uiSourceCode[Persistence.Persistence._binding]; | |
| 153 if (!binding || binding[Persistence.Persistence._postvalidatePromise]) | |
| 154 return; | |
| 155 var promise = this._validateBinding(binding).then(onBindingValidated.bind(th is)); | |
| 156 binding[Persistence.Persistence._postvalidatePromise] = promise; | |
| 157 | |
| 158 /** | |
| 159 * @this {Persistence.Persistence} | |
| 160 */ | |
| 161 function onBindingValidated(isValid) { | |
| 162 if (binding[Persistence.Persistence._postvalidatePromise] !== promise) | |
| 163 return; | |
| 164 binding[Persistence.Persistence._postvalidatePromise] = null; | |
| 165 if (!isValid) | |
| 166 this._onBindingRemoved(binding); | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 /** | |
| 171 * @param {!Common.Event} event | |
| 172 */ | |
| 88 _onWorkingCopyChanged(event) { | 173 _onWorkingCopyChanged(event) { |
| 89 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.target); | 174 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.target); |
| 90 var binding = uiSourceCode[Persistence.Persistence._binding]; | 175 var binding = uiSourceCode[Persistence.Persistence._binding]; |
| 91 if (!binding || binding[Persistence.Persistence._muteWorkingCopy]) | 176 if (!binding || binding[Persistence.Persistence._muteWorkingCopy]) |
| 92 return; | 177 return; |
| 93 var other = binding.network === uiSourceCode ? binding.fileSystem : binding. network; | 178 var other = binding.network === uiSourceCode ? binding.fileSystem : binding. network; |
| 94 var target = Bindings.NetworkProject.targetForUISourceCode(binding.network); | 179 var target = Bindings.NetworkProject.targetForUISourceCode(binding.network); |
| 95 if (target.isNodeJS()) { | 180 if (target.isNodeJS()) { |
| 96 var newContent = uiSourceCode.workingCopy(); | 181 var newContent = uiSourceCode.workingCopy(); |
| 97 other.requestContent().then(() => { | 182 other.requestContent().then(() => { |
| 98 var nodeJSContent = this._rewrapNodeJSContent(binding, other, other.work ingCopy(), newContent); | 183 var nodeJSContent = |
| 184 Persistence.Persistence._rewrapNodeJSContent(binding, other, other.w orkingCopy(), newContent); | |
| 99 setWorkingCopy.call(this, () => nodeJSContent); | 185 setWorkingCopy.call(this, () => nodeJSContent); |
| 100 }); | 186 }); |
| 101 return; | 187 return; |
| 102 } | 188 } |
| 103 | 189 |
| 104 setWorkingCopy.call(this, () => uiSourceCode.workingCopy()); | 190 setWorkingCopy.call(this, () => uiSourceCode.workingCopy()); |
| 105 | 191 |
| 106 /** | 192 /** |
| 107 * @param {function():string} workingCopyGetter | 193 * @param {function():string} workingCopyGetter |
| 108 * @this {Persistence.Persistence} | 194 * @this {Persistence.Persistence} |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 121 _onWorkingCopyCommitted(event) { | 207 _onWorkingCopyCommitted(event) { |
| 122 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.target); | 208 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.target); |
| 123 var binding = uiSourceCode[Persistence.Persistence._binding]; | 209 var binding = uiSourceCode[Persistence.Persistence._binding]; |
| 124 if (!binding || binding[Persistence.Persistence._muteCommit]) | 210 if (!binding || binding[Persistence.Persistence._muteCommit]) |
| 125 return; | 211 return; |
| 126 var newContent = /** @type {string} */ (event.data.content); | 212 var newContent = /** @type {string} */ (event.data.content); |
| 127 var other = binding.network === uiSourceCode ? binding.fileSystem : binding. network; | 213 var other = binding.network === uiSourceCode ? binding.fileSystem : binding. network; |
| 128 var target = Bindings.NetworkProject.targetForUISourceCode(binding.network); | 214 var target = Bindings.NetworkProject.targetForUISourceCode(binding.network); |
| 129 if (target.isNodeJS()) { | 215 if (target.isNodeJS()) { |
| 130 other.requestContent().then(currentContent => { | 216 other.requestContent().then(currentContent => { |
| 131 var nodeJSContent = this._rewrapNodeJSContent(binding, other, currentCon tent, newContent); | 217 var nodeJSContent = Persistence.Persistence._rewrapNodeJSContent(binding , other, currentContent, newContent); |
| 132 setContent.call(this, nodeJSContent); | 218 setContent.call(this, nodeJSContent); |
| 133 }); | 219 }); |
| 134 return; | 220 return; |
| 135 } | 221 } |
| 136 setContent.call(this, newContent); | 222 setContent.call(this, newContent); |
| 137 | 223 |
| 138 /** | 224 /** |
| 139 * @param {string} newContent | 225 * @param {string} newContent |
| 140 * @this {Persistence.Persistence} | 226 * @this {Persistence.Persistence} |
| 141 */ | 227 */ |
| 142 function setContent(newContent) { | 228 function setContent(newContent) { |
| 143 binding[Persistence.Persistence._muteCommit] = true; | 229 binding[Persistence.Persistence._muteCommit] = true; |
| 144 other.addRevision(newContent); | 230 other.addRevision(newContent); |
| 145 binding[Persistence.Persistence._muteCommit] = false; | 231 binding[Persistence.Persistence._muteCommit] = false; |
| 146 this._contentSyncedForTest(); | 232 this._contentSyncedForTest(); |
| 147 } | 233 } |
| 148 } | 234 } |
| 149 | 235 |
| 150 /** | 236 /** |
| 151 * @param {!Persistence.PersistenceBinding} binding | 237 * @param {!Persistence.PersistenceBinding} binding |
| 152 * @param {!Workspace.UISourceCode} uiSourceCode | 238 * @param {!Workspace.UISourceCode} uiSourceCode |
| 153 * @param {string} currentContent | 239 * @param {string} currentContent |
| 154 * @param {string} newContent | 240 * @param {string} newContent |
| 155 * @return {string} | 241 * @return {string} |
| 156 */ | 242 */ |
| 157 _rewrapNodeJSContent(binding, uiSourceCode, currentContent, newContent) { | 243 static _rewrapNodeJSContent(binding, uiSourceCode, currentContent, newContent) { |
| 158 if (uiSourceCode === binding.fileSystem) { | 244 if (uiSourceCode === binding.fileSystem) { |
| 159 if (newContent.startsWith(Persistence.Persistence._NodePrefix) && | 245 if (newContent.startsWith(Persistence.Persistence._NodePrefix) && |
| 160 newContent.endsWith(Persistence.Persistence._NodeSuffix)) { | 246 newContent.endsWith(Persistence.Persistence._NodeSuffix)) { |
| 161 newContent = newContent.substring( | 247 newContent = newContent.substring( |
| 162 Persistence.Persistence._NodePrefix.length, newContent.length - Pers istence.Persistence._NodeSuffix.length); | 248 Persistence.Persistence._NodePrefix.length, newContent.length - Pers istence.Persistence._NodeSuffix.length); |
| 163 } | 249 } |
| 164 if (currentContent.startsWith(Persistence.Persistence._NodeShebang)) | 250 if (currentContent.startsWith(Persistence.Persistence._NodeShebang)) |
| 165 newContent = Persistence.Persistence._NodeShebang + newContent; | 251 newContent = Persistence.Persistence._NodeShebang + newContent; |
| 166 } else { | 252 } else { |
| 167 if (newContent.startsWith(Persistence.Persistence._NodeShebang)) | 253 if (newContent.startsWith(Persistence.Persistence._NodeShebang)) |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 * @param {!Workspace.UISourceCode} fileSystem | 372 * @param {!Workspace.UISourceCode} fileSystem |
| 287 * @param {boolean} exactMatch | 373 * @param {boolean} exactMatch |
| 288 */ | 374 */ |
| 289 constructor(network, fileSystem, exactMatch) { | 375 constructor(network, fileSystem, exactMatch) { |
| 290 this.network = network; | 376 this.network = network; |
| 291 this.fileSystem = fileSystem; | 377 this.fileSystem = fileSystem; |
| 292 this.exactMatch = exactMatch; | 378 this.exactMatch = exactMatch; |
| 293 } | 379 } |
| 294 }; | 380 }; |
| 295 | 381 |
| 382 Persistence.Persistence._prevalidatePromise = Symbol('Persistence.prevalidatePro mise'); | |
|
dgozman
2016/12/07 01:45:41
Let's have private fields instead.
| |
| 383 Persistence.Persistence._postvalidatePromise = Symbol('Persistence.postvalidateP romise'); | |
| 384 | |
| 296 /** @type {!Persistence.Persistence} */ | 385 /** @type {!Persistence.Persistence} */ |
| 297 Persistence.persistence; | 386 Persistence.persistence; |
| OLD | NEW |