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"
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"
15 const char kAudio
[] = "mic";
16 const char kVideo
[] = "camera";
22 MediaDevicesSelectionHandler::MediaDevicesSelectionHandler() {}
24 MediaDevicesSelectionHandler::~MediaDevicesSelectionHandler() {
25 MediaCaptureDevicesDispatcher::GetInstance()->RemoveObserver(this);
28 void MediaDevicesSelectionHandler::GetLocalizedValues(
29 base::DictionaryValue
* 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
))) {
74 DCHECK(!type
.empty());
75 DCHECK(!device
.empty());
77 Profile
* profile
= Profile::FromWebUI(web_ui());
78 PrefService
* prefs
= profile
->GetPrefs();
80 prefs
->SetString(prefs::kDefaultAudioCaptureDevice
, device
);
81 else if (type
== kVideo
)
82 prefs
->SetString(prefs::kDefaultVideoCaptureDevice
, device
);
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
;
96 default_device
= prefs
->GetString(prefs::kDefaultAudioCaptureDevice
);
100 default_device
= prefs
->GetString(prefs::kDefaultVideoCaptureDevice
);
101 device_type
= kVideo
;
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",
130 void MediaDevicesSelectionHandler::UpdateDevicesMenuForType(DeviceType type
) {
131 content::MediaStreamDevices devices
;
134 devices
= MediaCaptureDevicesDispatcher::GetInstance()->
135 GetAudioCaptureDevices();
138 devices
= MediaCaptureDevicesDispatcher::GetInstance()->
139 GetVideoCaptureDevices();
143 UpdateDevicesMenu(type
, devices
);
146 } // namespace options