Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: third_party/WebKit/Source/modules/shapedetection/BarcodeDetector.cpp

Issue 2557513003: ShapeDetection: Eliminate DetectorType enum in ShapeDetector.cpp (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "modules/shapedetection/BarcodeDetector.h" 5 #include "modules/shapedetection/BarcodeDetector.h"
6 6
7 #include "core/dom/DOMException.h" 7 #include "core/dom/DOMException.h"
8 #include "core/dom/DOMRect.h"
8 #include "core/frame/LocalDOMWindow.h" 9 #include "core/frame/LocalDOMWindow.h"
9 #include "core/frame/LocalFrame.h" 10 #include "core/frame/LocalFrame.h"
10 #include "core/html/canvas/CanvasImageSource.h" 11 #include "core/html/canvas/CanvasImageSource.h"
12 #include "modules/shapedetection/DetectedBarcode.h"
13 #include "public/platform/InterfaceProvider.h"
11 14
12 namespace blink { 15 namespace blink {
13 16
14 BarcodeDetector* BarcodeDetector::create(Document& document) { 17 BarcodeDetector* BarcodeDetector::create(Document& document) {
15 return new BarcodeDetector(*document.frame()); 18 return new BarcodeDetector(*document.frame());
16 } 19 }
17 20
18 BarcodeDetector::BarcodeDetector(LocalFrame& frame) : ShapeDetector(frame) {} 21 BarcodeDetector::BarcodeDetector(LocalFrame& frame) : ShapeDetector(frame) {
22 DCHECK(!m_barcodeService.is_bound());
23 frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_barcodeService));
24 m_barcodeService.set_connection_error_handler(convertToBaseCallback(
25 WTF::bind(&BarcodeDetector::onBarcodeServiceConnectionError,
26 wrapWeakPersistent(this))));
27 }
19 28
20 ScriptPromise BarcodeDetector::detect( 29 ScriptPromise BarcodeDetector::detect(
21 ScriptState* scriptState, 30 ScriptState* scriptState,
22 const CanvasImageSourceUnion& imageSource) { 31 const CanvasImageSourceUnion& imageSource) {
23 return detectShapes(scriptState, ShapeDetector::DetectorType::Barcode, 32 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
24 imageSource); 33 ScriptPromise promise = resolver->promise();
34 if (!preprocessImageSource(scriptState, resolver, imageSource))
35 return promise;
36
37 if (!m_barcodeService) {
38 resolver->reject(DOMException::create(
39 NotSupportedError, "Barcode detection service unavailable."));
40 return promise;
41 }
42 m_barcodeServiceRequests.add(resolver);
43 m_barcodeService->Detect(
44 std::move(m_sharedBufferHandle), m_imageWidth, m_imageHeight,
45 convertToBaseCallback(WTF::bind(&BarcodeDetector::onDetectBarcodes,
46 wrapPersistent(this),
47 wrapPersistent(resolver))));
48 m_sharedBufferHandle.reset();
49 return promise;
50 }
51
52 void BarcodeDetector::onDetectBarcodes(
53 ScriptPromiseResolver* resolver,
54 Vector<mojom::blink::BarcodeDetectionResultPtr> barcodeDetectionResults) {
55 DCHECK(m_barcodeServiceRequests.contains(resolver));
56 m_barcodeServiceRequests.remove(resolver);
57
58 HeapVector<Member<DetectedBarcode>> detectedBarcodes;
59 for (const auto& barcode : barcodeDetectionResults) {
60 detectedBarcodes.append(DetectedBarcode::create(
61 barcode->raw_value,
62 DOMRect::create(barcode->bounding_box->x, barcode->bounding_box->y,
63 barcode->bounding_box->width,
64 barcode->bounding_box->height)));
65 }
66
67 resolver->resolve(detectedBarcodes);
68 }
69
70 void BarcodeDetector::onBarcodeServiceConnectionError() {
71 for (const auto& request : m_barcodeServiceRequests) {
72 request->reject(DOMException::create(NotSupportedError,
73 "Barcode Detection not implemented."));
74 }
75 m_barcodeServiceRequests.clear();
76 m_barcodeService.reset();
25 } 77 }
26 78
27 DEFINE_TRACE(BarcodeDetector) { 79 DEFINE_TRACE(BarcodeDetector) {
28 ShapeDetector::trace(visitor); 80 ShapeDetector::trace(visitor);
81 visitor->trace(m_barcodeServiceRequests);
29 } 82 }
30 83
31 } // namespace blink 84 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698