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. | |
45 MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this); | |
46 | |
47 // Update the device selection menus. | |
48 UpdateDevicesMenuForType(AUDIO); | |
49 UpdateDevicesMenuForType(VIDEO); | |
50 } | 38 } |
michaelpg
2016/06/08 16:02:32
observer_.RemoveAll();
Finnur
2016/06/08 21:15:45
Makes sense. Done.
| |
51 | 39 |
52 void MediaDevicesSelectionHandler::RegisterMessages() { | 40 void MediaDevicesSelectionHandler::RegisterMessages() { |
41 web_ui()->RegisterMessageCallback("getDefaultCaptureDevices", | |
42 base::Bind(&MediaDevicesSelectionHandler::GetDefaultCaptureDevices, | |
43 base::Unretained(this))); | |
53 web_ui()->RegisterMessageCallback("setDefaultCaptureDevice", | 44 web_ui()->RegisterMessageCallback("setDefaultCaptureDevice", |
54 base::Bind(&MediaDevicesSelectionHandler::SetDefaultCaptureDevice, | 45 base::Bind(&MediaDevicesSelectionHandler::SetDefaultCaptureDevice, |
55 base::Unretained(this))); | 46 base::Unretained(this))); |
56 } | 47 } |
57 | 48 |
58 void MediaDevicesSelectionHandler::OnUpdateAudioDevices( | 49 void MediaDevicesSelectionHandler::OnUpdateAudioDevices( |
59 const content::MediaStreamDevices& devices) { | 50 const content::MediaStreamDevices& devices) { |
60 UpdateDevicesMenu(AUDIO, devices); | 51 UpdateDevicesMenu(AUDIO, devices); |
61 } | 52 } |
62 | 53 |
63 void MediaDevicesSelectionHandler::OnUpdateVideoDevices( | 54 void MediaDevicesSelectionHandler::OnUpdateVideoDevices( |
64 const content::MediaStreamDevices& devices) { | 55 const content::MediaStreamDevices& devices) { |
65 UpdateDevicesMenu(VIDEO, devices); | 56 UpdateDevicesMenu(VIDEO, devices); |
66 } | 57 } |
67 | 58 |
59 void MediaDevicesSelectionHandler::GetDefaultCaptureDevices( | |
60 const base::ListValue* args) { | |
61 DCHECK_EQ(1U, args->GetSize()); | |
62 std::string type; | |
63 if (!args->GetString(0, &type)) { | |
64 NOTREACHED(); | |
65 return; | |
66 } | |
67 DCHECK(!type.empty()); | |
68 | |
69 if (type == kAudio) | |
70 UpdateDevicesMenuForType(AUDIO); | |
71 else if (type == kVideo) | |
72 UpdateDevicesMenuForType(VIDEO); | |
73 } | |
74 | |
68 void MediaDevicesSelectionHandler::SetDefaultCaptureDevice( | 75 void MediaDevicesSelectionHandler::SetDefaultCaptureDevice( |
69 const base::ListValue* args) { | 76 const base::ListValue* args) { |
70 DCHECK_EQ(2U, args->GetSize()); | 77 DCHECK_EQ(2U, args->GetSize()); |
71 std::string type, device; | 78 std::string type, device; |
72 if (!(args->GetString(0, &type) && args->GetString(1, &device))) { | 79 if (!(args->GetString(0, &type) && args->GetString(1, &device))) { |
73 NOTREACHED(); | 80 NOTREACHED(); |
74 return; | 81 return; |
75 } | 82 } |
76 | 83 |
77 DCHECK(!type.empty()); | 84 DCHECK(!type.empty()); |
78 DCHECK(!device.empty()); | 85 DCHECK(!device.empty()); |
79 | 86 |
80 Profile* profile = Profile::FromWebUI(web_ui()); | 87 PrefService* prefs = profile_->GetPrefs(); |
81 PrefService* prefs = profile->GetPrefs(); | |
82 if (type == kAudio) | 88 if (type == kAudio) |
83 prefs->SetString(prefs::kDefaultAudioCaptureDevice, device); | 89 prefs->SetString(prefs::kDefaultAudioCaptureDevice, device); |
84 else if (type == kVideo) | 90 else if (type == kVideo) |
85 prefs->SetString(prefs::kDefaultVideoCaptureDevice, device); | 91 prefs->SetString(prefs::kDefaultVideoCaptureDevice, device); |
86 else | 92 else |
87 NOTREACHED(); | 93 NOTREACHED(); |
88 } | 94 } |
89 | 95 |
90 void MediaDevicesSelectionHandler::UpdateDevicesMenu( | 96 void MediaDevicesSelectionHandler::UpdateDevicesMenu( |
91 DeviceType type, const content::MediaStreamDevices& devices) { | 97 DeviceType type, const content::MediaStreamDevices& devices) { |
98 AllowJavascript(); | |
99 | |
92 // Get the default device unique id from prefs. | 100 // Get the default device unique id from prefs. |
93 Profile* profile = Profile::FromWebUI(web_ui()); | 101 PrefService* prefs = profile_->GetPrefs(); |
94 PrefService* prefs = profile->GetPrefs(); | |
95 std::string default_device; | 102 std::string default_device; |
96 std::string device_type; | 103 std::string device_type; |
97 switch (type) { | 104 switch (type) { |
98 case AUDIO: | 105 case AUDIO: |
99 default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice); | 106 default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice); |
100 device_type = kAudio; | 107 device_type = kAudio; |
101 break; | 108 break; |
102 case VIDEO: | 109 case VIDEO: |
103 default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice); | 110 default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice); |
104 device_type = kVideo; | 111 device_type = kVideo; |
(...skipping 12 matching lines...) Expand all Loading... | |
117 default_id = default_device; | 124 default_id = default_device; |
118 } | 125 } |
119 | 126 |
120 // Use the first device as the default device if the preferred default device | 127 // Use the first device as the default device if the preferred default device |
121 // does not exist in the OS. | 128 // does not exist in the OS. |
122 if (!devices.empty() && default_id.empty()) | 129 if (!devices.empty() && default_id.empty()) |
123 default_id = devices[0].id; | 130 default_id = devices[0].id; |
124 | 131 |
125 base::StringValue default_value(default_id); | 132 base::StringValue default_value(default_id); |
126 base::StringValue type_value(device_type); | 133 base::StringValue type_value(device_type); |
127 web_ui()->CallJavascriptFunctionUnsafe("ContentSettings.updateDevicesMenu", | 134 CallJavascriptFunction("cr.webUIListenerCallback", |
128 type_value, device_list, | 135 base::StringValue("updateDevicesMenu"), |
129 default_value); | 136 type_value, |
137 device_list, | |
138 default_value); | |
130 } | 139 } |
131 | 140 |
132 void MediaDevicesSelectionHandler::UpdateDevicesMenuForType(DeviceType type) { | 141 void MediaDevicesSelectionHandler::UpdateDevicesMenuForType(DeviceType type) { |
133 content::MediaStreamDevices devices; | 142 content::MediaStreamDevices devices; |
134 switch (type) { | 143 switch (type) { |
135 case AUDIO: | 144 case AUDIO: |
136 devices = MediaCaptureDevicesDispatcher::GetInstance()-> | 145 devices = MediaCaptureDevicesDispatcher::GetInstance()-> |
137 GetAudioCaptureDevices(); | 146 GetAudioCaptureDevices(); |
138 break; | 147 break; |
139 case VIDEO: | 148 case VIDEO: |
140 devices = MediaCaptureDevicesDispatcher::GetInstance()-> | 149 devices = MediaCaptureDevicesDispatcher::GetInstance()-> |
141 GetVideoCaptureDevices(); | 150 GetVideoCaptureDevices(); |
142 break; | 151 break; |
143 } | 152 } |
144 | 153 |
145 UpdateDevicesMenu(type, devices); | 154 UpdateDevicesMenu(type, devices); |
146 } | 155 } |
147 | 156 |
148 } // namespace options | 157 } // namespace settings |
OLD | NEW |