File: diagnostics_log_controller.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 (103 lines) | stat: -rw-r--r-- 3,949 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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_DIAGNOSTICS_LOG_CONTROLLER_H_
#define ASH_SYSTEM_DIAGNOSTICS_DIAGNOSTICS_LOG_CONTROLLER_H_

#include <memory>
#include <string>

#include "ash/ash_export.h"
#include "ash/login_status.h"
#include "ash/public/cpp/session/session_observer.h"
#include "ash/system/diagnostics/diagnostics_browser_delegate.h"
#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"

namespace ash {
namespace diagnostics {

class KeyboardInputLog;
class NetworkingLog;
class RoutineLog;
class TelemetryLog;

// DiagnosticsLogController manages the lifetime of Diagnostics log writers such
// as the RoutineLog and ensures logs are written to the correct directory path
// for the current user. See go/cros-shared-diagnostics-session-log-dd.
class ASH_EXPORT DiagnosticsLogController : SessionObserver {
 public:
  DiagnosticsLogController();
  DiagnosticsLogController(const DiagnosticsLogController&) = delete;
  DiagnosticsLogController& operator=(const DiagnosticsLogController&) = delete;
  ~DiagnosticsLogController() override;

  // DiagnosticsLogController is created and destroyed with
  // the ash::Shell. DiagnosticsLogController::Get may be nullptr if accessed
  // outside the expected lifetime or when the
  // `ash::features::kEnableLogControllerForDiagnosticsApp` is false.
  static DiagnosticsLogController* Get();

  // Check if DiagnosticsLogController is ready for use.
  static bool IsInitialized();
  static void Initialize(std::unique_ptr<DiagnosticsBrowserDelegate> delegate);

  // GenerateSessionStringOnBlockingPool needs to be run on blocking thread.
  // Generates a string of the combined Diagnostics logs.
  std::string GenerateSessionStringOnBlockingPool() const;

  // GenerateSessionLogOnBlockingPool needs to be run on blocking
  // thread. Stores combined log at |save_file_path| and returns
  // whether file creation is successful.
  bool GenerateSessionLogOnBlockingPool(const base::FilePath& save_file_path);

  // Ensures DiagnosticsLogController is configured to match the current
  // environment. To be called from DiagnosticsDialog::ShowDialog prior to the
  // Diagnostics app being shown.
  void ResetAndInitializeLogWriters();

  // SessionObserver:
  // Ensure DiagnosticsLogController is re-configured to match the current
  // environment when LoginStatus changes. See ash/ash_login_status.h for
  // description of LoginStatus types.
  void OnLoginStatusChanged(LoginStatus login_status) override;

  KeyboardInputLog& GetKeyboardInputLog();
  NetworkingLog& GetNetworkingLog();
  RoutineLog& GetRoutineLog();
  TelemetryLog& GetTelemetryLog();

  void SetKeyboardInputLogForTesting(
      std::unique_ptr<KeyboardInputLog> keyboard_input_log);
  void SetNetworkingLogForTesting(std::unique_ptr<NetworkingLog> network_log);
  void SetRoutineLogForTesting(std::unique_ptr<RoutineLog> routine_log);
  void SetTelemetryLogForTesting(std::unique_ptr<TelemetryLog> telemetry_log);

 private:
  friend class DiagnosticsLogControllerTest;

  // Ensure log_base_path_ set based on current session and ash::LoginStatus.
  void ResetLogBasePath();

  // Removes directory at |path|.
  void RemoveDirectory(const base::FilePath& path);

  LoginStatus previous_status_;
  std::unique_ptr<DiagnosticsBrowserDelegate> delegate_;
  base::FilePath log_base_path_;
  std::unique_ptr<KeyboardInputLog> keyboard_input_log_;
  std::unique_ptr<NetworkingLog> networking_log_;
  std::unique_ptr<RoutineLog> routine_log_;
  std::unique_ptr<TelemetryLog> telemetry_log_;
  SEQUENCE_CHECKER(sequence_checker_);

  // Must be last.
  base::WeakPtrFactory<DiagnosticsLogController> weak_ptr_factory_{this};
};

}  // namespace diagnostics
}  // namespace ash

#endif  // ASH_SYSTEM_DIAGNOSTICS_DIAGNOSTICS_LOG_CONTROLLER_H_