barcode_detection_provider_mac.mm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/shape_detection/barcode_detection_provider_mac.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/logging.h"
  8. #include "mojo/public/cpp/bindings/pending_receiver.h"
  9. #include "mojo/public/cpp/bindings/self_owned_receiver.h"
  10. #include "services/shape_detection/barcode_detection_impl_mac.h"
  11. #include "services/shape_detection/barcode_detection_impl_mac_vision.h"
  12. namespace shape_detection {
  13. BarcodeDetectionProviderMac::BarcodeDetectionProviderMac() = default;
  14. BarcodeDetectionProviderMac::BarcodeDetectionProviderMac(
  15. std::unique_ptr<VisionAPIInterface> vision_api)
  16. : vision_api_(std::move(vision_api)) {}
  17. BarcodeDetectionProviderMac::~BarcodeDetectionProviderMac() = default;
  18. // static
  19. void BarcodeDetectionProviderMac::Create(
  20. mojo::PendingReceiver<mojom::BarcodeDetectionProvider> receiver) {
  21. mojo::MakeSelfOwnedReceiver(std::make_unique<BarcodeDetectionProviderMac>(),
  22. std::move(receiver));
  23. }
  24. void BarcodeDetectionProviderMac::CreateBarcodeDetection(
  25. mojo::PendingReceiver<mojom::BarcodeDetection> receiver,
  26. mojom::BarcodeDetectorOptionsPtr options) {
  27. if (!vision_api_)
  28. vision_api_ = VisionAPIInterface::Create();
  29. if (!BarcodeDetectionImplMacVision::IsBlockedMacOSVersion()) {
  30. auto impl =
  31. std::make_unique<BarcodeDetectionImplMacVision>(std::move(options));
  32. auto* impl_ptr = impl.get();
  33. impl_ptr->SetReceiver(
  34. mojo::MakeSelfOwnedReceiver(std::move(impl), std::move(receiver)));
  35. return;
  36. }
  37. mojo::MakeSelfOwnedReceiver(std::make_unique<BarcodeDetectionImplMac>(),
  38. std::move(receiver));
  39. }
  40. void BarcodeDetectionProviderMac::EnumerateSupportedFormats(
  41. EnumerateSupportedFormatsCallback callback) {
  42. // If we have supported formats already cached, return them.
  43. if (supported_formats_) {
  44. DLOG_IF(WARNING, supported_formats_->empty())
  45. << "Supported formats requested previously but error or none "
  46. << "recognized.";
  47. std::move(callback).Run(supported_formats_.value());
  48. return;
  49. }
  50. if (!vision_api_)
  51. vision_api_ = VisionAPIInterface::Create();
  52. if (!BarcodeDetectionImplMacVision::IsBlockedMacOSVersion()) {
  53. supported_formats_ = BarcodeDetectionImplMacVision::GetSupportedSymbologies(
  54. vision_api_.get());
  55. std::move(callback).Run(supported_formats_.value());
  56. return;
  57. }
  58. supported_formats_ = std::vector<mojom::BarcodeFormat>(
  59. BarcodeDetectionImplMac::GetSupportedSymbologies());
  60. std::move(callback).Run(supported_formats_.value());
  61. }
  62. } // namespace shape_detection