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
|
// Copyright (c) 2012 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/options/media_devices_selection_handler.h"
#include "base/bind.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
namespace {
const char kAudio[] = "mic";
const char kVideo[] = "camera";
} // namespace
namespace options {
MediaDevicesSelectionHandler::MediaDevicesSelectionHandler() {}
MediaDevicesSelectionHandler::~MediaDevicesSelectionHandler() {
MediaCaptureDevicesDispatcher::GetInstance()->RemoveObserver(this);
}
void MediaDevicesSelectionHandler::GetLocalizedValues(
base::DictionaryValue* values) {
DCHECK(values);
static OptionsStringResource resources[] = {
{ "mediaSelectMicLabel", IDS_MEDIA_SELECTED_MIC_LABEL },
{ "mediaSelectCameraLabel", IDS_MEDIA_SELECTED_CAMERA_LABEL },
};
RegisterStrings(values, resources, arraysize(resources));
}
void MediaDevicesSelectionHandler::InitializePage() {
// Register to the device observer list to get up-to-date device lists.
MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this);
// Update the device selection menus.
UpdateDevicesMenuForType(AUDIO);
UpdateDevicesMenuForType(VIDEO);
}
void MediaDevicesSelectionHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("setDefaultCaptureDevice",
base::Bind(&MediaDevicesSelectionHandler::SetDefaultCaptureDevice,
base::Unretained(this)));
}
void MediaDevicesSelectionHandler::OnUpdateAudioDevices(
const content::MediaStreamDevices& devices) {
UpdateDevicesMenu(AUDIO, devices);
}
void MediaDevicesSelectionHandler::OnUpdateVideoDevices(
const content::MediaStreamDevices& devices) {
UpdateDevicesMenu(VIDEO, devices);
}
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());
Profile* profile = Profile::FromWebUI(web_ui());
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 content::MediaStreamDevices& devices) {
// Get the default device unique id from prefs.
Profile* profile = Profile::FromWebUI(web_ui());
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) {
base::DictionaryValue* entry = new base::DictionaryValue();
entry->SetString("name", devices[i].name);
entry->SetString("id", devices[i].id);
device_list.Append(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::StringValue default_value(default_id);
base::StringValue type_value(device_type);
web_ui()->CallJavascriptFunction("ContentSettings.updateDevicesMenu",
type_value,
device_list,
default_value);
}
void MediaDevicesSelectionHandler::UpdateDevicesMenuForType(DeviceType type) {
content::MediaStreamDevices devices;
switch (type) {
case AUDIO:
devices = MediaCaptureDevicesDispatcher::GetInstance()->
GetAudioCaptureDevices();
break;
case VIDEO:
devices = MediaCaptureDevicesDispatcher::GetInstance()->
GetVideoCaptureDevices();
break;
}
UpdateDevicesMenu(type, devices);
}
} // namespace options
|