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

Side by Side Diff: third_party/WebKit/Source/modules/shapedetection/ShapeDetector.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
« no previous file with comments | « third_party/WebKit/Source/modules/shapedetection/ShapeDetector.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/ShapeDetector.h" 5 #include "modules/shapedetection/ShapeDetector.h"
6 6
7 #include "core/dom/DOMException.h" 7 #include "core/dom/DOMException.h"
8 #include "core/dom/DOMRect.h" 8 #include "core/dom/DOMRect.h"
9 #include "core/dom/Document.h" 9 #include "core/dom/Document.h"
10 #include "core/fetch/ImageResource.h" 10 #include "core/fetch/ImageResource.h"
11 #include "core/frame/ImageBitmap.h" 11 #include "core/frame/ImageBitmap.h"
12 #include "core/frame/LocalDOMWindow.h"
13 #include "core/frame/LocalFrame.h" 12 #include "core/frame/LocalFrame.h"
14 #include "core/html/HTMLImageElement.h" 13 #include "core/html/HTMLImageElement.h"
15 #include "core/html/HTMLVideoElement.h" 14 #include "core/html/HTMLVideoElement.h"
16 #include "core/html/canvas/CanvasImageSource.h" 15 #include "core/html/canvas/CanvasImageSource.h"
17 #include "modules/shapedetection/DetectedBarcode.h"
18 #include "platform/graphics/Image.h" 16 #include "platform/graphics/Image.h"
19 #include "public/platform/InterfaceProvider.h"
20 #include "third_party/skia/include/core/SkImage.h" 17 #include "third_party/skia/include/core/SkImage.h"
21 #include "third_party/skia/include/core/SkImageInfo.h" 18 #include "third_party/skia/include/core/SkImageInfo.h"
22 #include "wtf/CheckedNumeric.h" 19 #include "wtf/CheckedNumeric.h"
23 20
24 namespace blink { 21 namespace blink {
25 22
26 namespace { 23 namespace {
27 24
28 static CanvasImageSource* toImageSourceInternal( 25 static CanvasImageSource* toImageSourceInternal(
29 const CanvasImageSourceUnion& value) { 26 const CanvasImageSourceUnion& value) {
30 if (value.isHTMLImageElement()) 27 if (value.isHTMLImageElement())
31 return value.getAsHTMLImageElement(); 28 return value.getAsHTMLImageElement();
32 29
33 if (value.isImageBitmap() && 30 if (value.isImageBitmap() &&
34 !static_cast<ImageBitmap*>(value.getAsImageBitmap())->isNeutered()) { 31 !static_cast<ImageBitmap*>(value.getAsImageBitmap())->isNeutered()) {
35 return value.getAsImageBitmap(); 32 return value.getAsImageBitmap();
36 } 33 }
37 34
38 if (value.isHTMLVideoElement()) 35 if (value.isHTMLVideoElement())
39 return value.getAsHTMLVideoElement(); 36 return value.getAsHTMLVideoElement();
40 37
41 return nullptr; 38 return nullptr;
42 } 39 }
43 40
44 } // anonymous namespace 41 } // anonymous namespace
45 42
46 ShapeDetector::ShapeDetector(LocalFrame& frame) { 43 ShapeDetector::ShapeDetector(LocalFrame& frame) {
47 DCHECK(!m_faceService.is_bound());
48 DCHECK(!m_barcodeService.is_bound());
49 DCHECK(frame.interfaceProvider()); 44 DCHECK(frame.interfaceProvider());
50 frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_faceService));
51 frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_barcodeService));
52 m_faceService.set_connection_error_handler(convertToBaseCallback(WTF::bind(
53 &ShapeDetector::onFaceServiceConnectionError, wrapWeakPersistent(this))));
54 m_barcodeService.set_connection_error_handler(convertToBaseCallback(
55 WTF::bind(&ShapeDetector::onBarcodeServiceConnectionError,
56 wrapWeakPersistent(this))));
57 } 45 }
58 46
59 ShapeDetector::ShapeDetector(LocalFrame& frame, 47 bool ShapeDetector::preprocessImageSource(
60 const FaceDetectorOptions& options)
61 : ShapeDetector(frame) {
62 m_faceDetectorOptions = mojom::blink::FaceDetectorOptions::New();
63 m_faceDetectorOptions->max_detected_faces = options.maxDetectedFaces();
64 m_faceDetectorOptions->fast_mode = options.fastMode();
65 }
66
67 ScriptPromise ShapeDetector::detectShapes(
68 ScriptState* scriptState, 48 ScriptState* scriptState,
69 DetectorType detectorType, 49 ScriptPromiseResolver* resolver,
70 const CanvasImageSourceUnion& imageSource) { 50 const CanvasImageSourceUnion& imageSource) {
71 CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource); 51 CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource);
72 52
73 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
74 ScriptPromise promise = resolver->promise();
75 53
76 if (!imageSourceInternal) { 54 if (!imageSourceInternal) {
77 // TODO(mcasas): Implement more CanvasImageSources, https://crbug.com/659138 55 // TODO(mcasas): Implement more CanvasImageSources, https://crbug.com/659138
78 NOTIMPLEMENTED() << "Unsupported CanvasImageSource"; 56 NOTIMPLEMENTED() << "Unsupported CanvasImageSource";
79 resolver->reject( 57 resolver->reject(
80 DOMException::create(NotFoundError, "Unsupported source.")); 58 DOMException::create(NotFoundError, "Unsupported source."));
81 return promise; 59 return false;
82 } 60 }
83 61
84 if (imageSourceInternal->wouldTaintOrigin( 62 if (imageSourceInternal->wouldTaintOrigin(
85 scriptState->getExecutionContext()->getSecurityOrigin())) { 63 scriptState->getExecutionContext()->getSecurityOrigin())) {
86 resolver->reject( 64 resolver->reject(
87 DOMException::create(SecurityError, "Source would taint origin.")); 65 DOMException::create(SecurityError, "Source would taint origin."));
88 return promise; 66 return false;
89 } 67 }
90 68
91 if (imageSource.isHTMLImageElement()) { 69 if (imageSource.isHTMLImageElement()) {
92 return detectShapesOnImageElement( 70 return preprocessImageElement(
93 detectorType, resolver, 71 resolver, static_cast<HTMLImageElement*>(imageSourceInternal));
94 static_cast<HTMLImageElement*>(imageSourceInternal));
95 } 72 }
96 if (imageSourceInternal->isImageBitmap()) { 73 if (imageSourceInternal->isImageBitmap()) {
97 return detectShapesOnImageBitmap( 74 return preprocessImageBitmap(
98 detectorType, resolver, static_cast<ImageBitmap*>(imageSourceInternal)); 75 resolver, static_cast<ImageBitmap*>(imageSourceInternal));
99 } 76 }
100 if (imageSourceInternal->isVideoElement()) { 77 if (imageSourceInternal->isVideoElement()) {
101 return detectShapesOnVideoElement( 78 return preprocessVideoElement(
102 detectorType, resolver, 79 resolver, static_cast<HTMLVideoElement*>(imageSourceInternal));
103 static_cast<HTMLVideoElement*>(imageSourceInternal));
104 } 80 }
105 81
106 NOTREACHED(); 82 NOTREACHED();
107 return promise; 83 return false;
108 } 84 }
109 85
110 ScriptPromise ShapeDetector::detectShapesOnImageElement( 86 bool ShapeDetector::preprocessImageElement(ScriptPromiseResolver* resolver,
111 DetectorType detectorType, 87 const HTMLImageElement* img) {
112 ScriptPromiseResolver* resolver,
113 const HTMLImageElement* img) {
114 ScriptPromise promise = resolver->promise();
115 if (img->bitmapSourceSize().isZero()) { 88 if (img->bitmapSourceSize().isZero()) {
116 resolver->resolve(HeapVector<Member<DOMRect>>()); 89 resolver->resolve(HeapVector<Member<DOMRect>>());
117 return promise; 90 return false;
118 } 91 }
119 92
120 ImageResource* const imageResource = img->cachedImage(); 93 ImageResource* const imageResource = img->cachedImage();
121 if (!imageResource || imageResource->errorOccurred()) { 94 if (!imageResource || imageResource->errorOccurred()) {
122 resolver->reject(DOMException::create( 95 resolver->reject(DOMException::create(
123 InvalidStateError, "Failed to load or decode HTMLImageElement.")); 96 InvalidStateError, "Failed to load or decode HTMLImageElement."));
124 return promise; 97 return false;
125 } 98 }
126 99
127 Image* const blinkImage = imageResource->getImage(); 100 Image* const blinkImage = imageResource->getImage();
128 if (!blinkImage) { 101 if (!blinkImage) {
129 resolver->reject(DOMException::create( 102 resolver->reject(DOMException::create(
130 InvalidStateError, "Failed to get image from resource.")); 103 InvalidStateError, "Failed to get image from resource."));
131 return promise; 104 return false;
132 } 105 }
133 106
134 const sk_sp<SkImage> image = blinkImage->imageForCurrentFrame(); 107 const sk_sp<SkImage> image = blinkImage->imageForCurrentFrame();
135 DCHECK_EQ(img->naturalWidth(), static_cast<unsigned>(image->width())); 108 DCHECK_EQ(img->naturalWidth(), static_cast<unsigned>(image->width()));
136 DCHECK_EQ(img->naturalHeight(), static_cast<unsigned>(image->height())); 109 DCHECK_EQ(img->naturalHeight(), static_cast<unsigned>(image->height()));
137 110
138 if (!image) { 111 if (!image) {
139 resolver->reject(DOMException::create( 112 resolver->reject(DOMException::create(
140 InvalidStateError, "Failed to get image from current frame.")); 113 InvalidStateError, "Failed to get image from current frame."));
141 return promise; 114 return false;
142 } 115 }
143 116
144 const SkImageInfo skiaInfo = 117 const SkImageInfo skiaInfo =
145 SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType()); 118 SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType());
146 119
147 const uint32_t allocationSize = skiaInfo.getSafeSize(skiaInfo.minRowBytes()); 120 const uint32_t allocationSize = skiaInfo.getSafeSize(skiaInfo.minRowBytes());
148 121
149 mojo::ScopedSharedBufferHandle sharedBufferHandle = 122 m_sharedBufferHandle = mojo::SharedBufferHandle::Create(allocationSize);
150 mojo::SharedBufferHandle::Create(allocationSize); 123 if (!m_sharedBufferHandle.is_valid()) {
151 if (!sharedBufferHandle.is_valid()) {
152 DLOG(ERROR) << "Requested allocation : " << allocationSize 124 DLOG(ERROR) << "Requested allocation : " << allocationSize
153 << "B, larger than |mojo::edk::kMaxSharedBufferSize| == 16MB "; 125 << "B, larger than |mojo::edk::kMaxSharedBufferSize| == 16MB ";
154 // TODO(xianglu): For now we reject the promise if the image is too large. 126 // TODO(xianglu): For now we reject the promise if the image is too large.
155 // But consider resizing the image to remove restriction on the user side. 127 // But consider resizing the image to remove restriction on the user side.
156 // Also, add LayoutTests for this case later. 128 // Also, add LayoutTests for this case later.
157 resolver->reject( 129 resolver->reject(
158 DOMException::create(InvalidStateError, "Image exceeds size limit.")); 130 DOMException::create(InvalidStateError, "Image exceeds size limit."));
159 return promise; 131 return false;
160 } 132 }
161 133
162 const mojo::ScopedSharedBufferMapping mappedBuffer = 134 const mojo::ScopedSharedBufferMapping mappedBuffer =
163 sharedBufferHandle->Map(allocationSize); 135 m_sharedBufferHandle->Map(allocationSize);
164 136
165 const SkPixmap pixmap(skiaInfo, mappedBuffer.get(), skiaInfo.minRowBytes()); 137 const SkPixmap pixmap(skiaInfo, mappedBuffer.get(), skiaInfo.minRowBytes());
166 if (!image->readPixels(pixmap, 0, 0)) { 138 if (!image->readPixels(pixmap, 0, 0)) {
167 resolver->reject(DOMException::create( 139 resolver->reject(DOMException::create(
168 InvalidStateError, 140 InvalidStateError,
169 "Failed to read pixels: Unable to decompress or unsupported format.")); 141 "Failed to read pixels: Unable to decompress or unsupported format."));
170 return promise; 142 return false;
171 } 143 }
172 144
173 if (detectorType == DetectorType::Face) { 145 m_imageWidth = img->naturalWidth();
174 if (!m_faceService) { 146 m_imageHeight = img->naturalHeight();
mcasas 2016/12/06 04:40:20 Instead of holding on to |m_sharedBufferHandle|, |
xianglu 2016/12/06 20:20:10 Done.
175 resolver->reject(DOMException::create(
176 NotSupportedError, "Face detection service unavailable."));
177 return promise;
178 }
179 m_faceServiceRequests.add(resolver);
180 m_faceService->Detect(std::move(sharedBufferHandle), img->naturalWidth(),
181 img->naturalHeight(), m_faceDetectorOptions.Clone(),
182 convertToBaseCallback(WTF::bind(
183 &ShapeDetector::onDetectFaces,
184 wrapPersistent(this), wrapPersistent(resolver))));
185 } else if (detectorType == DetectorType::Barcode) {
186 if (!m_barcodeService) {
187 resolver->reject(DOMException::create(
188 NotSupportedError, "Barcode detection service unavailable."));
189 return promise;
190 }
191 m_barcodeServiceRequests.add(resolver);
192 m_barcodeService->Detect(
193 std::move(sharedBufferHandle), img->naturalWidth(),
194 img->naturalHeight(),
195 convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectBarcodes,
196 wrapPersistent(this),
197 wrapPersistent(resolver))));
198 } else {
199 NOTREACHED() << "Unsupported detector type";
200 }
201 147
202 return promise; 148 return true;
203 } 149 }
204 150
205 ScriptPromise ShapeDetector::detectShapesOnImageBitmap( 151 bool ShapeDetector::preprocessImageBitmap(ScriptPromiseResolver* resolver,
206 DetectorType detectorType, 152 ImageBitmap* imageBitmap) {
207 ScriptPromiseResolver* resolver,
208 ImageBitmap* imageBitmap) {
209 ScriptPromise promise = resolver->promise();
210 if (!imageBitmap->originClean()) { 153 if (!imageBitmap->originClean()) {
211 resolver->reject( 154 resolver->reject(
212 DOMException::create(SecurityError, "ImageBitmap is not origin clean")); 155 DOMException::create(SecurityError, "ImageBitmap is not origin clean"));
213 return promise; 156 return false;
214 } 157 }
215 158
216 if (imageBitmap->size().area() == 0) { 159 if (imageBitmap->size().area() == 0) {
217 resolver->resolve(HeapVector<Member<DOMRect>>()); 160 resolver->resolve(HeapVector<Member<DOMRect>>());
218 return promise; 161 return false;
219 } 162 }
220 163
221 SkPixmap pixmap; 164 SkPixmap pixmap;
222 RefPtr<Uint8Array> pixelData; 165 RefPtr<Uint8Array> pixelData;
223 uint8_t* pixelDataPtr = nullptr; 166 uint8_t* pixelDataPtr = nullptr;
224 WTF::CheckedNumeric<int> allocationSize = 0; 167 WTF::CheckedNumeric<int> allocationSize = 0;
225 // Use |skImage|'s pixels if it has direct access to them, otherwise retrieve 168 // Use |skImage|'s pixels if it has direct access to them, otherwise retrieve
226 // them from elsewhere via copyBitmapData(). 169 // them from elsewhere via copyBitmapData().
227 sk_sp<SkImage> skImage = imageBitmap->bitmapImage()->imageForCurrentFrame(); 170 sk_sp<SkImage> skImage = imageBitmap->bitmapImage()->imageForCurrentFrame();
228 if (skImage->peekPixels(&pixmap)) { 171 if (skImage->peekPixels(&pixmap)) {
229 pixelDataPtr = static_cast<uint8_t*>(pixmap.writable_addr()); 172 pixelDataPtr = static_cast<uint8_t*>(pixmap.writable_addr());
230 allocationSize = pixmap.getSafeSize(); 173 allocationSize = pixmap.getSafeSize();
231 } else { 174 } else {
232 pixelData = imageBitmap->copyBitmapData(imageBitmap->isPremultiplied() 175 pixelData = imageBitmap->copyBitmapData(imageBitmap->isPremultiplied()
233 ? PremultiplyAlpha 176 ? PremultiplyAlpha
234 : DontPremultiplyAlpha, 177 : DontPremultiplyAlpha,
235 N32ColorType); 178 N32ColorType);
236 pixelDataPtr = pixelData->data(); 179 pixelDataPtr = pixelData->data();
237 allocationSize = imageBitmap->size().area() * 4 /* bytes per pixel */; 180 allocationSize = imageBitmap->size().area() * 4 /* bytes per pixel */;
238 } 181 }
239 182
240 return detectShapesOnData(detectorType, resolver, pixelDataPtr, 183 m_imageWidth = imageBitmap->width();
241 allocationSize.ValueOrDefault(0), 184 m_imageHeight = imageBitmap->height();
242 imageBitmap->width(), imageBitmap->height()); 185 return getSharedBufferOnData(resolver, pixelDataPtr,
186 allocationSize.ValueOrDefault(0),
187 imageBitmap->width(), imageBitmap->height());
243 } 188 }
244 189
245 ScriptPromise ShapeDetector::detectShapesOnVideoElement( 190 bool ShapeDetector::preprocessVideoElement(ScriptPromiseResolver* resolver,
246 DetectorType detectorType, 191 const HTMLVideoElement* video) {
247 ScriptPromiseResolver* resolver,
248 const HTMLVideoElement* video) {
249 ScriptPromise promise = resolver->promise();
250
251 // TODO(mcasas): Check if |video| is actually playing a MediaStream by using 192 // TODO(mcasas): Check if |video| is actually playing a MediaStream by using
252 // HTMLMediaElement::isMediaStreamURL(video->currentSrc().getString()); if 193 // HTMLMediaElement::isMediaStreamURL(video->currentSrc().getString()); if
253 // there is a local WebCam associated, there might be sophisticated ways to 194 // there is a local WebCam associated, there might be sophisticated ways to
254 // detect faces on it. Until then, treat as a normal <video> element. 195 // detect faces on it. Until then, treat as a normal <video> element.
255 196
256 // !hasAvailableVideoFrame() is a bundle of invalid states. 197 // !hasAvailableVideoFrame() is a bundle of invalid states.
257 if (!video->hasAvailableVideoFrame()) { 198 if (!video->hasAvailableVideoFrame()) {
258 resolver->reject(DOMException::create( 199 resolver->reject(DOMException::create(
259 InvalidStateError, "Invalid HTMLVideoElement or state.")); 200 InvalidStateError, "Invalid HTMLVideoElement or state."));
260 return promise; 201 return false;
261 } 202 }
262 203
263 const FloatSize videoSize(video->videoWidth(), video->videoHeight()); 204 const FloatSize videoSize(video->videoWidth(), video->videoHeight());
264 SourceImageStatus sourceImageStatus = InvalidSourceImageStatus; 205 SourceImageStatus sourceImageStatus = InvalidSourceImageStatus;
265 RefPtr<Image> image = 206 RefPtr<Image> image =
266 video->getSourceImageForCanvas(&sourceImageStatus, PreferNoAcceleration, 207 video->getSourceImageForCanvas(&sourceImageStatus, PreferNoAcceleration,
267 SnapshotReasonDrawImage, videoSize); 208 SnapshotReasonDrawImage, videoSize);
268 209
269 DCHECK_EQ(NormalSourceImageStatus, sourceImageStatus); 210 DCHECK_EQ(NormalSourceImageStatus, sourceImageStatus);
270 211
271 SkPixmap pixmap; 212 SkPixmap pixmap;
272 RefPtr<Uint8Array> pixelData; 213 RefPtr<Uint8Array> pixelData;
273 uint8_t* pixelDataPtr = nullptr; 214 uint8_t* pixelDataPtr = nullptr;
274 WTF::CheckedNumeric<int> allocationSize = 0; 215 WTF::CheckedNumeric<int> allocationSize = 0;
275 // Use |skImage|'s pixels if it has direct access to them. 216 // Use |skImage|'s pixels if it has direct access to them.
276 sk_sp<SkImage> skImage = image->imageForCurrentFrame(); 217 sk_sp<SkImage> skImage = image->imageForCurrentFrame();
277 if (skImage->peekPixels(&pixmap)) { 218 if (skImage->peekPixels(&pixmap)) {
278 pixelDataPtr = static_cast<uint8_t*>(pixmap.writable_addr()); 219 pixelDataPtr = static_cast<uint8_t*>(pixmap.writable_addr());
279 allocationSize = pixmap.getSafeSize(); 220 allocationSize = pixmap.getSafeSize();
280 } else { 221 } else {
281 // TODO(mcasas): retrieve the pixels from elsewhere. 222 // TODO(mcasas): retrieve the pixels from elsewhere.
282 NOTREACHED(); 223 NOTREACHED();
283 resolver->reject(DOMException::create( 224 resolver->reject(DOMException::create(
284 InvalidStateError, "Failed to get pixels for current frame.")); 225 InvalidStateError, "Failed to get pixels for current frame."));
285 return promise; 226 return false;
286 } 227 }
287 228
288 return detectShapesOnData(detectorType, resolver, pixelDataPtr, 229 m_imageWidth = image->width();
289 allocationSize.ValueOrDefault(0), image->width(), 230 m_imageHeight = image->height();
290 image->height()); 231 return getSharedBufferOnData(resolver, pixelDataPtr,
232 allocationSize.ValueOrDefault(0), image->width(),
233 image->height());
291 } 234 }
292 235
293 ScriptPromise ShapeDetector::detectShapesOnData(DetectorType detectorType, 236 bool ShapeDetector::getSharedBufferOnData(ScriptPromiseResolver* resolver,
294 ScriptPromiseResolver* resolver, 237 uint8_t* data,
295 uint8_t* data, 238 int size,
296 int size, 239 int width,
297 int width, 240 int height) {
298 int height) {
299 DCHECK(data); 241 DCHECK(data);
300 DCHECK(size); 242 DCHECK(size);
301 ScriptPromise promise = resolver->promise();
302 243
303 mojo::ScopedSharedBufferHandle sharedBufferHandle = 244 m_sharedBufferHandle = mojo::SharedBufferHandle::Create(size);
304 mojo::SharedBufferHandle::Create(size); 245 if (!m_sharedBufferHandle->is_valid()) {
305 if (!sharedBufferHandle->is_valid()) {
306 resolver->reject( 246 resolver->reject(
307 DOMException::create(InvalidStateError, "Internal allocation error")); 247 DOMException::create(InvalidStateError, "Internal allocation error"));
308 return promise; 248 return false;
309 } 249 }
310 250
311 const mojo::ScopedSharedBufferMapping mappedBuffer = 251 const mojo::ScopedSharedBufferMapping mappedBuffer =
312 sharedBufferHandle->Map(size); 252 m_sharedBufferHandle->Map(size);
313 DCHECK(mappedBuffer.get()); 253 DCHECK(mappedBuffer.get());
314 254
315 memcpy(mappedBuffer.get(), data, size); 255 memcpy(mappedBuffer.get(), data, size);
316 256
317 if (detectorType == DetectorType::Face) { 257 return true;
318 if (!m_faceService) {
319 resolver->reject(DOMException::create(
320 NotSupportedError, "Face detection service unavailable."));
321 return promise;
322 }
323 m_faceServiceRequests.add(resolver);
324 m_faceService->Detect(std::move(sharedBufferHandle), width, height,
325 m_faceDetectorOptions.Clone(),
326 convertToBaseCallback(WTF::bind(
327 &ShapeDetector::onDetectFaces,
328 wrapPersistent(this), wrapPersistent(resolver))));
329 } else if (detectorType == DetectorType::Barcode) {
330 if (!m_barcodeService) {
331 resolver->reject(DOMException::create(
332 NotSupportedError, "Barcode detection service unavailable."));
333 return promise;
334 }
335 m_barcodeServiceRequests.add(resolver);
336 m_barcodeService->Detect(
337 std::move(sharedBufferHandle), width, height,
338 convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectBarcodes,
339 wrapPersistent(this),
340 wrapPersistent(resolver))));
341 } else {
342 NOTREACHED() << "Unsupported detector type";
343 }
344 sharedBufferHandle.reset();
345 return promise;
346 } 258 }
347 259
348 void ShapeDetector::onDetectFaces(
349 ScriptPromiseResolver* resolver,
350 mojom::blink::FaceDetectionResultPtr faceDetectionResult) {
351 DCHECK(m_faceServiceRequests.contains(resolver));
352 m_faceServiceRequests.remove(resolver);
353
354 HeapVector<Member<DOMRect>> detectedFaces;
355 for (const auto& boundingBox : faceDetectionResult->bounding_boxes) {
356 detectedFaces.append(DOMRect::create(boundingBox->x, boundingBox->y,
357 boundingBox->width,
358 boundingBox->height));
359 }
360
361 resolver->resolve(detectedFaces);
362 }
363
364 void ShapeDetector::onDetectBarcodes(
365 ScriptPromiseResolver* resolver,
366 Vector<mojom::blink::BarcodeDetectionResultPtr> barcodeDetectionResults) {
367 DCHECK(m_barcodeServiceRequests.contains(resolver));
368 m_barcodeServiceRequests.remove(resolver);
369
370 HeapVector<Member<DetectedBarcode>> detectedBarcodes;
371 for (const auto& barcode : barcodeDetectionResults) {
372 detectedBarcodes.append(DetectedBarcode::create(
373 barcode->raw_value,
374 DOMRect::create(barcode->bounding_box->x, barcode->bounding_box->y,
375 barcode->bounding_box->width,
376 barcode->bounding_box->height)));
377 }
378
379 resolver->resolve(detectedBarcodes);
380 }
381
382 void ShapeDetector::onFaceServiceConnectionError() {
383 for (const auto& request : m_faceServiceRequests) {
384 request->reject(DOMException::create(NotSupportedError,
385 "Face Detection not implemented."));
386 }
387 m_faceServiceRequests.clear();
388 m_faceService.reset();
389 }
390
391 void ShapeDetector::onBarcodeServiceConnectionError() {
392 for (const auto& request : m_barcodeServiceRequests) {
393 request->reject(DOMException::create(NotSupportedError,
394 "Barcode Detection not implemented."));
395 }
396 m_barcodeServiceRequests.clear();
397 m_barcodeService.reset();
398 }
399
400 DEFINE_TRACE(ShapeDetector) {
401 visitor->trace(m_faceServiceRequests);
402 visitor->trace(m_barcodeServiceRequests);
403 }
404 260
405 } // namespace blink 261 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/shapedetection/ShapeDetector.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698