- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / options / media_devices_selection_handler.cc
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.
4
5 #include "chrome/browser/ui/webui/options/media_devices_selection_handler.h"
6
7 #include "base/bind.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/common/pref_names.h"
11
12 namespace {
13
14 const char kAudio[] = "mic";
15 const char kVideo[] = "camera";
16
17 }  // namespace
18
19 namespace options {
20
21 MediaDevicesSelectionHandler::MediaDevicesSelectionHandler() {}
22
23 MediaDevicesSelectionHandler::~MediaDevicesSelectionHandler() {
24   MediaCaptureDevicesDispatcher::GetInstance()->RemoveObserver(this);
25 }
26
27 void MediaDevicesSelectionHandler::GetLocalizedValues(DictionaryValue* values) {
28   DCHECK(values);
29
30   static OptionsStringResource resources[] = {
31     { "mediaSelectMicLabel", IDS_MEDIA_SELECTED_MIC_LABEL },
32     { "mediaSelectCameraLabel", IDS_MEDIA_SELECTED_CAMERA_LABEL },
33   };
34
35   RegisterStrings(values, resources, arraysize(resources));
36 }
37
38 void MediaDevicesSelectionHandler::InitializePage() {
39   // Register to the device observer list to get up-to-date device lists.
40   MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this);
41
42   // Update the device selection menus.
43   UpdateDevicesMenuForType(AUDIO);
44   UpdateDevicesMenuForType(VIDEO);
45 }
46
47 void MediaDevicesSelectionHandler::RegisterMessages() {
48   web_ui()->RegisterMessageCallback("setDefaultCaptureDevice",
49       base::Bind(&MediaDevicesSelectionHandler::SetDefaultCaptureDevice,
50                  base::Unretained(this)));
51 }
52
53 void MediaDevicesSelectionHandler::OnUpdateAudioDevices(
54     const content::MediaStreamDevices& devices) {
55   UpdateDevicesMenu(AUDIO, devices);
56 }
57
58 void MediaDevicesSelectionHandler::OnUpdateVideoDevices(
59     const content::MediaStreamDevices& devices) {
60   UpdateDevicesMenu(VIDEO, devices);
61 }
62
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))) {
68     NOTREACHED();
69     return;
70   }
71
72   DCHECK(!type.empty());
73   DCHECK(!device.empty());
74
75   Profile* profile = Profile::FromWebUI(web_ui());
76   PrefService* prefs = profile->GetPrefs();
77   if (type == kAudio)
78     prefs->SetString(prefs::kDefaultAudioCaptureDevice, device);
79   else if (type == kVideo)
80     prefs->SetString(prefs::kDefaultVideoCaptureDevice, device);
81   else
82     NOTREACHED();
83 }
84
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;
92   switch (type) {
93     case AUDIO:
94       default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice);
95       device_type = kAudio;
96       break;
97     case VIDEO:
98       default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice);
99       device_type = kVideo;
100       break;
101   }
102
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;
113   }
114
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;
119
120   StringValue default_value(default_id);
121   StringValue type_value(device_type);
122   web_ui()->CallJavascriptFunction("ContentSettings.updateDevicesMenu",
123                                    type_value,
124                                    device_list,
125                                    default_value);
126 }
127
128 void MediaDevicesSelectionHandler::UpdateDevicesMenuForType(DeviceType type) {
129   content::MediaStreamDevices devices;
130   switch (type) {
131     case AUDIO:
132       devices = MediaCaptureDevicesDispatcher::GetInstance()->
133           GetAudioCaptureDevices();
134       break;
135     case VIDEO:
136       devices = MediaCaptureDevicesDispatcher::GetInstance()->
137           GetVideoCaptureDevices();
138       break;
139   }
140
141   UpdateDevicesMenu(type, devices);
142 }
143
144 }  // namespace options