OLD | NEW |
| (Empty) |
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/chromeos/bluetooth_options_handler.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/command_line.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "base/values.h" | |
11 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | |
12 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" | |
13 #include "chrome/browser/ui/webui/options/chromeos/system_settings_provider.h" | |
14 #include "content/public/browser/web_ui.h" | |
15 #include "grit/chromium_strings.h" | |
16 #include "grit/generated_resources.h" | |
17 #include "third_party/cros_system_api/dbus/service_constants.h" | |
18 #include "ui/base/l10n/l10n_util.h" | |
19 | |
20 namespace { | |
21 | |
22 // |UpdateDeviceCallback| takes a variable length list as an argument. The | |
23 // value stored in each list element is indicated by the following constants. | |
24 const int kUpdateDeviceAddressIndex = 0; | |
25 const int kUpdateDeviceCommandIndex = 1; | |
26 const int kUpdateDevicePasskeyIndex = 2; | |
27 | |
28 } // namespace | |
29 | |
30 namespace chromeos { | |
31 | |
32 BluetoothOptionsHandler::BluetoothOptionsHandler() { | |
33 } | |
34 | |
35 BluetoothOptionsHandler::~BluetoothOptionsHandler() { | |
36 if (adapter_.get()) | |
37 adapter_->RemoveObserver(this); | |
38 } | |
39 | |
40 void BluetoothOptionsHandler::GetLocalizedValues( | |
41 DictionaryValue* localized_strings) { | |
42 DCHECK(localized_strings); | |
43 | |
44 localized_strings->SetString("bluetooth", | |
45 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_SECTION_TITLE_BLUETOOTH)); | |
46 localized_strings->SetString("disableBluetooth", | |
47 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_DISABLE)); | |
48 localized_strings->SetString("enableBluetooth", | |
49 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_ENABLE)); | |
50 localized_strings->SetString("addBluetoothDevice", | |
51 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_ADD_BLUETOOTH_DEVICE)); | |
52 localized_strings->SetString("bluetoothAddDeviceTitle", | |
53 l10n_util::GetStringUTF16( | |
54 IDS_OPTIONS_SETTINGS_BLUETOOTH_ADD_DEVICE_TITLE)); | |
55 localized_strings->SetString("bluetoothOptionsPageTabTitle", | |
56 l10n_util::GetStringUTF16( | |
57 IDS_OPTIONS_SETTINGS_BLUETOOTH_ADD_DEVICE_TITLE)); | |
58 localized_strings->SetString("findBluetoothDevices", | |
59 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_FIND_BLUETOOTH_DEVICES)); | |
60 localized_strings->SetString("bluetoothNoDevices", | |
61 l10n_util::GetStringUTF16( | |
62 IDS_OPTIONS_SETTINGS_BLUETOOTH_NO_DEVICES)); | |
63 localized_strings->SetString("bluetoothNoDevicesFound", | |
64 l10n_util::GetStringUTF16( | |
65 IDS_OPTIONS_SETTINGS_BLUETOOTH_NO_DEVICES_FOUND)); | |
66 localized_strings->SetString("bluetoothScanning", | |
67 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_SCANNING)); | |
68 localized_strings->SetString("bluetoothDeviceConnected", | |
69 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_CONNECTED)); | |
70 localized_strings->SetString("bluetoothDeviceNotConnected", | |
71 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_NOT_CONNECTED)); | |
72 localized_strings->SetString("bluetoothConnectDevice", | |
73 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_CONNECT)); | |
74 localized_strings->SetString("bluetoothDisconnectDevice", | |
75 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_DISCONNECT)); | |
76 localized_strings->SetString("bluetoothForgetDevice", | |
77 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_FORGET)); | |
78 localized_strings->SetString("bluetoothCancel", | |
79 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_CANCEL)); | |
80 localized_strings->SetString("bluetoothEnterKey", | |
81 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_ENTER_KEY)); | |
82 localized_strings->SetString("bluetoothAcceptPasskey", | |
83 l10n_util::GetStringUTF16( | |
84 IDS_OPTIONS_SETTINGS_BLUETOOTH_ACCEPT_PASSKEY)); | |
85 localized_strings->SetString("bluetoothRejectPasskey", | |
86 l10n_util::GetStringUTF16( | |
87 IDS_OPTIONS_SETTINGS_BLUETOOTH_REJECT_PASSKEY)); | |
88 localized_strings->SetString("bluetoothConfirmPasskey", | |
89 l10n_util::GetStringUTF16( | |
90 IDS_OPTIONS_SETTINGS_BLUETOOTH_CONFIRM_PASSKEY_REQUEST)); | |
91 localized_strings->SetString("bluetoothEnterPasskey", | |
92 l10n_util::GetStringUTF16( | |
93 IDS_OPTIONS_SETTINGS_BLUETOOTH_ENTER_PASSKEY_REQUEST)); | |
94 localized_strings->SetString("bluetoothRemotePasskey", | |
95 l10n_util::GetStringUTF16( | |
96 IDS_OPTIONS_SETTINGS_BLUETOOTH_REMOTE_PASSKEY_REQUEST)); | |
97 localized_strings->SetString("bluetoothDismissError", | |
98 l10n_util::GetStringUTF16( | |
99 IDS_OPTIONS_SETTINGS_BLUETOOTH_DISMISS_ERROR)); | |
100 } | |
101 | |
102 void BluetoothOptionsHandler::InitializeHandler() { | |
103 adapter_.reset(BluetoothAdapter::CreateDefaultAdapter()); | |
104 adapter_->AddObserver(this); | |
105 | |
106 // Show or hide the bluetooth settings and update the checkbox based | |
107 // on the current present/powered state. | |
108 AdapterPresentChanged(adapter_.get(), adapter_->IsPresent()); | |
109 } | |
110 | |
111 void BluetoothOptionsHandler::AdapterPresentChanged(BluetoothAdapter* adapter, | |
112 bool present) { | |
113 DCHECK(adapter == adapter_.get()); | |
114 if (present) { | |
115 web_ui()->CallJavascriptFunction( | |
116 "options.SystemOptions.showBluetoothSettings"); | |
117 | |
118 // Update the checkbox and visibility based on the powered state of the | |
119 // new adapter. | |
120 AdapterPoweredChanged(adapter_.get(), adapter_->IsPowered()); | |
121 } | |
122 } | |
123 | |
124 void BluetoothOptionsHandler::AdapterPoweredChanged(BluetoothAdapter* adapter, | |
125 bool powered) { | |
126 DCHECK(adapter == adapter_.get()); | |
127 base::FundamentalValue checked(powered); | |
128 web_ui()->CallJavascriptFunction( | |
129 "options.SystemOptions.setBluetoothState", checked); | |
130 } | |
131 | |
132 void BluetoothOptionsHandler::RegisterMessages() { | |
133 web_ui()->RegisterMessageCallback("bluetoothEnableChange", | |
134 base::Bind(&BluetoothOptionsHandler::EnableChangeCallback, | |
135 base::Unretained(this))); | |
136 web_ui()->RegisterMessageCallback("findBluetoothDevices", | |
137 base::Bind(&BluetoothOptionsHandler::FindDevicesCallback, | |
138 base::Unretained(this))); | |
139 web_ui()->RegisterMessageCallback("updateBluetoothDevice", | |
140 base::Bind(&BluetoothOptionsHandler::UpdateDeviceCallback, | |
141 base::Unretained(this))); | |
142 } | |
143 | |
144 void BluetoothOptionsHandler::EnableChangeCallback( | |
145 const ListValue* args) { | |
146 bool bluetooth_enabled; | |
147 args->GetBoolean(0, &bluetooth_enabled); | |
148 | |
149 adapter_->SetPowered(bluetooth_enabled, | |
150 base::Bind(&BluetoothOptionsHandler::ErrorCallback, | |
151 base::Unretained(this))); | |
152 } | |
153 | |
154 void BluetoothOptionsHandler::FindDevicesCallback( | |
155 const ListValue* args) { | |
156 adapter_->SetDiscovering(true, | |
157 base::Bind(&BluetoothOptionsHandler::ErrorCallback, | |
158 base::Unretained(this))); | |
159 } | |
160 | |
161 void BluetoothOptionsHandler::UpdateDeviceCallback( | |
162 const ListValue* args) { | |
163 // TODO(kevers): Trigger connect/disconnect. | |
164 int size = args->GetSize(); | |
165 std::string address; | |
166 std::string command; | |
167 args->GetString(kUpdateDeviceAddressIndex, &address); | |
168 args->GetString(kUpdateDeviceCommandIndex, &command); | |
169 if (size > kUpdateDevicePasskeyIndex) { | |
170 // Passkey confirmation as part of the pairing process. | |
171 std::string passkey; | |
172 args->GetString(kUpdateDevicePasskeyIndex, &passkey); | |
173 DVLOG(1) << "UpdateDeviceCallback: " << address << ": " << command | |
174 << " [" << passkey << "]"; | |
175 } else { | |
176 // Initiating a device connection or disconnecting | |
177 DVLOG(1) << "UpdateDeviceCallback: " << address << ": " << command; | |
178 } | |
179 } | |
180 | |
181 void BluetoothOptionsHandler::SendDeviceNotification( | |
182 const BluetoothDevice* device, | |
183 base::DictionaryValue* params) { | |
184 base::DictionaryValue js_properties; | |
185 js_properties.SetString("name", device->GetName()); | |
186 js_properties.SetString("address", device->address()); | |
187 js_properties.SetBoolean("paired", device->IsPaired()); | |
188 js_properties.SetBoolean("bonded", device->IsBonded()); | |
189 js_properties.SetBoolean("connected", device->IsConnected()); | |
190 if (params) { | |
191 js_properties.MergeDictionary(params); | |
192 } | |
193 web_ui()->CallJavascriptFunction( | |
194 "options.SystemOptions.addBluetoothDevice", | |
195 js_properties); | |
196 } | |
197 | |
198 void BluetoothOptionsHandler::RequestConfirmation( | |
199 const BluetoothDevice* device, | |
200 int passkey) { | |
201 DictionaryValue params; | |
202 params.SetString("pairing", "bluetoothConfirmPasskey"); | |
203 params.SetInteger("passkey", passkey); | |
204 SendDeviceNotification(device, ¶ms); | |
205 } | |
206 | |
207 void BluetoothOptionsHandler::DisplayPasskey( | |
208 const BluetoothDevice* device, | |
209 int passkey, | |
210 int entered) { | |
211 DictionaryValue params; | |
212 params.SetString("pairing", "bluetoothRemotePasskey"); | |
213 params.SetInteger("passkey", passkey); | |
214 params.SetInteger("entered", entered); | |
215 SendDeviceNotification(device, ¶ms); | |
216 } | |
217 | |
218 void BluetoothOptionsHandler::RequestPasskey( | |
219 const BluetoothDevice* device) { | |
220 DictionaryValue params; | |
221 params.SetString("pairing", "bluetoothEnterPasskey"); | |
222 SendDeviceNotification(device, ¶ms); | |
223 } | |
224 | |
225 void BluetoothOptionsHandler::ReportError( | |
226 const BluetoothDevice* device, | |
227 ConnectionError error) { | |
228 std::string errorCode; | |
229 switch (error) { | |
230 case DEVICE_NOT_FOUND: | |
231 errorCode = "bluetoothErrorNoDevice"; | |
232 break; | |
233 case INCORRECT_PIN: | |
234 errorCode = "bluetoothErrorIncorrectPin"; | |
235 break; | |
236 case CONNECTION_TIMEOUT: | |
237 errorCode = "bluetoothErrorTimeout"; | |
238 break; | |
239 case CONNECTION_REJECTED: | |
240 errorCode = "bluetoothErrorConnectionFailed"; | |
241 break; | |
242 } | |
243 DictionaryValue params; | |
244 params.SetString("pairing", errorCode); | |
245 SendDeviceNotification(device, ¶ms); | |
246 } | |
247 | |
248 void BluetoothOptionsHandler::AdapterDiscoveringChanged( | |
249 BluetoothAdapter* adapter, bool discovering) { | |
250 DCHECK(adapter == adapter_.get()); | |
251 if (!discovering) { | |
252 web_ui()->CallJavascriptFunction( | |
253 "options.SystemOptions.notifyBluetoothSearchComplete"); | |
254 | |
255 // Stop the discovery session. | |
256 // TODO(vlaviano): We may want to expose DeviceDisappeared, remove the | |
257 // "Find devices" button, and let the discovery session continue throughout | |
258 // the time that the page is visible rather than just doing a single | |
259 // discovery cycle in response to a button click. | |
260 adapter_->SetDiscovering(false, | |
261 base::Bind(&BluetoothOptionsHandler::ErrorCallback, | |
262 base::Unretained(this))); | |
263 } | |
264 } | |
265 | |
266 void BluetoothOptionsHandler::DeviceAdded(BluetoothAdapter* adapter, | |
267 BluetoothDevice* device) { | |
268 DCHECK(adapter == adapter_.get()); | |
269 DCHECK(device); | |
270 SendDeviceNotification(device, NULL); | |
271 } | |
272 | |
273 void BluetoothOptionsHandler::DeviceChanged(BluetoothAdapter* adapter, | |
274 BluetoothDevice* device) { | |
275 DCHECK(adapter == adapter_.get()); | |
276 DCHECK(device); | |
277 SendDeviceNotification(device, NULL); | |
278 } | |
279 | |
280 void BluetoothOptionsHandler::ErrorCallback() { | |
281 // TODO(keybuk): we don't get any form of error response from dbus:: | |
282 // yet, other than an error occurred. I'm going to fix that, then this | |
283 // gets replaced by genuine error information from the method which we | |
284 // can act on, rather than a debug log statement. | |
285 DVLOG(1) << "Failed."; | |
286 } | |
287 | |
288 } // namespace chromeos | |
OLD | NEW |