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
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375

content / browser / resources / private_aggregation / private_aggregation_internals.ts [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import './private_aggregation_internals_table.js';

import {assert} from 'chrome://resources/js/assert.js';

import type {AggregatableReportRequestID, ObserverInterface, WebUIAggregatableReport} from './private_aggregation_internals.mojom-webui.js';
import {Factory as PrivateAggregationInternalsFactory, HandlerRemote as PrivateAggregationInternalsHandlerRemote, ObserverReceiver, ReportStatus} from './private_aggregation_internals.mojom-webui.js';
import type {PrivateAggregationInternalsTableElement} from './private_aggregation_internals_table.js';
import type {Column} from './table_model.js';
import {TableModel} from './table_model.js';

function compareDefault<T>(a: T, b: T): number {
  return (a < b) ? -1 : ((a > b) ? 1 : 0);
}

// Converts the mojo_base.mojom.Uint128 to a string
function bucketReplacer(_key: string, value: any): any {
  if (_key === 'bucket') {
    return (value['high'] * 2n ** 64n + value['low']).toString();
  } else {
    return value;
  }
}

class ValueColumn<T, V> implements Column<T> {
  compare: (a: T, b: T) => number;
  header: string;
  private minWidth?: string;
  protected getValue: (param: T) => V;

  constructor(
      header: string, getValue: (param: T) => V, minWidth?: string,
      compare?: ((a: T, b: T) => number)) {
    this.header = header;
    this.getValue = getValue;
    this.minWidth = minWidth;
    this.compare =
        compare ?? ((a: T, b: T) => compareDefault(getValue(a), getValue(b)));
  }

  render(td: HTMLElement, row: T) {
    if (this.minWidth) {
      td.style.minWidth = this.minWidth;
    }
    td.textContent = `${this.getValue(row)}`;
  }

  renderHeader(th: HTMLElement) {
    th.textContent = `${this.header}`;
  }
}

/**
 * Column that holds date.
 */
class DateColumn<T> extends ValueColumn<T, Date> {
  constructor(header: string, getValue: (p: T) => Date) {
    super(header, getValue);
  }

  override render(td: HTMLElement, row: T) {
    td.innerText = this.getValue(row).toLocaleString();
  }
}

class CodeColumn<T> extends ValueColumn<T, string> {
  constructor(header: string, getValue: (p: T) => string) {
    super(header, getValue);
  }

  override render(td: HTMLElement, row: T) {
    const code = td.ownerDocument.createElement('code');
    code.innerText = this.getValue(row);

    const pre = td.ownerDocument.createElement('pre');
    pre.appendChild(code);

    td.appendChild(pre);
  }
}

/**
 * Wraps a checkbox.
 */
class Selectable {
  selectCheckbox: HTMLInputElement;

  constructor() {
    this.selectCheckbox = document.createElement('input');
    this.selectCheckbox.type = 'checkbox';
  }
}

/**
 * Checkbox column for selection.
 */
class SelectionColumn<T extends Selectable> implements Column<T> {
  compare: ((a: T, b: T) => number)|null;
  private model: TableModel<T>;
  private selectAllCheckbox: HTMLInputElement;
  selectionChangedListeners: Set<(param: boolean) => void>;
  header: string|null;
  private rowChangedListener: () => void;

  constructor(model: TableModel<T>) {
    // Selection column is not sortable.
    this.compare = null;
    this.model = model;
    this.header = null;

    this.selectAllCheckbox = document.createElement('input');
    this.selectAllCheckbox.type = 'checkbox';
    this.selectAllCheckbox.addEventListener('input', () => {
      const checked = this.selectAllCheckbox.checked;
      this.model.getRows().forEach((row) => {
        if (!row.selectCheckbox.disabled) {
          row.selectCheckbox.checked = checked;
        }
      });
      this.notifySelectionChanged(checked);
    });

    this.rowChangedListener = () => this.onChange();
    this.model.rowsChangedListeners.add(this.rowChangedListener);
    this.selectionChangedListeners = new Set();
  }

  render(td: HTMLElement, row: T) {
    td.appendChild(row.selectCheckbox);
  }

  renderHeader(th: HTMLElement) {
    th.appendChild(this.selectAllCheckbox);
  }

  onChange() {
    let anySelectable = false;
    let anySelected = false;
    let anyUnselected = false;

    this.model.getRows().forEach((row) => {
      // addEventListener deduplicates, so only one event will be fired per
      // input.
      row.selectCheckbox.addEventListener('input', this.rowChangedListener);

      if (row.selectCheckbox.disabled) {
        return;
      }

      anySelectable = true;
      if (row.selectCheckbox.checked) {
        anySelected = true;
      } else {
        anyUnselected = true;
      }
    });

    this.selectAllCheckbox.disabled = !anySelectable;
    this.selectAllCheckbox.checked = anySelected && !anyUnselected;
    this.selectAllCheckbox.indeterminate = anySelected && anyUnselected;

    this.notifySelectionChanged(anySelected);
  }

  notifySelectionChanged(anySelected: boolean) {
    this.selectionChangedListeners.forEach((f) => f(anySelected));
  }
}

function reportStatusToText(status: ReportStatus) {
  switch (status) {
    case ReportStatus.kPending:
      return 'Pending';
    case ReportStatus.kSent:
      return 'Sent';
    case ReportStatus.kFailedToAssemble:
      return 'Failed to assemble';
    case ReportStatus.kFailedToSend:
      return 'Failed to send';
  }
}

class Report extends Selectable {
  // `null` indicates a report that wasn't stored/scheduled.
  id: AggregatableReportRequestID|null;
  reportBody: string;
  reportUrl: string;
  reportTime: Date;
  status: string;
  apiIdentifier: string;
  apiVersion: string;
  contributions: string;

  constructor(mojo: WebUIAggregatableReport) {
    super();

    this.id = mojo.id;
    this.reportBody = mojo.reportBody;
    this.reportUrl = mojo.reportUrl.url;
    this.reportTime = new Date(mojo.reportTime);
    this.apiIdentifier = mojo.apiIdentifier;
    this.apiVersion = mojo.apiVersion;

    // Only pending stored/scheduled reports are selectable.
    if (mojo.status !== ReportStatus.kPending || mojo.id === undefined) {
      this.selectCheckbox.disabled = true;
    }

    this.status = reportStatusToText(mojo.status);

    this.contributions =
        JSON.stringify(mojo.contributions, bucketReplacer, ' ');
  }
}

class ReportTableModel extends TableModel<Report> {
  private sendReportsButton: HTMLButtonElement;
  private selectionColumn: SelectionColumn<Report>;
  private handledReports: Report[] = [];
  private storedReports: Report[] = [];

  constructor(sendReportsButton: HTMLButtonElement) {
    super();

    this.sendReportsButton = sendReportsButton;

    this.selectionColumn = new SelectionColumn(this);

    this.cols = [
      this.selectionColumn,
      new ValueColumn<Report, string>('Status', (e) => e.status),
      new ValueColumn<Report, string>(
          'Report URL', (e) => e.reportUrl, '250px'),
      new DateColumn<Report>('Report Time', (e) => e.reportTime),
      new ValueColumn<Report, string>(
          'API identifier', (e) => e.apiIdentifier, '90px'),
      new ValueColumn<Report, string>('API version', (e) => e.apiVersion),
      new CodeColumn<Report>(
          'Contributions', (e) => (e as Report).contributions),
      new CodeColumn<Report>('Report Body', (e) => e.reportBody),
    ];

    // Sort by report time by default.
    this.sortIdx = 3;
    assert(this.cols[this.sortIdx]!.header === 'Report Time');

    this.emptyRowText = 'No sent or pending reports.';

    this.sendReportsButton.addEventListener('click', () => this.sendReports_());
    this.selectionColumn.selectionChangedListeners.add(
        (anySelected: boolean) => {
          this.sendReportsButton.disabled = !anySelected;
        });
  }

  override getRows() {
    return this.handledReports.concat(this.storedReports);
  }

  setStoredReports(storedReports: Report[]) {
    this.storedReports = storedReports;
    this.notifyRowsChanged();
  }

  addHandledReport(report: Report) {
    // Prevent the page from consuming ever more memory if the user leaves the
    // page open for a long time.
    if (this.handledReports.length >= 1000) {
      this.handledReports = [];
    }

    this.handledReports.push(report);

    this.notifyRowsChanged();
  }

  clear() {
    this.storedReports = [];
    this.handledReports = [];
    this.notifyRowsChanged();
  }

  /**
   * Sends all selected reports.
   * Disables the button while the reports are still being sent.
   * Observer.onRequestStorageModified will be called automatically as reports
   * are deleted, so there's no need to manually refresh the data on completion.
   */
  private sendReports_() {
    const ids: AggregatableReportRequestID[] = [];
    this.storedReports.forEach((report) => {
      if (!report.selectCheckbox.disabled && report.selectCheckbox.checked) {
        ids.push(report.id as AggregatableReportRequestID);
      }
    });

    if (ids.length === 0) {
      return;
    }

    const previousText = this.sendReportsButton.innerText;

    this.sendReportsButton.disabled = true;
    this.sendReportsButton.innerText = 'Sending...';

    pageHandler!.sendReports(ids).then(() => {
      this.sendReportsButton.innerText = previousText;
    });
  }
}

/**
 * Reference to the backend providing all the data.
 */
let pageHandler: PrivateAggregationInternalsHandlerRemote|null = null;

let reportTableModel: ReportTableModel|null = null;

/**
 * Fetches all pending reports from the backend and populate the tables.
 */
function updateReports() {
  pageHandler!.getReports().then((response) => {
    reportTableModel!.setStoredReports(
        response.reports.map((mojo) => new Report(mojo)));
  });
}

/**
 * Deletes all data stored by the aggregation service backend.
 * Observer.onRequestStorageModified will be called automatically as reports are
 * deleted, so there's no need to manually refresh the data on completion.
 */
function clearStorage() {
  reportTableModel!.clear();
  pageHandler!.clearStorage();
}

class Observer implements ObserverInterface {
  onRequestStorageModified() {
    updateReports();
  }

  onReportHandled(mojo: WebUIAggregatableReport) {
    reportTableModel!.addHandledReport(new Report(mojo));
  }
}

document.addEventListener('DOMContentLoaded', () => {
  // Setup the mojo interface.
  pageHandler = new PrivateAggregationInternalsHandlerRemote();

  const sendReports =
      document.querySelector<HTMLButtonElement>('#send-reports');
  reportTableModel = new ReportTableModel(sendReports!);

  const refresh = document.querySelector('#refresh');
  refresh!.addEventListener('click', updateReports);
  const clearData = document.querySelector('#clear-data');
  clearData!.addEventListener('click', clearStorage);

  const reportTable =
      document.querySelector<PrivateAggregationInternalsTableElement<Report>>(
          '#reportTable');
  reportTable!.setModel(reportTableModel!);

  PrivateAggregationInternalsFactory.getRemote().create(
    new ObserverReceiver(new Observer()).$.bindNewPipeAndPassRemote(),
    pageHandler.$.bindNewPipeAndPassReceiver());

  updateReports();
});