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
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117

content / browser / media / capture / screen_capture_kit_fullscreen_module.h [blame]

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

#ifndef CONTENT_BROWSER_MEDIA_CAPTURE_SCREEN_CAPTURE_KIT_FULLSCREEN_MODULE_H_
#define CONTENT_BROWSER_MEDIA_CAPTURE_SCREEN_CAPTURE_KIT_FULLSCREEN_MODULE_H_

#include <CoreGraphics/CoreGraphics.h>
#import <ScreenCaptureKit/ScreenCaptureKit.h>

#include "base/memory/raw_ref.h"
#import "base/task/single_thread_task_runner.h"
#include "base/timer/timer.h"
#include "content/common/content_export.h"

namespace content {

class API_AVAILABLE(macos(12.3))
    CONTENT_EXPORT ScreenCaptureKitResetStreamInterface {
 public:
  // This function is called if the ScreenCaptureKitFullScreenModule detects a
  // fullscreen presentation that corresponds to the originally captured window.
  // The parameter is the fullscreen window. It is also called with the original
  // window as parameter if the fullscreen window is no longer on screen.
  virtual void ResetStreamTo(SCWindow* window) = 0;
};

class API_AVAILABLE(macos(12.3))
    CONTENT_EXPORT ScreenCaptureKitFullscreenModule {
 public:
  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  enum class Mode {
    kUnsupported = 0,
    kPowerPoint = 1,
    kOpenOffice = 2,
    kKeynote = 3,
    kLibreOffice = 4,
    kMaxValue = kLibreOffice,
  };

  using ContentHandler = base::OnceCallback<void(SCShareableContent*)>;
  using GetShareableContentCallback =
      base::RepeatingCallback<void(ContentHandler)>;

  ScreenCaptureKitFullscreenModule(
      scoped_refptr<base::SingleThreadTaskRunner> device_task_runner,
      ScreenCaptureKitResetStreamInterface& reset_stream_interface,
      CGWindowID original_window_id,
      pid_t original_window_pid,
      Mode mode);
  ~ScreenCaptureKitFullscreenModule();

  void Start();
  void Reset();

  bool is_fullscreen_window_active() const { return fullscreen_mode_active_; }
  Mode get_mode() const { return mode_; }

  // Sets a callback to be used whenever the ScreenCaptureKit OS API
  // GetShareableContent is called. Used in tests to enable mocking of this API.
  void set_get_sharable_content_for_test(
      GetShareableContentCallback get_shareable_content) {
    get_shareable_content_for_test_ = get_shareable_content;
  }

 private:
  void CheckForFullscreenPresentation();
  void OnFullscreenShareableContentCreated(SCShareableContent* content);
  void OnExitFullscreenShareableContentCreated(SCShareableContent* content);
  SCWindow* GetFullscreenWindow(SCShareableContent* content,
                                SCWindow* editor_window,
                                int number_of_impress_editor_windows) const;

  const scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_;

  // Interface to the owner of the SCK stream.
  const raw_ref<ScreenCaptureKitResetStreamInterface> reset_stream_interface_;

  // Identifier of the original window that is captured.
  const CGWindowID original_window_id_;
  const pid_t original_window_pid_;

  // The mode, corresponding to what slideshow application we're tracking.
  const Mode mode_;

  // True, if we've detected a fullscreen presentation and requested the stream
  // to be reset to the new window.
  bool fullscreen_mode_active_ = false;

  // Identified of the fullscreen window.
  CGWindowID fullscreen_window_id_ = 0;

  // Callback to mock function that is used in tests to mock the SCK OS API
  // GetShareableContent.
  GetShareableContentCallback get_shareable_content_for_test_;

  base::RepeatingTimer timer_;

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

// Creates and returns a ScreenCaptureKitFullscreenModule if `original_window`
// corresponds to a supported slideshow application. `reset_stream_interface` is
// a reference to the object that owns the SCK stream. Upon detection of a
// fullscreen slideshow, the reset function will be called.
// `reset_stream_interface` must outlive the returned fullscreen module.
// `device_task_runner` specifies the task runner where all operations are run.
std::unique_ptr<ScreenCaptureKitFullscreenModule> CONTENT_EXPORT
MaybeCreateScreenCaptureKitFullscreenModule(
    scoped_refptr<base::SingleThreadTaskRunner> device_task_runner,
    ScreenCaptureKitResetStreamInterface& reset_stream_interface,
    SCWindow* original_window) API_AVAILABLE(macos(12.3));

}  // namespace content

#endif  // CONTENT_BROWSER_MEDIA_CAPTURE_SCREEN_CAPTURE_KIT_FULLSCREEN_MODULE_H_