OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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( |
26 content::WebUI* webui) | |
michaelpg
2016/06/07 16:03:30
just take a Profile* instead
Finnur
2016/06/07 16:48:24
Done.
| |
27 : profile_(Profile::FromWebUI(webui)), initialized_(false) { | |
28 } | |
26 | 29 |
27 MediaDevicesSelectionHandler::~MediaDevicesSelectionHandler() { | 30 MediaDevicesSelectionHandler::~MediaDevicesSelectionHandler() { |
28 MediaCaptureDevicesDispatcher::GetInstance()->RemoveObserver(this); | 31 if (initialized_) |
michaelpg
2016/06/07 16:03:30
can this be a ScopedObserver instead of checking i
Finnur
2016/06/07 16:48:23
Actually, that's not a bad idea. Done.
| |
32 MediaCaptureDevicesDispatcher::GetInstance()->RemoveObserver(this); | |
29 } | 33 } |
30 | 34 |
31 void MediaDevicesSelectionHandler::GetLocalizedValues( | 35 void MediaDevicesSelectionHandler::OnJavascriptAllowed() { |
32 base::DictionaryValue* values) { | |
33 DCHECK(values); | |
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 } | 36 } |
42 | 37 |
43 void MediaDevicesSelectionHandler::InitializePage() { | 38 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 } | 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 if (!initialized_) { | |
michaelpg
2016/06/07 16:03:30
if (!scoped_observer_->IsObserving...
Finnur
2016/06/07 16:48:24
Acknowledged.
| |
63 // Register to the device observer list to get up-to-date device lists. | |
64 MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this); | |
65 initialized_ = true; | |
66 } | |
67 | |
68 DCHECK_EQ(1U, args->GetSize()); | |
69 std::string type; | |
70 if (!args->GetString(0, &type)) { | |
71 NOTREACHED(); | |
72 return; | |
73 } | |
74 DCHECK(!type.empty()); | |
75 | |
76 if (type == kAudio) | |
77 UpdateDevicesMenuForType(AUDIO); | |
78 else if (type == kVideo) | |
79 UpdateDevicesMenuForType(VIDEO); | |
80 } | |
81 | |
68 void MediaDevicesSelectionHandler::SetDefaultCaptureDevice( | 82 void MediaDevicesSelectionHandler::SetDefaultCaptureDevice( |
69 const base::ListValue* args) { | 83 const base::ListValue* args) { |
84 DCHECK(initialized_); | |
70 DCHECK_EQ(2U, args->GetSize()); | 85 DCHECK_EQ(2U, args->GetSize()); |
71 std::string type, device; | 86 std::string type, device; |
72 if (!(args->GetString(0, &type) && args->GetString(1, &device))) { | 87 if (!(args->GetString(0, &type) && args->GetString(1, &device))) { |
73 NOTREACHED(); | 88 NOTREACHED(); |
74 return; | 89 return; |
75 } | 90 } |
76 | 91 |
77 DCHECK(!type.empty()); | 92 DCHECK(!type.empty()); |
78 DCHECK(!device.empty()); | 93 DCHECK(!device.empty()); |
79 | 94 |
80 Profile* profile = Profile::FromWebUI(web_ui()); | 95 Profile* profile = Profile::FromWebUI(web_ui()); |
michaelpg
2016/06/07 16:03:30
profile_
Finnur
2016/06/07 16:48:23
Done.
| |
81 PrefService* prefs = profile->GetPrefs(); | 96 PrefService* prefs = profile->GetPrefs(); |
82 if (type == kAudio) | 97 if (type == kAudio) |
83 prefs->SetString(prefs::kDefaultAudioCaptureDevice, device); | 98 prefs->SetString(prefs::kDefaultAudioCaptureDevice, device); |
84 else if (type == kVideo) | 99 else if (type == kVideo) |
85 prefs->SetString(prefs::kDefaultVideoCaptureDevice, device); | 100 prefs->SetString(prefs::kDefaultVideoCaptureDevice, device); |
86 else | 101 else |
87 NOTREACHED(); | 102 NOTREACHED(); |
88 } | 103 } |
89 | 104 |
90 void MediaDevicesSelectionHandler::UpdateDevicesMenu( | 105 void MediaDevicesSelectionHandler::UpdateDevicesMenu( |
91 DeviceType type, const content::MediaStreamDevices& devices) { | 106 DeviceType type, const content::MediaStreamDevices& devices) { |
107 AllowJavascript(); | |
108 | |
92 // Get the default device unique id from prefs. | 109 // Get the default device unique id from prefs. |
93 Profile* profile = Profile::FromWebUI(web_ui()); | 110 PrefService* prefs = profile_->GetPrefs(); |
94 PrefService* prefs = profile->GetPrefs(); | |
95 std::string default_device; | 111 std::string default_device; |
96 std::string device_type; | 112 std::string device_type; |
97 switch (type) { | 113 switch (type) { |
98 case AUDIO: | 114 case AUDIO: |
99 default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice); | 115 default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice); |
100 device_type = kAudio; | 116 device_type = kAudio; |
101 break; | 117 break; |
102 case VIDEO: | 118 case VIDEO: |
103 default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice); | 119 default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice); |
104 device_type = kVideo; | 120 device_type = kVideo; |
(...skipping 12 matching lines...) Expand all Loading... | |
117 default_id = default_device; | 133 default_id = default_device; |
118 } | 134 } |
119 | 135 |
120 // Use the first device as the default device if the preferred default device | 136 // Use the first device as the default device if the preferred default device |
121 // does not exist in the OS. | 137 // does not exist in the OS. |
122 if (!devices.empty() && default_id.empty()) | 138 if (!devices.empty() && default_id.empty()) |
123 default_id = devices[0].id; | 139 default_id = devices[0].id; |
124 | 140 |
125 base::StringValue default_value(default_id); | 141 base::StringValue default_value(default_id); |
126 base::StringValue type_value(device_type); | 142 base::StringValue type_value(device_type); |
127 web_ui()->CallJavascriptFunction("ContentSettings.updateDevicesMenu", | 143 CallJavascriptFunction("cr.webUIListenerCallback", |
128 type_value, | 144 base::StringValue("updateDevicesMenu"), |
129 device_list, | 145 type_value, |
130 default_value); | 146 device_list, |
147 default_value); | |
131 } | 148 } |
132 | 149 |
133 void MediaDevicesSelectionHandler::UpdateDevicesMenuForType(DeviceType type) { | 150 void MediaDevicesSelectionHandler::UpdateDevicesMenuForType(DeviceType type) { |
134 content::MediaStreamDevices devices; | 151 content::MediaStreamDevices devices; |
135 switch (type) { | 152 switch (type) { |
136 case AUDIO: | 153 case AUDIO: |
137 devices = MediaCaptureDevicesDispatcher::GetInstance()-> | 154 devices = MediaCaptureDevicesDispatcher::GetInstance()-> |
138 GetAudioCaptureDevices(); | 155 GetAudioCaptureDevices(); |
139 break; | 156 break; |
140 case VIDEO: | 157 case VIDEO: |
141 devices = MediaCaptureDevicesDispatcher::GetInstance()-> | 158 devices = MediaCaptureDevicesDispatcher::GetInstance()-> |
142 GetVideoCaptureDevices(); | 159 GetVideoCaptureDevices(); |
143 break; | 160 break; |
144 } | 161 } |
145 | 162 |
146 UpdateDevicesMenu(type, devices); | 163 UpdateDevicesMenu(type, devices); |
147 } | 164 } |
148 | 165 |
149 } // namespace options | 166 } // namespace settings |
OLD | NEW |