| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/shapedetection/TextDetector.h" |
| 6 |
| 7 #include "core/dom/DOMException.h" |
| 8 #include "core/dom/DOMRect.h" |
| 9 #include "core/frame/LocalFrame.h" |
| 10 #include "core/html/canvas/CanvasImageSource.h" |
| 11 #include "modules/shapedetection/DetectedText.h" |
| 12 #include "public/platform/InterfaceProvider.h" |
| 13 |
| 14 namespace blink { |
| 15 |
| 16 TextDetector* TextDetector::create(Document& document) { |
| 17 return new TextDetector(*document.frame()); |
| 18 } |
| 19 |
| 20 TextDetector::TextDetector(LocalFrame& frame) : ShapeDetector(frame) { |
| 21 frame.interfaceProvider()->getInterface(mojo::MakeRequest(&m_textService)); |
| 22 m_textService.set_connection_error_handler(convertToBaseCallback(WTF::bind( |
| 23 &TextDetector::onTextServiceConnectionError, wrapWeakPersistent(this)))); |
| 24 } |
| 25 |
| 26 ScriptPromise TextDetector::doDetect( |
| 27 ScriptPromiseResolver* resolver, |
| 28 mojo::ScopedSharedBufferHandle sharedBufferHandle, |
| 29 int imageWidth, |
| 30 int imageHeight) { |
| 31 ScriptPromise promise = resolver->promise(); |
| 32 if (!m_textService) { |
| 33 resolver->reject(DOMException::create( |
| 34 NotSupportedError, "Text detection service unavailable.")); |
| 35 return promise; |
| 36 } |
| 37 m_textServiceRequests.add(resolver); |
| 38 m_textService->Detect(std::move(sharedBufferHandle), imageWidth, imageHeight, |
| 39 convertToBaseCallback(WTF::bind( |
| 40 &TextDetector::onDetectText, wrapPersistent(this), |
| 41 wrapPersistent(resolver)))); |
| 42 return promise; |
| 43 } |
| 44 |
| 45 void TextDetector::onDetectText( |
| 46 ScriptPromiseResolver* resolver, |
| 47 Vector<mojom::blink::TextDetectionResultPtr> textDetectionResults) { |
| 48 DCHECK(m_textServiceRequests.contains(resolver)); |
| 49 m_textServiceRequests.remove(resolver); |
| 50 |
| 51 HeapVector<Member<DetectedText>> detectedText; |
| 52 for (const auto& text : textDetectionResults) { |
| 53 detectedText.append(DetectedText::create( |
| 54 text->raw_value, |
| 55 DOMRect::create(text->bounding_box->x, text->bounding_box->y, |
| 56 text->bounding_box->width, |
| 57 text->bounding_box->height))); |
| 58 } |
| 59 |
| 60 resolver->resolve(detectedText); |
| 61 } |
| 62 |
| 63 void TextDetector::onTextServiceConnectionError() { |
| 64 for (const auto& request : m_textServiceRequests) { |
| 65 request->reject(DOMException::create(NotSupportedError, |
| 66 "Text Detection not implemented.")); |
| 67 } |
| 68 m_textServiceRequests.clear(); |
| 69 m_textService.reset(); |
| 70 } |
| 71 |
| 72 DEFINE_TRACE(TextDetector) { |
| 73 ShapeDetector::trace(visitor); |
| 74 visitor->trace(m_textServiceRequests); |
| 75 } |
| 76 |
| 77 } // namespace blink |
| OLD | NEW |