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 DCHECK(m_faceService); |
|
Reilly Grant (use Gerrit)
2016/11/30 21:43:28
We should still have the code to reject the promis
mcasas
2016/11/30 21:49:51
Done.
| |
| 171 resolver->reject(DOMException::create( | 175 m_faceServiceRequests.add(resolver); |
| 172 NotSupportedError, "Face detection service unavailable.")); | |
| 173 return promise; | |
| 174 } | |
| 175 m_faceService->Detect(std::move(sharedBufferHandle), img->naturalWidth(), | 176 m_faceService->Detect(std::move(sharedBufferHandle), img->naturalWidth(), |
| 176 img->naturalHeight(), m_faceDetectorOptions.Clone(), | 177 img->naturalHeight(), m_faceDetectorOptions.Clone(), |
| 177 convertToBaseCallback(WTF::bind( | 178 convertToBaseCallback(WTF::bind( |
| 178 &ShapeDetector::onDetectFaces, | 179 &ShapeDetector::onDetectFaces, |
| 179 wrapPersistent(this), wrapPersistent(resolver)))); | 180 wrapPersistent(this), wrapPersistent(resolver)))); |
| 180 } else if (detectorType == DetectorType::Barcode) { | 181 } else if (detectorType == DetectorType::Barcode) { |
| 181 if (!m_barcodeService) { | 182 DCHECK(m_barcodeService); |
| 182 resolver->reject(DOMException::create( | 183 m_barcodeServiceRequests.add(resolver); |
| 183 NotSupportedError, "Barcode detection service unavailable.")); | |
| 184 return promise; | |
| 185 } | |
| 186 m_barcodeService->Detect( | 184 m_barcodeService->Detect( |
| 187 std::move(sharedBufferHandle), img->naturalWidth(), | 185 std::move(sharedBufferHandle), img->naturalWidth(), |
| 188 img->naturalHeight(), | 186 img->naturalHeight(), |
| 189 convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectBarcodes, | 187 convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectBarcodes, |
| 190 wrapPersistent(this), | 188 wrapPersistent(this), |
| 191 wrapPersistent(resolver)))); | 189 wrapPersistent(resolver)))); |
| 192 } else { | 190 } else { |
| 193 NOTREACHED() << "Unsupported detector type"; | 191 NOTREACHED() << "Unsupported detector type"; |
| 194 } | 192 } |
| 195 | 193 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 301 DOMException::create(InvalidStateError, "Internal allocation error")); | 299 DOMException::create(InvalidStateError, "Internal allocation error")); |
| 302 return promise; | 300 return promise; |
| 303 } | 301 } |
| 304 | 302 |
| 305 const mojo::ScopedSharedBufferMapping mappedBuffer = | 303 const mojo::ScopedSharedBufferMapping mappedBuffer = |
| 306 sharedBufferHandle->Map(size); | 304 sharedBufferHandle->Map(size); |
| 307 DCHECK(mappedBuffer.get()); | 305 DCHECK(mappedBuffer.get()); |
| 308 | 306 |
| 309 memcpy(mappedBuffer.get(), data, size); | 307 memcpy(mappedBuffer.get(), data, size); |
| 310 | 308 |
| 311 m_serviceRequests.add(resolver); | |
| 312 if (detectorType == DetectorType::Face) { | 309 if (detectorType == DetectorType::Face) { |
| 313 if (!m_faceService) { | 310 DCHECK(m_faceService); |
| 314 resolver->reject(DOMException::create( | 311 m_faceServiceRequests.add(resolver); |
| 315 NotSupportedError, "Face detection service unavailable.")); | |
| 316 return promise; | |
| 317 } | |
| 318 m_faceService->Detect(std::move(sharedBufferHandle), width, height, | 312 m_faceService->Detect(std::move(sharedBufferHandle), width, height, |
| 319 m_faceDetectorOptions.Clone(), | 313 m_faceDetectorOptions.Clone(), |
| 320 convertToBaseCallback(WTF::bind( | 314 convertToBaseCallback(WTF::bind( |
| 321 &ShapeDetector::onDetectFaces, | 315 &ShapeDetector::onDetectFaces, |
| 322 wrapPersistent(this), wrapPersistent(resolver)))); | 316 wrapPersistent(this), wrapPersistent(resolver)))); |
| 323 } else if (detectorType == DetectorType::Barcode) { | 317 } else if (detectorType == DetectorType::Barcode) { |
| 324 if (!m_barcodeService) { | 318 DCHECK(m_barcodeService); |
| 325 resolver->reject(DOMException::create( | 319 m_barcodeServiceRequests.add(resolver); |
| 326 NotSupportedError, "Barcode detection service unavailable.")); | |
| 327 return promise; | |
| 328 } | |
| 329 m_barcodeService->Detect( | 320 m_barcodeService->Detect( |
| 330 std::move(sharedBufferHandle), width, height, | 321 std::move(sharedBufferHandle), width, height, |
| 331 convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectBarcodes, | 322 convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectBarcodes, |
| 332 wrapPersistent(this), | 323 wrapPersistent(this), |
| 333 wrapPersistent(resolver)))); | 324 wrapPersistent(resolver)))); |
| 334 } else { | 325 } else { |
| 335 NOTREACHED() << "Unsupported detector type"; | 326 NOTREACHED() << "Unsupported detector type"; |
| 336 } | 327 } |
| 337 sharedBufferHandle.reset(); | 328 sharedBufferHandle.reset(); |
| 338 return promise; | 329 return promise; |
| 339 } | 330 } |
| 340 | 331 |
| 341 void ShapeDetector::onDetectFaces( | 332 void ShapeDetector::onDetectFaces( |
| 342 ScriptPromiseResolver* resolver, | 333 ScriptPromiseResolver* resolver, |
| 343 mojom::blink::FaceDetectionResultPtr faceDetectionResult) { | 334 mojom::blink::FaceDetectionResultPtr faceDetectionResult) { |
| 344 if (!m_serviceRequests.contains(resolver)) | 335 if (!m_faceServiceRequests.contains(resolver)) |
| 345 return; | 336 return; |
| 337 m_faceServiceRequests.remove(resolver); | |
| 346 | 338 |
| 347 HeapVector<Member<DOMRect>> detectedFaces; | 339 HeapVector<Member<DOMRect>> detectedFaces; |
| 348 for (const auto& boundingBox : faceDetectionResult->bounding_boxes) { | 340 for (const auto& boundingBox : faceDetectionResult->bounding_boxes) { |
| 349 detectedFaces.append(DOMRect::create(boundingBox->x, boundingBox->y, | 341 detectedFaces.append(DOMRect::create(boundingBox->x, boundingBox->y, |
| 350 boundingBox->width, | 342 boundingBox->width, |
| 351 boundingBox->height)); | 343 boundingBox->height)); |
| 352 } | 344 } |
| 353 | 345 |
| 354 resolver->resolve(detectedFaces); | 346 resolver->resolve(detectedFaces); |
| 355 m_serviceRequests.remove(resolver); | |
| 356 } | 347 } |
| 357 | 348 |
| 358 void ShapeDetector::onDetectBarcodes( | 349 void ShapeDetector::onDetectBarcodes( |
| 359 ScriptPromiseResolver* resolver, | 350 ScriptPromiseResolver* resolver, |
| 360 Vector<mojom::blink::BarcodeDetectionResultPtr> barcodeDetectionResults) { | 351 Vector<mojom::blink::BarcodeDetectionResultPtr> barcodeDetectionResults) { |
| 361 if (!m_serviceRequests.contains(resolver)) | 352 if (!m_barcodeServiceRequests.contains(resolver)) |
| 362 return; | 353 return; |
| 363 m_serviceRequests.remove(resolver); | 354 m_barcodeServiceRequests.remove(resolver); |
| 364 | 355 |
| 365 HeapVector<Member<DetectedBarcode>> detectedBarcodes; | 356 HeapVector<Member<DetectedBarcode>> detectedBarcodes; |
| 366 for (const auto& barcode : barcodeDetectionResults) { | 357 for (const auto& barcode : barcodeDetectionResults) { |
| 367 detectedBarcodes.append(DetectedBarcode::create( | 358 detectedBarcodes.append(DetectedBarcode::create( |
| 368 barcode->raw_value, | 359 barcode->raw_value, |
| 369 DOMRect::create(barcode->bounding_box->x, barcode->bounding_box->y, | 360 DOMRect::create(barcode->bounding_box->x, barcode->bounding_box->y, |
| 370 barcode->bounding_box->width, | 361 barcode->bounding_box->width, |
| 371 barcode->bounding_box->height))); | 362 barcode->bounding_box->height))); |
| 372 } | 363 } |
| 373 | 364 |
| 374 resolver->resolve(detectedBarcodes); | 365 resolver->resolve(detectedBarcodes); |
| 375 } | 366 } |
| 376 | 367 |
| 368 void ShapeDetector::onFaceServiceConnectionError() { | |
| 369 for (const auto& request : m_faceServiceRequests) { | |
| 370 request->reject(DOMException::create(NotSupportedError, | |
| 371 "Face Detection not implemented.")); | |
| 372 } | |
| 373 m_faceServiceRequests.clear(); | |
|
Reilly Grant (use Gerrit)
2016/11/30 21:43:28
Reset m_faceService so that code above knows the c
mcasas
2016/11/30 21:49:51
Done.
| |
| 374 } | |
| 375 | |
| 376 void ShapeDetector::onBarcodeServiceConnectionError() { | |
| 377 for (const auto& request : m_barcodeServiceRequests) { | |
| 378 request->reject(DOMException::create(NotSupportedError, | |
| 379 "Barcode Detection not implemented.")); | |
| 380 } | |
| 381 m_barcodeServiceRequests.clear(); | |
| 382 } | |
| 383 | |
| 377 DEFINE_TRACE(ShapeDetector) { | 384 DEFINE_TRACE(ShapeDetector) { |
| 378 visitor->trace(m_serviceRequests); | 385 visitor->trace(m_faceServiceRequests); |
| 386 visitor->trace(m_barcodeServiceRequests); | |
| 379 } | 387 } |
| 380 | 388 |
| 381 } // namespace blink | 389 } // namespace blink |
| OLD | NEW |