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

Side by Side Diff: chrome/browser/ui/webui/options/chromeos/system_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: replace empty device names with mac address Created 9 years, 2 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) 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/system_options_handler.h" 5 #include "chrome/browser/ui/webui/options/chromeos/system_options_handler.h"
6 6
7 #include <string>
8
9 #include "base/basictypes.h" 7 #include "base/basictypes.h"
10 #include "base/bind.h" 8 #include "base/bind.h"
11 #include "base/bind_helpers.h" 9 #include "base/bind_helpers.h"
12 #include "base/command_line.h" 10 #include "base/command_line.h"
13 #include "base/json/json_value_serializer.h" 11 #include "base/json/json_value_serializer.h"
14 #include "base/string_number_conversions.h" 12 #include "base/string_number_conversions.h"
15 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
16 #include "base/values.h" 14 #include "base/values.h"
17 #include "content/browser/tab_contents/tab_contents.h" 15 #include "content/browser/tab_contents/tab_contents.h"
18 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/chromeos/accessibility_util.h" 17 #include "chrome/browser/chromeos/accessibility_util.h"
18 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h"
20 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" 19 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h"
21 #include "chrome/browser/chromeos/dbus/power_manager_client.h" 20 #include "chrome/browser/chromeos/dbus/power_manager_client.h"
22 #include "chrome/browser/chromeos/language_preferences.h" 21 #include "chrome/browser/chromeos/language_preferences.h"
22 #include "chrome/browser/chromeos/system/runtime_environment.h"
23 #include "chrome/browser/extensions/extension_service.h" 23 #include "chrome/browser/extensions/extension_service.h"
24 #include "chrome/browser/prefs/pref_service.h" 24 #include "chrome/browser/prefs/pref_service.h"
25 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/ui/webui/options/chromeos/system_settings_provider.h" 26 #include "chrome/browser/ui/webui/options/chromeos/system_settings_provider.h"
27 #include "chrome/common/chrome_switches.h" 27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/pref_names.h" 28 #include "chrome/common/pref_names.h"
29 #include "chrome/common/extensions/extension.h" 29 #include "chrome/common/extensions/extension.h"
30 #include "grit/browser_resources.h" 30 #include "grit/browser_resources.h"
31 #include "grit/chromium_strings.h" 31 #include "grit/chromium_strings.h"
32 #include "grit/generated_resources.h" 32 #include "grit/generated_resources.h"
33 #include "grit/locale_settings.h" 33 #include "grit/locale_settings.h"
34 #include "grit/theme_resources.h" 34 #include "grit/theme_resources.h"
35 #include "ui/base/l10n/l10n_util.h" 35 #include "ui/base/l10n/l10n_util.h"
36 #include "ui/base/resource/resource_bundle.h" 36 #include "ui/base/resource/resource_bundle.h"
37 37
38 SystemOptionsHandler::SystemOptionsHandler() 38 SystemOptionsHandler::SystemOptionsHandler()
39 : chromeos::CrosOptionsPageUIHandler( 39 : chromeos::CrosOptionsPageUIHandler(
40 new chromeos::SystemSettingsProvider()) { 40 new chromeos::SystemSettingsProvider()) {
41 } 41 }
42 42
43 SystemOptionsHandler::~SystemOptionsHandler() { 43 SystemOptionsHandler::~SystemOptionsHandler() {
44 ShutdownBluetooth();
44 } 45 }
45 46
46 void SystemOptionsHandler::GetLocalizedValues( 47 void SystemOptionsHandler::GetLocalizedValues(
47 DictionaryValue* localized_strings) { 48 DictionaryValue* localized_strings) {
48 DCHECK(localized_strings); 49 DCHECK(localized_strings);
49 50
50 RegisterTitle(localized_strings, "systemPage", IDS_OPTIONS_SYSTEM_TAB_LABEL); 51 RegisterTitle(localized_strings, "systemPage", IDS_OPTIONS_SYSTEM_TAB_LABEL);
51 localized_strings->SetString("datetimeTitle", 52 localized_strings->SetString("datetimeTitle",
52 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_SECTION_TITLE_DATETIME)); 53 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_SECTION_TITLE_DATETIME));
53 localized_strings->SetString("timezone", 54 localized_strings->SetString("timezone",
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 settings_provider_.get())->GetTimezoneList()); 117 settings_provider_.get())->GetTimezoneList());
117 } 118 }
118 119
119 void SystemOptionsHandler::Initialize() { 120 void SystemOptionsHandler::Initialize() {
120 DCHECK(web_ui_); 121 DCHECK(web_ui_);
121 PrefService* pref_service = g_browser_process->local_state(); 122 PrefService* pref_service = g_browser_process->local_state();
122 bool acc_enabled = pref_service->GetBoolean(prefs::kAccessibilityEnabled); 123 bool acc_enabled = pref_service->GetBoolean(prefs::kAccessibilityEnabled);
123 base::FundamentalValue checked(acc_enabled); 124 base::FundamentalValue checked(acc_enabled);
124 web_ui_->CallJavascriptFunction( 125 web_ui_->CallJavascriptFunction(
125 "options.SystemOptions.SetAccessibilityCheckboxState", checked); 126 "options.SystemOptions.SetAccessibilityCheckboxState", checked);
126 127 InitializeBluetooth();
127 // Bluetooth support is a work in progress. Supress the feature unless
128 // explicitly enabled via a command line flag.
129 // TODO (kevers) - Test for presence of bluetooth hardware.
130 if (CommandLine::ForCurrentProcess()
131 ->HasSwitch(switches::kEnableBluetooth)) {
132 web_ui_->CallJavascriptFunction(
133 "options.SystemOptions.showBluetoothSettings");
134 }
135 } 128 }
136 129
137 void SystemOptionsHandler::RegisterMessages() { 130 void SystemOptionsHandler::RegisterMessages() {
138 DCHECK(web_ui_); 131 DCHECK(web_ui_);
139 web_ui_->RegisterMessageCallback("accessibilityChange", 132 web_ui_->RegisterMessageCallback("accessibilityChange",
140 base::Bind(&SystemOptionsHandler::AccessibilityChangeCallback, 133 base::Bind(&SystemOptionsHandler::AccessibilityChangeCallback,
141 base::Unretained(this))); 134 base::Unretained(this)));
142 135
143 web_ui_->RegisterMessageCallback("decreaseScreenBrightness", 136 web_ui_->RegisterMessageCallback("decreaseScreenBrightness",
144 base::Bind(&SystemOptionsHandler::DecreaseScreenBrightnessCallback, 137 base::Bind(&SystemOptionsHandler::DecreaseScreenBrightnessCallback,
(...skipping 27 matching lines...) Expand all
172 } 165 }
173 166
174 void SystemOptionsHandler::IncreaseScreenBrightnessCallback( 167 void SystemOptionsHandler::IncreaseScreenBrightnessCallback(
175 const ListValue* args) { 168 const ListValue* args) {
176 chromeos::DBusThreadManager::Get()->power_manager_client()-> 169 chromeos::DBusThreadManager::Get()->power_manager_client()->
177 IncreaseScreenBrightness(); 170 IncreaseScreenBrightness();
178 } 171 }
179 172
180 void SystemOptionsHandler::BluetoothEnableChangeCallback( 173 void SystemOptionsHandler::BluetoothEnableChangeCallback(
181 const ListValue* args) { 174 const ListValue* args) {
182 // TODO (kevers) - Call Bluetooth API to enable or disable. 175 // TODO(kevers) - Call Bluetooth API to enable or disable.
183 } 176 }
184 177
185 void SystemOptionsHandler::FindBluetoothDevicesCallback( 178 void SystemOptionsHandler::FindBluetoothDevicesCallback(
186 const ListValue* args) { 179 const ListValue* args) {
187 // TODO (kevers) - Call Bluetooth API to fetch devices. 180 // We only initiate a scan if we're running on Chrome OS. Otherwise, we
188 // Generate a fake list until the bluetooth API is ready. 181 // generate a fake device list.
189 // Afterwards keep fake list only for cases where emulating 182 if (!chromeos::system::runtime_environment::IsRunningOnChromeOS()) {
190 // ChromeOS from the desktop launch. 183 GenerateFakeDeviceList();
191 GenerateFakeDeviceList(); 184 return;
185 }
186
187 chromeos::BluetoothManager* bluetooth_manager =
188 chromeos::BluetoothManager::GetInstance();
189 CHECK(bluetooth_manager != NULL);
190
191 chromeos::BluetoothAdapter* default_adapter =
192 bluetooth_manager->DefaultAdapter();
193
194 if ((default_adapter == NULL && !default_adapter_id_.empty()) ||
195 (default_adapter != NULL &&
196 default_adapter_id_ != default_adapter->id())) {
197 LOG(WARNING) << "unexpected default adapter change from \""
198 << default_adapter_id_ << "\" to \"" << default_adapter->id() << "\"";
199 DefaultAdapterChanged(default_adapter);
200 }
201
202 if (default_adapter == NULL) {
203 LOG(WARNING) << "FindBluetoothDevicesCallback: no default adapter";
204 return;
205 }
206
207 default_adapter->StartDiscovery();
192 } 208 }
193 209
194 void SystemOptionsHandler::BluetoothDeviceNotification( 210 void SystemOptionsHandler::BluetoothDeviceNotification(
195 const DictionaryValue& device) { 211 const DictionaryValue& device) {
196 web_ui_->CallJavascriptFunction( 212 web_ui_->CallJavascriptFunction(
197 "options.SystemOptions.addBluetoothDevice", device); 213 "options.SystemOptions.addBluetoothDevice", device);
198 } 214 }
199 215
216 void SystemOptionsHandler::DefaultAdapterChanged(
217 chromeos::BluetoothAdapter* adapter) {
218 std::string old_default_adapter_id = default_adapter_id_;
219
220 if (adapter == NULL) {
221 default_adapter_id_.clear();
222 LOG(INFO) << "DefaultAdapterChanged: no default bluetooth adapter";
223 } else {
224 default_adapter_id_ = adapter->id();
225 LOG(INFO) << "DefaultAdapterChanged: " << default_adapter_id_;
226 }
227
228 if (default_adapter_id_ == old_default_adapter_id) {
229 return;
230 }
231
232 if (adapter != NULL) {
233 adapter->AddObserver(this);
234 }
235
236 // TODO(vlaviano): Respond to adapter change.
237 }
238
239 void SystemOptionsHandler::DiscoveryStarted(const std::string& adapter_id) {
240 VLOG(1) << "Discovery started on " << adapter_id;
241 }
242
243 void SystemOptionsHandler::DiscoveryEnded(const std::string& adapter_id) {
244 VLOG(1) << "Discovery ended on " << adapter_id;
245 web_ui_->CallJavascriptFunction(
246 "options.SystemOptions.notifyBluetoothSearchComplete");
247
248 // Stop the discovery session.
249 // TODO(vlaviano): We may want to expose DeviceDisappeared, remove the
250 // "Find devices" button, and let the discovery session continue throughout
251 // the time that the page is visible rather than just doing a single discovery
252 // cycle in response to a button click.
253 chromeos::BluetoothManager* bluetooth_manager =
254 chromeos::BluetoothManager::GetInstance();
255 CHECK(bluetooth_manager != NULL);
256
257 chromeos::BluetoothAdapter* default_adapter =
258 bluetooth_manager->DefaultAdapter();
259
260 if ((default_adapter == NULL && !default_adapter_id_.empty()) ||
261 (default_adapter != NULL &&
262 default_adapter_id_ != default_adapter->id())) {
263 LOG(WARNING) << "unexpected default adapter change from \""
264 << default_adapter_id_ << "\" to \"" << default_adapter->id() << "\"";
265 DefaultAdapterChanged(default_adapter);
266 }
267
268 if (default_adapter == NULL) {
269 LOG(WARNING) << "FindBluetoothDevicesCallback: no default adapter";
270 return;
271 }
272
273 default_adapter->StopDiscovery();
274 }
275
276 void SystemOptionsHandler::DeviceFound(const std::string& adapter_id,
277 chromeos::BluetoothDevice* device) {
278 VLOG(1) << "Device found on " << adapter_id;
279 CHECK(device != NULL);
280
281 // TODO(vlaviano): create more consistency between the UI and the
282 // underlying BluetoothDevice properties so that we can use
283 // BluetoothDevice::dictionary_rep() here.
284 DictionaryValue device_js_rep;
285 device_js_rep.SetString("deviceName", device->name());
286 device_js_rep.SetString("deviceId", device->address());
287 device_js_rep.SetString("deviceType", device->icon());
288 if (device->paired()) {
289 device_js_rep.SetString("deviceStatus", "bluetoothDeviceConnected");
290 } else {
291 device_js_rep.SetString("deviceStatus", "bluetoothDeviceNotPaired");
292 }
293
294 web_ui_->CallJavascriptFunction(
295 "options.SystemOptions.addBluetoothDevice", device_js_rep);
296 }
297
298 void SystemOptionsHandler::InitializeBluetooth() {
299 // Bluetooth support is a work in progress. Supress the feature unless
300 // explicitly enabled via a command line flag.
301 // TODO(kevers) - Test for presence of bluetooth hardware.
302 if (!CommandLine::ForCurrentProcess()
303 ->HasSwitch(switches::kEnableBluetooth)) {
304 return;
305 }
306 VLOG(1) << "InitializeBluetooth";
307
308 web_ui_->CallJavascriptFunction(
309 "options.SystemOptions.showBluetoothSettings");
310
311 chromeos::BluetoothManager* bluetooth_manager =
312 chromeos::BluetoothManager::GetInstance();
313 CHECK(bluetooth_manager != NULL);
314 bluetooth_manager->AddObserver(this);
315
316 chromeos::BluetoothAdapter* default_adapter =
317 bluetooth_manager->DefaultAdapter();
318 DefaultAdapterChanged(default_adapter);
319 }
320
321 void SystemOptionsHandler::ShutdownBluetooth() {
322 if (!CommandLine::ForCurrentProcess()
323 ->HasSwitch(switches::kEnableBluetooth)) {
324 return;
325 }
326 VLOG(1) << "ShutdownBluetooth";
327
328 chromeos::BluetoothManager* bluetooth_manager =
329 chromeos::BluetoothManager::GetInstance();
330 CHECK(bluetooth_manager != NULL);
331
332 chromeos::BluetoothAdapter* default_adapter =
333 bluetooth_manager->DefaultAdapter();
334
335 if (default_adapter != NULL) {
336 default_adapter->RemoveObserver(this);
337 }
338
339 bluetooth_manager->RemoveObserver(this);
340 }
341
200 void SystemOptionsHandler::GenerateFakeDeviceList() { 342 void SystemOptionsHandler::GenerateFakeDeviceList() {
201 // TODO (kevers) - Send notifications asynchronously simulating that the 343 // TODO(kevers) - Send notifications asynchronously simulating that the
202 // process of discovering bluetooth devices takes time. 344 // process of discovering bluetooth devices takes time.
203 // Fire each notification using OneShotTimer with a 345 // Fire each notification using OneShotTimer with a
204 // varying delay. 346 // varying delay.
205 std::string data[9] = { 347 std::string data[9] = {
206 "Fake Wireless Keyboard", "01-02-03-04-05", "keyboard", 348 "Fake Wireless Keyboard", "01-02-03-04-05", "keyboard",
207 "Fake Wireless Mouse", "02-03-04-05-01", "mouse", 349 "Fake Wireless Mouse", "02-03-04-05-01", "mouse",
208 "Fake Wireless Headset", "03-04-05-01-02", "headset"}; 350 "Fake Wireless Headset", "03-04-05-01-02", "headset"};
209 351
210 for (int i = 0; i < 3; i++) { 352 for (int i = 0; i < 3; i++) {
211 DictionaryValue device; 353 DictionaryValue device;
212 device.SetString("deviceName", data[3*i]); 354 device.SetString("deviceName", data[3*i]);
213 device.SetString("deviceId", data[3*i+1]); 355 device.SetString("deviceId", data[3*i+1]);
214 device.SetString("deviceType", data[3*i+2]); 356 device.SetString("deviceType", data[3*i+2]);
215 device.SetString("deviceStatus", "bluetoothDeviceNotPaired"); 357 device.SetString("deviceStatus", "bluetoothDeviceNotPaired");
216 web_ui_->CallJavascriptFunction( 358 web_ui_->CallJavascriptFunction(
217 "options.SystemOptions.addBluetoothDevice", device); 359 "options.SystemOptions.addBluetoothDevice", device);
218 } 360 }
219 web_ui_->CallJavascriptFunction( 361 web_ui_->CallJavascriptFunction(
220 "options.SystemOptions.notifyBluetoothSearchComplete"); 362 "options.SystemOptions.notifyBluetoothSearchComplete");
221 } 363 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698