File: barcode_detector.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 (231 lines) | stat: -rw-r--r-- 9,611 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
// Copyright 2016 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/shapedetection/barcode_detector.h"

#include <utility>

#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_throw_dom_exception.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_barcode_detector_options.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_detected_barcode.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_point_2d.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/geometry/dom_rect.h"
#include "third_party/blink/renderer/core/workers/worker_thread.h"
#include "third_party/blink/renderer/modules/canvas/imagebitmap/image_bitmap_source_util.h"
#include "third_party/blink/renderer/modules/shapedetection/barcode_detector_statics.h"
#include "third_party/blink/renderer/platform/bindings/enumeration_base.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"

namespace blink {

namespace {

shape_detection::mojom::blink::BarcodeFormat StringToBarcodeFormat(
    const WebString& format_string) {
  if (format_string == "aztec")
    return shape_detection::mojom::blink::BarcodeFormat::AZTEC;
  if (format_string == "code_128")
    return shape_detection::mojom::blink::BarcodeFormat::CODE_128;
  if (format_string == "code_39")
    return shape_detection::mojom::blink::BarcodeFormat::CODE_39;
  if (format_string == "code_93")
    return shape_detection::mojom::blink::BarcodeFormat::CODE_93;
  if (format_string == "codabar")
    return shape_detection::mojom::blink::BarcodeFormat::CODABAR;
  if (format_string == "data_matrix")
    return shape_detection::mojom::blink::BarcodeFormat::DATA_MATRIX;
  if (format_string == "ean_13")
    return shape_detection::mojom::blink::BarcodeFormat::EAN_13;
  if (format_string == "ean_8")
    return shape_detection::mojom::blink::BarcodeFormat::EAN_8;
  if (format_string == "itf")
    return shape_detection::mojom::blink::BarcodeFormat::ITF;
  if (format_string == "pdf417")
    return shape_detection::mojom::blink::BarcodeFormat::PDF417;
  if (format_string == "qr_code")
    return shape_detection::mojom::blink::BarcodeFormat::QR_CODE;
  if (format_string == "upc_a")
    return shape_detection::mojom::blink::BarcodeFormat::UPC_A;
  if (format_string == "upc_e")
    return shape_detection::mojom::blink::BarcodeFormat::UPC_E;
  return shape_detection::mojom::blink::BarcodeFormat::UNKNOWN;
}

}  // anonymous namespace

BarcodeDetector* BarcodeDetector::Create(ExecutionContext* context,
                                         const BarcodeDetectorOptions* options,
                                         ExceptionState& exception_state) {
  return MakeGarbageCollected<BarcodeDetector>(context, options,
                                               exception_state);
}

BarcodeDetector::BarcodeDetector(ExecutionContext* context,
                                 const BarcodeDetectorOptions* options,
                                 ExceptionState& exception_state)
    : service_(context) {
  auto barcode_detector_options =
      shape_detection::mojom::blink::BarcodeDetectorOptions::New();

  if (options->hasFormats()) {
    // TODO(https://github.com/WICG/shape-detection-api/issues/66):
    // potentially process UNKNOWN as platform-specific formats.
    for (const auto& format_string : options->formats()) {
      auto format = StringToBarcodeFormat(IDLEnumAsString(format_string));
      if (format != shape_detection::mojom::blink::BarcodeFormat::UNKNOWN)
        barcode_detector_options->formats.push_back(format);
    }

    if (barcode_detector_options->formats.empty()) {
      exception_state.ThrowTypeError("Hint option provided, but is empty.");
      return;
    }
  }

  // See https://bit.ly/2S0zRAS for task types.
  auto task_runner = context->GetTaskRunner(TaskType::kMiscPlatformAPI);

  BarcodeDetectorStatics::From(context)->CreateBarcodeDetection(
      service_.BindNewPipeAndPassReceiver(task_runner),
      std::move(barcode_detector_options));
  service_.set_disconnect_handler(WTF::BindOnce(
      &BarcodeDetector::OnConnectionError, WrapWeakPersistent(this)));
}

// static
ScriptPromise<IDLSequence<V8BarcodeFormat>>
BarcodeDetector::getSupportedFormats(ScriptState* script_state) {
  ExecutionContext* context = ExecutionContext::From(script_state);
  return BarcodeDetectorStatics::From(context)->EnumerateSupportedFormats(
      script_state);
}

// static
V8BarcodeFormat::Enum BarcodeDetector::BarcodeFormatToEnum(
    const shape_detection::mojom::BarcodeFormat format) {
  switch (format) {
    case shape_detection::mojom::BarcodeFormat::AZTEC:
      return V8BarcodeFormat::Enum::kAztec;
    case shape_detection::mojom::BarcodeFormat::CODE_128:
      return V8BarcodeFormat::Enum::kCode128;
    case shape_detection::mojom::BarcodeFormat::CODE_39:
      return V8BarcodeFormat::Enum::kCode39;
    case shape_detection::mojom::BarcodeFormat::CODE_93:
      return V8BarcodeFormat::Enum::kCode93;
    case shape_detection::mojom::BarcodeFormat::CODABAR:
      return V8BarcodeFormat::Enum::kCodabar;
    case shape_detection::mojom::BarcodeFormat::DATA_MATRIX:
      return V8BarcodeFormat::Enum::kDataMatrix;
    case shape_detection::mojom::BarcodeFormat::EAN_13:
      return V8BarcodeFormat::Enum::kEan13;
    case shape_detection::mojom::BarcodeFormat::EAN_8:
      return V8BarcodeFormat::Enum::kEan8;
    case shape_detection::mojom::BarcodeFormat::ITF:
      return V8BarcodeFormat::Enum::kItf;
    case shape_detection::mojom::BarcodeFormat::PDF417:
      return V8BarcodeFormat::Enum::kPdf417;
    case shape_detection::mojom::BarcodeFormat::QR_CODE:
      return V8BarcodeFormat::Enum::kQrCode;
    case shape_detection::mojom::BarcodeFormat::UNKNOWN:
      return V8BarcodeFormat::Enum::kUnknown;
    case shape_detection::mojom::BarcodeFormat::UPC_A:
      return V8BarcodeFormat::Enum::kUpcA;
    case shape_detection::mojom::BarcodeFormat::UPC_E:
      return V8BarcodeFormat::Enum::kUpcE;
  }
}

ScriptPromise<IDLSequence<DetectedBarcode>> BarcodeDetector::detect(
    ScriptState* script_state,
    const V8ImageBitmapSource* image_source,
    ExceptionState& exception_state) {
  std::optional<SkBitmap> bitmap = GetBitmapFromV8ImageBitmapSource(
      script_state, image_source, exception_state);
  if (!bitmap) {
    return ScriptPromise<IDLSequence<DetectedBarcode>>();
  }

  auto* resolver =
      MakeGarbageCollected<ScriptPromiseResolver<IDLSequence<DetectedBarcode>>>(
          script_state, exception_state.GetContext());
  auto promise = resolver->Promise();
  if (bitmap->isNull()) {
    resolver->Resolve(HeapVector<Member<DetectedBarcode>>());
    return promise;
  }

  if (!service_.is_bound()) {
    resolver->RejectWithDOMException(DOMExceptionCode::kNotSupportedError,
                                     "Barcode detection service unavailable.");
    return promise;
  }
  detect_requests_.insert(resolver);
  service_->Detect(
      std::move(*bitmap),
      WTF::BindOnce(&BarcodeDetector::OnDetectBarcodes, WrapPersistent(this),
                    WrapPersistent(resolver)));
  return promise;
}

void BarcodeDetector::OnDetectBarcodes(
    ScriptPromiseResolver<IDLSequence<DetectedBarcode>>* resolver,
    Vector<shape_detection::mojom::blink::BarcodeDetectionResultPtr>
        barcode_detection_results) {
  DCHECK(detect_requests_.Contains(resolver));
  detect_requests_.erase(resolver);

  HeapVector<Member<DetectedBarcode>> detected_barcodes;
  for (const auto& barcode : barcode_detection_results) {
    HeapVector<Member<Point2D>> corner_points;
    for (const auto& corner_point : barcode->corner_points) {
      Point2D* point = Point2D::Create();
      point->setX(corner_point.x());
      point->setY(corner_point.y());
      corner_points.push_back(point);
    }

    DetectedBarcode* detected_barcode = DetectedBarcode::Create();
    detected_barcode->setRawValue(barcode->raw_value);
    detected_barcode->setBoundingBox(DOMRectReadOnly::Create(
        barcode->bounding_box.x(), barcode->bounding_box.y(),
        barcode->bounding_box.width(), barcode->bounding_box.height()));
    detected_barcode->setFormat(BarcodeFormatToEnum(barcode->format));
    detected_barcode->setCornerPoints(corner_points);
    detected_barcodes.push_back(detected_barcode);
  }

  resolver->Resolve(detected_barcodes);
}

void BarcodeDetector::OnConnectionError() {
  service_.reset();

  HeapHashSet<Member<ScriptPromiseResolverBase>> resolvers;
  resolvers.swap(detect_requests_);
  for (const auto& resolver : resolvers) {
    // Check if callback's resolver is still valid.
    if (!IsInParallelAlgorithmRunnable(resolver->GetExecutionContext(),
                                       resolver->GetScriptState())) {
      continue;
    }
    // Enter into resolver's context to support creating DOMException.
    ScriptState::Scope script_state_scope(resolver->GetScriptState());

    resolver->Reject(V8ThrowDOMException::CreateOrDie(
        resolver->GetScriptState()->GetIsolate(),
        DOMExceptionCode::kNotSupportedError,
        "Barcode Detection not implemented."));
  }
}

void BarcodeDetector::Trace(Visitor* visitor) const {
  ScriptWrappable::Trace(visitor);
  visitor->Trace(service_);
  visitor->Trace(detect_requests_);
}

}  // namespace blink