Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 } | 42 } |
| 43 | 43 |
| 44 } // anonymous namespace | 44 } // anonymous namespace |
| 45 | 45 |
| 46 ShapeDetector::ShapeDetector(LocalFrame& frame) { | 46 ShapeDetector::ShapeDetector(LocalFrame& frame) { |
| 47 DCHECK(!m_faceService.is_bound()); | 47 DCHECK(!m_faceService.is_bound()); |
| 48 DCHECK(!m_barcodeService.is_bound()); | 48 DCHECK(!m_barcodeService.is_bound()); |
| 49 DCHECK(frame.interfaceProvider()); | 49 DCHECK(frame.interfaceProvider()); |
| 50 frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_faceService)); | 50 frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_faceService)); |
| 51 frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_barcodeService)); | 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)))); | |
| 52 } | 57 } |
| 53 | 58 |
| 54 ShapeDetector::ShapeDetector(LocalFrame& frame, | 59 ShapeDetector::ShapeDetector(LocalFrame& frame, |
| 55 const FaceDetectorOptions& options) | 60 const FaceDetectorOptions& options) |
| 56 : ShapeDetector(frame) { | 61 : ShapeDetector(frame) { |
| 57 m_faceDetectorOptions = mojom::blink::FaceDetectorOptions::New(); | 62 m_faceDetectorOptions = mojom::blink::FaceDetectorOptions::New(); |
| 58 m_faceDetectorOptions->max_detected_faces = options.maxDetectedFaces(); | 63 m_faceDetectorOptions->max_detected_faces = options.maxDetectedFaces(); |
| 59 m_faceDetectorOptions->fast_mode = options.fastMode(); | 64 m_faceDetectorOptions->fast_mode = options.fastMode(); |
| 60 } | 65 } |
| 61 | 66 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 158 sharedBufferHandle->Map(allocationSize); | 163 sharedBufferHandle->Map(allocationSize); |
| 159 | 164 |
| 160 const SkPixmap pixmap(skiaInfo, mappedBuffer.get(), skiaInfo.minRowBytes()); | 165 const SkPixmap pixmap(skiaInfo, mappedBuffer.get(), skiaInfo.minRowBytes()); |
| 161 if (!image->readPixels(pixmap, 0, 0)) { | 166 if (!image->readPixels(pixmap, 0, 0)) { |
| 162 resolver->reject(DOMException::create( | 167 resolver->reject(DOMException::create( |
| 163 InvalidStateError, | 168 InvalidStateError, |
| 164 "Failed to read pixels: Unable to decompress or unsupported format.")); | 169 "Failed to read pixels: Unable to decompress or unsupported format.")); |
| 165 return promise; | 170 return promise; |
| 166 } | 171 } |
| 167 | 172 |
| 168 m_serviceRequests.add(resolver); | |
| 169 if (detectorType == DetectorType::Face) { | 173 if (detectorType == DetectorType::Face) { |
| 170 if (!m_faceService) { | 174 if (!m_faceService) { |
| 171 resolver->reject(DOMException::create( | 175 resolver->reject(DOMException::create( |
| 172 NotSupportedError, "Face detection service unavailable.")); | 176 NotSupportedError, "Face detection service unavailable.")); |
| 173 return promise; | 177 return promise; |
| 174 } | 178 } |
| 179 m_faceServiceRequests.add(resolver); | |
| 175 m_faceService->Detect(std::move(sharedBufferHandle), img->naturalWidth(), | 180 m_faceService->Detect(std::move(sharedBufferHandle), img->naturalWidth(), |
| 176 img->naturalHeight(), m_faceDetectorOptions.Clone(), | 181 img->naturalHeight(), m_faceDetectorOptions.Clone(), |
| 177 convertToBaseCallback(WTF::bind( | 182 convertToBaseCallback(WTF::bind( |
| 178 &ShapeDetector::onDetectFaces, | 183 &ShapeDetector::onDetectFaces, |
| 179 wrapPersistent(this), wrapPersistent(resolver)))); | 184 wrapPersistent(this), wrapPersistent(resolver)))); |
| 180 } else if (detectorType == DetectorType::Barcode) { | 185 } else if (detectorType == DetectorType::Barcode) { |
| 181 if (!m_barcodeService) { | 186 if (!m_barcodeService) { |
| 182 resolver->reject(DOMException::create( | 187 resolver->reject(DOMException::create( |
| 183 NotSupportedError, "Barcode detection service unavailable.")); | 188 NotSupportedError, "Barcode detection service unavailable.")); |
| 184 return promise; | 189 return promise; |
| 185 } | 190 } |
| 191 m_barcodeServiceRequests.add(resolver); | |
| 186 m_barcodeService->Detect( | 192 m_barcodeService->Detect( |
| 187 std::move(sharedBufferHandle), img->naturalWidth(), | 193 std::move(sharedBufferHandle), img->naturalWidth(), |
| 188 img->naturalHeight(), | 194 img->naturalHeight(), |
| 189 convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectBarcodes, | 195 convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectBarcodes, |
| 190 wrapPersistent(this), | 196 wrapPersistent(this), |
| 191 wrapPersistent(resolver)))); | 197 wrapPersistent(resolver)))); |
| 192 } else { | 198 } else { |
| 193 NOTREACHED() << "Unsupported detector type"; | 199 NOTREACHED() << "Unsupported detector type"; |
| 194 } | 200 } |
| 195 | 201 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 301 DOMException::create(InvalidStateError, "Internal allocation error")); | 307 DOMException::create(InvalidStateError, "Internal allocation error")); |
| 302 return promise; | 308 return promise; |
| 303 } | 309 } |
| 304 | 310 |
| 305 const mojo::ScopedSharedBufferMapping mappedBuffer = | 311 const mojo::ScopedSharedBufferMapping mappedBuffer = |
| 306 sharedBufferHandle->Map(size); | 312 sharedBufferHandle->Map(size); |
| 307 DCHECK(mappedBuffer.get()); | 313 DCHECK(mappedBuffer.get()); |
| 308 | 314 |
| 309 memcpy(mappedBuffer.get(), data, size); | 315 memcpy(mappedBuffer.get(), data, size); |
| 310 | 316 |
| 311 m_serviceRequests.add(resolver); | |
| 312 if (detectorType == DetectorType::Face) { | 317 if (detectorType == DetectorType::Face) { |
| 313 if (!m_faceService) { | 318 if (!m_faceService) { |
| 314 resolver->reject(DOMException::create( | 319 resolver->reject(DOMException::create( |
| 315 NotSupportedError, "Face detection service unavailable.")); | 320 NotSupportedError, "Face detection service unavailable.")); |
| 316 return promise; | 321 return promise; |
| 317 } | 322 } |
| 323 m_faceServiceRequests.add(resolver); | |
| 318 m_faceService->Detect(std::move(sharedBufferHandle), width, height, | 324 m_faceService->Detect(std::move(sharedBufferHandle), width, height, |
| 319 m_faceDetectorOptions.Clone(), | 325 m_faceDetectorOptions.Clone(), |
| 320 convertToBaseCallback(WTF::bind( | 326 convertToBaseCallback(WTF::bind( |
| 321 &ShapeDetector::onDetectFaces, | 327 &ShapeDetector::onDetectFaces, |
| 322 wrapPersistent(this), wrapPersistent(resolver)))); | 328 wrapPersistent(this), wrapPersistent(resolver)))); |
| 323 } else if (detectorType == DetectorType::Barcode) { | 329 } else if (detectorType == DetectorType::Barcode) { |
| 324 if (!m_barcodeService) { | 330 if (!m_barcodeService) { |
| 325 resolver->reject(DOMException::create( | 331 resolver->reject(DOMException::create( |
| 326 NotSupportedError, "Barcode detection service unavailable.")); | 332 NotSupportedError, "Barcode detection service unavailable.")); |
| 327 return promise; | 333 return promise; |
| 328 } | 334 } |
| 335 m_barcodeServiceRequests.add(resolver); | |
| 329 m_barcodeService->Detect( | 336 m_barcodeService->Detect( |
| 330 std::move(sharedBufferHandle), width, height, | 337 std::move(sharedBufferHandle), width, height, |
| 331 convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectBarcodes, | 338 convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectBarcodes, |
| 332 wrapPersistent(this), | 339 wrapPersistent(this), |
| 333 wrapPersistent(resolver)))); | 340 wrapPersistent(resolver)))); |
| 334 } else { | 341 } else { |
| 335 NOTREACHED() << "Unsupported detector type"; | 342 NOTREACHED() << "Unsupported detector type"; |
| 336 } | 343 } |
| 337 sharedBufferHandle.reset(); | 344 sharedBufferHandle.reset(); |
| 338 return promise; | 345 return promise; |
| 339 } | 346 } |
| 340 | 347 |
| 341 void ShapeDetector::onDetectFaces( | 348 void ShapeDetector::onDetectFaces( |
| 342 ScriptPromiseResolver* resolver, | 349 ScriptPromiseResolver* resolver, |
| 343 mojom::blink::FaceDetectionResultPtr faceDetectionResult) { | 350 mojom::blink::FaceDetectionResultPtr faceDetectionResult) { |
| 344 if (!m_serviceRequests.contains(resolver)) | 351 if (!m_faceServiceRequests.contains(resolver)) |
|
Reilly Grant (use Gerrit)
2016/11/30 21:58:47
I think this can be a DCHECK. The reason we have t
mcasas
2016/11/30 22:03:16
Done.
| |
| 345 return; | 352 return; |
| 353 m_faceServiceRequests.remove(resolver); | |
| 346 | 354 |
| 347 HeapVector<Member<DOMRect>> detectedFaces; | 355 HeapVector<Member<DOMRect>> detectedFaces; |
| 348 for (const auto& boundingBox : faceDetectionResult->bounding_boxes) { | 356 for (const auto& boundingBox : faceDetectionResult->bounding_boxes) { |
| 349 detectedFaces.append(DOMRect::create(boundingBox->x, boundingBox->y, | 357 detectedFaces.append(DOMRect::create(boundingBox->x, boundingBox->y, |
| 350 boundingBox->width, | 358 boundingBox->width, |
| 351 boundingBox->height)); | 359 boundingBox->height)); |
| 352 } | 360 } |
| 353 | 361 |
| 354 resolver->resolve(detectedFaces); | 362 resolver->resolve(detectedFaces); |
| 355 m_serviceRequests.remove(resolver); | |
| 356 } | 363 } |
| 357 | 364 |
| 358 void ShapeDetector::onDetectBarcodes( | 365 void ShapeDetector::onDetectBarcodes( |
| 359 ScriptPromiseResolver* resolver, | 366 ScriptPromiseResolver* resolver, |
| 360 Vector<mojom::blink::BarcodeDetectionResultPtr> barcodeDetectionResults) { | 367 Vector<mojom::blink::BarcodeDetectionResultPtr> barcodeDetectionResults) { |
| 361 if (!m_serviceRequests.contains(resolver)) | 368 if (!m_barcodeServiceRequests.contains(resolver)) |
|
Reilly Grant (use Gerrit)
2016/11/30 21:58:47
Ditto.
mcasas
2016/11/30 22:03:16
Done.
| |
| 362 return; | 369 return; |
| 363 m_serviceRequests.remove(resolver); | 370 m_barcodeServiceRequests.remove(resolver); |
| 364 | 371 |
| 365 HeapVector<Member<DetectedBarcode>> detectedBarcodes; | 372 HeapVector<Member<DetectedBarcode>> detectedBarcodes; |
| 366 for (const auto& barcode : barcodeDetectionResults) { | 373 for (const auto& barcode : barcodeDetectionResults) { |
| 367 detectedBarcodes.append(DetectedBarcode::create( | 374 detectedBarcodes.append(DetectedBarcode::create( |
| 368 barcode->raw_value, | 375 barcode->raw_value, |
| 369 DOMRect::create(barcode->bounding_box->x, barcode->bounding_box->y, | 376 DOMRect::create(barcode->bounding_box->x, barcode->bounding_box->y, |
| 370 barcode->bounding_box->width, | 377 barcode->bounding_box->width, |
| 371 barcode->bounding_box->height))); | 378 barcode->bounding_box->height))); |
| 372 } | 379 } |
| 373 | 380 |
| 374 resolver->resolve(detectedBarcodes); | 381 resolver->resolve(detectedBarcodes); |
| 375 } | 382 } |
| 376 | 383 |
| 384 void ShapeDetector::onFaceServiceConnectionError() { | |
| 385 for (const auto& request : m_faceServiceRequests) { | |
| 386 request->reject(DOMException::create(NotSupportedError, | |
| 387 "Face Detection not implemented.")); | |
| 388 } | |
| 389 m_faceServiceRequests.clear(); | |
| 390 m_faceService.reset(); | |
| 391 } | |
| 392 | |
| 393 void ShapeDetector::onBarcodeServiceConnectionError() { | |
| 394 for (const auto& request : m_barcodeServiceRequests) { | |
| 395 request->reject(DOMException::create(NotSupportedError, | |
| 396 "Barcode Detection not implemented.")); | |
| 397 } | |
| 398 m_barcodeServiceRequests.clear(); | |
| 399 m_barcodeService.reset(); | |
| 400 } | |
| 401 | |
| 377 DEFINE_TRACE(ShapeDetector) { | 402 DEFINE_TRACE(ShapeDetector) { |
| 378 visitor->trace(m_serviceRequests); | 403 visitor->trace(m_faceServiceRequests); |
| 404 visitor->trace(m_barcodeServiceRequests); | |
| 379 } | 405 } |
| 380 | 406 |
| 381 } // namespace blink | 407 } // namespace blink |
| OLD | NEW |