Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Side by Side Diff: chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.cc

Issue 10899037: Refactoring bluetooth API code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added #include <string> into bluetooth_adapter_dbus.cc. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/chromeos/bluetooth_options_handler.h" 5 #include "chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/string_number_conversions.h" 11 #include "base/string_number_conversions.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" 14 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter_dbus.h"
15 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" 15 #include "chrome/browser/chromeos/bluetooth/bluetooth_device_dbus.h"
16 #include "content/public/browser/web_ui.h" 16 #include "content/public/browser/web_ui.h"
17 #include "grit/chromium_strings.h" 17 #include "grit/chromium_strings.h"
18 #include "grit/generated_resources.h" 18 #include "grit/generated_resources.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h" 19 #include "third_party/cros_system_api/dbus/service_constants.h"
20 #include "ui/base/l10n/l10n_util.h" 20 #include "ui/base/l10n/l10n_util.h"
21 21
22 namespace { 22 namespace {
23 23
24 // |UpdateDeviceCallback| takes a variable length list as an argument. The 24 // |UpdateDeviceCallback| takes a variable length list as an argument. The
25 // value stored in each list element is indicated by the following constants. 25 // value stored in each list element is indicated by the following constants.
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 { "bluetoothDisconnectFailed", 116 { "bluetoothDisconnectFailed",
117 IDS_OPTIONS_SETTINGS_BLUETOOTH_DISCONNECT_FAILED }, 117 IDS_OPTIONS_SETTINGS_BLUETOOTH_DISCONNECT_FAILED },
118 { "bluetoothForgetFailed", 118 { "bluetoothForgetFailed",
119 IDS_OPTIONS_SETTINGS_BLUETOOTH_FORGET_FAILED }}; 119 IDS_OPTIONS_SETTINGS_BLUETOOTH_FORGET_FAILED }};
120 120
121 RegisterStrings(localized_strings, resources, arraysize(resources)); 121 RegisterStrings(localized_strings, resources, arraysize(resources));
122 } 122 }
123 123
124 // TODO(kevers): Reorder methods to match ordering in the header file. 124 // TODO(kevers): Reorder methods to match ordering in the header file.
125 125
126 void BluetoothOptionsHandler::AdapterPresentChanged(BluetoothAdapter* adapter, 126 void BluetoothOptionsHandler::AdapterPresentChanged(
127 bool present) { 127 BluetoothAdapterDBus* adapter,
128 bool present) {
128 DCHECK(adapter == adapter_.get()); 129 DCHECK(adapter == adapter_.get());
129 if (present) { 130 if (present) {
130 web_ui()->CallJavascriptFunction( 131 web_ui()->CallJavascriptFunction(
131 "options.BrowserOptions.showBluetoothSettings"); 132 "options.BrowserOptions.showBluetoothSettings");
132 133
133 // Update the checkbox and visibility based on the powered state of the 134 // Update the checkbox and visibility based on the powered state of the
134 // new adapter. 135 // new adapter.
135 AdapterPoweredChanged(adapter_.get(), adapter_->IsPowered()); 136 AdapterPoweredChanged(adapter_.get(), adapter_->IsPowered());
136 } else { 137 } else {
137 web_ui()->CallJavascriptFunction( 138 web_ui()->CallJavascriptFunction(
138 "options.BrowserOptions.hideBluetoothSettings"); 139 "options.BrowserOptions.hideBluetoothSettings");
139 } 140 }
140 } 141 }
141 142
142 void BluetoothOptionsHandler::AdapterPoweredChanged(BluetoothAdapter* adapter, 143 void BluetoothOptionsHandler::AdapterPoweredChanged(
143 bool powered) { 144 BluetoothAdapterDBus* adapter,
145 bool powered) {
144 DCHECK(adapter == adapter_.get()); 146 DCHECK(adapter == adapter_.get());
145 base::FundamentalValue checked(powered); 147 base::FundamentalValue checked(powered);
146 web_ui()->CallJavascriptFunction( 148 web_ui()->CallJavascriptFunction(
147 "options.BrowserOptions.setBluetoothState", checked); 149 "options.BrowserOptions.setBluetoothState", checked);
148 } 150 }
149 151
150 void BluetoothOptionsHandler::RegisterMessages() { 152 void BluetoothOptionsHandler::RegisterMessages() {
151 web_ui()->RegisterMessageCallback("bluetoothEnableChange", 153 web_ui()->RegisterMessageCallback("bluetoothEnableChange",
152 base::Bind(&BluetoothOptionsHandler::EnableChangeCallback, 154 base::Bind(&BluetoothOptionsHandler::EnableChangeCallback,
153 base::Unretained(this))); 155 base::Unretained(this)));
154 web_ui()->RegisterMessageCallback("findBluetoothDevices", 156 web_ui()->RegisterMessageCallback("findBluetoothDevices",
155 base::Bind(&BluetoothOptionsHandler::FindDevicesCallback, 157 base::Bind(&BluetoothOptionsHandler::FindDevicesCallback,
156 base::Unretained(this))); 158 base::Unretained(this)));
157 web_ui()->RegisterMessageCallback("updateBluetoothDevice", 159 web_ui()->RegisterMessageCallback("updateBluetoothDevice",
158 base::Bind(&BluetoothOptionsHandler::UpdateDeviceCallback, 160 base::Bind(&BluetoothOptionsHandler::UpdateDeviceCallback,
159 base::Unretained(this))); 161 base::Unretained(this)));
160 web_ui()->RegisterMessageCallback("stopBluetoothDeviceDiscovery", 162 web_ui()->RegisterMessageCallback("stopBluetoothDeviceDiscovery",
161 base::Bind(&BluetoothOptionsHandler::StopDiscoveryCallback, 163 base::Bind(&BluetoothOptionsHandler::StopDiscoveryCallback,
162 base::Unretained(this))); 164 base::Unretained(this)));
163 web_ui()->RegisterMessageCallback("getPairedBluetoothDevices", 165 web_ui()->RegisterMessageCallback("getPairedBluetoothDevices",
164 base::Bind(&BluetoothOptionsHandler::GetPairedDevicesCallback, 166 base::Bind(&BluetoothOptionsHandler::GetPairedDevicesCallback,
165 base::Unretained(this))); 167 base::Unretained(this)));
166 } 168 }
167 169
168 void BluetoothOptionsHandler::InitializeHandler() { 170 void BluetoothOptionsHandler::InitializeHandler() {
169 adapter_ = BluetoothAdapter::DefaultAdapter(); 171 adapter_ = BluetoothAdapterDBus::DefaultAdapter();
170 adapter_->AddObserver(this); 172 adapter_->AddObserver(this);
171 } 173 }
172 174
173 void BluetoothOptionsHandler::InitializePage() { 175 void BluetoothOptionsHandler::InitializePage() {
174 // Show or hide the bluetooth settings and update the checkbox based 176 // Show or hide the bluetooth settings and update the checkbox based
175 // on the current present/powered state. 177 // on the current present/powered state.
176 AdapterPresentChanged(adapter_.get(), adapter_->IsPresent()); 178 AdapterPresentChanged(adapter_.get(), adapter_->IsPresent());
177 // Automatically start device discovery if the "Add Bluetooth Device" 179 // Automatically start device discovery if the "Add Bluetooth Device"
178 // overlay is visible. 180 // overlay is visible.
179 web_ui()->CallJavascriptFunction( 181 web_ui()->CallJavascriptFunction(
(...skipping 28 matching lines...) Expand all
208 void BluetoothOptionsHandler::FindDevicesError() { 210 void BluetoothOptionsHandler::FindDevicesError() {
209 DVLOG(1) << "Failed to start discovery."; 211 DVLOG(1) << "Failed to start discovery.";
210 ReportError("bluetoothStartDiscoveryFailed", std::string()); 212 ReportError("bluetoothStartDiscoveryFailed", std::string());
211 } 213 }
212 214
213 void BluetoothOptionsHandler::UpdateDeviceCallback( 215 void BluetoothOptionsHandler::UpdateDeviceCallback(
214 const ListValue* args) { 216 const ListValue* args) {
215 std::string address; 217 std::string address;
216 args->GetString(kUpdateDeviceAddressIndex, &address); 218 args->GetString(kUpdateDeviceAddressIndex, &address);
217 219
218 BluetoothDevice* device = adapter_->GetDevice(address); 220 BluetoothDeviceDBus* device = static_cast<BluetoothDeviceDBus *>(
221 adapter_->GetDevice(address));
219 if (!device) 222 if (!device)
220 return; 223 return;
221 224
222 std::string command; 225 std::string command;
223 args->GetString(kUpdateDeviceCommandIndex, &command); 226 args->GetString(kUpdateDeviceCommandIndex, &command);
224 227
225 if (command == kConnectCommand) { 228 if (command == kConnectCommand) {
226 int size = args->GetSize(); 229 int size = args->GetSize();
227 if (size > kUpdateDeviceAuthTokenIndex) { 230 if (size > kUpdateDeviceAuthTokenIndex) {
228 // PIN code or Passkey entry during the pairing process. 231 // PIN code or Passkey entry during the pairing process.
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 weak_ptr_factory_.GetWeakPtr())); 319 weak_ptr_factory_.GetWeakPtr()));
317 } 320 }
318 321
319 void BluetoothOptionsHandler::StopDiscoveryError() { 322 void BluetoothOptionsHandler::StopDiscoveryError() {
320 DVLOG(1) << "Failed to stop discovery."; 323 DVLOG(1) << "Failed to stop discovery.";
321 ReportError("bluetoothStopDiscoveryFailed", std::string()); 324 ReportError("bluetoothStopDiscoveryFailed", std::string());
322 } 325 }
323 326
324 void BluetoothOptionsHandler::GetPairedDevicesCallback( 327 void BluetoothOptionsHandler::GetPairedDevicesCallback(
325 const ListValue* args) { 328 const ListValue* args) {
326 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); 329 BluetoothAdapterDBus::DeviceList devices = adapter_->GetDevices();
327 330
328 for (BluetoothAdapter::DeviceList::iterator iter = devices.begin(); 331 for (BluetoothAdapterDBus::DeviceList::iterator iter = devices.begin();
329 iter != devices.end(); ++iter) 332 iter != devices.end(); ++iter)
330 SendDeviceNotification(*iter, NULL); 333 SendDeviceNotification(*iter, NULL);
331 } 334 }
332 335
333 void BluetoothOptionsHandler::SendDeviceNotification( 336 void BluetoothOptionsHandler::SendDeviceNotification(
334 const BluetoothDevice* device, 337 const BluetoothDeviceInterface* device,
335 base::DictionaryValue* params) { 338 base::DictionaryValue* params) {
336 base::DictionaryValue js_properties; 339 base::DictionaryValue js_properties;
337 js_properties.SetString("name", device->GetName()); 340 js_properties.SetString("name", device->GetName());
338 js_properties.SetString("address", device->address()); 341 js_properties.SetString("address", device->address());
339 js_properties.SetBoolean("paired", device->IsPaired()); 342 js_properties.SetBoolean("paired", device->IsPaired());
340 js_properties.SetBoolean("bonded", device->IsBonded()); 343 js_properties.SetBoolean("bonded", device->IsBonded());
341 js_properties.SetBoolean("connected", device->IsConnected()); 344 js_properties.SetBoolean("connected", device->IsConnected());
342 if (params) { 345 if (params) {
343 js_properties.MergeDictionary(params); 346 js_properties.MergeDictionary(params);
344 } 347 }
345 web_ui()->CallJavascriptFunction( 348 web_ui()->CallJavascriptFunction(
346 "options.BrowserOptions.addBluetoothDevice", 349 "options.BrowserOptions.addBluetoothDevice",
347 js_properties); 350 js_properties);
348 } 351 }
349 352
350 void BluetoothOptionsHandler::RequestPinCode(BluetoothDevice* device) { 353 void BluetoothOptionsHandler::RequestPinCode(BluetoothDeviceDBus* device) {
351 DictionaryValue params; 354 DictionaryValue params;
352 params.SetString("pairing", kEnterPinCode); 355 params.SetString("pairing", kEnterPinCode);
353 SendDeviceNotification(device, &params); 356 SendDeviceNotification(device, &params);
354 } 357 }
355 358
356 void BluetoothOptionsHandler::RequestPasskey(BluetoothDevice* device) { 359 void BluetoothOptionsHandler::RequestPasskey(BluetoothDeviceDBus* device) {
357 DictionaryValue params; 360 DictionaryValue params;
358 params.SetString("pairing", kEnterPasskey); 361 params.SetString("pairing", kEnterPasskey);
359 SendDeviceNotification(device, &params); 362 SendDeviceNotification(device, &params);
360 } 363 }
361 364
362 void BluetoothOptionsHandler::DisplayPinCode(BluetoothDevice* device, 365 void BluetoothOptionsHandler::DisplayPinCode(BluetoothDeviceDBus* device,
363 const std::string& pincode) { 366 const std::string& pincode) {
364 DictionaryValue params; 367 DictionaryValue params;
365 params.SetString("pairing", kRemotePinCode); 368 params.SetString("pairing", kRemotePinCode);
366 params.SetString("pincode", pincode); 369 params.SetString("pincode", pincode);
367 SendDeviceNotification(device, &params); 370 SendDeviceNotification(device, &params);
368 } 371 }
369 372
370 void BluetoothOptionsHandler::DisplayPasskey(BluetoothDevice* device, 373 void BluetoothOptionsHandler::DisplayPasskey(BluetoothDeviceDBus* device,
371 uint32 passkey) { 374 uint32 passkey) {
372 DictionaryValue params; 375 DictionaryValue params;
373 params.SetString("pairing", kRemotePasskey); 376 params.SetString("pairing", kRemotePasskey);
374 params.SetInteger("passkey", passkey); 377 params.SetInteger("passkey", passkey);
375 SendDeviceNotification(device, &params); 378 SendDeviceNotification(device, &params);
376 } 379 }
377 380
378 void BluetoothOptionsHandler::ConfirmPasskey(BluetoothDevice* device, 381 void BluetoothOptionsHandler::ConfirmPasskey(BluetoothDeviceDBus* device,
379 uint32 passkey) { 382 uint32 passkey) {
380 DictionaryValue params; 383 DictionaryValue params;
381 params.SetString("pairing", kConfirmPasskey); 384 params.SetString("pairing", kConfirmPasskey);
382 params.SetInteger("passkey", passkey); 385 params.SetInteger("passkey", passkey);
383 SendDeviceNotification(device, &params); 386 SendDeviceNotification(device, &params);
384 } 387 }
385 388
386 void BluetoothOptionsHandler::DismissDisplayOrConfirm() { 389 void BluetoothOptionsHandler::DismissDisplayOrConfirm() {
387 web_ui()->CallJavascriptFunction( 390 web_ui()->CallJavascriptFunction(
388 "options.BluetoothPairing.dismissDialog"); 391 "options.BluetoothPairing.dismissDialog");
389 } 392 }
390 393
391 void BluetoothOptionsHandler::ReportError( 394 void BluetoothOptionsHandler::ReportError(
392 const std::string& error, 395 const std::string& error,
393 const std::string& address) { 396 const std::string& address) {
394 base::DictionaryValue properties; 397 base::DictionaryValue properties;
395 properties.SetString("label", error); 398 properties.SetString("label", error);
396 properties.SetString("address", address); 399 properties.SetString("address", address);
397 web_ui()->CallJavascriptFunction( 400 web_ui()->CallJavascriptFunction(
398 "options.BluetoothPairing.showMessage", 401 "options.BluetoothPairing.showMessage",
399 properties); 402 properties);
400 } 403 }
401 404
402 void BluetoothOptionsHandler::DeviceAdded(BluetoothAdapter* adapter, 405 void BluetoothOptionsHandler::DeviceAdded(BluetoothAdapterDBus* adapter,
403 BluetoothDevice* device) { 406 BluetoothDeviceDBus* device) {
404 DCHECK(adapter == adapter_.get()); 407 DCHECK(adapter == adapter_.get());
405 DCHECK(device); 408 DCHECK(device);
406 SendDeviceNotification(device, NULL); 409 SendDeviceNotification(device, NULL);
407 } 410 }
408 411
409 void BluetoothOptionsHandler::DeviceChanged(BluetoothAdapter* adapter, 412 void BluetoothOptionsHandler::DeviceChanged(BluetoothAdapterDBus* adapter,
410 BluetoothDevice* device) { 413 BluetoothDeviceDBus* device) {
411 DCHECK(adapter == adapter_.get()); 414 DCHECK(adapter == adapter_.get());
412 DCHECK(device); 415 DCHECK(device);
413 SendDeviceNotification(device, NULL); 416 SendDeviceNotification(device, NULL);
414 } 417 }
415 418
416 void BluetoothOptionsHandler::DeviceRemoved(BluetoothAdapter* adapter, 419 void BluetoothOptionsHandler::DeviceRemoved(BluetoothAdapterDBus* adapter,
417 BluetoothDevice* device) { 420 BluetoothDeviceDBus* device) {
418 DCHECK(adapter == adapter_.get()); 421 DCHECK(adapter == adapter_.get());
419 DCHECK(device); 422 DCHECK(device);
420 423
421 base::StringValue address(device->address()); 424 base::StringValue address(device->address());
422 web_ui()->CallJavascriptFunction( 425 web_ui()->CallJavascriptFunction(
423 "options.BrowserOptions.removeBluetoothDevice", 426 "options.BrowserOptions.removeBluetoothDevice",
424 address); 427 address);
425 } 428 }
426 429
427 } // namespace options 430 } // namespace options
428 } // namespace chromeos 431 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698