File: telemetry_diagnostic_routine_service_ash.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; 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 (150 lines) | stat: -rw-r--r-- 5,877 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
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/telemetry_extension/routines/telemetry_diagnostic_routine_service_ash.h"

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/ash/components/telemetry_extension/common/self_owned_mojo_proxy.h"
#include "chromeos/ash/components/telemetry_extension/common/telemetry_extension_converters.h"
#include "chromeos/ash/components/telemetry_extension/routines/routine_control.h"
#include "chromeos/ash/components/telemetry_extension/routines/routine_converters.h"
#include "chromeos/ash/components/telemetry_extension/routines/routine_events_forwarder.h"
#include "chromeos/ash/services/cros_healthd/public/cpp/service_connection.h"
#include "chromeos/ash/services/cros_healthd/public/mojom/cros_healthd_routines.mojom.h"
#include "chromeos/crosapi/mojom/telemetry_diagnostic_routine_service.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"

namespace ash {

namespace {

namespace crosapi = crosapi::mojom;
namespace healthd = cros_healthd::mojom;

using RoutineControlProxy =
    SelfOwnedMojoProxy<healthd::RoutineControl,
                       crosapi::TelemetryDiagnosticRoutineControl,
                       CrosHealthdRoutineControl>;
using RoutineObserverProxy =
    SelfOwnedMojoProxy<crosapi::TelemetryDiagnosticRoutineObserver,
                       healthd::RoutineObserver,
                       CrosHealthdRoutineEventsForwarder>;

}  // namespace

// static
TelemetryDiagnosticsRoutineServiceAsh::Factory*
    TelemetryDiagnosticsRoutineServiceAsh::Factory::test_factory_ = nullptr;

// static
std::unique_ptr<crosapi::TelemetryDiagnosticRoutinesService>
TelemetryDiagnosticsRoutineServiceAsh::Factory::Create(
    mojo::PendingReceiver<crosapi::TelemetryDiagnosticRoutinesService>
        receiver) {
  if (test_factory_) {
    return test_factory_->CreateInstance(std::move(receiver));
  }

  auto routine_service =
      std::make_unique<TelemetryDiagnosticsRoutineServiceAsh>();
  routine_service->BindReceiver(std::move(receiver));
  return routine_service;
}

// static
void TelemetryDiagnosticsRoutineServiceAsh::Factory::SetForTesting(
    Factory* test_factory) {
  test_factory_ = test_factory;
}

TelemetryDiagnosticsRoutineServiceAsh::Factory::~Factory() = default;

TelemetryDiagnosticsRoutineServiceAsh::TelemetryDiagnosticsRoutineServiceAsh() =
    default;

TelemetryDiagnosticsRoutineServiceAsh::
    ~TelemetryDiagnosticsRoutineServiceAsh() {
  for (auto&& proxy : routine_controls_and_observers_) {
    if (proxy) {
      proxy->OnServiceDestroyed();
    }
  }
  routine_controls_and_observers_.clear();
}

void TelemetryDiagnosticsRoutineServiceAsh::BindReceiver(
    mojo::PendingReceiver<crosapi::TelemetryDiagnosticRoutinesService>
        receiver) {
  receivers_.Add(this, std::move(receiver));
}

void TelemetryDiagnosticsRoutineServiceAsh::CreateRoutine(
    crosapi::TelemetryDiagnosticRoutineArgumentPtr routine_argument,
    mojo::PendingReceiver<crosapi::TelemetryDiagnosticRoutineControl>
        routine_receiver,
    mojo::PendingRemote<crosapi::TelemetryDiagnosticRoutineObserver> observer) {
  // Setup the RoutineControl.
  mojo::PendingRemote<healthd::RoutineControl> cros_healthd_remote;
  auto cros_healthd_receiver =
      cros_healthd_remote.InitWithNewPipeAndPassReceiver();

  // SAFETY: We can use `base::Unretained` here since we signal the
  // `SelfOwnedMojoProxy` in the destructor.
  auto control_delete_cb =
      base::BindOnce(&TelemetryDiagnosticsRoutineServiceAsh::OnConnectionClosed,
                     base::Unretained(this));
  auto routine_control = RoutineControlProxy::Create(
      std::move(routine_receiver), std::move(cros_healthd_remote),
      std::move(control_delete_cb));
  routine_controls_and_observers_.insert(std::move(routine_control));

  // Setup the RoutineObserver.
  mojo::PendingRemote<healthd::RoutineObserver> cros_healthd_observer;
  if (observer.is_valid()) {
    // SAFETY: We can use `base::Unretained` here since we signal the
    // `SelfOwnedMojoProxy` in the destructor.
    auto observer_delete_cb = base::BindOnce(
        &TelemetryDiagnosticsRoutineServiceAsh::OnConnectionClosed,
        base::Unretained(this));
    auto routine_observer = RoutineObserverProxy::Create(
        cros_healthd_observer.InitWithNewPipeAndPassReceiver(),
        std::move(observer), std::move(observer_delete_cb));
    routine_controls_and_observers_.insert(std::move(routine_observer));
  }

  // Register the two objects with cros_healthd.
  cros_healthd::ServiceConnection::GetInstance()
      ->GetRoutinesService()
      ->CreateRoutine(
          converters::ConvertRoutinePtr(std::move(routine_argument)),
          std::move(cros_healthd_receiver), std::move(cros_healthd_observer));
}

void TelemetryDiagnosticsRoutineServiceAsh::IsRoutineArgumentSupported(
    crosapi::TelemetryDiagnosticRoutineArgumentPtr arg,
    IsRoutineArgumentSupportedCallback callback) {
  cros_healthd::ServiceConnection::GetInstance()
      ->GetRoutinesService()
      ->IsRoutineArgumentSupported(
          converters::ConvertRoutinePtr(std::move(arg)),
          base::BindOnce(
              [](IsRoutineArgumentSupportedCallback callback,
                 healthd::SupportStatusPtr status) {
                std::move(callback).Run(
                    converters::ConvertCommonPtr(std::move(status)));
              },
              std::move(callback)));
}

void TelemetryDiagnosticsRoutineServiceAsh::OnConnectionClosed(
    base::WeakPtr<SelfOwnedMojoProxyInterface> closed_connection) {
  routine_controls_and_observers_.erase(closed_connection);
}

}  // namespace ash