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

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

Issue 2398493002: DevTools: [Persistence] split out WI.DefaultMapping (Closed)
Patch Set: address comments Created 4 years, 2 months 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 /** 5 /**
6 * @constructor 6 * @constructor
7 * @extends {WebInspector.Object} 7 * @extends {WebInspector.Object}
8 * @param {!WebInspector.Workspace} workspace 8 * @param {!WebInspector.Workspace} workspace
9 * @param {!WebInspector.BreakpointManager} breakpointManager 9 * @param {!WebInspector.BreakpointManager} breakpointManager
10 * @param {!WebInspector.FileSystemMapping} fileSystemMapping 10 * @param {!WebInspector.FileSystemMapping} fileSystemMapping
11 */ 11 */
12 WebInspector.Persistence = function(workspace, breakpointManager, fileSystemMapp ing) 12 WebInspector.Persistence = function(workspace, breakpointManager, fileSystemMapp ing)
13 { 13 {
14 WebInspector.Object.call(this); 14 WebInspector.Object.call(this);
15 this._workspace = workspace; 15 this._workspace = workspace;
16 this._breakpointManager = breakpointManager; 16 this._breakpointManager = breakpointManager;
17 this._fileSystemMapping = fileSystemMapping;
18 /** @type {!Set<!WebInspector.PersistenceBinding>} */
19 this._bindings = new Set();
20
21 /** @type {!Map<string, number>} */ 17 /** @type {!Map<string, number>} */
22 this._filePathPrefixesToBindingCount = new Map(); 18 this._filePathPrefixesToBindingCount = new Map();
23 19
24 this._eventListeners = [ 20 this._mapping = new WebInspector.DefaultMapping(workspace, fileSystemMapping , this._onBindingCreated.bind(this), this._onBindingRemoved.bind(this));
25 workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeAdd ed, this._onUISourceCodeAdded, this),
26 workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeRem oved, this._onUISourceCodeRemoved, this),
27 workspace.addEventListener(WebInspector.Workspace.Events.ProjectRemoved, this._onProjectRemoved, this),
28 this._fileSystemMapping.addEventListener(WebInspector.FileSystemMapping. Events.FileMappingAdded, this._remap, this),
29 this._fileSystemMapping.addEventListener(WebInspector.FileSystemMapping. Events.FileMappingRemoved, this._remap, this)
30 ];
31 this._remap();
32 } 21 }
33 22
34 WebInspector.Persistence._binding = Symbol("Persistence.Binding"); 23 WebInspector.Persistence._binding = Symbol("Persistence.Binding");
35 WebInspector.Persistence._muteCommit = Symbol("Persistence.MuteCommit"); 24 WebInspector.Persistence._muteCommit = Symbol("Persistence.MuteCommit");
36 25
37 WebInspector.Persistence._NodePrefix = "(function (exports, require, module, __f ilename, __dirname) { "; 26 WebInspector.Persistence._NodePrefix = "(function (exports, require, module, __f ilename, __dirname) { ";
38 WebInspector.Persistence._NodeSuffix = "\n});" 27 WebInspector.Persistence._NodeSuffix = "\n});"
39 WebInspector.Persistence._NodeShebang = "#!/usr/bin/env node\n"; 28 WebInspector.Persistence._NodeShebang = "#!/usr/bin/env node\n";
40 29
41 WebInspector.Persistence.Events = { 30 WebInspector.Persistence.Events = {
42 BindingCreated: Symbol("BindingCreated"), 31 BindingCreated: Symbol("BindingCreated"),
43 BindingRemoved: Symbol("BindingRemoved") 32 BindingRemoved: Symbol("BindingRemoved")
44 } 33 }
45 34
46 WebInspector.Persistence.prototype = { 35 WebInspector.Persistence.prototype = {
47 _remap: function() 36 /**
37 * @param {!WebInspector.PersistenceBinding} binding
38 */
39 _onBindingCreated: function(binding)
48 { 40 {
49 for (var binding of this._bindings.valuesArray())
50 this._unbind(binding.network);
51 var networkProjects = this._workspace.projectsForType(WebInspector.proje ctTypes.Network);
52 for (var networkProject of networkProjects) {
53 for (var uiSourceCode of networkProject.uiSourceCodes())
54 this._bind(uiSourceCode);
55 }
56 },
57
58 /**
59 * @param {!WebInspector.Event} event
60 */
61 _onUISourceCodeAdded: function(event)
62 {
63 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */(event.data) ;
64 this._bind(uiSourceCode);
65 },
66
67 /**
68 * @param {!WebInspector.Event} event
69 */
70 _onUISourceCodeRemoved: function(event)
71 {
72 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */(event.data) ;
73 this._unbind(uiSourceCode);
74 },
75
76 /**
77 * @param {!WebInspector.Event} event
78 */
79 _onProjectRemoved: function(event)
80 {
81 var project = /** @type {!WebInspector.Project} */(event.data);
82 for (var uiSourceCode of project.uiSourceCodes())
83 this._unbind(uiSourceCode);
84 },
85
86 /**
87 * @param {!WebInspector.UISourceCode} uiSourceCode
88 * @return {?WebInspector.PersistenceBinding}
89 */
90 _createBinding: function(uiSourceCode)
91 {
92 if (uiSourceCode.project().type() === WebInspector.projectTypes.FileSyst em) {
93 var fileSystemPath = WebInspector.FileSystemWorkspaceBinding.fileSys temPath(uiSourceCode.project().id());
94 var networkURL = this._fileSystemMapping.networkURLForFileSystemURL( fileSystemPath, uiSourceCode.url());
95 var networkSourceCode = networkURL ? this._workspace.uiSourceCodeFor URL(networkURL) : null;
96 return networkSourceCode ? new WebInspector.PersistenceBinding(netwo rkSourceCode, uiSourceCode) : null;
97 }
98 if (uiSourceCode.project().type() === WebInspector.projectTypes.Network) {
99 var file = this._fileSystemMapping.fileForURL(uiSourceCode.url());
100 var projectId = file ? WebInspector.FileSystemWorkspaceBinding.proje ctId(file.fileSystemPath) : null;
101 var fileSourceCode = file && projectId ? this._workspace.uiSourceCod e(projectId, file.fileURL) : null;
102 return fileSourceCode ? new WebInspector.PersistenceBinding(uiSource Code, fileSourceCode) : null;
103 }
104 return null;
105 },
106
107 /**
108 * @param {!WebInspector.UISourceCode} uiSourceCode
109 */
110 _bind: function(uiSourceCode)
111 {
112 console.assert(!uiSourceCode[WebInspector.Persistence._binding], "Cannot bind already bound UISourceCode!");
113 var binding = this._createBinding(uiSourceCode);
114 if (!binding)
115 return;
116 if (binding.network.isDirty() || binding.fileSystem.isDirty()) { 41 if (binding.network.isDirty() || binding.fileSystem.isDirty()) {
117 WebInspector.console.log(WebInspector.UIString("%s can not be persis ted to file system due to unsaved changes.", binding.network.name())); 42 WebInspector.console.log(WebInspector.UIString("%s can not be persis ted to file system due to unsaved changes.", binding.network.name()));
118 return; 43 return;
119 } 44 }
120 this._bindings.add(binding);
121 binding.network[WebInspector.Persistence._binding] = binding; 45 binding.network[WebInspector.Persistence._binding] = binding;
122 binding.fileSystem[WebInspector.Persistence._binding] = binding; 46 binding.fileSystem[WebInspector.Persistence._binding] = binding;
123 47
124 binding.fileSystem.forceLoadOnCheckContent(); 48 binding.fileSystem.forceLoadOnCheckContent();
125 49
126 binding.network.addEventListener(WebInspector.UISourceCode.Events.Workin gCopyCommitted, this._onWorkingCopyCommitted, this); 50 binding.network.addEventListener(WebInspector.UISourceCode.Events.Workin gCopyCommitted, this._onWorkingCopyCommitted, this);
127 binding.fileSystem.addEventListener(WebInspector.UISourceCode.Events.Wor kingCopyCommitted, this._onWorkingCopyCommitted, this); 51 binding.fileSystem.addEventListener(WebInspector.UISourceCode.Events.Wor kingCopyCommitted, this._onWorkingCopyCommitted, this);
128 binding.fileSystem.addEventListener(WebInspector.UISourceCode.Events.Tit leChanged, this._onFileSystemUISourceCodeRenamed, this);
129 52
130 this._addFilePathBindingPrefixes(binding.fileSystem.url()); 53 this._addFilePathBindingPrefixes(binding.fileSystem.url());
131 54
132 this._moveBreakpoints(binding.fileSystem, binding.network); 55 this._moveBreakpoints(binding.fileSystem, binding.network);
133 this.dispatchEventToListeners(WebInspector.Persistence.Events.BindingCre ated, binding); 56 this.dispatchEventToListeners(WebInspector.Persistence.Events.BindingCre ated, binding);
134 }, 57 },
135 58
136 /** 59 /**
137 * @param {!WebInspector.UISourceCode} uiSourceCode 60 * @param {!WebInspector.PersistenceBinding} binding
138 */ 61 */
139 _unbind: function(uiSourceCode) 62 _onBindingRemoved: function(binding)
140 { 63 {
141 var binding = uiSourceCode[WebInspector.Persistence._binding];
142 if (!binding)
143 return;
144 this._bindings.delete(binding);
145 binding.network[WebInspector.Persistence._binding] = null; 64 binding.network[WebInspector.Persistence._binding] = null;
146 binding.fileSystem[WebInspector.Persistence._binding] = null; 65 binding.fileSystem[WebInspector.Persistence._binding] = null;
147 66
148 binding.network.removeEventListener(WebInspector.UISourceCode.Events.Wor kingCopyCommitted, this._onWorkingCopyCommitted, this); 67 binding.network.removeEventListener(WebInspector.UISourceCode.Events.Wor kingCopyCommitted, this._onWorkingCopyCommitted, this);
149 binding.fileSystem.removeEventListener(WebInspector.UISourceCode.Events. WorkingCopyCommitted, this._onWorkingCopyCommitted, this); 68 binding.fileSystem.removeEventListener(WebInspector.UISourceCode.Events. WorkingCopyCommitted, this._onWorkingCopyCommitted, this);
150 binding.fileSystem.removeEventListener(WebInspector.UISourceCode.Events. TitleChanged, this._onFileSystemUISourceCodeRenamed, this);
151 69
152 this._removeFilePathBindingPrefixes(binding.fileSystem.url()); 70 this._removeFilePathBindingPrefixes(binding.fileSystem.url());
153 71
154 this._copyBreakpoints(binding.network, binding.fileSystem); 72 this._copyBreakpoints(binding.network, binding.fileSystem);
155 this.dispatchEventToListeners(WebInspector.Persistence.Events.BindingRem oved, binding); 73 this.dispatchEventToListeners(WebInspector.Persistence.Events.BindingRem oved, binding);
156 }, 74 },
157 75
158 /** 76 /**
159 * @param {!WebInspector.Event} event 77 * @param {!WebInspector.Event} event
160 */ 78 */
161 _onFileSystemUISourceCodeRenamed: function(event)
162 {
163 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */(event.targe t);
164 var binding = uiSourceCode[WebInspector.Persistence._binding];
165 this._unbind(binding.network);
166 this._bind(binding.network);
167 },
168
169 /**
170 * @param {!WebInspector.Event} event
171 */
172 _onWorkingCopyCommitted: function(event) 79 _onWorkingCopyCommitted: function(event)
173 { 80 {
174 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */(event.targe t); 81 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */(event.targe t);
175 var binding = uiSourceCode[WebInspector.Persistence._binding]; 82 var binding = uiSourceCode[WebInspector.Persistence._binding];
176 if (!binding || binding[WebInspector.Persistence._muteCommit]) 83 if (!binding || binding[WebInspector.Persistence._muteCommit])
177 return; 84 return;
178 var newContent = /** @type {string} */(event.data.content); 85 var newContent = /** @type {string} */(event.data.content);
179 var other = binding.network === uiSourceCode ? binding.fileSystem : bind ing.network; 86 var other = binding.network === uiSourceCode ? binding.fileSystem : bind ing.network;
180 if (Runtime.queryParam("v8only")) { 87 if (Runtime.queryParam("v8only")) {
181 other.requestContent().then(currentContent => this._syncNodeJSConten t(binding, other, currentContent, newContent)); 88 other.requestContent().then(currentContent => this._syncNodeJSConten t(binding, other, currentContent, newContent));
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 */ 216 */
310 filePathHasBindings: function(filePath) 217 filePathHasBindings: function(filePath)
311 { 218 {
312 if (!filePath.endsWith("/")) 219 if (!filePath.endsWith("/"))
313 filePath += "/"; 220 filePath += "/";
314 return this._filePathPrefixesToBindingCount.has(filePath); 221 return this._filePathPrefixesToBindingCount.has(filePath);
315 }, 222 },
316 223
317 dispose: function() 224 dispose: function()
318 { 225 {
319 WebInspector.EventTarget.removeEventListeners(this._eventListeners); 226 this._mapping.dispose();
320 }, 227 },
321 228
322 __proto__: WebInspector.Object.prototype 229 __proto__: WebInspector.Object.prototype
323 } 230 }
324 231
325 /** 232 /**
326 * @constructor 233 * @constructor
327 * @param {!WebInspector.UISourceCode} network 234 * @param {!WebInspector.UISourceCode} network
328 * @param {!WebInspector.UISourceCode} fileSystem 235 * @param {!WebInspector.UISourceCode} fileSystem
329 */ 236 */
330 WebInspector.PersistenceBinding = function(network, fileSystem) 237 WebInspector.PersistenceBinding = function(network, fileSystem)
331 { 238 {
332 this.network = network; 239 this.network = network;
333 this.fileSystem = fileSystem; 240 this.fileSystem = fileSystem;
334 } 241 }
335 242
336 /** @type {!WebInspector.Persistence} */ 243 /** @type {!WebInspector.Persistence} */
337 WebInspector.persistence; 244 WebInspector.persistence;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698