xref: /aosp_15_r20/external/perfetto/ui/src/frontend/trace_share_utils.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2020 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import m from 'mithril';
16import {TraceUrlSource} from '../core/trace_source';
17import {createPermalink} from './permalink';
18import {showModal} from '../widgets/modal';
19import {globals} from './globals';
20import {AppImpl} from '../core/app_impl';
21import {Trace} from '../public/trace';
22import {TraceImpl} from '../core/trace_impl';
23import {CopyableLink} from '../widgets/copyable_link';
24
25export function isShareable(trace: Trace) {
26  return globals.isInternalUser && trace.traceInfo.downloadable;
27}
28
29export async function shareTrace(trace: TraceImpl) {
30  const traceSource = trace.traceInfo.source;
31  const traceUrl = (traceSource as TraceUrlSource).url ?? '';
32
33  // If the trace is not shareable (has been pushed via postMessage()) but has
34  // a url, create a pseudo-permalink by echoing back the URL.
35  if (!isShareable(trace)) {
36    const msg = [
37      m(
38        'p',
39        'This trace was opened by an external site and as such cannot ' +
40          'be re-shared preserving the UI state.',
41      ),
42    ];
43    if (traceUrl) {
44      msg.push(m('p', 'By using the URL below you can open this trace again.'));
45      msg.push(m('p', 'Clicking will copy the URL into the clipboard.'));
46      msg.push(m(CopyableLink, {url: traceUrl}));
47    }
48
49    showModal({
50      title: 'Cannot create permalink from external trace',
51      content: m('div', msg),
52    });
53    return;
54  }
55
56  if (!isShareable(trace)) return;
57
58  const result = confirm(
59    `Upload UI state and generate a permalink. ` +
60      `The trace will be accessible by anybody with the permalink.`,
61  );
62  if (result) {
63    AppImpl.instance.analytics.logEvent('Trace Actions', 'Create permalink');
64    return await createPermalink();
65  }
66}
67