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

Side by Side Diff: third_party/WebKit/Source/modules/shapedetection/FaceDetector.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/FaceDetector.h" 5 #include "modules/shapedetection/FaceDetector.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 "public/platform/InterfaceProvider.h"
11 13
12 namespace blink { 14 namespace blink {
13 15
14 FaceDetector* FaceDetector::create(Document& document, 16 FaceDetector* FaceDetector::create(Document& document,
15 const FaceDetectorOptions& options) { 17 const FaceDetectorOptions& options) {
16 return new FaceDetector(*document.frame(), options); 18 return new FaceDetector(*document.frame(), options);
17 } 19 }
18 20
19 FaceDetector::FaceDetector(LocalFrame& frame, 21 FaceDetector::FaceDetector(LocalFrame& frame,
20 const FaceDetectorOptions& options) 22 const FaceDetectorOptions& options)
21 : ShapeDetector(frame, options) {} 23 : ShapeDetector(frame),
24 m_faceDetectorOptions(mojom::blink::FaceDetectorOptions::New()) {
25 DCHECK(!m_faceService.is_bound());
26 frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_faceService));
27 m_faceService.set_connection_error_handler(convertToBaseCallback(WTF::bind(
28 &FaceDetector::onFaceServiceConnectionError, wrapWeakPersistent(this))));
29 m_faceDetectorOptions->max_detected_faces = options.maxDetectedFaces();
30 m_faceDetectorOptions->fast_mode = options.fastMode();
31 }
22 32
23 ScriptPromise FaceDetector::detect(ScriptState* scriptState, 33 ScriptPromise FaceDetector::detect(ScriptState* scriptState,
24 const CanvasImageSourceUnion& imageSource) { 34 const CanvasImageSourceUnion& imageSource) {
25 return detectShapes(scriptState, ShapeDetector::DetectorType::Face, 35 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
26 imageSource); 36 ScriptPromise promise = resolver->promise();
37 if (!preprocessImageSource(scriptState, resolver, imageSource))
38 return promise;
39
40 if (!m_faceService) {
41 resolver->reject(DOMException::create(
42 NotSupportedError, "Face detection service unavailable."));
43 return promise;
44 }
45 m_faceServiceRequests.add(resolver);
46 m_faceService->Detect(std::move(m_sharedBufferHandle), m_imageWidth,
47 m_imageHeight, m_faceDetectorOptions.Clone(),
48 convertToBaseCallback(WTF::bind(
49 &FaceDetector::onDetectFaces, wrapPersistent(this),
50 wrapPersistent(resolver))));
51 m_sharedBufferHandle.reset();
52 return promise;
53 }
54
55 void FaceDetector::onDetectFaces(
56 ScriptPromiseResolver* resolver,
57 mojom::blink::FaceDetectionResultPtr faceDetectionResult) {
58 DCHECK(m_faceServiceRequests.contains(resolver));
59 m_faceServiceRequests.remove(resolver);
60
61 HeapVector<Member<DOMRect>> detectedFaces;
62 for (const auto& boundingBox : faceDetectionResult->bounding_boxes) {
63 detectedFaces.append(DOMRect::create(boundingBox->x, boundingBox->y,
64 boundingBox->width,
65 boundingBox->height));
66 }
67
68 resolver->resolve(detectedFaces);
69 }
70
71 void FaceDetector::onFaceServiceConnectionError() {
72 for (const auto& request : m_faceServiceRequests) {
73 request->reject(DOMException::create(NotSupportedError,
74 "Face Detection not implemented."));
75 }
76 m_faceServiceRequests.clear();
77 m_faceService.reset();
27 } 78 }
28 79
29 DEFINE_TRACE(FaceDetector) { 80 DEFINE_TRACE(FaceDetector) {
30 ShapeDetector::trace(visitor); 81 ShapeDetector::trace(visitor);
82 visitor->trace(m_faceServiceRequests);
31 } 83 }
32 84
33 } // namespace blink 85 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698