| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/options/media_devices_selection_handler.h" | 5 #include "chrome/browser/ui/webui/settings/settings_media_devices_selection_hand
ler.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/grit/generated_resources.h" | 13 #include "chrome/grit/generated_resources.h" |
| 14 #include "components/prefs/pref_service.h" | 14 #include "components/prefs/pref_service.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 const char kAudio[] = "mic"; | 18 const char kAudio[] = "mic"; |
| 19 const char kVideo[] = "camera"; | 19 const char kVideo[] = "camera"; |
| 20 | 20 |
| 21 } // namespace | 21 } // namespace |
| 22 | 22 |
| 23 namespace options { | 23 namespace settings { |
| 24 | 24 |
| 25 MediaDevicesSelectionHandler::MediaDevicesSelectionHandler() {} | 25 MediaDevicesSelectionHandler::MediaDevicesSelectionHandler(Profile* profile) |
| 26 : profile_(profile), observer_(this) { |
| 27 } |
| 26 | 28 |
| 27 MediaDevicesSelectionHandler::~MediaDevicesSelectionHandler() { | 29 MediaDevicesSelectionHandler::~MediaDevicesSelectionHandler() { |
| 28 MediaCaptureDevicesDispatcher::GetInstance()->RemoveObserver(this); | |
| 29 } | 30 } |
| 30 | 31 |
| 31 void MediaDevicesSelectionHandler::GetLocalizedValues( | 32 void MediaDevicesSelectionHandler::OnJavascriptAllowed() { |
| 32 base::DictionaryValue* values) { | 33 // Register to the device observer list to get up-to-date device lists. |
| 33 DCHECK(values); | 34 observer_.Add(MediaCaptureDevicesDispatcher::GetInstance()); |
| 34 | |
| 35 static OptionsStringResource resources[] = { | |
| 36 { "mediaSelectMicLabel", IDS_MEDIA_SELECTED_MIC_LABEL }, | |
| 37 { "mediaSelectCameraLabel", IDS_MEDIA_SELECTED_CAMERA_LABEL }, | |
| 38 }; | |
| 39 | |
| 40 RegisterStrings(values, resources, arraysize(resources)); | |
| 41 } | 35 } |
| 42 | 36 |
| 43 void MediaDevicesSelectionHandler::InitializePage() { | 37 void MediaDevicesSelectionHandler::OnJavascriptDisallowed() { |
| 44 // Register to the device observer list to get up-to-date device lists. | 38 observer_.RemoveAll(); |
| 45 MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this); | |
| 46 | |
| 47 // Update the device selection menus. | |
| 48 UpdateDevicesMenuForType(AUDIO); | |
| 49 UpdateDevicesMenuForType(VIDEO); | |
| 50 } | 39 } |
| 51 | 40 |
| 52 void MediaDevicesSelectionHandler::RegisterMessages() { | 41 void MediaDevicesSelectionHandler::RegisterMessages() { |
| 42 web_ui()->RegisterMessageCallback("getDefaultCaptureDevices", |
| 43 base::Bind(&MediaDevicesSelectionHandler::GetDefaultCaptureDevices, |
| 44 base::Unretained(this))); |
| 53 web_ui()->RegisterMessageCallback("setDefaultCaptureDevice", | 45 web_ui()->RegisterMessageCallback("setDefaultCaptureDevice", |
| 54 base::Bind(&MediaDevicesSelectionHandler::SetDefaultCaptureDevice, | 46 base::Bind(&MediaDevicesSelectionHandler::SetDefaultCaptureDevice, |
| 55 base::Unretained(this))); | 47 base::Unretained(this))); |
| 56 } | 48 } |
| 57 | 49 |
| 58 void MediaDevicesSelectionHandler::OnUpdateAudioDevices( | 50 void MediaDevicesSelectionHandler::OnUpdateAudioDevices( |
| 59 const content::MediaStreamDevices& devices) { | 51 const content::MediaStreamDevices& devices) { |
| 60 UpdateDevicesMenu(AUDIO, devices); | 52 UpdateDevicesMenu(AUDIO, devices); |
| 61 } | 53 } |
| 62 | 54 |
| 63 void MediaDevicesSelectionHandler::OnUpdateVideoDevices( | 55 void MediaDevicesSelectionHandler::OnUpdateVideoDevices( |
| 64 const content::MediaStreamDevices& devices) { | 56 const content::MediaStreamDevices& devices) { |
| 65 UpdateDevicesMenu(VIDEO, devices); | 57 UpdateDevicesMenu(VIDEO, devices); |
| 66 } | 58 } |
| 67 | 59 |
| 60 void MediaDevicesSelectionHandler::GetDefaultCaptureDevices( |
| 61 const base::ListValue* args) { |
| 62 DCHECK_EQ(1U, args->GetSize()); |
| 63 std::string type; |
| 64 if (!args->GetString(0, &type)) { |
| 65 NOTREACHED(); |
| 66 return; |
| 67 } |
| 68 DCHECK(!type.empty()); |
| 69 |
| 70 if (type == kAudio) |
| 71 UpdateDevicesMenuForType(AUDIO); |
| 72 else if (type == kVideo) |
| 73 UpdateDevicesMenuForType(VIDEO); |
| 74 } |
| 75 |
| 68 void MediaDevicesSelectionHandler::SetDefaultCaptureDevice( | 76 void MediaDevicesSelectionHandler::SetDefaultCaptureDevice( |
| 69 const base::ListValue* args) { | 77 const base::ListValue* args) { |
| 70 DCHECK_EQ(2U, args->GetSize()); | 78 DCHECK_EQ(2U, args->GetSize()); |
| 71 std::string type, device; | 79 std::string type, device; |
| 72 if (!(args->GetString(0, &type) && args->GetString(1, &device))) { | 80 if (!(args->GetString(0, &type) && args->GetString(1, &device))) { |
| 73 NOTREACHED(); | 81 NOTREACHED(); |
| 74 return; | 82 return; |
| 75 } | 83 } |
| 76 | 84 |
| 77 DCHECK(!type.empty()); | 85 DCHECK(!type.empty()); |
| 78 DCHECK(!device.empty()); | 86 DCHECK(!device.empty()); |
| 79 | 87 |
| 80 Profile* profile = Profile::FromWebUI(web_ui()); | 88 PrefService* prefs = profile_->GetPrefs(); |
| 81 PrefService* prefs = profile->GetPrefs(); | |
| 82 if (type == kAudio) | 89 if (type == kAudio) |
| 83 prefs->SetString(prefs::kDefaultAudioCaptureDevice, device); | 90 prefs->SetString(prefs::kDefaultAudioCaptureDevice, device); |
| 84 else if (type == kVideo) | 91 else if (type == kVideo) |
| 85 prefs->SetString(prefs::kDefaultVideoCaptureDevice, device); | 92 prefs->SetString(prefs::kDefaultVideoCaptureDevice, device); |
| 86 else | 93 else |
| 87 NOTREACHED(); | 94 NOTREACHED(); |
| 88 } | 95 } |
| 89 | 96 |
| 90 void MediaDevicesSelectionHandler::UpdateDevicesMenu( | 97 void MediaDevicesSelectionHandler::UpdateDevicesMenu( |
| 91 DeviceType type, const content::MediaStreamDevices& devices) { | 98 DeviceType type, const content::MediaStreamDevices& devices) { |
| 99 AllowJavascript(); |
| 100 |
| 92 // Get the default device unique id from prefs. | 101 // Get the default device unique id from prefs. |
| 93 Profile* profile = Profile::FromWebUI(web_ui()); | 102 PrefService* prefs = profile_->GetPrefs(); |
| 94 PrefService* prefs = profile->GetPrefs(); | |
| 95 std::string default_device; | 103 std::string default_device; |
| 96 std::string device_type; | 104 std::string device_type; |
| 97 switch (type) { | 105 switch (type) { |
| 98 case AUDIO: | 106 case AUDIO: |
| 99 default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice); | 107 default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice); |
| 100 device_type = kAudio; | 108 device_type = kAudio; |
| 101 break; | 109 break; |
| 102 case VIDEO: | 110 case VIDEO: |
| 103 default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice); | 111 default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice); |
| 104 device_type = kVideo; | 112 device_type = kVideo; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 117 default_id = default_device; | 125 default_id = default_device; |
| 118 } | 126 } |
| 119 | 127 |
| 120 // Use the first device as the default device if the preferred default device | 128 // Use the first device as the default device if the preferred default device |
| 121 // does not exist in the OS. | 129 // does not exist in the OS. |
| 122 if (!devices.empty() && default_id.empty()) | 130 if (!devices.empty() && default_id.empty()) |
| 123 default_id = devices[0].id; | 131 default_id = devices[0].id; |
| 124 | 132 |
| 125 base::StringValue default_value(default_id); | 133 base::StringValue default_value(default_id); |
| 126 base::StringValue type_value(device_type); | 134 base::StringValue type_value(device_type); |
| 127 web_ui()->CallJavascriptFunctionUnsafe("ContentSettings.updateDevicesMenu", | 135 CallJavascriptFunction("cr.webUIListenerCallback", |
| 128 type_value, device_list, | 136 base::StringValue("updateDevicesMenu"), |
| 129 default_value); | 137 type_value, |
| 138 device_list, |
| 139 default_value); |
| 130 } | 140 } |
| 131 | 141 |
| 132 void MediaDevicesSelectionHandler::UpdateDevicesMenuForType(DeviceType type) { | 142 void MediaDevicesSelectionHandler::UpdateDevicesMenuForType(DeviceType type) { |
| 133 content::MediaStreamDevices devices; | 143 content::MediaStreamDevices devices; |
| 134 switch (type) { | 144 switch (type) { |
| 135 case AUDIO: | 145 case AUDIO: |
| 136 devices = MediaCaptureDevicesDispatcher::GetInstance()-> | 146 devices = MediaCaptureDevicesDispatcher::GetInstance()-> |
| 137 GetAudioCaptureDevices(); | 147 GetAudioCaptureDevices(); |
| 138 break; | 148 break; |
| 139 case VIDEO: | 149 case VIDEO: |
| 140 devices = MediaCaptureDevicesDispatcher::GetInstance()-> | 150 devices = MediaCaptureDevicesDispatcher::GetInstance()-> |
| 141 GetVideoCaptureDevices(); | 151 GetVideoCaptureDevices(); |
| 142 break; | 152 break; |
| 143 } | 153 } |
| 144 | 154 |
| 145 UpdateDevicesMenu(type, devices); | 155 UpdateDevicesMenu(type, devices); |
| 146 } | 156 } |
| 147 | 157 |
| 148 } // namespace options | 158 } // namespace settings |
| OLD | NEW |