mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-11-05 04:39:20 +08:00
Bug: chromium:1213943 Change-Id: I309436e8608c3f89ac42a117a3f454b5e43eef16 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2933138 Commit-Queue: Sigurd Schneider <sigurds@chromium.org> Reviewed-by: Tim van der Lippe <tvanderlippe@chromium.org>
171 lines
7.0 KiB
TypeScript
171 lines
7.0 KiB
TypeScript
/*
|
|
* Copyright (C) 2012 Google Inc. All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are
|
|
* met:
|
|
*
|
|
* * Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* * Redistributions in binary form must reproduce the above
|
|
* copyright notice, this list of conditions and the following disclaimer
|
|
* in the documentation and/or other materials provided with the
|
|
* distribution.
|
|
* * Neither the name of Google Inc. nor the names of its
|
|
* contributors may be used to endorse or promote products derived from
|
|
* this software without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
/* eslint-disable rulesdir/no_underscored_properties */
|
|
|
|
import * as Common from '../../core/common/common.js';
|
|
import * as SDK from '../../core/sdk/sdk.js';
|
|
import type * as Workspace from '../workspace/workspace.js'; // eslint-disable-line no-unused-vars
|
|
|
|
const uiSourceCodeToAttributionMap = new WeakMap<Workspace.UISourceCode.UISourceCode, Map<string, {
|
|
frame: SDK.ResourceTreeModel.ResourceTreeFrame,
|
|
count: number,
|
|
}>>();
|
|
const projectToTargetMap = new WeakMap<Workspace.Workspace.Project, SDK.Target.Target>();
|
|
|
|
let networkProjectManagerInstance: NetworkProjectManager;
|
|
|
|
export class NetworkProjectManager extends Common.ObjectWrapper.ObjectWrapper {
|
|
private constructor() {
|
|
super();
|
|
}
|
|
|
|
static instance({forceNew}: {
|
|
forceNew: boolean,
|
|
} = {forceNew: false}): NetworkProjectManager {
|
|
if (!networkProjectManagerInstance || forceNew) {
|
|
networkProjectManagerInstance = new NetworkProjectManager();
|
|
}
|
|
|
|
return networkProjectManagerInstance;
|
|
}
|
|
}
|
|
|
|
export const Events = {
|
|
FrameAttributionAdded: Symbol('FrameAttributionAdded'),
|
|
FrameAttributionRemoved: Symbol('FrameAttributionRemoved'),
|
|
};
|
|
|
|
export class NetworkProject {
|
|
static _resolveFrame(uiSourceCode: Workspace.UISourceCode.UISourceCode, frameId: string):
|
|
SDK.ResourceTreeModel.ResourceTreeFrame|null {
|
|
const target = NetworkProject.targetForUISourceCode(uiSourceCode);
|
|
const resourceTreeModel = target && target.model(SDK.ResourceTreeModel.ResourceTreeModel);
|
|
return resourceTreeModel ? resourceTreeModel.frameForId(frameId) : null;
|
|
}
|
|
|
|
static setInitialFrameAttribution(uiSourceCode: Workspace.UISourceCode.UISourceCode, frameId: string): void {
|
|
const frame = NetworkProject._resolveFrame(uiSourceCode, frameId);
|
|
if (!frame) {
|
|
return;
|
|
}
|
|
const attribution = new Map<string, {
|
|
frame: SDK.ResourceTreeModel.ResourceTreeFrame,
|
|
count: number,
|
|
}>();
|
|
attribution.set(frameId, {frame: frame, count: 1});
|
|
uiSourceCodeToAttributionMap.set(uiSourceCode, attribution);
|
|
}
|
|
|
|
static cloneInitialFrameAttribution(
|
|
fromUISourceCode: Workspace.UISourceCode.UISourceCode,
|
|
toUISourceCode: Workspace.UISourceCode.UISourceCode): void {
|
|
const fromAttribution = uiSourceCodeToAttributionMap.get(fromUISourceCode);
|
|
if (!fromAttribution) {
|
|
return;
|
|
}
|
|
const toAttribution = new Map<string, {
|
|
frame: SDK.ResourceTreeModel.ResourceTreeFrame,
|
|
count: number,
|
|
}>();
|
|
for (const frameId of fromAttribution.keys()) {
|
|
const value = fromAttribution.get(frameId);
|
|
if (typeof value !== 'undefined') {
|
|
toAttribution.set(frameId, {frame: value.frame, count: value.count});
|
|
}
|
|
}
|
|
uiSourceCodeToAttributionMap.set(toUISourceCode, toAttribution);
|
|
}
|
|
|
|
static addFrameAttribution(uiSourceCode: Workspace.UISourceCode.UISourceCode, frameId: string): void {
|
|
const frame = NetworkProject._resolveFrame(uiSourceCode, frameId);
|
|
if (!frame) {
|
|
return;
|
|
}
|
|
const frameAttribution = uiSourceCodeToAttributionMap.get(uiSourceCode);
|
|
if (!frameAttribution) {
|
|
return;
|
|
}
|
|
const attributionInfo = frameAttribution.get(frameId) || {frame: frame, count: 0};
|
|
attributionInfo.count += 1;
|
|
frameAttribution.set(frameId, attributionInfo);
|
|
if (attributionInfo.count !== 1) {
|
|
return;
|
|
}
|
|
|
|
const data = {uiSourceCode: uiSourceCode, frame: frame};
|
|
NetworkProjectManager.instance().dispatchEventToListeners(Events.FrameAttributionAdded, data);
|
|
}
|
|
|
|
static removeFrameAttribution(uiSourceCode: Workspace.UISourceCode.UISourceCode, frameId: string): void {
|
|
const frameAttribution = uiSourceCodeToAttributionMap.get(uiSourceCode);
|
|
if (!frameAttribution) {
|
|
return;
|
|
}
|
|
const attributionInfo = frameAttribution.get(frameId);
|
|
console.assert(Boolean(attributionInfo), 'Failed to remove frame attribution for url: ' + uiSourceCode.url());
|
|
if (!attributionInfo) {
|
|
return;
|
|
}
|
|
attributionInfo.count -= 1;
|
|
if (attributionInfo.count > 0) {
|
|
return;
|
|
}
|
|
frameAttribution.delete(frameId);
|
|
const data = {uiSourceCode: uiSourceCode, frame: attributionInfo.frame};
|
|
NetworkProjectManager.instance().dispatchEventToListeners(Events.FrameAttributionRemoved, data);
|
|
}
|
|
|
|
static targetForUISourceCode(uiSourceCode: Workspace.UISourceCode.UISourceCode): SDK.Target.Target|null {
|
|
return projectToTargetMap.get(uiSourceCode.project()) || null;
|
|
}
|
|
|
|
static setTargetForProject(project: Workspace.Workspace.Project, target: SDK.Target.Target): void {
|
|
projectToTargetMap.set(project, target);
|
|
}
|
|
|
|
static getTargetForProject(project: Workspace.Workspace.Project): SDK.Target.Target|null {
|
|
return projectToTargetMap.get(project) || null;
|
|
}
|
|
|
|
static framesForUISourceCode(uiSourceCode: Workspace.UISourceCode.UISourceCode):
|
|
SDK.ResourceTreeModel.ResourceTreeFrame[] {
|
|
const target = NetworkProject.targetForUISourceCode(uiSourceCode);
|
|
const resourceTreeModel = target && target.model(SDK.ResourceTreeModel.ResourceTreeModel);
|
|
const attribution = uiSourceCodeToAttributionMap.get(uiSourceCode);
|
|
if (!resourceTreeModel || !attribution) {
|
|
return [];
|
|
}
|
|
const frames = Array.from(attribution.keys()).map(frameId => resourceTreeModel.frameForId(frameId));
|
|
return /** @type {!Array<!SDK.ResourceTreeModel.ResourceTreeFrame>} */ frames.filter(frame => Boolean(frame)) as
|
|
SDK.ResourceTreeModel.ResourceTreeFrame[];
|
|
}
|
|
}
|