diagnostics_log_controller.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_SYSTEM_DIAGNOSTICS_DIAGNOSTICS_LOG_CONTROLLER_H_
  5. #define ASH_SYSTEM_DIAGNOSTICS_DIAGNOSTICS_LOG_CONTROLLER_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ash/login_status.h"
  9. #include "ash/public/cpp/session/session_observer.h"
  10. #include "ash/system/diagnostics/diagnostics_browser_delegate.h"
  11. #include "base/files/file_path.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/sequence_checker.h"
  14. namespace ash {
  15. namespace diagnostics {
  16. class NetworkingLog;
  17. class RoutineLog;
  18. class TelemetryLog;
  19. // DiagnosticsLogController manages the lifetime of Diagnostics log writers such
  20. // as the RoutineLog and ensures logs are written to the correct directory path
  21. // for the current user. See go/cros-shared-diagnostics-session-log-dd.
  22. class ASH_EXPORT DiagnosticsLogController : SessionObserver {
  23. public:
  24. DiagnosticsLogController();
  25. DiagnosticsLogController(const DiagnosticsLogController&) = delete;
  26. DiagnosticsLogController& operator=(const DiagnosticsLogController&) = delete;
  27. ~DiagnosticsLogController() override;
  28. // DiagnosticsLogController is created and destroyed with
  29. // the ash::Shell. DiagnosticsLogController::Get may be nullptr if accessed
  30. // outside the expected lifetime or when the
  31. // `ash::features::kEnableLogControllerForDiagnosticsApp` is false.
  32. static DiagnosticsLogController* Get();
  33. // Check if DiagnosticsLogController is ready for use.
  34. static bool IsInitialized();
  35. static void Initialize(std::unique_ptr<DiagnosticsBrowserDelegate> delegate);
  36. // GenerateSessionLogOnBlockingPool needs to be run on blocking
  37. // thread. Stores combined log at |save_file_path| and returns
  38. // whether file creation is successful.
  39. bool GenerateSessionLogOnBlockingPool(const base::FilePath& save_file_path);
  40. // Ensures DiagnosticsLogController is configured to match the current
  41. // environment. To be called from DiagnosticsDialog::ShowDialog prior to the
  42. // Diagnostics app being shown.
  43. void ResetAndInitializeLogWriters();
  44. // SessionObserver:
  45. // Ensure DiagnosticsLogController is re-configured to match the current
  46. // environment when LoginStatus changes. See ash/ash_login_status.h for
  47. // description of LoginStatus types.
  48. void OnLoginStatusChanged(LoginStatus login_status) override;
  49. NetworkingLog* GetNetworkingLog();
  50. RoutineLog* GetRoutineLog();
  51. TelemetryLog* GetTelemetryLog();
  52. private:
  53. friend class DiagnosticsLogControllerTest;
  54. // Ensure log_base_path_ set based on current session and ash::LoginStatus.
  55. void ResetLogBasePath();
  56. // Removes directory at |path|.
  57. void RemoveDirectory(const base::FilePath& path);
  58. LoginStatus previous_status_;
  59. std::unique_ptr<DiagnosticsBrowserDelegate> delegate_;
  60. base::FilePath log_base_path_;
  61. std::unique_ptr<NetworkingLog> networking_log_;
  62. std::unique_ptr<RoutineLog> routine_log_;
  63. std::unique_ptr<TelemetryLog> telemetry_log_;
  64. SEQUENCE_CHECKER(sequence_checker_);
  65. // Must be last.
  66. base::WeakPtrFactory<DiagnosticsLogController> weak_ptr_factory_{this};
  67. };
  68. } // namespace diagnostics
  69. } // namespace ash
  70. #endif // ASH_SYSTEM_DIAGNOSTICS_DIAGNOSTICS_LOG_CONTROLLER_H_