Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/persistence/Persistence.js

Issue 2542073002: DevTools: [Persistence] validate persistence binding. (Closed)
Patch Set: revert test changes Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
11 * @param {!Workspace.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 Persistence.PersistenceUtils.LinkDecorator(this); 21 var linkDecorator = new Persistence.PersistenceUtils.LinkDecorator(this);
22 Components.Linkifier.setLinkDecorator(linkDecorator); 22 Components.Linkifier.setLinkDecorator(linkDecorator);
23 this._mapping = 23 this._mapping =
24 new Persistence.Automapping(workspace, this._onBindingCreated.bind(thi s), this._onBindingRemoved.bind(this)); 24 new Persistence.Automapping(workspace, this._validateBinding.bind(this ), this._onBindingRemoved.bind(this));
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._validateBinding.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 _validateBinding(binding) {
35 if (binding.network.isDirty()) { 35 if (!Runtime.experiments.isEnabled('persistenceValidation') || binding.netwo rk.contentType().isFromSourceMap()) {
36 Common.console.log( 36 this._establishBinding(binding);
37 Common.UIString('%s can not be persisted to file system due to unsaved changes.', binding.network.name()));
38 return; 37 return;
39 } 38 }
40 if (binding.fileSystem.isDirty())
41 binding.network.setWorkingCopy(binding.fileSystem.workingCopy());
42 39
40 Promise.all([binding.network.requestContent(), binding.fileSystem.requestCon tent()]).then(onContents.bind(this));
41
42 /**
43 * @this {Persistence.Persistence}
44 */
45 function onContents() {
46 if (binding._removed)
47 return;
48
49 var fileSystemContent = binding.fileSystem.workingCopy();
50 var networkContent = binding.network.workingCopy();
51 var target = Bindings.NetworkProject.targetForUISourceCode(binding.network );
52 var isValid = false;
53 if (target.isNodeJS()) {
54 var rewrappedNetworkContent = Persistence.Persistence._rewrapNodeJSConte nt(
55 binding, binding.fileSystem, fileSystemContent, networkContent);
56 isValid = (fileSystemContent === rewrappedNetworkContent);
57 } else {
58 // Trim trailing whitespaces because V8 adds trailing newline.
59 isValid = (fileSystemContent.trimRight() === networkContent.trimRight()) ;
60 }
61
62 if (isValid)
63 this._establishBinding(binding);
64 else
65 this._prevalidationFailedForTest(binding);
66 }
67 }
68
69 /**
70 * @param {!Persistence.PersistenceBinding} binding
71 */
72 _prevalidationFailedForTest(binding) {
73 }
74
75 /**
76 * @param {!Persistence.PersistenceBinding} binding
77 */
78 _establishBinding(binding) {
43 binding.network[Persistence.Persistence._binding] = binding; 79 binding.network[Persistence.Persistence._binding] = binding;
44 binding.fileSystem[Persistence.Persistence._binding] = binding; 80 binding.fileSystem[Persistence.Persistence._binding] = binding;
45 81
46 binding.fileSystem.forceLoadOnCheckContent(); 82 binding.fileSystem.forceLoadOnCheckContent();
47 83
48 binding.network.addEventListener( 84 binding.network.addEventListener(
49 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this); 85 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this);
50 binding.fileSystem.addEventListener( 86 binding.fileSystem.addEventListener(
51 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this); 87 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this);
52 binding.network.addEventListener( 88 binding.network.addEventListener(
53 Workspace.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyCha nged, this); 89 Workspace.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyCha nged, this);
54 binding.fileSystem.addEventListener( 90 binding.fileSystem.addEventListener(
55 Workspace.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyCha nged, this); 91 Workspace.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyCha nged, this);
56 92
57 this._addFilePathBindingPrefixes(binding.fileSystem.url()); 93 this._addFilePathBindingPrefixes(binding.fileSystem.url());
58 94
59 this._moveBreakpoints(binding.fileSystem, binding.network); 95 this._moveBreakpoints(binding.fileSystem, binding.network);
60 this.dispatchEventToListeners(Persistence.Persistence.Events.BindingCreated, binding); 96 this.dispatchEventToListeners(Persistence.Persistence.Events.BindingCreated, binding);
61 } 97 }
62 98
63 /** 99 /**
64 * @param {!Persistence.PersistenceBinding} binding 100 * @param {!Persistence.PersistenceBinding} binding
65 */ 101 */
66 _onBindingRemoved(binding) { 102 _onBindingRemoved(binding) {
103 binding._removed = true;
104 if (!binding.network[Persistence.Persistence._binding] || !binding.fileSyste m[Persistence.Persistence._binding])
dgozman 2016/12/08 22:38:53 if (binding.network[Persistence.Persistence._bindi
lushnikov 2016/12/08 23:02:24 Done.
105 return;
106
67 binding.network[Persistence.Persistence._binding] = null; 107 binding.network[Persistence.Persistence._binding] = null;
68 binding.fileSystem[Persistence.Persistence._binding] = null; 108 binding.fileSystem[Persistence.Persistence._binding] = null;
69 109
70 binding.network.removeEventListener( 110 binding.network.removeEventListener(
71 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this); 111 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this);
72 binding.fileSystem.removeEventListener( 112 binding.fileSystem.removeEventListener(
73 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this); 113 Workspace.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyC ommitted, this);
74 binding.network.removeEventListener( 114 binding.network.removeEventListener(
75 Workspace.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyCha nged, this); 115 Workspace.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyCha nged, this);
76 binding.fileSystem.removeEventListener( 116 binding.fileSystem.removeEventListener(
(...skipping 11 matching lines...) Expand all
88 _onWorkingCopyChanged(event) { 128 _onWorkingCopyChanged(event) {
89 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.target); 129 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.target);
90 var binding = uiSourceCode[Persistence.Persistence._binding]; 130 var binding = uiSourceCode[Persistence.Persistence._binding];
91 if (!binding || binding[Persistence.Persistence._muteWorkingCopy]) 131 if (!binding || binding[Persistence.Persistence._muteWorkingCopy])
92 return; 132 return;
93 var other = binding.network === uiSourceCode ? binding.fileSystem : binding. network; 133 var other = binding.network === uiSourceCode ? binding.fileSystem : binding. network;
94 var target = Bindings.NetworkProject.targetForUISourceCode(binding.network); 134 var target = Bindings.NetworkProject.targetForUISourceCode(binding.network);
95 if (target.isNodeJS()) { 135 if (target.isNodeJS()) {
96 var newContent = uiSourceCode.workingCopy(); 136 var newContent = uiSourceCode.workingCopy();
97 other.requestContent().then(() => { 137 other.requestContent().then(() => {
98 var nodeJSContent = this._rewrapNodeJSContent(binding, other, other.work ingCopy(), newContent); 138 var nodeJSContent =
139 Persistence.Persistence._rewrapNodeJSContent(binding, other, other.w orkingCopy(), newContent);
99 setWorkingCopy.call(this, () => nodeJSContent); 140 setWorkingCopy.call(this, () => nodeJSContent);
100 }); 141 });
101 return; 142 return;
102 } 143 }
103 144
104 setWorkingCopy.call(this, () => uiSourceCode.workingCopy()); 145 setWorkingCopy.call(this, () => uiSourceCode.workingCopy());
105 146
106 /** 147 /**
107 * @param {function():string} workingCopyGetter 148 * @param {function():string} workingCopyGetter
108 * @this {Persistence.Persistence} 149 * @this {Persistence.Persistence}
(...skipping 12 matching lines...) Expand all
121 _onWorkingCopyCommitted(event) { 162 _onWorkingCopyCommitted(event) {
122 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.target); 163 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.target);
123 var binding = uiSourceCode[Persistence.Persistence._binding]; 164 var binding = uiSourceCode[Persistence.Persistence._binding];
124 if (!binding || binding[Persistence.Persistence._muteCommit]) 165 if (!binding || binding[Persistence.Persistence._muteCommit])
125 return; 166 return;
126 var newContent = /** @type {string} */ (event.data.content); 167 var newContent = /** @type {string} */ (event.data.content);
127 var other = binding.network === uiSourceCode ? binding.fileSystem : binding. network; 168 var other = binding.network === uiSourceCode ? binding.fileSystem : binding. network;
128 var target = Bindings.NetworkProject.targetForUISourceCode(binding.network); 169 var target = Bindings.NetworkProject.targetForUISourceCode(binding.network);
129 if (target.isNodeJS()) { 170 if (target.isNodeJS()) {
130 other.requestContent().then(currentContent => { 171 other.requestContent().then(currentContent => {
131 var nodeJSContent = this._rewrapNodeJSContent(binding, other, currentCon tent, newContent); 172 var nodeJSContent = Persistence.Persistence._rewrapNodeJSContent(binding , other, currentContent, newContent);
132 setContent.call(this, nodeJSContent); 173 setContent.call(this, nodeJSContent);
133 }); 174 });
134 return; 175 return;
135 } 176 }
136 setContent.call(this, newContent); 177 setContent.call(this, newContent);
137 178
138 /** 179 /**
139 * @param {string} newContent 180 * @param {string} newContent
140 * @this {Persistence.Persistence} 181 * @this {Persistence.Persistence}
141 */ 182 */
142 function setContent(newContent) { 183 function setContent(newContent) {
143 binding[Persistence.Persistence._muteCommit] = true; 184 binding[Persistence.Persistence._muteCommit] = true;
144 other.addRevision(newContent); 185 other.addRevision(newContent);
145 binding[Persistence.Persistence._muteCommit] = false; 186 binding[Persistence.Persistence._muteCommit] = false;
146 this._contentSyncedForTest(); 187 this._contentSyncedForTest();
147 } 188 }
148 } 189 }
149 190
150 /** 191 /**
151 * @param {!Persistence.PersistenceBinding} binding 192 * @param {!Persistence.PersistenceBinding} binding
152 * @param {!Workspace.UISourceCode} uiSourceCode 193 * @param {!Workspace.UISourceCode} uiSourceCode
153 * @param {string} currentContent 194 * @param {string} currentContent
154 * @param {string} newContent 195 * @param {string} newContent
155 * @return {string} 196 * @return {string}
156 */ 197 */
157 _rewrapNodeJSContent(binding, uiSourceCode, currentContent, newContent) { 198 static _rewrapNodeJSContent(binding, uiSourceCode, currentContent, newContent) {
158 if (uiSourceCode === binding.fileSystem) { 199 if (uiSourceCode === binding.fileSystem) {
159 if (newContent.startsWith(Persistence.Persistence._NodePrefix) && 200 if (newContent.startsWith(Persistence.Persistence._NodePrefix) &&
160 newContent.endsWith(Persistence.Persistence._NodeSuffix)) { 201 newContent.endsWith(Persistence.Persistence._NodeSuffix)) {
161 newContent = newContent.substring( 202 newContent = newContent.substring(
162 Persistence.Persistence._NodePrefix.length, newContent.length - Pers istence.Persistence._NodeSuffix.length); 203 Persistence.Persistence._NodePrefix.length, newContent.length - Pers istence.Persistence._NodeSuffix.length);
163 } 204 }
164 if (currentContent.startsWith(Persistence.Persistence._NodeShebang)) 205 if (currentContent.startsWith(Persistence.Persistence._NodeShebang))
165 newContent = Persistence.Persistence._NodeShebang + newContent; 206 newContent = Persistence.Persistence._NodeShebang + newContent;
166 } else { 207 } else {
167 if (newContent.startsWith(Persistence.Persistence._NodeShebang)) 208 if (newContent.startsWith(Persistence.Persistence._NodeShebang))
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 Persistence.PersistenceBinding = class { 324 Persistence.PersistenceBinding = class {
284 /** 325 /**
285 * @param {!Workspace.UISourceCode} network 326 * @param {!Workspace.UISourceCode} network
286 * @param {!Workspace.UISourceCode} fileSystem 327 * @param {!Workspace.UISourceCode} fileSystem
287 * @param {boolean} exactMatch 328 * @param {boolean} exactMatch
288 */ 329 */
289 constructor(network, fileSystem, exactMatch) { 330 constructor(network, fileSystem, exactMatch) {
290 this.network = network; 331 this.network = network;
291 this.fileSystem = fileSystem; 332 this.fileSystem = fileSystem;
292 this.exactMatch = exactMatch; 333 this.exactMatch = exactMatch;
334 this._removed = false;
293 } 335 }
294 }; 336 };
295 337
296 /** @type {!Persistence.Persistence} */ 338 /** @type {!Persistence.Persistence} */
297 Persistence.persistence; 339 Persistence.persistence;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698