1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66

ash / webui / camera_app_ui / document_scanner_service_host.h [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_WEBUI_CAMERA_APP_UI_DOCUMENT_SCANNER_SERVICE_HOST_H_
#define ASH_WEBUI_CAMERA_APP_UI_DOCUMENT_SCANNER_SERVICE_HOST_H_

#include "ash/webui/camera_app_ui/document_scanner_service_client.h"
#include "base/memory/singleton.h"
#include "base/sequence_checker.h"
#include "chromeos/ash/components/mojo_service_manager/mojom/mojo_service_manager.mojom.h"
#include "media/capture/video/chromeos/mojom/document_scanner.mojom.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"

namespace ash {

// A host for ChromeOS VCD to request document scanning service to detect
// corners.
class DocumentScannerServiceHost
    : public cros::mojom::CrosDocumentScanner,
      public chromeos::mojo_service_manager::mojom::ServiceProvider {
 public:
  DocumentScannerServiceHost(const DocumentScannerServiceHost&) = delete;
  DocumentScannerServiceHost& operator=(const DocumentScannerServiceHost&) =
      delete;

  // TODO(b/333927344): Find an owner to avoid Singleton pattern.
  // MediaStreamManager is not a good candidate since it initializes before ML
  // service.
  static DocumentScannerServiceHost* GetInstance();

  void Start();

  void DetectCornersFromNV12Image(
      base::ReadOnlySharedMemoryRegion nv12_image,
      DetectCornersFromNV12ImageCallback callback) override;

  // chromeos::mojo_service_manager::mojom::ServiceProvider overrides.
  void Request(
      chromeos::mojo_service_manager::mojom::ProcessIdentityPtr identity,
      mojo::ScopedMessagePipeHandle receiver) override;

 private:
  friend struct base::DefaultSingletonTraits<DocumentScannerServiceHost>;

  DocumentScannerServiceHost();

  ~DocumentScannerServiceHost() override;

  std::unique_ptr<DocumentScannerServiceClient> document_scanner_service_;

  mojo::ReceiverSet<cros::mojom::CrosDocumentScanner> receiver_set_;

  // Receiver for mojo service manager service provider.
  mojo::Receiver<chromeos::mojo_service_manager::mojom::ServiceProvider>
      provider_receiver_{this};

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<DocumentScannerServiceHost> weak_factory_{this};
};

}  // namespace ash

#endif  // ASH_WEBUI_CAMERA_APP_UI_DOCUMENT_SCANNER_SERVICE_HOST_H_