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

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

Issue 2557513003: ShapeDetection: Eliminate DetectorType enum in ShapeDetector.cpp (Closed)
Patch Set: Remove state info from parent class, add doDetect() pure virtual method. 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());
Reilly Grant (use Gerrit) 2016/12/06 20:40:13 Same comment.
xianglu 2016/12/06 22:31:56 Done.
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::doDetect(
24 const CanvasImageSourceUnion& imageSource) { 34 ScriptPromiseResolver* resolver,
25 return detectShapes(scriptState, ShapeDetector::DetectorType::Face, 35 mojo::ScopedSharedBufferHandle sharedBufferHandle,
26 imageSource); 36 int imageWidth,
37 int imageHeight) {
38 ScriptPromise promise = resolver->promise();
39 if (!m_faceService) {
40 resolver->reject(DOMException::create(
41 NotSupportedError, "Face detection service unavailable."));
42 return promise;
43 }
44 m_faceServiceRequests.add(resolver);
45 m_faceService->Detect(std::move(sharedBufferHandle), imageWidth, imageHeight,
46 m_faceDetectorOptions.Clone(),
47 convertToBaseCallback(WTF::bind(
48 &FaceDetector::onDetectFaces, wrapPersistent(this),
49 wrapPersistent(resolver))));
50 sharedBufferHandle.reset();
Reilly Grant (use Gerrit) 2016/12/06 20:40:13 Same comment here.
xianglu 2016/12/06 22:31:56 Done.
51 return promise;
52 }
53
54 void FaceDetector::onDetectFaces(
55 ScriptPromiseResolver* resolver,
56 mojom::blink::FaceDetectionResultPtr faceDetectionResult) {
57 DCHECK(m_faceServiceRequests.contains(resolver));
58 m_faceServiceRequests.remove(resolver);
59
60 HeapVector<Member<DOMRect>> detectedFaces;
61 for (const auto& boundingBox : faceDetectionResult->bounding_boxes) {
62 detectedFaces.append(DOMRect::create(boundingBox->x, boundingBox->y,
63 boundingBox->width,
64 boundingBox->height));
65 }
66
67 resolver->resolve(detectedFaces);
68 }
69
70 void FaceDetector::onFaceServiceConnectionError() {
71 for (const auto& request : m_faceServiceRequests) {
72 request->reject(DOMException::create(NotSupportedError,
73 "Face Detection not implemented."));
74 }
75 m_faceServiceRequests.clear();
76 m_faceService.reset();
27 } 77 }
28 78
29 DEFINE_TRACE(FaceDetector) { 79 DEFINE_TRACE(FaceDetector) {
30 ShapeDetector::trace(visitor); 80 ShapeDetector::trace(visitor);
81 visitor->trace(m_faceServiceRequests);
31 } 82 }
32 83
33 } // namespace blink 84 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698