File: fake_smart_card_device_service.cc

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 (213 lines) | stat: -rw-r--r-- 7,323 bytes parent folder | download | duplicates (6)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// 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/smart_card/fake_smart_card_device_service.h"
#include "base/containers/map_util.h"

namespace {
using device::mojom::SmartCardConnectResult;
using device::mojom::SmartCardContext;
using device::mojom::SmartCardCreateContextResult;
using device::mojom::SmartCardCreateContextResultPtr;
using device::mojom::SmartCardError;
using device::mojom::SmartCardListReadersResult;
using device::mojom::SmartCardReaderStateFlags;
using device::mojom::SmartCardReaderStateIn;
using device::mojom::SmartCardReaderStateInPtr;
using device::mojom::SmartCardReaderStateOut;
using device::mojom::SmartCardReaderStateOutPtr;
using device::mojom::SmartCardResult;
using device::mojom::SmartCardStatusChangeResult;
using device::mojom::SmartCardSuccess;

constexpr char kFooReader[] = "Foo Reader";
constexpr char kAcmeReader[] = "Acme Reader";

bool StateHasChanged(const SmartCardReaderStateOut& state_out,
                     const SmartCardReaderStateIn& state_in) {
  // Try the easy cases first:

  if (state_in.current_state->unaware) {
    return true;
  }

  if (state_in.current_count != state_out.event_count) {
    return true;
  }

  // And only then go for a full diff:
  const SmartCardReaderStateFlags& flags_in = *state_in.current_state;
  const SmartCardReaderStateFlags& flags_out = *state_out.event_state;
  // clang-format off
  return flags_in.unknown != flags_out.unknown ||
         flags_in.unavailable != flags_out.unavailable ||
         flags_in.empty != flags_out.empty ||
         flags_in.present != flags_out.present ||
         flags_in.exclusive != flags_out.exclusive ||
         flags_in.inuse != flags_out.inuse ||
         flags_in.mute != flags_out.mute ||
         flags_in.unpowered != flags_out.unpowered;
  // clang-format on
}
}  // anonymous namespace

struct FakeSmartCardDeviceService::ReaderState {
  bool unknown;
  bool unavailable;
  bool empty;
  bool present;
  bool exclusive;
  bool inuse;
  bool mute;
  bool unpowered;

  uint16_t event_count;
};

struct FakeSmartCardDeviceService::PendingStatusChange {
  PendingStatusChange(std::vector<SmartCardReaderStateInPtr> reader_states,
                      SmartCardContext::GetStatusChangeCallback callback)
      : reader_states(std::move(reader_states)),
        callback(std::move(callback)) {}

  ~PendingStatusChange() = default;

  std::vector<SmartCardReaderStateInPtr> reader_states;
  SmartCardContext::GetStatusChangeCallback callback;
};

FakeSmartCardDeviceService::FakeSmartCardDeviceService() {
  readers_[kFooReader] = {.empty = true};
  readers_[kAcmeReader] = {.present = true};
}

FakeSmartCardDeviceService::~FakeSmartCardDeviceService() = default;

mojo::PendingRemote<device::mojom::SmartCardContextFactory>
FakeSmartCardDeviceService::GetSmartCardContextFactory() {
  mojo::PendingRemote<device::mojom::SmartCardContextFactory> pending_remote;
  context_factory_receivers_.Add(
      this, pending_remote.InitWithNewPipeAndPassReceiver());
  return pending_remote;
}

void FakeSmartCardDeviceService::CreateContext(CreateContextCallback callback) {
  mojo::PendingRemote<SmartCardContext> context_remote;
  context_receivers_.Add(this, context_remote.InitWithNewPipeAndPassReceiver());

  std::move(callback).Run(
      SmartCardCreateContextResult::NewContext(std::move(context_remote)));
}

void FakeSmartCardDeviceService::ListReaders(ListReadersCallback callback) {
  std::vector<std::string> names;
  for (const auto& [name, state] : readers_) {
    names.push_back(name);
  }
  std::move(callback).Run(SmartCardListReadersResult::NewReaders(names));
}

void FakeSmartCardDeviceService::GetStatusChange(
    base::TimeDelta timeout,
    std::vector<device::mojom::SmartCardReaderStateInPtr> reader_states,
    GetStatusChangeCallback callback) {
  pending_status_changes_.push_back(std::make_unique<PendingStatusChange>(
      std::move(reader_states), std::move(callback)));

  TryResolvePendingStatusChanges();
}

void FakeSmartCardDeviceService::Cancel(CancelCallback callback) {
  auto pending_list = std::move(pending_status_changes_);
  for (auto& pending_change : pending_list) {
    std::move(pending_change->callback)
        .Run(SmartCardStatusChangeResult::NewError(SmartCardError::kCancelled));
  }

  std::move(callback).Run(SmartCardResult::NewSuccess(SmartCardSuccess::kOk));
}

void FakeSmartCardDeviceService::Connect(
    const std::string& reader,
    device::mojom::SmartCardShareMode share_mode,
    device::mojom::SmartCardProtocolsPtr preferred_protocols,
    mojo::PendingRemote<device::mojom::SmartCardConnectionWatcher>
        connection_watcher,
    ConnectCallback callback) {
  std::move(callback).Run(
      SmartCardConnectResult::NewError(SmartCardError::kUnresponsiveCard));
}

void FakeSmartCardDeviceService::TryResolvePendingStatusChanges() {
  std::erase_if(pending_status_changes_,
                [this](std::unique_ptr<PendingStatusChange>& p) {
                  return TryResolve(*p);
                });
}

bool FakeSmartCardDeviceService::TryResolve(
    PendingStatusChange& pending_status_change) {
  std::vector<device::mojom::SmartCardReaderStateOutPtr> states_out;

  bool state_changed = false;

  for (const SmartCardReaderStateInPtr& state_in :
       pending_status_change.reader_states) {
    if (state_in->current_state->ignore) {
      continue;
    }

    const ReaderState* reader_state =
        base::FindOrNull(readers_, state_in->reader);
    auto state_out = SmartCardReaderStateOut::New();

    if (reader_state) {
      FillStateOut(*state_out, *state_in, *reader_state);
      state_changed = state_out->event_state->changed;
    } else {
      // Inform that this reader is unknown.
      auto flags = SmartCardReaderStateFlags::New();
      flags->unknown = true;
      state_out->reader = state_in->reader;
      state_out->event_state = std::move(flags);
      state_changed = true;
    }

    states_out.push_back(std::move(state_out));
  }

  if (state_changed) {
    // We only finish an outstanding GetStatusChange() request if the current
    // state of the readers is different from any of requests input states.
    std::move(pending_status_change.callback)
        .Run(SmartCardStatusChangeResult::NewReaderStates(
            std::move(states_out)));
    return true;
  }

  return false;
}

// static
void FakeSmartCardDeviceService::FillStateOut(
    SmartCardReaderStateOut& state_out,
    const SmartCardReaderStateIn& state_in,
    const ReaderState& reader_state) {
  state_out.reader = state_in.reader;

  state_out.event_state = SmartCardReaderStateFlags::New();

  state_out.event_state->unknown = reader_state.unknown;
  state_out.event_state->unavailable = reader_state.unavailable;
  state_out.event_state->empty = reader_state.empty;
  state_out.event_state->present = reader_state.present;
  state_out.event_state->exclusive = reader_state.exclusive;
  state_out.event_state->inuse = reader_state.inuse;
  state_out.event_state->mute = reader_state.mute;
  state_out.event_state->unpowered = reader_state.unpowered;

  state_out.event_count = reader_state.event_count;

  state_out.event_state->changed = StateHasChanged(state_out, state_in);
}