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

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: rebase 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 settings_provider_.get())->GetTimezoneList()); 113 settings_provider_.get())->GetTimezoneList());
113 } 114 }
114 115
115 void SystemOptionsHandler::Initialize() { 116 void SystemOptionsHandler::Initialize() {
116 DCHECK(web_ui_); 117 DCHECK(web_ui_);
117 PrefService* pref_service = g_browser_process->local_state(); 118 PrefService* pref_service = g_browser_process->local_state();
118 bool acc_enabled = pref_service->GetBoolean(prefs::kAccessibilityEnabled); 119 bool acc_enabled = pref_service->GetBoolean(prefs::kAccessibilityEnabled);
119 base::FundamentalValue checked(acc_enabled); 120 base::FundamentalValue checked(acc_enabled);
120 web_ui_->CallJavascriptFunction( 121 web_ui_->CallJavascriptFunction(
121 "options.SystemOptions.SetAccessibilityCheckboxState", checked); 122 "options.SystemOptions.SetAccessibilityCheckboxState", checked);
122 123 InitializeBluetooth();
123 // Bluetooth support is a work in progress. Supress the feature unless
124 // explicitly enabled via a command line flag.
125 // TODO (kevers) - Test for presence of bluetooth hardware.
126 if (CommandLine::ForCurrentProcess()
127 ->HasSwitch(switches::kEnableBluetooth)) {
128 web_ui_->CallJavascriptFunction(
129 "options.SystemOptions.showBluetoothSettings");
130 }
131 } 124 }
132 125
133 void SystemOptionsHandler::RegisterMessages() { 126 void SystemOptionsHandler::RegisterMessages() {
134 DCHECK(web_ui_); 127 DCHECK(web_ui_);
135 web_ui_->RegisterMessageCallback("accessibilityChange", 128 web_ui_->RegisterMessageCallback("accessibilityChange",
136 base::Bind(&SystemOptionsHandler::AccessibilityChangeCallback, 129 base::Bind(&SystemOptionsHandler::AccessibilityChangeCallback,
137 base::Unretained(this))); 130 base::Unretained(this)));
138 131
139 web_ui_->RegisterMessageCallback("decreaseScreenBrightness", 132 web_ui_->RegisterMessageCallback("decreaseScreenBrightness",
140 base::Bind(&SystemOptionsHandler::DecreaseScreenBrightnessCallback, 133 base::Bind(&SystemOptionsHandler::DecreaseScreenBrightnessCallback,
(...skipping 27 matching lines...) Expand all
168 } 161 }
169 162
170 void SystemOptionsHandler::IncreaseScreenBrightnessCallback( 163 void SystemOptionsHandler::IncreaseScreenBrightnessCallback(
171 const ListValue* args) { 164 const ListValue* args) {
172 chromeos::DBusThreadManager::Get()->power_manager_client()-> 165 chromeos::DBusThreadManager::Get()->power_manager_client()->
173 IncreaseScreenBrightness(); 166 IncreaseScreenBrightness();
174 } 167 }
175 168
176 void SystemOptionsHandler::BluetoothEnableChangeCallback( 169 void SystemOptionsHandler::BluetoothEnableChangeCallback(
177 const ListValue* args) { 170 const ListValue* args) {
178 // TODO (kevers) - Call Bluetooth API to enable or disable. 171 // TODO(kevers) - Call Bluetooth API to enable or disable.
179 } 172 }
180 173
181 void SystemOptionsHandler::FindBluetoothDevicesCallback( 174 void SystemOptionsHandler::FindBluetoothDevicesCallback(
182 const ListValue* args) { 175 const ListValue* args) {
183 // TODO (kevers) - Call Bluetooth API to fetch devices. 176 // We only initiate a scan if we're running on Chrome OS. Otherwise, we
184 // Generate a fake list until the bluetooth API is ready. 177 // generate a fake device list.
185 // Afterwards keep fake list only for cases where emulating 178 if (!chromeos::system::runtime_environment::IsRunningOnChromeOS()) {
186 // ChromeOS from the desktop launch. 179 GenerateFakeDeviceList();
187 GenerateFakeDeviceList(); 180 return;
181 }
182
183 chromeos::BluetoothManager* bluetooth_manager =
184 chromeos::BluetoothManager::GetInstance();
185 CHECK(bluetooth_manager != NULL);
186
187 chromeos::BluetoothAdapter* default_adapter =
188 bluetooth_manager->DefaultAdapter();
189
190 if ((default_adapter == NULL && !default_adapter_id_.empty()) ||
191 (default_adapter != NULL &&
192 default_adapter_id_ != default_adapter->id())) {
193 LOG(WARNING) << "unexpected default adapter change from \""
194 << default_adapter_id_ << "\" to \"" << default_adapter->id() << "\"";
195 DefaultAdapterChanged(default_adapter);
196 }
197
198 if (default_adapter == NULL) {
199 LOG(WARNING) << "FindBluetoothDevicesCallback: no default adapter";
200 return;
201 }
202
203 default_adapter->StartDiscovery();
188 } 204 }
189 205
190 void SystemOptionsHandler::BluetoothDeviceNotification( 206 void SystemOptionsHandler::BluetoothDeviceNotification(
191 const DictionaryValue& device) { 207 const DictionaryValue& device) {
192 web_ui_->CallJavascriptFunction( 208 web_ui_->CallJavascriptFunction(
193 "options.SystemOptions.addBluetoothDevice", device); 209 "options.SystemOptions.addBluetoothDevice", device);
194 } 210 }
195 211
212 void SystemOptionsHandler::DefaultAdapterChanged(
213 chromeos::BluetoothAdapter* adapter) {
214 std::string old_default_adapter_id = default_adapter_id_;
215
216 if (adapter == NULL) {
217 default_adapter_id_.clear();
218 LOG(INFO) << "DefaultAdapterChanged: no default bluetooth adapter";
219 } else {
220 default_adapter_id_ = adapter->id();
221 LOG(INFO) << "DefaultAdapterChanged: " << default_adapter_id_;
222 }
223
224 if (default_adapter_id_ == old_default_adapter_id) {
225 return;
226 }
227
228 if (adapter != NULL) {
229 adapter->AddObserver(this);
230 }
231
232 // TODO(vlaviano): Respond to adapter change.
233 }
234
235 void SystemOptionsHandler::DiscoveryStarted(const std::string& adapter_id) {
236 VLOG(1) << "Discovery started on " << adapter_id;
237 }
238
239 void SystemOptionsHandler::DiscoveryEnded(const std::string& adapter_id) {
240 VLOG(1) << "Discovery ended on " << adapter_id;
241 web_ui_->CallJavascriptFunction(
242 "options.SystemOptions.notifyBluetoothSearchComplete");
243
244 // Stop the discovery session.
245 // TODO(vlaviano): We may want to expose DeviceDisappeared, remove the
246 // "Find devices" button, and let the discovery session continue throughout
247 // the time that the page is visible rather than just doing a single discovery
248 // cycle in response to a button click.
249 chromeos::BluetoothManager* bluetooth_manager =
250 chromeos::BluetoothManager::GetInstance();
251 CHECK(bluetooth_manager != NULL);
252
253 chromeos::BluetoothAdapter* default_adapter =
254 bluetooth_manager->DefaultAdapter();
255
256 if ((default_adapter == NULL && !default_adapter_id_.empty()) ||
257 (default_adapter != NULL &&
258 default_adapter_id_ != default_adapter->id())) {
259 LOG(WARNING) << "unexpected default adapter change from \""
260 << default_adapter_id_ << "\" to \"" << default_adapter->id() << "\"";
261 DefaultAdapterChanged(default_adapter);
262 }
263
264 if (default_adapter == NULL) {
265 LOG(WARNING) << "FindBluetoothDevicesCallback: no default adapter";
266 return;
267 }
268
269 default_adapter->StopDiscovery();
270 }
271
272 void SystemOptionsHandler::DeviceFound(const std::string& adapter_id,
273 chromeos::BluetoothDevice* device) {
274 VLOG(1) << "Device found on " << adapter_id;
275 CHECK(device != NULL);
276
277 // TODO(vlaviano): create more consistency between the UI and the
278 // underlying BluetoothDevice properties so that we can use
279 // BluetoothDevice::dictionary_rep() here.
280 DictionaryValue device_js_rep;
281 device_js_rep.SetString("deviceName", device->name());
282 device_js_rep.SetString("deviceId", device->address());
283 device_js_rep.SetString("deviceType", device->icon());
284 if (device->paired()) {
285 device_js_rep.SetString("deviceStatus", "bluetoothDeviceConnected");
286 } else {
287 device_js_rep.SetString("deviceStatus", "bluetoothDeviceNotPaired");
288 }
289
290 web_ui_->CallJavascriptFunction(
291 "options.SystemOptions.addBluetoothDevice", device_js_rep);
292 }
293
294 void SystemOptionsHandler::InitializeBluetooth() {
295 // Bluetooth support is a work in progress. Supress the feature unless
296 // explicitly enabled via a command line flag.
297 // TODO(kevers) - Test for presence of bluetooth hardware.
298 if (!CommandLine::ForCurrentProcess()
299 ->HasSwitch(switches::kEnableBluetooth)) {
300 return;
301 }
302 VLOG(1) << "InitializeBluetooth";
303
304 web_ui_->CallJavascriptFunction(
305 "options.SystemOptions.showBluetoothSettings");
306
307 chromeos::BluetoothManager* bluetooth_manager =
308 chromeos::BluetoothManager::GetInstance();
309 CHECK(bluetooth_manager != NULL);
310 bluetooth_manager->AddObserver(this);
311
312 chromeos::BluetoothAdapter* default_adapter =
313 bluetooth_manager->DefaultAdapter();
314 DefaultAdapterChanged(default_adapter);
315 }
316
317 void SystemOptionsHandler::ShutdownBluetooth() {
318 if (!CommandLine::ForCurrentProcess()
319 ->HasSwitch(switches::kEnableBluetooth)) {
320 return;
321 }
322 VLOG(1) << "ShutdownBluetooth";
323
324 chromeos::BluetoothManager* bluetooth_manager =
325 chromeos::BluetoothManager::GetInstance();
326 CHECK(bluetooth_manager != NULL);
327 bluetooth_manager->RemoveObserver(this);
328 }
329
196 void SystemOptionsHandler::GenerateFakeDeviceList() { 330 void SystemOptionsHandler::GenerateFakeDeviceList() {
197 // TODO (kevers) - Send notifications asynchronously simulating that the 331 // TODO(kevers) - Send notifications asynchronously simulating that the
198 // process of discovering bluetooth devices takes time. 332 // process of discovering bluetooth devices takes time.
199 // Fire each notification using OneShotTimer with a 333 // Fire each notification using OneShotTimer with a
200 // varying delay. 334 // varying delay.
201 std::string data[9] = { 335 std::string data[9] = {
202 "Fake Wireless Keyboard", "01-02-03-04-05", "keyboard", 336 "Fake Wireless Keyboard", "01-02-03-04-05", "keyboard",
203 "Fake Wireless Mouse", "02-03-04-05-01", "mouse", 337 "Fake Wireless Mouse", "02-03-04-05-01", "mouse",
204 "Fake Wireless Headset", "03-04-05-01-02", "headset"}; 338 "Fake Wireless Headset", "03-04-05-01-02", "headset"};
205 339
206 for (int i = 0; i < 3; i++) { 340 for (int i = 0; i < 3; i++) {
207 DictionaryValue device; 341 DictionaryValue device;
208 device.SetString("deviceName", data[3*i]); 342 device.SetString("deviceName", data[3*i]);
209 device.SetString("deviceId", data[3*i+1]); 343 device.SetString("deviceId", data[3*i+1]);
210 device.SetString("deviceType", data[3*i+2]); 344 device.SetString("deviceType", data[3*i+2]);
211 device.SetString("deviceStatus", "bluetoothDeviceNotPaired"); 345 device.SetString("deviceStatus", "bluetoothDeviceNotPaired");
212 web_ui_->CallJavascriptFunction( 346 web_ui_->CallJavascriptFunction(
213 "options.SystemOptions.addBluetoothDevice", device); 347 "options.SystemOptions.addBluetoothDevice", device);
214 } 348 }
215 web_ui_->CallJavascriptFunction( 349 web_ui_->CallJavascriptFunction(
216 "options.SystemOptions.notifyBluetoothSearchComplete"); 350 "options.SystemOptions.notifyBluetoothSearchComplete");
217 } 351 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698