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"
14 const char kAudio[] = "mic";
15 const char kVideo[] = "camera";
21 MediaDevicesSelectionHandler::MediaDevicesSelectionHandler() {}
23 MediaDevicesSelectionHandler::~MediaDevicesSelectionHandler() {
24 MediaCaptureDevicesDispatcher::GetInstance()->RemoveObserver(this);
27 void MediaDevicesSelectionHandler::GetLocalizedValues(DictionaryValue* values) {
30 static OptionsStringResource resources[] = {
31 { "mediaSelectMicLabel", IDS_MEDIA_SELECTED_MIC_LABEL },
32 { "mediaSelectCameraLabel", IDS_MEDIA_SELECTED_CAMERA_LABEL },
35 RegisterStrings(values, resources, arraysize(resources));
38 void MediaDevicesSelectionHandler::InitializePage() {
39 // Register to the device observer list to get up-to-date device lists.
40 MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this);
42 // Update the device selection menus.
43 UpdateDevicesMenuForType(AUDIO);
44 UpdateDevicesMenuForType(VIDEO);
47 void MediaDevicesSelectionHandler::RegisterMessages() {
48 web_ui()->RegisterMessageCallback("setDefaultCaptureDevice",
49 base::Bind(&MediaDevicesSelectionHandler::SetDefaultCaptureDevice,
50 base::Unretained(this)));
53 void MediaDevicesSelectionHandler::OnUpdateAudioDevices(
54 const content::MediaStreamDevices& devices) {
55 UpdateDevicesMenu(AUDIO, devices);
58 void MediaDevicesSelectionHandler::OnUpdateVideoDevices(
59 const content::MediaStreamDevices& devices) {
60 UpdateDevicesMenu(VIDEO, devices);
63 void MediaDevicesSelectionHandler::SetDefaultCaptureDevice(
64 const ListValue* args) {
65 DCHECK_EQ(2U, args->GetSize());
66 std::string type, device;
67 if (!(args->GetString(0, &type) && args->GetString(1, &device))) {
72 DCHECK(!type.empty());
73 DCHECK(!device.empty());
75 Profile* profile = Profile::FromWebUI(web_ui());
76 PrefService* prefs = profile->GetPrefs();
78 prefs->SetString(prefs::kDefaultAudioCaptureDevice, device);
79 else if (type == kVideo)
80 prefs->SetString(prefs::kDefaultVideoCaptureDevice, device);
85 void MediaDevicesSelectionHandler::UpdateDevicesMenu(
86 DeviceType type, const content::MediaStreamDevices& devices) {
87 // Get the default device unique id from prefs.
88 Profile* profile = Profile::FromWebUI(web_ui());
89 PrefService* prefs = profile->GetPrefs();
90 std::string default_device;
91 std::string device_type;
94 default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice);
98 default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice);
103 // Build the list of devices to send to JS.
104 std::string default_id;
105 ListValue device_list;
106 for (size_t i = 0; i < devices.size(); ++i) {
107 DictionaryValue* entry = new DictionaryValue();
108 entry->SetString("name", devices[i].name);
109 entry->SetString("id", devices[i].id);
110 device_list.Append(entry);
111 if (devices[i].id == default_device)
112 default_id = default_device;
115 // Use the first device as the default device if the preferred default device
116 // does not exist in the OS.
117 if (!devices.empty() && default_id.empty())
118 default_id = devices[0].id;
120 StringValue default_value(default_id);
121 StringValue type_value(device_type);
122 web_ui()->CallJavascriptFunction("ContentSettings.updateDevicesMenu",
128 void MediaDevicesSelectionHandler::UpdateDevicesMenuForType(DeviceType type) {
129 content::MediaStreamDevices devices;
132 devices = MediaCaptureDevicesDispatcher::GetInstance()->
133 GetAudioCaptureDevices();
136 devices = MediaCaptureDevicesDispatcher::GetInstance()->
137 GetVideoCaptureDevices();
141 UpdateDevicesMenu(type, devices);
144 } // namespace options