Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / media_devices_selection_handler.cc
blob3d37cc64d6f9aa050201c5e0399564f3ac68b129
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/ui/webui/options/media_devices_selection_handler.h"
7 #include "base/bind.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/common/pref_names.h"
11 #include "chrome/grit/generated_resources.h"
13 namespace {
15 const char kAudio[] = "mic";
16 const char kVideo[] = "camera";
18 } // namespace
20 namespace options {
22 MediaDevicesSelectionHandler::MediaDevicesSelectionHandler() {}
24 MediaDevicesSelectionHandler::~MediaDevicesSelectionHandler() {
25 MediaCaptureDevicesDispatcher::GetInstance()->RemoveObserver(this);
28 void MediaDevicesSelectionHandler::GetLocalizedValues(
29 base::DictionaryValue* values) {
30 DCHECK(values);
32 static OptionsStringResource resources[] = {
33 { "mediaSelectMicLabel", IDS_MEDIA_SELECTED_MIC_LABEL },
34 { "mediaSelectCameraLabel", IDS_MEDIA_SELECTED_CAMERA_LABEL },
37 RegisterStrings(values, resources, arraysize(resources));
40 void MediaDevicesSelectionHandler::InitializePage() {
41 // Register to the device observer list to get up-to-date device lists.
42 MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this);
44 // Update the device selection menus.
45 UpdateDevicesMenuForType(AUDIO);
46 UpdateDevicesMenuForType(VIDEO);
49 void MediaDevicesSelectionHandler::RegisterMessages() {
50 web_ui()->RegisterMessageCallback("setDefaultCaptureDevice",
51 base::Bind(&MediaDevicesSelectionHandler::SetDefaultCaptureDevice,
52 base::Unretained(this)));
55 void MediaDevicesSelectionHandler::OnUpdateAudioDevices(
56 const content::MediaStreamDevices& devices) {
57 UpdateDevicesMenu(AUDIO, devices);
60 void MediaDevicesSelectionHandler::OnUpdateVideoDevices(
61 const content::MediaStreamDevices& devices) {
62 UpdateDevicesMenu(VIDEO, devices);
65 void MediaDevicesSelectionHandler::SetDefaultCaptureDevice(
66 const base::ListValue* args) {
67 DCHECK_EQ(2U, args->GetSize());
68 std::string type, device;
69 if (!(args->GetString(0, &type) && args->GetString(1, &device))) {
70 NOTREACHED();
71 return;
74 DCHECK(!type.empty());
75 DCHECK(!device.empty());
77 Profile* profile = Profile::FromWebUI(web_ui());
78 PrefService* prefs = profile->GetPrefs();
79 if (type == kAudio)
80 prefs->SetString(prefs::kDefaultAudioCaptureDevice, device);
81 else if (type == kVideo)
82 prefs->SetString(prefs::kDefaultVideoCaptureDevice, device);
83 else
84 NOTREACHED();
87 void MediaDevicesSelectionHandler::UpdateDevicesMenu(
88 DeviceType type, const content::MediaStreamDevices& devices) {
89 // Get the default device unique id from prefs.
90 Profile* profile = Profile::FromWebUI(web_ui());
91 PrefService* prefs = profile->GetPrefs();
92 std::string default_device;
93 std::string device_type;
94 switch (type) {
95 case AUDIO:
96 default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice);
97 device_type = kAudio;
98 break;
99 case VIDEO:
100 default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice);
101 device_type = kVideo;
102 break;
105 // Build the list of devices to send to JS.
106 std::string default_id;
107 base::ListValue device_list;
108 for (size_t i = 0; i < devices.size(); ++i) {
109 base::DictionaryValue* entry = new base::DictionaryValue();
110 entry->SetString("name", devices[i].name);
111 entry->SetString("id", devices[i].id);
112 device_list.Append(entry);
113 if (devices[i].id == default_device)
114 default_id = default_device;
117 // Use the first device as the default device if the preferred default device
118 // does not exist in the OS.
119 if (!devices.empty() && default_id.empty())
120 default_id = devices[0].id;
122 base::StringValue default_value(default_id);
123 base::StringValue type_value(device_type);
124 web_ui()->CallJavascriptFunction("ContentSettings.updateDevicesMenu",
125 type_value,
126 device_list,
127 default_value);
130 void MediaDevicesSelectionHandler::UpdateDevicesMenuForType(DeviceType type) {
131 content::MediaStreamDevices devices;
132 switch (type) {
133 case AUDIO:
134 devices = MediaCaptureDevicesDispatcher::GetInstance()->
135 GetAudioCaptureDevices();
136 break;
137 case VIDEO:
138 devices = MediaCaptureDevicesDispatcher::GetInstance()->
139 GetVideoCaptureDevices();
140 break;
143 UpdateDevicesMenu(type, devices);
146 } // namespace options