File: debug_log_page.js

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 (133 lines) | stat: -rw-r--r-- 3,968 bytes parent folder | download | duplicates (4)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

import {BluetoothInternalsHandlerRemote, DebugLogsChangeHandlerRemote} from './bluetooth_internals.mojom-webui.js';
import {Page} from './page.js';

/** @const {string} */
const LOGS_NOT_SUPPORTED_STRING = 'Debug logs not supported';

/**
 * Page that allows user to enable/disable debug logs.
 */
export class DebugLogPage extends Page {
  /**
   * @param {!BluetoothInternalsHandlerRemote} bluetoothInternalsHandler
   */
  constructor(bluetoothInternalsHandler) {
    super('debug', 'Debug Logs', 'debug');

    /**
     * @private {?DebugLogsChangeHandlerRemote}
     */
    this.debugLogsChangeHandler_ = null;

    /** @private {?HTMLInputElement} */
    this.inputElement_ = null;

    /** @private {!HTMLDivElement} */
    this.debugContainer_ =
        /** @type {!HTMLDivElement} */ ($('debug-container'));

    this.bluetoothInternalsHandler_ = bluetoothInternalsHandler;
    this.btsnoopInterface_ = null;

    // <if expr="chromeos_ash">
    this.prepareBtsnoopTemplate();
    // </if>

    this.bluetoothInternalsHandler_.getDebugLogsChangeHandler().then(
        (params) => {
          if (params.handler) {
            this.setUpInput(params.handler, params.initialToggleValue);
          } else {
            this.debugContainer_.textContent = LOGS_NOT_SUPPORTED_STRING;
          }
        });
  }

  /**
   * @param {!DebugLogsChangeHandlerRemote} handler
   * @param {boolean} initialInputValue
   */
  setUpInput(handler, initialInputValue) {
    this.debugLogsChangeHandler_ = handler;

    this.inputElement_ =
        /** @type {!HTMLInputElement} */ (document.createElement('input'));
    this.inputElement_.setAttribute('type', 'checkbox');
    this.inputElement_.checked = initialInputValue;
    this.inputElement_.addEventListener(
        'change', this.onToggleChange.bind(this));
    this.debugContainer_.appendChild(this.inputElement_);
  }

  setUpBtmonButton() {
    const elem = /** @type {!HTMLInputElement} */ ($('btmon-start-btn'));
    elem.addEventListener('click', this.onStartBtsnoopClick.bind(this));
    this.setBtmonButtonText('Start logging');
  }

  setBtmonButtonText(text) {
    const elem = /** @type {!HTMLInputElement} */ ($('btmon-start-btn'));
    elem.textContent = text;
  }

  setBtmonStatusText(text) {
    const elem = /** @type {!HTMLDivElement} */ ($('btmon-status-bar'));
    elem.textContent = text;
  }

  onToggleChange() {
    this.debugLogsChangeHandler_.changeDebugLogsState(
        this.inputElement_.checked);
  }

  onStartBtsnoopClick() {
    this.btsnoopInterface_ ? this.onStopBtsnoop() : this.onStartBtsnoop();
  }

  async onStartBtsnoop() {
    const {btsnoop: btsnoopInterface} =
        await this.bluetoothInternalsHandler_.startBtsnoop();
    if (btsnoopInterface != null) {
      this.setBtmonStatusText('Logging is ongoing.');
      this.setBtmonButtonText('Stop logging');
      this.btsnoopInterface_ = btsnoopInterface;
    } else {
      this.setBtmonStatusText('Fail to start logging.');
      this.btsnoopInterface_ = null;
    }
  }

  async onStopBtsnoop() {
    if (!this.btsnoopInterface_) {
      return;
    }

    const {success} = await this.btsnoopInterface_.stop();
    if (success) {
      this.setBtmonStatusText(
          'Logging is stopped. Log is saved to Downloads as capture.btsnoop.');
    } else {
      this.setBtmonStatusText('Fail to save debug log.');
    }
    this.setBtmonButtonText('Start logging');
    this.btsnoopInterface_ = null;
  }

  async prepareBtsnoopTemplate() {
    const {enabled} =
        await this.bluetoothInternalsHandler_.isBtsnoopFeatureEnabled();
    if (!enabled) {
      return;
    }

    this.pageDiv.appendChild(
        document.importNode($('btsnoop-template').content, true /* deep */));
    this.setUpBtmonButton();
  }
}