File: smart_card_error.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 (235 lines) | stat: -rw-r--r-- 11,098 bytes parent folder | download | duplicates (5)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// 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.

#include "third_party/blink/renderer/modules/smart_card/smart_card_error.h"
#include "services/device/public/mojom/smart_card.mojom-shared.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_throw_dom_exception.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_smart_card_error_options.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"

namespace blink {

// static
SmartCardError* SmartCardError::Create(String message,
                                       const SmartCardErrorOptions* options) {
  return MakeGarbageCollected<SmartCardError>(std::move(message),
                                              options->responseCode());
}

// static
void SmartCardError::MaybeReject(
    ScriptPromiseResolverBase* resolver,
    device::mojom::blink::SmartCardError mojom_error) {
  ScriptState* script_state = resolver->GetScriptState();

  if (!IsInParallelAlgorithmRunnable(resolver->GetExecutionContext(),
                                     script_state)) {
    return;
  }

  // Enter the associated v8 context.
  // Otherwise a RejectWithDOMException() or RejectWithTypeError() call will
  // abort, as they need it in order to add call site context to the error
  // message text.
  ScriptState::Scope script_state_scope(script_state);

  switch (mojom_error) {
    // SmartCardError:
    // The response code messages are mostly from
    // https://learn.microsoft.com/en-us/windows/win32/secauthn/authentication-return-values,
    // which are also used by PCSC lite.
    case device::mojom::blink::SmartCardError::kNoService:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "No smart card service available in the system.",
          V8SmartCardResponseCode::Enum::kNoService));
      break;
    case device::mojom::blink::SmartCardError::kNoSmartcard:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "The operation requires a smart card, but no smart card is "
          "currently in the device.",
          V8SmartCardResponseCode::Enum::kNoSmartcard));
      break;
    case device::mojom::blink::SmartCardError::kNotReady:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "The reader or smart card is not ready to accept commands.",
          V8SmartCardResponseCode::Enum::kNotReady));
      break;
    case device::mojom::blink::SmartCardError::kNotTransacted:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "An attempt was made to end a non-existent transaction.",
          V8SmartCardResponseCode::Enum::kNotTransacted));
      break;
    case device::mojom::blink::SmartCardError::kProtoMismatch:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "The requested protocols are incompatible with the protocol "
          "currently in use with the smart card.",
          V8SmartCardResponseCode::Enum::kProtoMismatch));
      break;
    case device::mojom::blink::SmartCardError::kReaderUnavailable:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "The specified reader is not currently available for use.",
          V8SmartCardResponseCode::Enum::kReaderUnavailable));
      break;
    case device::mojom::blink::SmartCardError::kRemovedCard:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "The smart card has been removed, so further communication is not "
          "possible.",
          V8SmartCardResponseCode::Enum::kRemovedCard));
      break;
    case device::mojom::blink::SmartCardError::kResetCard:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "The smart card has been reset, so any shared state information "
          "is invalid.",
          V8SmartCardResponseCode::Enum::kResetCard));
      break;
    case device::mojom::blink::SmartCardError::kServerTooBusy:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "The smart card resource manager is too busy to complete this "
          "operation.",
          V8SmartCardResponseCode::Enum::kServerTooBusy));
      break;
    case device::mojom::blink::SmartCardError::kSharingViolation:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "The smart card cannot be accessed because of other connections "
          "outstanding.",
          V8SmartCardResponseCode::Enum::kSharingViolation));
      break;
    case device::mojom::blink::SmartCardError::kSystemCancelled:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "The action was cancelled by the system, presumably to log off or "
          "shut down.",
          V8SmartCardResponseCode::Enum::kSystemCancelled));
      break;
    case device::mojom::blink::SmartCardError::kUnknownReader:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "The specified reader name is not recognized.",
          V8SmartCardResponseCode::Enum::kUnknownReader));
      break;
    case device::mojom::blink::SmartCardError::kUnpoweredCard:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "Power has been removed from the smart card, so that further "
          "communication is not possible.",
          V8SmartCardResponseCode::Enum::kUnpoweredCard));
      break;
    case device::mojom::blink::SmartCardError::kUnresponsiveCard:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "The smart card is not responding to a reset.",
          V8SmartCardResponseCode::Enum::kUnresponsiveCard));
      break;
    case device::mojom::blink::SmartCardError::kUnsupportedCard:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "The reader cannot communicate with the card, due to ATR string "
          "configuration conflicts.",
          V8SmartCardResponseCode::Enum::kUnsupportedCard));
      break;
    case device::mojom::blink::SmartCardError::kUnsupportedFeature:
      resolver->Reject(MakeGarbageCollected<SmartCardError>(
          "This smart card does not support the requested feature.",
          V8SmartCardResponseCode::Enum::kUnsupportedFeature));
      break;

    // TypeError:
    // This is not only triggered by bad PC/SC API usage (e.g., passing a null
    // context), which would be a browser implementation bug. It can also be
    // returned by the reader driver or card on input that, from a pure PC/SC
    // API perspective, is perfectly valid.
    case device::mojom::blink::SmartCardError::kInvalidParameter:
      resolver->RejectWithTypeError(
          "One or more of the supplied parameters could not be properly "
          "interpreted.");
      break;

    // DOMException:
    // "InvalidStateError"
    case device::mojom::blink::SmartCardError::kInvalidHandle:
      resolver->RejectWithDOMException(DOMExceptionCode::kInvalidStateError,
                                       "Connection is invalid.");
      break;
    case device::mojom::blink::SmartCardError::kServiceStopped:
      resolver->RejectWithDOMException(
          DOMExceptionCode::kInvalidStateError,
          "The smart card resource manager has shut down.");
      break;
    // "AbortError"
    case device::mojom::blink::SmartCardError::kShutdown:
      resolver->RejectWithDOMException(
          DOMExceptionCode::kAbortError,
          "The operation has been aborted to allow the server application to "
          "exit.");
      break;
    // "NotAllowedError"
    case device::mojom::blink::SmartCardError::kPermissionDenied:
      resolver->RejectWithDOMException(
          DOMExceptionCode::kNotAllowedError,
          "The user has denied permission or the user attention requirement "
          "was not fulfilled.");
      break;
    // "UnknownError"
    case device::mojom::blink::SmartCardError::kCommError:
      resolver->RejectWithDOMException(
          DOMExceptionCode::kUnknownError,
          "An internal communications error has been detected.");
      break;
    case device::mojom::blink::SmartCardError::kInternalError:
      resolver->RejectWithDOMException(DOMExceptionCode::kUnknownError,
                                       "An internal consistency check failed.");
      break;
    case device::mojom::blink::SmartCardError::kNoMemory:
      resolver->RejectWithDOMException(
          DOMExceptionCode::kUnknownError,
          "Not enough memory available to complete this command.");
      break;
    case device::mojom::blink::SmartCardError::kUnexpected:
      resolver->RejectWithDOMException(
          DOMExceptionCode::kUnknownError,
          "An unexpected card error has occurred.");
      break;
    case device::mojom::blink::SmartCardError::kUnknownError:
      resolver->RejectWithDOMException(
          DOMExceptionCode::kUnknownError,
          "An internal error has been detected, but the source is unknown.");
      break;
    case device::mojom::blink::SmartCardError::kUnknown:
      // NB: kUnknownError is an actual PC/SC error code returned from the
      // platform's PC/SC stack. kUnknown means that PC/SC returned an error
      // code not yet represented in our enum and therefore is unknown to us.
      LOG(WARNING) << "An unmapped PC/SC error has occurred.";
      resolver->RejectWithDOMException(DOMExceptionCode::kUnknownError,
                                       "An unknown error has occurred.");
      break;
    // Handled internally but listed here for completeness.
    // Also, technically nothing stops the PC/SC stack from spilling those
    // unexpectedly (eg, in unrelated requests).
    case device::mojom::blink::SmartCardError::kCancelled:
    case device::mojom::blink::SmartCardError::kTimeout:
    case device::mojom::blink::SmartCardError::kNoReadersAvailable:
    // Errors that indicate bad usage of the API (ie, a programming
    // error in browser code).
    // Again, technically nothing stops the PC/SC stack from spilling those
    // unexpectedly.
    case device::mojom::blink::SmartCardError::kInsufficientBuffer:
    case device::mojom::blink::SmartCardError::kInvalidValue:
      LOG(WARNING) << "An unexpected PC/SC error has occurred: " << mojom_error;
      resolver->RejectWithDOMException(
          DOMExceptionCode::kUnknownError,
          "An unexpected card error has occurred.");
      break;
  }
}

SmartCardError::SmartCardError(String message,
                               V8SmartCardResponseCode::Enum response_code_enum)
    : SmartCardError(std::move(message),
                     V8SmartCardResponseCode(response_code_enum)) {}

SmartCardError::SmartCardError(String message,
                               V8SmartCardResponseCode response_code)
    : DOMException(DOMExceptionCode::kSmartCardError, std::move(message)),
      response_code_(std::move(response_code)) {}

SmartCardError::~SmartCardError() = default;

}  // namespace blink