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

#ifndef REMOTING_PROTOCOL_DESKTOP_CAPTURER_PROXY_H_
#define REMOTING_PROTOCOL_DESKTOP_CAPTURER_PROXY_H_

#include <cstdint>
#include <memory>

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "base/threading/thread_checker.h"
#include "remoting/protocol/desktop_capturer.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
#include "third_party/webrtc/modules/desktop_capture/shared_memory.h"

#if defined(WEBRTC_USE_GIO)
#include "base/functional/callback.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_capture_metadata.h"
#endif

namespace remoting {

// DesktopCapturerProxy is responsible for calling remoting::DesktopCapturer on
// the capturer thread and then returning results to the caller's thread.
// GetSourceList() is not implemented by this class, it always returns false.
class DesktopCapturerProxy : public DesktopCapturer {
 public:
  explicit DesktopCapturerProxy(
      scoped_refptr<base::SequencedTaskRunner> capture_task_runner);

  DesktopCapturerProxy(const DesktopCapturerProxy&) = delete;
  DesktopCapturerProxy& operator=(const DesktopCapturerProxy&) = delete;

  ~DesktopCapturerProxy() override;

  // CreateCapturer() should be used if the capturer needs to be created on the
  // capturer thread. Otherwise, the capturer can be passed to set_capturer().
  void CreateCapturer(
      base::OnceCallback<std::unique_ptr<DesktopCapturer>()> creator);
  base::WeakPtr<DesktopCapturerProxy> GetWeakPtr();
  void set_capturer(std::unique_ptr<DesktopCapturer> capturer);

  // Returns a callback that can be used to safely set the capturer from any
  // thread.
  base::OnceCallback<void(std::unique_ptr<DesktopCapturer>)>
  GetSetCapturerCallback();

  // DesktopCapturer interface.
  void Start(Callback* callback) override;
  void SetSharedMemoryFactory(std::unique_ptr<webrtc::SharedMemoryFactory>
                                  shared_memory_factory) override;
  void CaptureFrame() override;
  bool GetSourceList(SourceList* sources) override;
  bool SelectSource(SourceId id) override;
  void SetMaxFrameRate(std::uint32_t max_frame_rate) override;
  void Pause(bool pause) override;
  void BoostCaptureRate(base::TimeDelta capture_interval,
                        base::TimeDelta duration) override;

#if defined(WEBRTC_USE_GIO)
  void GetMetadataAsync(base::OnceCallback<void(webrtc::DesktopCaptureMetadata)>
                            callback) override;
#endif

 private:
  class Core;

  void OnFrameCaptureStarting();
  void OnFrameCaptured(webrtc::DesktopCapturer::Result result,
                       std::unique_ptr<webrtc::DesktopFrame> frame);

#if defined(WEBRTC_USE_GIO)
  void OnMetadata(
      base::OnceCallback<void(webrtc::DesktopCaptureMetadata)> callback,
      webrtc::DesktopCaptureMetadata metadata);
#endif

  std::unique_ptr<Core> core_;
  scoped_refptr<base::SequencedTaskRunner> capture_task_runner_;

  raw_ptr<webrtc::DesktopCapturer::Callback> callback_ = nullptr;

  THREAD_CHECKER(thread_checker_);

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

}  // namespace remoting

#endif  // REMOTING_PROTOCOL_DESKTOP_CAPTURER_PROXY_H_
