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

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

Issue 8233042: Chrome OS: Attach bluetooth UI to device discovery API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase after separate bluetooth core code commit Created 9 years, 1 month 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
« no previous file with comments | « chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/command_line.h" 8 #include "base/command_line.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h"
11 #include "chrome/browser/chromeos/system/runtime_environment.h" 12 #include "chrome/browser/chromeos/system/runtime_environment.h"
12 #include "chrome/browser/ui/webui/options/chromeos/system_settings_provider.h" 13 #include "chrome/browser/ui/webui/options/chromeos/system_settings_provider.h"
13 #include "chrome/common/chrome_switches.h" 14 #include "chrome/common/chrome_switches.h"
14 #include "grit/chromium_strings.h" 15 #include "grit/chromium_strings.h"
15 #include "grit/generated_resources.h" 16 #include "grit/generated_resources.h"
16 #include "ui/base/l10n/l10n_util.h" 17 #include "ui/base/l10n/l10n_util.h"
17 18
18 namespace chromeos { 19 namespace chromeos {
19 20
20 BluetoothOptionsHandler::BluetoothOptionsHandler() 21 BluetoothOptionsHandler::BluetoothOptionsHandler()
21 : chromeos::CrosOptionsPageUIHandler( 22 : chromeos::CrosOptionsPageUIHandler(
22 new chromeos::SystemSettingsProvider()) { 23 new chromeos::SystemSettingsProvider()) {
23 } 24 }
24 25
25 BluetoothOptionsHandler::~BluetoothOptionsHandler() { 26 BluetoothOptionsHandler::~BluetoothOptionsHandler() {
26 // TODO(kevers): Shutdown bluetooth. 27 VLOG(1) << "BluetoothOptionsHandler dtor";
kevers 2011/10/27 16:14:43 Do we intend to keep this comment in the final rel
Vince Laviano 2011/10/27 20:15:54 Removed.
28
29 if (!CommandLine::ForCurrentProcess()
30 ->HasSwitch(switches::kEnableBluetooth)) {
31 return;
32 }
33
34 chromeos::BluetoothManager* bluetooth_manager =
35 chromeos::BluetoothManager::GetInstance();
36 DCHECK(bluetooth_manager);
37
38 chromeos::BluetoothAdapter* default_adapter =
39 bluetooth_manager->DefaultAdapter();
40
41 if (default_adapter != NULL) {
42 default_adapter->RemoveObserver(this);
43 }
44
45 bluetooth_manager->RemoveObserver(this);
27 } 46 }
28 47
29 void BluetoothOptionsHandler::GetLocalizedValues( 48 void BluetoothOptionsHandler::GetLocalizedValues(
30 DictionaryValue* localized_strings) { 49 DictionaryValue* localized_strings) {
31 DCHECK(localized_strings); 50 DCHECK(localized_strings);
32 51
33 localized_strings->SetString("bluetooth", 52 localized_strings->SetString("bluetooth",
34 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_SECTION_TITLE_BLUETOOTH)); 53 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_SECTION_TITLE_BLUETOOTH));
35 localized_strings->SetString("enableBluetooth", 54 localized_strings->SetString("enableBluetooth",
36 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_ENABLE)); 55 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_BLUETOOTH_ENABLE));
(...skipping 13 matching lines...) Expand all
50 69
51 void BluetoothOptionsHandler::Initialize() { 70 void BluetoothOptionsHandler::Initialize() {
52 DCHECK(web_ui_); 71 DCHECK(web_ui_);
53 // Bluetooth support is a work in progress. Supress the feature unless 72 // Bluetooth support is a work in progress. Supress the feature unless
54 // explicitly enabled via a command line flag. 73 // explicitly enabled via a command line flag.
55 // TODO(kevers): Test for presence of bluetooth hardware. 74 // TODO(kevers): Test for presence of bluetooth hardware.
56 if (!CommandLine::ForCurrentProcess() 75 if (!CommandLine::ForCurrentProcess()
57 ->HasSwitch(switches::kEnableBluetooth)) { 76 ->HasSwitch(switches::kEnableBluetooth)) {
58 return; 77 return;
59 } 78 }
79 VLOG(1) << "Initialize";
kevers 2011/10/27 16:14:43 Message appears to be too generic.
Vince Laviano 2011/10/27 20:15:54 Removed.
80
60 web_ui_->CallJavascriptFunction( 81 web_ui_->CallJavascriptFunction(
61 "options.SystemOptions.showBluetoothSettings"); 82 "options.SystemOptions.showBluetoothSettings");
62 // TODO(kevers): Initialize bluetooth. 83
84 chromeos::BluetoothManager* bluetooth_manager =
85 chromeos::BluetoothManager::GetInstance();
86 DCHECK(bluetooth_manager);
87 bluetooth_manager->AddObserver(this);
88
89 chromeos::BluetoothAdapter* default_adapter =
90 bluetooth_manager->DefaultAdapter();
91 DefaultAdapterChanged(default_adapter);
63 } 92 }
64 93
65 void BluetoothOptionsHandler::RegisterMessages() { 94 void BluetoothOptionsHandler::RegisterMessages() {
66 DCHECK(web_ui_); 95 DCHECK(web_ui_);
67 web_ui_->RegisterMessageCallback("bluetoothEnableChange", 96 web_ui_->RegisterMessageCallback("bluetoothEnableChange",
68 base::Bind(&BluetoothOptionsHandler::EnableChangeCallback, 97 base::Bind(&BluetoothOptionsHandler::EnableChangeCallback,
69 base::Unretained(this))); 98 base::Unretained(this)));
70 web_ui_->RegisterMessageCallback("findBluetoothDevices", 99 web_ui_->RegisterMessageCallback("findBluetoothDevices",
71 base::Bind(&BluetoothOptionsHandler::FindDevicesCallback, 100 base::Bind(&BluetoothOptionsHandler::FindDevicesCallback,
72 base::Unretained(this))); 101 base::Unretained(this)));
73 web_ui_->RegisterMessageCallback("updateBluetoothDevice", 102 web_ui_->RegisterMessageCallback("updateBluetoothDevice",
74 base::Bind(&BluetoothOptionsHandler::UpdateDeviceCallback, 103 base::Bind(&BluetoothOptionsHandler::UpdateDeviceCallback,
75 base::Unretained(this))); 104 base::Unretained(this)));
76 } 105 }
77 106
78 void BluetoothOptionsHandler::EnableChangeCallback( 107 void BluetoothOptionsHandler::EnableChangeCallback(
79 const ListValue* args) { 108 const ListValue* args) {
80 // TODO(kevers): Call Bluetooth API to enable or disable. 109 // TODO(kevers): Call Bluetooth API to enable or disable.
81 } 110 }
82 111
83 void BluetoothOptionsHandler::FindDevicesCallback( 112 void BluetoothOptionsHandler::FindDevicesCallback(
84 const ListValue* args) { 113 const ListValue* args) {
85 // We only initiate a scan if we're running on Chrome OS. Otherwise, we 114 // We only initiate a scan if we're running on Chrome OS. Otherwise, we
86 // generate a fake device list. 115 // generate a fake device list.
87 if (!chromeos::system::runtime_environment::IsRunningOnChromeOS()) { 116 if (!chromeos::system::runtime_environment::IsRunningOnChromeOS()) {
88 GenerateFakeDeviceList(); 117 GenerateFakeDeviceList();
89 return; 118 return;
90 } 119 }
91 // TODO(kevers): Fetch real Bluetooth devices. 120
121 chromeos::BluetoothManager* bluetooth_manager =
122 chromeos::BluetoothManager::GetInstance();
123 DCHECK(bluetooth_manager);
124
125 chromeos::BluetoothAdapter* default_adapter =
126 bluetooth_manager->DefaultAdapter();
127
128 if ((default_adapter == NULL && !default_adapter_id_.empty()) ||
129 (default_adapter != NULL &&
130 default_adapter_id_ != default_adapter->Id())) {
131 LOG(WARNING) << "unexpected default adapter change from \""
kevers 2011/10/27 16:14:43 Seems to be a developing trend to migrate away fro
Vince Laviano 2011/10/27 20:15:54 Done.
132 << default_adapter_id_ << "\" to \"" << default_adapter->Id()
133 << "\"";
134 DefaultAdapterChanged(default_adapter);
135 }
136
137 if (default_adapter == NULL) {
138 LOG(WARNING) << "FindDevicesCallback: no default adapter";
139 return;
140 }
141
142 default_adapter->StartDiscovery();
92 } 143 }
93 144
94 void BluetoothOptionsHandler::UpdateDeviceCallback( 145 void BluetoothOptionsHandler::UpdateDeviceCallback(
95 const ListValue* args) { 146 const ListValue* args) {
96 // TODO(kevers): Trigger connect/disconnect. 147 // TODO(kevers): Trigger connect/disconnect.
97 } 148 }
98 149
99 void BluetoothOptionsHandler::DeviceNotification( 150 void BluetoothOptionsHandler::DeviceNotification(
100 const DictionaryValue& device) { 151 const DictionaryValue& device) {
101 web_ui_->CallJavascriptFunction( 152 web_ui_->CallJavascriptFunction(
102 "options.SystemOptions.addBluetoothDevice", device); 153 "options.SystemOptions.addBluetoothDevice", device);
103 } 154 }
104 155
156 void BluetoothOptionsHandler::DefaultAdapterChanged(
157 chromeos::BluetoothAdapter* adapter) {
158 std::string old_default_adapter_id = default_adapter_id_;
159
160 if (adapter == NULL) {
161 default_adapter_id_.clear();
162 LOG(INFO) << "DefaultAdapterChanged: no default bluetooth adapter";
kevers 2011/10/27 16:14:43 VLOG(2)?
Vince Laviano 2011/10/27 20:15:54 Done.
163 } else {
164 default_adapter_id_ = adapter->Id();
165 LOG(INFO) << "DefaultAdapterChanged: " << default_adapter_id_;
166 }
167
168 if (default_adapter_id_ == old_default_adapter_id) {
169 return;
170 }
171
172 if (adapter != NULL) {
173 adapter->AddObserver(this);
174 }
175
176 // TODO(vlaviano): Respond to adapter change.
177 }
178
179 void BluetoothOptionsHandler::DiscoveryStarted(const std::string& adapter_id) {
180 VLOG(1) << "Discovery started on " << adapter_id;
181 }
182
183 void BluetoothOptionsHandler::DiscoveryEnded(const std::string& adapter_id) {
184 VLOG(1) << "Discovery ended on " << adapter_id;
185 web_ui_->CallJavascriptFunction(
186 "options.SystemOptions.notifyBluetoothSearchComplete");
187
188 // Stop the discovery session.
189 // TODO(vlaviano): We may want to expose DeviceDisappeared, remove the
190 // "Find devices" button, and let the discovery session continue throughout
191 // the time that the page is visible rather than just doing a single discovery
192 // cycle in response to a button click.
193 chromeos::BluetoothManager* bluetooth_manager =
194 chromeos::BluetoothManager::GetInstance();
195 DCHECK(bluetooth_manager);
196
197 chromeos::BluetoothAdapter* default_adapter =
198 bluetooth_manager->DefaultAdapter();
199
200 if ((default_adapter == NULL && !default_adapter_id_.empty()) ||
kevers 2011/10/27 16:14:43 Lines 200-207 appear to duplicate lines 128-135.
Vince Laviano 2011/10/27 20:15:54 Done.
201 (default_adapter != NULL &&
202 default_adapter_id_ != default_adapter->Id())) {
203 LOG(WARNING) << "unexpected default adapter change from \""
204 << default_adapter_id_ << "\" to \"" << default_adapter->Id()
205 << "\"";
206 DefaultAdapterChanged(default_adapter);
207 }
208
209 if (default_adapter == NULL) {
210 LOG(WARNING) << "DiscoveryEnded: no default adapter";
211 return;
212 }
213
214 default_adapter->StopDiscovery();
215 }
216
217 void BluetoothOptionsHandler::DeviceFound(const std::string& adapter_id,
218 chromeos::BluetoothDevice* device) {
219 VLOG(1) << "Device found on " << adapter_id;
220 DCHECK(device);
221
222 // TODO(vlaviano): eliminate inconsistencies between the javascript rep and
223 // BluetoothDevice so that we can use BluetoothDevice::AsDictionary() here.
224 DictionaryValue device_js_rep;
225 device_js_rep.SetString("deviceName", device->GetName());
226 device_js_rep.SetString("deviceId", device->GetAddress());
227 device_js_rep.SetString("deviceType", device->GetIcon());
228 if (device->IsPaired()) {
229 device_js_rep.SetString("deviceStatus", "bluetoothDeviceConnected");
230 } else {
231 device_js_rep.SetString("deviceStatus", "bluetoothDeviceNotPaired");
232 }
233
234 web_ui_->CallJavascriptFunction(
235 "options.SystemOptions.addBluetoothDevice", device_js_rep);
236 }
237
105 void BluetoothOptionsHandler::GenerateFakeDeviceList() { 238 void BluetoothOptionsHandler::GenerateFakeDeviceList() {
106 // TODO(kevers): Send notifications asynchronously simulating that the 239 // TODO(kevers): Send notifications asynchronously simulating that the
107 // process of discovering bluetooth devices takes time. 240 // process of discovering bluetooth devices takes time.
108 // Fire each notification using OneShotTimer with a 241 // Fire each notification using OneShotTimer with a
109 // varying delay. 242 // varying delay.
110 std::string data[9] = { 243 std::string data[9] = {
111 "Fake Wireless Keyboard", "01-02-03-04-05", "keyboard", 244 "Fake Wireless Keyboard", "01-02-03-04-05", "keyboard",
112 "Fake Wireless Mouse", "02-03-04-05-01", "mouse", 245 "Fake Wireless Mouse", "02-03-04-05-01", "mouse",
113 "Fake Wireless Headset", "03-04-05-01-02", "headset"}; 246 "Fake Wireless Headset", "03-04-05-01-02", "headset"};
114 247
115 for (int i = 0; i < 3; i++) { 248 for (int i = 0; i < 3; i++) {
116 DictionaryValue device; 249 DictionaryValue device;
117 device.SetString("deviceName", data[3*i]); 250 device.SetString("deviceName", data[3*i]);
118 device.SetString("deviceId", data[3*i+1]); 251 device.SetString("deviceId", data[3*i+1]);
119 device.SetString("deviceType", data[3*i+2]); 252 device.SetString("deviceType", data[3*i+2]);
120 device.SetString("deviceStatus", "bluetoothDeviceNotPaired"); 253 device.SetString("deviceStatus", "bluetoothDeviceNotPaired");
121 web_ui_->CallJavascriptFunction( 254 web_ui_->CallJavascriptFunction(
122 "options.SystemOptions.addBluetoothDevice", device); 255 "options.SystemOptions.addBluetoothDevice", device);
123 } 256 }
124 web_ui_->CallJavascriptFunction( 257 web_ui_->CallJavascriptFunction(
125 "options.SystemOptions.notifyBluetoothSearchComplete"); 258 "options.SystemOptions.notifyBluetoothSearchComplete");
126 } 259 }
127 260
128 } 261 } // namespace chromeos
129
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698