File: access_code_cast.ts

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (337 lines) | stat: -rw-r--r-- 10,727 bytes parent folder | download | duplicates (3)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import './passcode_input/passcode_input.js';
import './error_message/error_message.js';
import 'chrome://resources/cr_elements/cr_button/cr_button.js';
import 'chrome://resources/cr_elements/cr_dialog/cr_dialog.js';
import 'chrome://resources/cr_elements/cr_shared_style.css.js';
import 'chrome://resources/cr_elements/cr_shared_vars.css.js';
import 'chrome://resources/cr_elements/icons.html.js';
import 'chrome://resources/cr_elements/cr_icon/cr_icon.js';

import {ColorChangeUpdater} from 'chrome://resources/cr_components/color_change_listener/colors_css_updater.js';
import type {CrButtonElement} from 'chrome://resources/cr_elements/cr_button/cr_button.js';
import type {CrDialogElement} from 'chrome://resources/cr_elements/cr_dialog/cr_dialog.js';
import {I18nMixin} from 'chrome://resources/cr_elements/i18n_mixin.js';
import {WebUiListenerMixin} from 'chrome://resources/cr_elements/web_ui_listener_mixin.js';
import {loadTimeData} from 'chrome://resources/js/load_time_data.js';
import {isWindows} from 'chrome://resources/js/platform.js';
import {PluralStringProxyImpl} from 'chrome://resources/js/plural_string_proxy.js';
import {PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';

import {getTemplate} from './access_code_cast.html.js';
import type {PageCallbackRouter} from './access_code_cast.mojom-webui.js';
import {AddSinkResultCode, CastDiscoveryMethod} from './access_code_cast.mojom-webui.js';
import {BrowserProxy, DialogCloseReason} from './browser_proxy.js';
import type {ErrorMessageElement} from './error_message/error_message.js';
import type {PasscodeInputElement} from './passcode_input/passcode_input.js';
import {RouteRequestResultCode} from './route_request_result_code.mojom-webui.js';

enum PageState {
  CODE_INPUT,
  QR_INPUT,
}

export interface AccessCodeCastElement {
  $: {
    backButton: CrButtonElement,
    castButton: CrButtonElement,
    codeInputView: HTMLElement,
    codeInput: PasscodeInputElement,
    dialog: CrDialogElement,
    errorMessage: ErrorMessageElement,
    qrInputView: HTMLElement,
  };
}

const AccessCodeCastElementBase =
    WebUiListenerMixin(I18nMixin(PolymerElement));

const ECMASCRIPT_EPOCH_START_YEAR = 1970;
const SECONDS_PER_DAY = 86400;
const SECONDS_PER_HOUR = 3600;
const SECONDS_PER_MONTH = 2592000;
const SECONDS_PER_YEAR = 31536000;

export class AccessCodeCastElement extends AccessCodeCastElementBase {
  static get is() {
    return 'access-code-cast-app';
  }

  static get template() {
    return getTemplate();
  }

  static get properties() {
    return {
      accessCode: {
        type: String,
        value: '',
        observer: 'castStateChange',
      },
      canCast: {
        type: Boolean,
        value: true,
        observer: 'castStateChange',
      },
      inputLabel: String,
      isWin: Boolean,
      managedFootnote: String,
      qrScannerEnabled: Boolean,
      rememberDevices: Boolean,
      submitDisabled: Boolean,
    };
  }

  private listenerIds: number[];
  private router: PageCallbackRouter;

  private static readonly ACCESS_CODE_LENGTH = 6;

  declare private accessCode: string;
  declare private canCast: boolean;
  private inputEnabledStartTime: number;
  declare private inputLabel: string;
  declare private isWin: boolean;
  declare private managedFootnote: string;
  declare private qrScannerEnabled: boolean;
  declare private rememberDevices: boolean;
  private state: PageState;
  declare private submitDisabled: boolean;

  constructor() {
    super();
    this.listenerIds = [];
    this.router = BrowserProxy.getInstance().callbackRouter;
    this.inputLabel = this.i18n('inputLabel');
    this.isWin = isWindows;

    // Enable dynamic colors for the dialog.
    ColorChangeUpdater.forDocument().start();

    this.createManagedFootnote(
        loadTimeData.getInteger('rememberedDeviceDuration'));

    this.accessCode = '';
    this.inputEnabledStartTime = Date.now();
    BrowserProxy.getInstance().isQrScanningAvailable().then((available) => {
      this.qrScannerEnabled = available;
    });

    document.addEventListener('keydown', (e: KeyboardEvent) => {
      if (e.key === 'Enter') {
        this.handleEnterPressed();
      }
    });
  }

  override ready() {
    super.ready();
    this.setState(PageState.CODE_INPUT);
    this.$.errorMessage.setNoError();
    this.$.dialog.showModal();
  }

  override connectedCallback() {
    super.connectedCallback();
  }

  override disconnectedCallback() {
    super.disconnectedCallback();
    this.listenerIds.forEach(id => this.router.removeListener(id));
  }

  cancelButtonPressed() {
    BrowserProxy.recordDialogCloseReason(DialogCloseReason.CANCEL_BUTTON);
    BrowserProxy.getInstance().closeDialog();
  }

  switchToCodeInput() {
    this.setState(PageState.CODE_INPUT);
  }

  switchToQrInput() {
    this.setState(PageState.QR_INPUT);
  }

  async addSinkAndCast() {
    BrowserProxy.recordAccessCodeEntryTime(
        Date.now() - this.inputEnabledStartTime);

    if (!BrowserProxy.getInstance().isDialog()) {
      return;
    }
    if (this.accessCode.length !== AccessCodeCastElement.ACCESS_CODE_LENGTH) {
      return;
    }
    if (!this.canCast) {
      return;
    }

    this.set('canCast', false);
    this.$.errorMessage.setNoError();
    const castAttemptStartTime = Date.now();

    const method = this.state === PageState.CODE_INPUT ?
        CastDiscoveryMethod.INPUT_ACCESS_CODE :
        CastDiscoveryMethod.QR_CODE;

    const addResult = await this.addSink(method).catch(() => {
      return AddSinkResultCode.UNKNOWN_ERROR;
    });

    if (addResult !== AddSinkResultCode.OK) {
      this.$.errorMessage.setAddSinkError(addResult);
      this.afterFailedAddAndCast(castAttemptStartTime);
      return;
    }

    const castResult = await this.cast().catch(() => {
      return RouteRequestResultCode.UNKNOWN_ERROR;
    });

    if (castResult !== RouteRequestResultCode.OK) {
      this.$.errorMessage.setCastError(castResult);
      this.afterFailedAddAndCast(castAttemptStartTime);
      return;
    }

    BrowserProxy.recordDialogCloseReason(DialogCloseReason.CAST_SUCCESS);
    BrowserProxy.recordCastAttemptLength(Date.now() - castAttemptStartTime);
    BrowserProxy.getInstance().closeDialog();
  }

  async createManagedFootnote(duration: number) {
    if (duration === 0) {
      return;
    }


    // Handle the cases from the policy enum.
    if (duration === SECONDS_PER_HOUR) {
      return this.makeFootnote('managedFootnoteHours', 1);
    } else if (duration === SECONDS_PER_DAY) {
      return this.makeFootnote('managedFootnoteDays', 1);
    } else if (duration === SECONDS_PER_MONTH) {
      return this.makeFootnote('managedFootnoteMonths', 1);
    } else if (duration === SECONDS_PER_YEAR) {
      return this.makeFootnote('managedFootnoteYears', 1);
    }

    // Handle the general case.
    const durationAsDate = new Date(duration * 1000);
    // ECMAscript epoch starts at 1970.
    if (durationAsDate.getUTCFullYear() - ECMASCRIPT_EPOCH_START_YEAR > 0) {
      return this.makeFootnote('managedFootnoteYears',
          durationAsDate.getUTCFullYear() - ECMASCRIPT_EPOCH_START_YEAR);
    // Months are zero indexed.
    } else if (durationAsDate.getUTCMonth() > 0) {
      return this.makeFootnote('managedFootnoteMonths',
          durationAsDate.getUTCMonth());
    // Dates start at 1.
    } else if (durationAsDate.getUTCDate() - 1 > 0) {
      return this.makeFootnote('managedFootnoteDays',
          durationAsDate.getUTCDate() - 1);
    // Hours start at 0.
    } else if (durationAsDate.getUTCHours() > 0) {
      return this.makeFootnote('managedFootnoteHours',
          durationAsDate.getUTCHours());
    // The given duration is either minutes, seconds, or a negative time. These
    // are not valid so we should not show the managed footnote.
    }

    this.rememberDevices = false;
    return;
  }

  setAccessCodeForTest(value: string) {
    this.accessCode = value;
  }

  getManagedFootnoteForTest() {
    return this.managedFootnote;
  }

  private afterFailedAddAndCast(attemptStartDate: number) {
    this.set('canCast', true);
    this.$.codeInput.focusInput();
    this.inputEnabledStartTime = Date.now();
    BrowserProxy.recordCastAttemptLength(Date.now() - attemptStartDate);
  }

  private castStateChange() {
    this.submitDisabled = !this.canCast ||
        this.accessCode.length !== AccessCodeCastElement.ACCESS_CODE_LENGTH;
    if (this.$.errorMessage.getMessageCode() !== 0 &&
        this.accessCode.length <=
            AccessCodeCastElement.ACCESS_CODE_LENGTH - 1) {
      // Hide error message once user starts editing the access code entered
      // previously. Checking for access code's length
      // <= (AccessCodeCastElement.ACCESS_CODE_LENGTH - 1 ) because it's
      // possible to for the user to deletes more than one characters at a
      // time.
      this.$.errorMessage.setNoError();
    }
  }

  private setState(state: PageState) {
    this.state = state;
    this.$.errorMessage.setNoError();

    this.$.codeInputView.hidden = state !== PageState.CODE_INPUT;
    this.$.castButton.hidden = state !== PageState.CODE_INPUT;
    this.$.qrInputView.hidden = state !== PageState.QR_INPUT;
    this.$.backButton.hidden = state !== PageState.QR_INPUT;

    if (state === PageState.CODE_INPUT) {
      this.$.codeInput.value = '';
      this.$.codeInput.focusInput();
    }
  }

  private handleCodeInput(e: any) {
    this.accessCode = e.detail.value;
  }

  private handleEnterPressed() {
    if (this.submitDisabled) {
      return;
    }
    if (!this.$.codeInput.focused) {
      return;
    }
    if (this.state !== PageState.CODE_INPUT) {
      return;
    }

    this.addSinkAndCast();
  }

  private async addSink(method: CastDiscoveryMethod):
      Promise<AddSinkResultCode> {
    const addSinkResult = await BrowserProxy.getInstance().handler
        .addSink(this.accessCode, method);
    return addSinkResult.resultCode;
  }

  private async cast(): Promise<RouteRequestResultCode> {
    const castResult = await BrowserProxy.getInstance().handler.castToSink();
    return castResult.resultCode;
  }

  private async makeFootnote(messageName: string, value: number) {
    const proxy = PluralStringProxyImpl.getInstance();
    this.managedFootnote = await proxy.getPluralString(messageName, value);
    this.rememberDevices = true;
  }
}

declare global {
  interface HTMLElementTagNameMap {
    'access-code-cast-app': AccessCodeCastElement;
  }
}

customElements.define(AccessCodeCastElement.is, AccessCodeCastElement);