File: desktop_media_picker_controller.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (107 lines) | stat: -rw-r--r-- 4,928 bytes parent folder | download | duplicates (3)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_MEDIA_WEBRTC_DESKTOP_MEDIA_PICKER_CONTROLLER_H_
#define CHROME_BROWSER_MEDIA_WEBRTC_DESKTOP_MEDIA_PICKER_CONTROLLER_H_

#include <memory>
#include <string>
#include <utility>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/types/expected.h"
#include "chrome/browser/media/webrtc/desktop_media_list.h"
#include "chrome/browser/media/webrtc/desktop_media_picker.h"
#include "content/public/browser/desktop_media_id.h"
#include "content/public/browser/web_contents_observer.h"
#include "third_party/blink/public/mojom/mediastream/media_stream.mojom.h"
#include "ui/base/ui_base_types.h"

class DesktopMediaList;
class DesktopMediaPickerFactory;

// The main entry point for the desktop picker dialog box, which prompts the
// user to select a desktop or an application window whose content will be made
// available as a video stream.
//
// TODO(crbug.com/40637301): Rename this class.  Consider merging with
// DesktopMediaPickerImpl and naming the merged class just DesktopMediaPicker.
class DesktopMediaPickerController : private content::WebContentsObserver {
 public:
  using Params = DesktopMediaPicker::Params;

  // Callback for desktop selection results.  There are three possible cases:
  //
  // - If |err| is non-empty, it contains an error message regarding why the
  //   dialog could not be displayed, and the value of |id| should not be used.
  //
  // - If |err| is empty and id.is_null() is true, the user canceled the dialog.
  //
  // - Otherwise, |id| represents the user's selection.
  using DoneCallback = base::OnceCallback<void(const std::string& err,
                                               content::DesktopMediaID id)>;

  explicit DesktopMediaPickerController(
      DesktopMediaPickerFactory* picker_factory = nullptr);
  DesktopMediaPickerController(const DesktopMediaPickerController&) = delete;
  DesktopMediaPickerController& operator=(const DesktopMediaPickerController&) =
      delete;
  ~DesktopMediaPickerController() override;

  // Checks if system audio capture is supported on the current platform.
  static bool IsSystemAudioCaptureSupported(
      Params::RequestSource request_source);

  // Show the desktop picker dialog using the parameters specified by |params|,
  // with the possible selections restricted to those included in |sources|.  If
  // an error is detected synchronously, it is reported by returning an error
  // string.  Otherwise, the return value is nullopt, and the closure passed as
  // |done_callback| is called when the dialog is closed.  If the dialog is
  // canceled, the argument to |done_callback| will be an instance of
  // DesktopMediaID whose is_null() method returns true.
  //
  // As a special case, if |params.select_only_screen| is true, and the only
  // selection type is TYPE_SCREEN, and there is only one screen,
  // |done_callback| is called immediately with the screen's ID, and the dialog
  // is not shown.  This option must be used with care, because even when the
  // dialog has only one option to select, the dialog itself helps prevent the
  // user for accidentally sharing their screen and gives them the option to
  // prevent their screen from being shared.
  //
  // |includable_web_contents_filter| is used to restrict any
  // DesktopMediaList::Type::kWebContents sources. It should return true if a
  // given WebContents is a valid target, or false if it should be excluded.
  //
  // Note that |done_callback| is called only if the dialog completes normally.
  // If an instance of this class is destroyed while the dialog is visible, the
  // dialog will be cleaned up, but |done_callback| will not be invoked.
  void Show(const Params& params,
            const std::vector<DesktopMediaList::Type>& sources,
            DesktopMediaList::WebContentsFilter includable_web_contents_filter,
            DoneCallback done_callback);

  // content::WebContentsObserver overrides.
  void WebContentsDestroyed() override;

 private:
  void OnInitialMediaListFound();
  void ShowPickerDialog();
  // This function is responsible to call |done_callback_| and after running the
  // callback |this| might be destroyed. Do **not** access fields after calling
  // this function.
  void OnPickerDialogResults(
      const std::string& err,
      base::expected<content::DesktopMediaID,
                     blink::mojom::MediaStreamRequestResult> result);

  Params params_;
  DoneCallback done_callback_;
  std::vector<std::unique_ptr<DesktopMediaList>> source_lists_;
  std::unique_ptr<DesktopMediaPicker> picker_;
  raw_ptr<DesktopMediaPickerFactory> picker_factory_;
  base::WeakPtrFactory<DesktopMediaPickerController> weak_factory_{this};
};

#endif  // CHROME_BROWSER_MEDIA_WEBRTC_DESKTOP_MEDIA_PICKER_CONTROLLER_H_