• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {assertExists} from '../base/logging';
16import {TraceArrayBufferSource} from '../common/state';
17import {createPermalink} from './permalink';
18import {showModal} from '../widgets/modal';
19
20import {onClickCopy} from './clipboard';
21import {globals} from './globals';
22import {isTraceLoaded} from './sidebar';
23
24export function isShareable() {
25  return globals.isInternalUser && isDownloadable();
26}
27
28export function isDownloadable() {
29  const engine = globals.getCurrentEngine();
30  if (!engine) {
31    return false;
32  }
33  if (engine.source.type === 'ARRAY_BUFFER' && engine.source.localOnly) {
34    return false;
35  }
36  if (engine.source.type === 'HTTP_RPC') {
37    return false;
38  }
39  return true;
40}
41
42export function shareTrace() {
43  const engine = assertExists(globals.getCurrentEngine());
44  const traceUrl = (engine.source as TraceArrayBufferSource).url || '';
45
46  // If the trace is not shareable (has been pushed via postMessage()) but has
47  // a url, create a pseudo-permalink by echoing back the URL.
48  if (!isShareable()) {
49    const msg = [
50      m(
51        'p',
52        'This trace was opened by an external site and as such cannot ' +
53          'be re-shared preserving the UI state.',
54      ),
55    ];
56    if (traceUrl) {
57      msg.push(m('p', 'By using the URL below you can open this trace again.'));
58      msg.push(m('p', 'Clicking will copy the URL into the clipboard.'));
59      msg.push(createTraceLink(traceUrl, traceUrl));
60    }
61
62    showModal({
63      title: 'Cannot create permalink from external trace',
64      content: m('div', msg),
65    });
66    return;
67  }
68
69  if (!isShareable() || !isTraceLoaded()) return;
70
71  const result = confirm(
72    `Upload UI state and generate a permalink. ` +
73      `The trace will be accessible by anybody with the permalink.`,
74  );
75  if (result) {
76    globals.logging.logEvent('Trace Actions', 'Create permalink');
77    createPermalink();
78  }
79}
80
81export function createTraceLink(title: string, url: string) {
82  if (url === '') {
83    return m('a.trace-file-name', title);
84  }
85  const linkProps = {
86    href: url,
87    title: 'Click to copy the URL',
88    target: '_blank',
89    onclick: onClickCopy(url),
90  };
91  return m('a.trace-file-name', linkProps, title);
92}
93