1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @unrestricted
*/
Persistence.DefaultMapping = class {
/**
* @param {!Workspace.Workspace} workspace
* @param {!Workspace.FileSystemMapping} fileSystemMapping
* @param {function(!Persistence.PersistenceBinding)} onBindingCreated
* @param {function(!Persistence.PersistenceBinding)} onBindingRemoved
*/
constructor(workspace, fileSystemMapping, onBindingCreated, onBindingRemoved) {
this._workspace = workspace;
this._fileSystemMapping = fileSystemMapping;
/** @type {!Set<!Persistence.PersistenceBinding>} */
this._bindings = new Set();
this._onBindingCreated = onBindingCreated;
this._onBindingRemoved = onBindingRemoved;
this._eventListeners = [
workspace.addEventListener(Workspace.Workspace.Events.UISourceCodeAdded, this._onUISourceCodeAdded, this),
workspace.addEventListener(Workspace.Workspace.Events.UISourceCodeRemoved, this._onUISourceCodeRemoved, this),
workspace.addEventListener(Workspace.Workspace.Events.ProjectRemoved, this._onProjectRemoved, this),
this._fileSystemMapping.addEventListener(Workspace.FileSystemMapping.Events.FileMappingAdded, this._remap, this),
this._fileSystemMapping.addEventListener(Workspace.FileSystemMapping.Events.FileMappingRemoved, this._remap, this)
];
this._remap();
}
_remap() {
for (var binding of this._bindings.valuesArray())
this._unbind(binding.network);
var networkProjects = this._workspace.projectsForType(Workspace.projectTypes.Network);
for (var networkProject of networkProjects) {
for (var uiSourceCode of networkProject.uiSourceCodes())
this._bind(uiSourceCode);
}
}
/**
* @param {!Common.Event} event
*/
_onUISourceCodeAdded(event) {
var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.data);
this._bind(uiSourceCode);
}
/**
* @param {!Common.Event} event
*/
_onUISourceCodeRemoved(event) {
var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.data);
this._unbind(uiSourceCode);
}
/**
* @param {!Common.Event} event
*/
_onProjectRemoved(event) {
var project = /** @type {!Workspace.Project} */ (event.data);
for (var uiSourceCode of project.uiSourceCodes())
this._unbind(uiSourceCode);
}
/**
* @param {!Workspace.UISourceCode} uiSourceCode
* @return {?Persistence.PersistenceBinding}
*/
_createBinding(uiSourceCode) {
if (uiSourceCode.project().type() === Workspace.projectTypes.FileSystem) {
var fileSystemPath = Persistence.FileSystemWorkspaceBinding.fileSystemPath(uiSourceCode.project().id());
var networkURL = this._fileSystemMapping.networkURLForFileSystemURL(fileSystemPath, uiSourceCode.url());
var networkSourceCode = networkURL ? this._workspace.uiSourceCodeForURL(networkURL) : null;
return networkSourceCode ? new Persistence.PersistenceBinding(networkSourceCode, uiSourceCode, false) : null;
}
if (uiSourceCode.project().type() === Workspace.projectTypes.Network) {
var file = this._fileSystemMapping.fileForURL(uiSourceCode.url());
var projectId = file ? Persistence.FileSystemWorkspaceBinding.projectId(file.fileSystemPath) : null;
var fileSourceCode = file && projectId ? this._workspace.uiSourceCode(projectId, file.fileURL) : null;
return fileSourceCode ? new Persistence.PersistenceBinding(uiSourceCode, fileSourceCode, false) : null;
}
return null;
}
/**
* @param {!Workspace.UISourceCode} uiSourceCode
*/
_bind(uiSourceCode) {
console.assert(!uiSourceCode[Persistence.DefaultMapping._binding], 'Cannot bind already bound UISourceCode!');
var binding = this._createBinding(uiSourceCode);
if (!binding)
return;
// TODO(lushnikov): remove this check once there's a single uiSourceCode per url. @see crbug.com/670180
if (binding.network[Persistence.DefaultMapping._binding] || binding.fileSystem[Persistence.DefaultMapping._binding])
return;
this._bindings.add(binding);
binding.network[Persistence.DefaultMapping._binding] = binding;
binding.fileSystem[Persistence.DefaultMapping._binding] = binding;
binding.fileSystem.addEventListener(
Workspace.UISourceCode.Events.TitleChanged, this._onFileSystemUISourceCodeRenamed, this);
this._onBindingCreated.call(null, binding);
}
/**
* @param {!Workspace.UISourceCode} uiSourceCode
*/
_unbind(uiSourceCode) {
var binding = uiSourceCode[Persistence.DefaultMapping._binding];
if (!binding)
return;
this._bindings.delete(binding);
binding.network[Persistence.DefaultMapping._binding] = null;
binding.fileSystem[Persistence.DefaultMapping._binding] = null;
binding.fileSystem.removeEventListener(
Workspace.UISourceCode.Events.TitleChanged, this._onFileSystemUISourceCodeRenamed, this);
this._onBindingRemoved.call(null, binding);
}
/**
* @param {!Common.Event} event
*/
_onFileSystemUISourceCodeRenamed(event) {
var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.data);
var binding = uiSourceCode[Persistence.DefaultMapping._binding];
this._unbind(binding.network);
this._bind(binding.network);
}
dispose() {
Common.EventTarget.removeEventListeners(this._eventListeners);
}
};
Persistence.DefaultMapping._binding = Symbol('DefaultMapping.Binding');
|