File: keyboard_input_log.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (88 lines) | stat: -rw-r--r-- 3,194 bytes parent folder | download | duplicates (7)
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
// 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.

#ifndef ASH_SYSTEM_DIAGNOSTICS_KEYBOARD_INPUT_LOG_H_
#define ASH_SYSTEM_DIAGNOSTICS_KEYBOARD_INPUT_LOG_H_

#include <cstdint>
#include <string>

#include "ash/ash_export.h"
#include "ash/system/diagnostics/async_log.h"
#include "ash/system/diagnostics/mojom/input.mojom.h"
#include "ash/webui/diagnostics_ui/mojom/input_data_provider.mojom.h"
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"

namespace ash::diagnostics {
// Contains information related to a key press.
struct ASH_EXPORT KeyPressData {
  KeyPressData(uint32_t key_code, uint32_t scan_code);

  uint32_t key_code;
  uint32_t scan_code;

  // We're overriding this operator so that KeyPressData can be used within a
  // set.
  bool operator<(const KeyPressData& rhs) const {
    if (key_code < rhs.key_code) {
      return true;
    };

    return scan_code < rhs.scan_code;
  }
};

// For a given keyboard, KeyboardLogData stores the keyboard name along with a
// list of each key that was pressed while testing the keyboard.
struct KeyboardLogData {
  explicit KeyboardLogData(const std::string& keyboard_name);
  KeyboardLogData();
  KeyboardLogData(KeyboardLogData&&);
  KeyboardLogData& operator=(KeyboardLogData&&);
  ~KeyboardLogData();

  std::string name;
  // Contains the key code and scan code for a pressed key.
  base::flat_set<KeyPressData> key_press_data;
};

// KeyboardInputLog is used to record information about each key that is
// pressed while a keyboard is being tested in the Diagnostics App.
class ASH_EXPORT KeyboardInputLog {
 public:
  explicit KeyboardInputLog(const base::FilePath& log_base_path);
  ~KeyboardInputLog();
  KeyboardInputLog(const KeyboardInputLog&) = delete;
  KeyboardInputLog& operator=(const KeyboardInputLog&) = delete;

  // Creates an entry in |keyboard_log_data_map_| for |id|.
  void AddKeyboard(uint32_t id, const std::string& name);

  // Stores the key code and scan code of the pressed key in
  // |key_press_data_map|. Note: we're only interested in recording the first
  // time a key is pressed. Subsequent presses for a key are ignored.
  void RecordKeyPressForKeyboard(uint32_t id, mojom::KeyEventPtr key_event);
  // Formats the pressed keys for the matching keyboard |id| and appends the
  // formatted content to |log_|. Additionally, we remove |id| from
  // |keyboard_log_data_map_| as this method will be called when we're no
  // longer observing input events for the associated keyboard.
  void CreateLogAndRemoveKeyboard(uint32_t id);
  // Checks if |id| is present in |keyboard_log_data_map|.
  bool KeyboardHasBeenAdded(uint32_t id) const;
  // Returns stored log contents as a string.
  std::string GetLogContents() const;

 private:
  void Append(const std::string& text);

  AsyncLog log_;
  // Maps a keyboard id to a struct containing the keyboard name and info
  // about the keys that were pressed during testing.
  base::flat_map<uint32_t, KeyboardLogData> keyboard_log_data_map_;
};

}  // namespace ash::diagnostics

#endif  // ASH_SYSTEM_DIAGNOSTICS_KEYBOARD_INPUT_LOG_H_