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 
5 #include "chrome/browser/media/webrtc/desktop_media_picker_factory_impl.h"
6 
7 #include "base/no_destructor.h"
8 #include "build/build_config.h"
9 #include "chrome/browser/media/webrtc/desktop_media_list_ash.h"
10 #include "chrome/browser/media/webrtc/native_desktop_media_list.h"
11 #include "chrome/browser/media/webrtc/tab_desktop_media_list.h"
12 #include "content/public/browser/desktop_capture.h"
13 
14 DesktopMediaPickerFactoryImpl::DesktopMediaPickerFactoryImpl() = default;
15 
16 DesktopMediaPickerFactoryImpl::~DesktopMediaPickerFactoryImpl() = default;
17 
18 // static
GetInstance()19 DesktopMediaPickerFactoryImpl* DesktopMediaPickerFactoryImpl::GetInstance() {
20   static base::NoDestructor<DesktopMediaPickerFactoryImpl> impl;
21   return impl.get();
22 }
23 
24 std::unique_ptr<DesktopMediaPicker>
CreatePicker()25 DesktopMediaPickerFactoryImpl::CreatePicker() {
26 // DesktopMediaPicker is implemented only for Windows, OSX and Aura Linux
27 // builds.
28 #if defined(TOOLKIT_VIEWS) || defined(OS_MAC)
29   return DesktopMediaPicker::Create();
30 #else
31   return nullptr;
32 #endif
33 }
34 
35 std::vector<std::unique_ptr<DesktopMediaList>>
CreateMediaList(const std::vector<content::DesktopMediaID::Type> & types)36 DesktopMediaPickerFactoryImpl::CreateMediaList(
37     const std::vector<content::DesktopMediaID::Type>& types) {
38   // Keep same order as the input |sources| and avoid duplicates.
39   std::vector<std::unique_ptr<DesktopMediaList>> source_lists;
40   bool have_screen_list = false;
41   bool have_window_list = false;
42   bool have_tab_list = false;
43   for (auto source_type : types) {
44     switch (source_type) {
45       case content::DesktopMediaID::TYPE_NONE:
46         break;
47       case content::DesktopMediaID::TYPE_SCREEN: {
48         if (have_screen_list)
49           continue;
50         std::unique_ptr<DesktopMediaList> screen_list;
51 #if defined(OS_CHROMEOS)
52         screen_list = std::make_unique<DesktopMediaListAsh>(
53             content::DesktopMediaID::TYPE_SCREEN);
54 #else   // !defined(OS_CHROMEOS)
55         // If screen capture is not supported on the platform, then we should
56         // not attempt to create an instance of NativeDesktopMediaList. Doing so
57         // will hit a DCHECK.
58         std::unique_ptr<webrtc::DesktopCapturer> capturer =
59             content::desktop_capture::CreateScreenCapturer();
60         if (!capturer)
61           continue;
62 
63         screen_list = std::make_unique<NativeDesktopMediaList>(
64             content::DesktopMediaID::TYPE_SCREEN, std::move(capturer));
65 #endif  // !defined(OS_CHROMEOS)
66         have_screen_list = true;
67         source_lists.push_back(std::move(screen_list));
68         break;
69       }
70       case content::DesktopMediaID::TYPE_WINDOW: {
71         if (have_window_list)
72           continue;
73         std::unique_ptr<DesktopMediaList> window_list;
74 #if defined(OS_CHROMEOS)
75         window_list = std::make_unique<DesktopMediaListAsh>(
76             content::DesktopMediaID::TYPE_WINDOW);
77 #else   // !defined(OS_CHROMEOS)
78         // If window capture is not supported on the platform, then we should
79         // not attempt to create an instance of NativeDesktopMediaList. Doing so
80         // will hit a DCHECK.
81         std::unique_ptr<webrtc::DesktopCapturer> capturer =
82             content::desktop_capture::CreateWindowCapturer();
83         if (!capturer)
84           continue;
85         window_list = std::make_unique<NativeDesktopMediaList>(
86             content::DesktopMediaID::TYPE_WINDOW, std::move(capturer));
87 #endif  // !defined(OS_CHROMEOS)
88         have_window_list = true;
89         source_lists.push_back(std::move(window_list));
90         break;
91       }
92       case content::DesktopMediaID::TYPE_WEB_CONTENTS: {
93         if (have_tab_list)
94           continue;
95         std::unique_ptr<DesktopMediaList> tab_list =
96             std::make_unique<TabDesktopMediaList>();
97         have_tab_list = true;
98         source_lists.push_back(std::move(tab_list));
99         break;
100       }
101     }
102   }
103   return source_lists;
104 }
105