summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/ui/webui/settings/settings_media_devices_selection_handler.cc
blob: 42d27c5dd1983c3592f33d30a5fd24f65609608e (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/settings/settings_media_devices_selection_handler.h"

#include <stddef.h>

#include <memory>
#include <utility>

#include "base/bind.h"
#include "base/macros.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "extensions/strings/grit/extensions_strings.h"
#include "ui/base/l10n/l10n_util.h"
#endif

namespace {

const char kAudio[] = "mic";
const char kVideo[] = "camera";

}  // namespace

namespace settings {

MediaDevicesSelectionHandler::MediaDevicesSelectionHandler(Profile* profile)
    : profile_(profile), observer_(this) {
}

MediaDevicesSelectionHandler::~MediaDevicesSelectionHandler() {
}

void MediaDevicesSelectionHandler::OnJavascriptAllowed() {
  // Register to the device observer list to get up-to-date device lists.
  observer_.Add(MediaCaptureDevicesDispatcher::GetInstance());
}

void MediaDevicesSelectionHandler::OnJavascriptDisallowed() {
  observer_.RemoveAll();
}

void MediaDevicesSelectionHandler::RegisterMessages() {
  web_ui()->RegisterMessageCallback(
      "getDefaultCaptureDevices",
      base::BindRepeating(
          &MediaDevicesSelectionHandler::GetDefaultCaptureDevices,
          base::Unretained(this)));
  web_ui()->RegisterMessageCallback(
      "setDefaultCaptureDevice",
      base::BindRepeating(
          &MediaDevicesSelectionHandler::SetDefaultCaptureDevice,
          base::Unretained(this)));
}

void MediaDevicesSelectionHandler::OnUpdateAudioDevices(
    const blink::MediaStreamDevices& devices) {
  UpdateDevicesMenu(AUDIO, devices);
}

void MediaDevicesSelectionHandler::OnUpdateVideoDevices(
    const blink::MediaStreamDevices& devices) {
  UpdateDevicesMenu(VIDEO, devices);
}

void MediaDevicesSelectionHandler::GetDefaultCaptureDevices(
    const base::ListValue* args) {
  DCHECK_EQ(1U, args->GetSize());
  std::string type;
  if (!args->GetString(0, &type)) {
    NOTREACHED();
    return;
  }
  DCHECK(!type.empty());

  if (type == kAudio)
    UpdateDevicesMenuForType(AUDIO);
  else if (type == kVideo)
    UpdateDevicesMenuForType(VIDEO);
}

void MediaDevicesSelectionHandler::SetDefaultCaptureDevice(
    const base::ListValue* args) {
  DCHECK_EQ(2U, args->GetSize());
  std::string type, device;
  if (!(args->GetString(0, &type) && args->GetString(1, &device))) {
    NOTREACHED();
    return;
  }

  DCHECK(!type.empty());
  DCHECK(!device.empty());

  PrefService* prefs = profile_->GetPrefs();
  if (type == kAudio)
    prefs->SetString(prefs::kDefaultAudioCaptureDevice, device);
  else if (type == kVideo)
    prefs->SetString(prefs::kDefaultVideoCaptureDevice, device);
  else
    NOTREACHED();
}

void MediaDevicesSelectionHandler::UpdateDevicesMenu(
    DeviceType type,
    const blink::MediaStreamDevices& devices) {
  AllowJavascript();

  // Get the default device unique id from prefs.
  PrefService* prefs = profile_->GetPrefs();
  std::string default_device;
  std::string device_type;
  switch (type) {
    case AUDIO:
      default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice);
      device_type = kAudio;
      break;
    case VIDEO:
      default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice);
      device_type = kVideo;
      break;
  }

  // Build the list of devices to send to JS.
  std::string default_id;
  base::ListValue device_list;
  for (size_t i = 0; i < devices.size(); ++i) {
    std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue());
    entry->SetString("name", GetDeviceDisplayName(devices[i]));
    entry->SetString("id",  devices[i].id);
    device_list.Append(std::move(entry));
    if (devices[i].id == default_device)
      default_id = default_device;
  }

  // Use the first device as the default device if the preferred default device
  // does not exist in the OS.
  if (!devices.empty() && default_id.empty())
    default_id = devices[0].id;

  base::Value default_value(default_id);
  base::Value type_value(device_type);
  FireWebUIListener("updateDevicesMenu", type_value, device_list,
                    default_value);
}

std::string MediaDevicesSelectionHandler::GetDeviceDisplayName(
    const blink::MediaStreamDevice& device) const {
  std::string facing_info;

#if BUILDFLAG(ENABLE_EXTENSIONS)
  switch (device.video_facing) {
    case media::VideoFacingMode::MEDIA_VIDEO_FACING_USER:
      facing_info = l10n_util::GetStringUTF8(IDS_CAMERA_FACING_USER);
      break;
    case media::VideoFacingMode::MEDIA_VIDEO_FACING_ENVIRONMENT:
      facing_info = l10n_util::GetStringUTF8(IDS_CAMERA_FACING_ENVIRONMENT);
      break;
    case media::VideoFacingMode::MEDIA_VIDEO_FACING_NONE:
      break;
    case media::VideoFacingMode::NUM_MEDIA_VIDEO_FACING_MODES:
      NOTREACHED();
      break;
  }
#endif

  if (facing_info.empty())
    return device.name;
  return device.name + " " + facing_info;
}

void MediaDevicesSelectionHandler::UpdateDevicesMenuForType(DeviceType type) {
  blink::MediaStreamDevices devices;
  switch (type) {
    case AUDIO:
      devices = MediaCaptureDevicesDispatcher::GetInstance()->
          GetAudioCaptureDevices();
      break;
    case VIDEO:
      devices = MediaCaptureDevicesDispatcher::GetInstance()->
          GetVideoCaptureDevices();
      break;
  }

  UpdateDevicesMenu(type, devices);
}

}  // namespace settings