Index: chrome/browser/ui/webui/options/chromeos/system_options_handler.cc |
diff --git a/chrome/browser/ui/webui/options/chromeos/system_options_handler.cc b/chrome/browser/ui/webui/options/chromeos/system_options_handler.cc |
index d4656453e0f299b666071117d68613e6c792c13c..ec482797387a641726b5f37246f8347a2af44800 100644 |
--- a/chrome/browser/ui/webui/options/chromeos/system_options_handler.cc |
+++ b/chrome/browser/ui/webui/options/chromeos/system_options_handler.cc |
@@ -4,8 +4,6 @@ |
#include "chrome/browser/ui/webui/options/chromeos/system_options_handler.h" |
-#include <string> |
- |
#include "base/basictypes.h" |
#include "base/bind.h" |
#include "base/bind_helpers.h" |
@@ -17,9 +15,11 @@ |
#include "content/browser/tab_contents/tab_contents.h" |
#include "chrome/browser/browser_process.h" |
#include "chrome/browser/chromeos/accessibility_util.h" |
+#include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" |
#include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" |
#include "chrome/browser/chromeos/dbus/power_manager_client.h" |
#include "chrome/browser/chromeos/language_preferences.h" |
+#include "chrome/browser/chromeos/system/runtime_environment.h" |
#include "chrome/browser/extensions/extension_service.h" |
#include "chrome/browser/prefs/pref_service.h" |
#include "chrome/browser/profiles/profile.h" |
@@ -41,6 +41,7 @@ SystemOptionsHandler::SystemOptionsHandler() |
} |
SystemOptionsHandler::~SystemOptionsHandler() { |
+ ShutdownBluetooth(); |
} |
void SystemOptionsHandler::GetLocalizedValues( |
@@ -119,15 +120,7 @@ void SystemOptionsHandler::Initialize() { |
base::FundamentalValue checked(acc_enabled); |
web_ui_->CallJavascriptFunction( |
"options.SystemOptions.SetAccessibilityCheckboxState", checked); |
- |
- // Bluetooth support is a work in progress. Supress the feature unless |
- // explicitly enabled via a command line flag. |
- // TODO (kevers) - Test for presence of bluetooth hardware. |
- if (CommandLine::ForCurrentProcess() |
- ->HasSwitch(switches::kEnableBluetooth)) { |
- web_ui_->CallJavascriptFunction( |
- "options.SystemOptions.showBluetoothSettings"); |
- } |
+ InitializeBluetooth(); |
} |
void SystemOptionsHandler::RegisterMessages() { |
@@ -175,16 +168,39 @@ void SystemOptionsHandler::IncreaseScreenBrightnessCallback( |
void SystemOptionsHandler::BluetoothEnableChangeCallback( |
const ListValue* args) { |
- // TODO (kevers) - Call Bluetooth API to enable or disable. |
+ // TODO(kevers) - Call Bluetooth API to enable or disable. |
} |
void SystemOptionsHandler::FindBluetoothDevicesCallback( |
const ListValue* args) { |
- // TODO (kevers) - Call Bluetooth API to fetch devices. |
- // Generate a fake list until the bluetooth API is ready. |
- // Afterwards keep fake list only for cases where emulating |
- // ChromeOS from the desktop launch. |
- GenerateFakeDeviceList(); |
+ // We only initiate a scan if we're running on Chrome OS. Otherwise, we |
+ // generate a fake device list. |
+ if (!chromeos::system::runtime_environment::IsRunningOnChromeOS()) { |
+ GenerateFakeDeviceList(); |
+ return; |
+ } |
+ |
+ chromeos::BluetoothManager* bluetooth_manager = |
+ chromeos::BluetoothManager::GetInstance(); |
+ CHECK(bluetooth_manager != NULL); |
+ |
+ chromeos::BluetoothAdapter* default_adapter = |
+ bluetooth_manager->DefaultAdapter(); |
+ |
+ if ((default_adapter == NULL && !default_adapter_id_.empty()) || |
+ (default_adapter != NULL && |
+ default_adapter_id_ != default_adapter->id())) { |
+ LOG(WARNING) << "unexpected default adapter change from \"" |
+ << default_adapter_id_ << "\" to \"" << default_adapter->id() << "\""; |
+ DefaultAdapterChanged(default_adapter); |
+ } |
+ |
+ if (default_adapter == NULL) { |
+ LOG(WARNING) << "FindBluetoothDevicesCallback: no default adapter"; |
+ return; |
+ } |
+ |
+ default_adapter->StartDiscovery(); |
} |
void SystemOptionsHandler::BluetoothDeviceNotification( |
@@ -193,11 +209,129 @@ void SystemOptionsHandler::BluetoothDeviceNotification( |
"options.SystemOptions.addBluetoothDevice", device); |
} |
+void SystemOptionsHandler::DefaultAdapterChanged( |
+ chromeos::BluetoothAdapter* adapter) { |
+ std::string old_default_adapter_id = default_adapter_id_; |
+ |
+ if (adapter == NULL) { |
+ default_adapter_id_.clear(); |
+ LOG(INFO) << "DefaultAdapterChanged: no default bluetooth adapter"; |
+ } else { |
+ default_adapter_id_ = adapter->id(); |
+ LOG(INFO) << "DefaultAdapterChanged: " << default_adapter_id_; |
+ } |
+ |
+ if (default_adapter_id_ == old_default_adapter_id) { |
+ return; |
+ } |
+ |
+ if (adapter != NULL) { |
+ adapter->AddObserver(this); |
+ } |
+ |
+ // TODO(vlaviano): Respond to adapter change. |
+} |
+ |
+void SystemOptionsHandler::DiscoveryStarted(const std::string& adapter_id) { |
+ VLOG(1) << "Discovery started on " << adapter_id; |
+} |
+ |
+void SystemOptionsHandler::DiscoveryEnded(const std::string& adapter_id) { |
+ VLOG(1) << "Discovery ended on " << adapter_id; |
+ web_ui_->CallJavascriptFunction( |
+ "options.SystemOptions.notifyBluetoothSearchComplete"); |
+ |
+ // Stop the discovery session. |
+ // TODO(vlaviano): We may want to expose DeviceDisappeared, remove the |
+ // "Find devices" button, and let the discovery session continue throughout |
+ // the time that the page is visible rather than just doing a single discovery |
+ // cycle in response to a button click. |
+ chromeos::BluetoothManager* bluetooth_manager = |
+ chromeos::BluetoothManager::GetInstance(); |
+ CHECK(bluetooth_manager != NULL); |
+ |
+ chromeos::BluetoothAdapter* default_adapter = |
+ bluetooth_manager->DefaultAdapter(); |
+ |
+ if ((default_adapter == NULL && !default_adapter_id_.empty()) || |
+ (default_adapter != NULL && |
+ default_adapter_id_ != default_adapter->id())) { |
+ LOG(WARNING) << "unexpected default adapter change from \"" |
+ << default_adapter_id_ << "\" to \"" << default_adapter->id() << "\""; |
+ DefaultAdapterChanged(default_adapter); |
+ } |
+ |
+ if (default_adapter == NULL) { |
+ LOG(WARNING) << "FindBluetoothDevicesCallback: no default adapter"; |
+ return; |
+ } |
+ |
+ default_adapter->StopDiscovery(); |
+} |
+ |
+void SystemOptionsHandler::DeviceFound(const std::string& adapter_id, |
+ chromeos::BluetoothDevice* device) { |
+ VLOG(1) << "Device found on " << adapter_id; |
+ CHECK(device != NULL); |
+ |
+ // TODO(vlaviano): create more consistency between the UI and the |
+ // underlying BluetoothDevice properties so that we can use |
+ // BluetoothDevice::dictionary_rep() here. |
+ DictionaryValue device_js_rep; |
+ device_js_rep.SetString("deviceName", device->name()); |
+ device_js_rep.SetString("deviceId", device->address()); |
+ device_js_rep.SetString("deviceType", device->icon()); |
+ if (device->paired()) { |
+ device_js_rep.SetString("deviceStatus", "bluetoothDeviceConnected"); |
+ } else { |
+ device_js_rep.SetString("deviceStatus", "bluetoothDeviceNotPaired"); |
+ } |
+ |
+ web_ui_->CallJavascriptFunction( |
+ "options.SystemOptions.addBluetoothDevice", device_js_rep); |
+} |
+ |
+void SystemOptionsHandler::InitializeBluetooth() { |
+ // Bluetooth support is a work in progress. Supress the feature unless |
+ // explicitly enabled via a command line flag. |
+ // TODO(kevers) - Test for presence of bluetooth hardware. |
+ if (!CommandLine::ForCurrentProcess() |
+ ->HasSwitch(switches::kEnableBluetooth)) { |
+ return; |
+ } |
+ VLOG(1) << "InitializeBluetooth"; |
+ |
+ web_ui_->CallJavascriptFunction( |
+ "options.SystemOptions.showBluetoothSettings"); |
+ |
+ chromeos::BluetoothManager* bluetooth_manager = |
+ chromeos::BluetoothManager::GetInstance(); |
+ CHECK(bluetooth_manager != NULL); |
+ bluetooth_manager->AddObserver(this); |
+ |
+ chromeos::BluetoothAdapter* default_adapter = |
+ bluetooth_manager->DefaultAdapter(); |
+ DefaultAdapterChanged(default_adapter); |
+} |
+ |
+void SystemOptionsHandler::ShutdownBluetooth() { |
+ if (!CommandLine::ForCurrentProcess() |
+ ->HasSwitch(switches::kEnableBluetooth)) { |
+ return; |
+ } |
+ VLOG(1) << "ShutdownBluetooth"; |
+ |
+ chromeos::BluetoothManager* bluetooth_manager = |
+ chromeos::BluetoothManager::GetInstance(); |
+ CHECK(bluetooth_manager != NULL); |
+ bluetooth_manager->RemoveObserver(this); |
+} |
+ |
void SystemOptionsHandler::GenerateFakeDeviceList() { |
- // TODO (kevers) - Send notifications asynchronously simulating that the |
- // process of discovering bluetooth devices takes time. |
- // Fire each notification using OneShotTimer with a |
- // varying delay. |
+ // TODO(kevers) - Send notifications asynchronously simulating that the |
+ // process of discovering bluetooth devices takes time. |
+ // Fire each notification using OneShotTimer with a |
+ // varying delay. |
std::string data[9] = { |
"Fake Wireless Keyboard", "01-02-03-04-05", "keyboard", |
"Fake Wireless Mouse", "02-03-04-05-01", "mouse", |