Chromium Code Reviews| Index: third_party/WebKit/Source/modules/shapedetection/BarcodeDetector.cpp |
| diff --git a/third_party/WebKit/Source/modules/shapedetection/BarcodeDetector.cpp b/third_party/WebKit/Source/modules/shapedetection/BarcodeDetector.cpp |
| index 969ff229b2bd7abce4cbc75cc1d2966aa5680880..7f3e8f3bd5516a8c4585f76bec5d59a4ad5bc926 100644 |
| --- a/third_party/WebKit/Source/modules/shapedetection/BarcodeDetector.cpp |
| +++ b/third_party/WebKit/Source/modules/shapedetection/BarcodeDetector.cpp |
| @@ -5,9 +5,12 @@ |
| #include "modules/shapedetection/BarcodeDetector.h" |
| #include "core/dom/DOMException.h" |
| +#include "core/dom/DOMRect.h" |
| #include "core/frame/LocalDOMWindow.h" |
| #include "core/frame/LocalFrame.h" |
| #include "core/html/canvas/CanvasImageSource.h" |
| +#include "modules/shapedetection/DetectedBarcode.h" |
| +#include "public/platform/InterfaceProvider.h" |
| namespace blink { |
| @@ -15,17 +18,65 @@ BarcodeDetector* BarcodeDetector::create(Document& document) { |
| return new BarcodeDetector(*document.frame()); |
| } |
| -BarcodeDetector::BarcodeDetector(LocalFrame& frame) : ShapeDetector(frame) {} |
| +BarcodeDetector::BarcodeDetector(LocalFrame& frame) : ShapeDetector(frame) { |
| + DCHECK(!m_barcodeService.is_bound()); |
|
Reilly Grant (use Gerrit)
2016/12/06 20:40:12
Just DCHECK(!m_barcodeService), though in the cons
xianglu
2016/12/06 22:31:56
Done.
|
| + frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_barcodeService)); |
| + m_barcodeService.set_connection_error_handler(convertToBaseCallback( |
| + WTF::bind(&BarcodeDetector::onBarcodeServiceConnectionError, |
| + wrapWeakPersistent(this)))); |
| +} |
| + |
| +ScriptPromise BarcodeDetector::doDetect( |
| + ScriptPromiseResolver* resolver, |
| + mojo::ScopedSharedBufferHandle sharedBufferHandle, |
| + int imageWidth, |
| + int imageHeight) { |
| + ScriptPromise promise = resolver->promise(); |
| + if (!m_barcodeService) { |
| + resolver->reject(DOMException::create( |
| + NotSupportedError, "Barcode detection service unavailable.")); |
| + return promise; |
| + } |
| + m_barcodeServiceRequests.add(resolver); |
| + m_barcodeService->Detect( |
| + std::move(sharedBufferHandle), imageWidth, imageHeight, |
| + convertToBaseCallback(WTF::bind(&BarcodeDetector::onDetectBarcodes, |
| + wrapPersistent(this), |
| + wrapPersistent(resolver)))); |
| + sharedBufferHandle.reset(); |
|
Reilly Grant (use Gerrit)
2016/12/06 20:40:12
This line should be a no-op because std::move(shar
xianglu
2016/12/06 22:31:56
Done.
|
| + return promise; |
| +} |
| + |
| +void BarcodeDetector::onDetectBarcodes( |
| + ScriptPromiseResolver* resolver, |
| + Vector<mojom::blink::BarcodeDetectionResultPtr> barcodeDetectionResults) { |
| + DCHECK(m_barcodeServiceRequests.contains(resolver)); |
| + m_barcodeServiceRequests.remove(resolver); |
| + |
| + HeapVector<Member<DetectedBarcode>> detectedBarcodes; |
| + for (const auto& barcode : barcodeDetectionResults) { |
| + detectedBarcodes.append(DetectedBarcode::create( |
| + barcode->raw_value, |
| + DOMRect::create(barcode->bounding_box->x, barcode->bounding_box->y, |
| + barcode->bounding_box->width, |
| + barcode->bounding_box->height))); |
| + } |
| + |
| + resolver->resolve(detectedBarcodes); |
| +} |
| -ScriptPromise BarcodeDetector::detect( |
| - ScriptState* scriptState, |
| - const CanvasImageSourceUnion& imageSource) { |
| - return detectShapes(scriptState, ShapeDetector::DetectorType::Barcode, |
| - imageSource); |
| +void BarcodeDetector::onBarcodeServiceConnectionError() { |
| + for (const auto& request : m_barcodeServiceRequests) { |
| + request->reject(DOMException::create(NotSupportedError, |
| + "Barcode Detection not implemented.")); |
| + } |
| + m_barcodeServiceRequests.clear(); |
| + m_barcodeService.reset(); |
| } |
| DEFINE_TRACE(BarcodeDetector) { |
| ShapeDetector::trace(visitor); |
| + visitor->trace(m_barcodeServiceRequests); |
| } |
| } // namespace blink |