UNPKG

4.42 kBPlain TextView Raw
1// Copyright 2021 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/*
6 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
7 * Copyright (C) 2007 Matt Lilek (pewtermoose@gmail.com).
8 * Copyright (C) 2009 Joseph Pecoraro
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
12 * met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
20 * its contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35import * as Common from '../../core/common/common.js';
36import * as Platform from '../../core/platform/platform.js';
37import * as SDK from '../../core/sdk/sdk.js';
38import type * as Protocol from '../../generated/protocol.js';
39import * as Workspace from '../workspace/workspace.js';
40
41export function resourceForURL(url: string): SDK.Resource.Resource|null {
42 for (const resourceTreeModel of SDK.TargetManager.TargetManager.instance().models(
43 SDK.ResourceTreeModel.ResourceTreeModel)) {
44 const resource = resourceTreeModel.resourceForURL(url);
45 if (resource) {
46 return resource;
47 }
48 }
49 return null;
50}
51
52export function displayNameForURL(url: string): string {
53 if (!url) {
54 return '';
55 }
56
57 const resource = resourceForURL(url);
58 if (resource) {
59 return resource.displayName;
60 }
61
62 const uiSourceCode = Workspace.Workspace.WorkspaceImpl.instance().uiSourceCodeForURL(url);
63 if (uiSourceCode) {
64 return uiSourceCode.displayName();
65 }
66
67 const mainTarget = SDK.TargetManager.TargetManager.instance().mainTarget();
68 const inspectedURL = mainTarget && mainTarget.inspectedURL();
69 if (!inspectedURL) {
70 return Platform.StringUtilities.trimURL(url, '');
71 }
72
73 const parsedURL = Common.ParsedURL.ParsedURL.fromString(inspectedURL);
74 if (!parsedURL) {
75 return url;
76 }
77
78 const lastPathComponent = parsedURL.lastPathComponent;
79 const index = inspectedURL.indexOf(lastPathComponent);
80 if (index !== -1 && index + lastPathComponent.length === inspectedURL.length) {
81 const baseURL = inspectedURL.substring(0, index);
82 if (url.startsWith(baseURL)) {
83 return url.substring(index);
84 }
85 }
86
87 const displayName = Platform.StringUtilities.trimURL(url, parsedURL.host);
88 return displayName === '/' ? parsedURL.host + '/' : displayName;
89}
90
91export function metadataForURL(target: SDK.Target.Target, frameId: Protocol.Page.FrameId, url: string):
92 Workspace.UISourceCode.UISourceCodeMetadata|null {
93 const resourceTreeModel = target.model(SDK.ResourceTreeModel.ResourceTreeModel);
94 if (!resourceTreeModel) {
95 return null;
96 }
97 const frame = resourceTreeModel.frameForId(frameId);
98 if (!frame) {
99 return null;
100 }
101 return resourceMetadata(frame.resourceForURL(url));
102}
103
104export function resourceMetadata(resource: SDK.Resource.Resource|null): Workspace.UISourceCode.UISourceCodeMetadata|
105 null {
106 if (!resource || (typeof resource.contentSize() !== 'number' && !resource.lastModified())) {
107 return null;
108 }
109 return new Workspace.UISourceCode.UISourceCodeMetadata(resource.lastModified(), resource.contentSize());
110}