File: fake_events_service.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (121 lines) | stat: -rw-r--r-- 3,886 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
// 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 "chrome/browser/chromeos/extensions/telemetry/api/events/fake_events_service.h"

#include <tuple>
#include <utility>

#include "base/functional/callback.h"
#include "base/location.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "chromeos/crosapi/mojom/telemetry_event_service.mojom.h"
#include "chromeos/crosapi/mojom/telemetry_extension_exception.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote_set.h"

namespace chromeos {

namespace {

namespace crosapi = ::crosapi::mojom;

}  // namespace

FakeEventsService::FakeEventsService() = default;

FakeEventsService::~FakeEventsService() = default;

void FakeEventsService::BindPendingReceiver(
    mojo::PendingReceiver<crosapi::TelemetryEventService> receiver) {
  receiver_.Bind(std::move(receiver));
}

mojo::PendingRemote<crosapi::TelemetryEventService>
FakeEventsService::BindNewPipeAndPassRemote() {
  return receiver_.BindNewPipeAndPassRemote();
}

void FakeEventsService::AddEventObserver(
    crosapi::TelemetryEventCategoryEnum category,
    mojo::PendingRemote<crosapi::TelemetryEventObserver> observer) {
  auto it = event_observers_.find(category);
  if (it == event_observers_.end()) {
    it = event_observers_.emplace_hint(it, std::piecewise_construct,
                                       std::forward_as_tuple(category),
                                       std::forward_as_tuple());
  }

  it->second.Add(std::move(observer));

  // Register the call to on_subscription_change in case the connection is
  // reset. SAFETY: We can pass `this` to the callback, since this can only be
  // invoked on a connected ReceiverSet, which lives shorter than `this`.
  it->second.set_disconnect_handler(
      base::BindLambdaForTesting([this](mojo::RemoteSetElementId _) {
        if (on_subscription_change_) {
          base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
              FROM_HERE, std::move(on_subscription_change_));
        }
      }));

  // Invoke the callback for on_subscription_change, since we added a
  // subscription.
  if (on_subscription_change_) {
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, std::move(on_subscription_change_));
  }
}

void FakeEventsService::IsEventSupported(
    crosapi::TelemetryEventCategoryEnum category,
    IsEventSupportedCallback callback) {
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback),
                                is_event_supported_response_.Clone()));
}

void FakeEventsService::SetIsEventSupportedResponse(
    crosapi::TelemetryExtensionSupportStatusPtr status) {
  is_event_supported_response_.Swap(&status);
}

void FakeEventsService::EmitEventForCategory(
    crosapi::TelemetryEventCategoryEnum category,
    crosapi::TelemetryEventInfoPtr info) {
  // Flush the receiver, so any pending observers are registered before the
  // event is emitted.
  if (receiver_.is_bound()) {
    receiver_.FlushForTesting();
  }

  auto it = event_observers_.find(category);
  if (it == event_observers_.end()) {
    return;
  }

  for (auto& observer : it->second) {
    observer->OnEvent(info.Clone());
  }
}

mojo::RemoteSet<crosapi::TelemetryEventObserver>*
FakeEventsService::GetObserversByCategory(
    crosapi::TelemetryEventCategoryEnum category) {
  auto it = event_observers_.find(category);
  if (it == event_observers_.end()) {
    return nullptr;
  }

  return &it->second;
}

void FakeEventsService::SetOnSubscriptionChange(
    base::RepeatingClosure callback) {
  on_subscription_change_ = std::move(callback);
}

}  // namespace chromeos