OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extensions/api/braille_display_private/braille_controll
er_brlapi.h" | 5 #include "chrome/browser/extensions/api/braille_display_private/braille_controll
er_brlapi.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <cerrno> | 10 #include <cerrno> |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 "libbrlapi.so.0.5", | 75 "libbrlapi.so.0.5", |
76 "libbrlapi.so.0.6" | 76 "libbrlapi.so.0.6" |
77 }; | 77 }; |
78 for (size_t i = 0; i < arraysize(kSupportedVersions); ++i) { | 78 for (size_t i = 0; i < arraysize(kSupportedVersions); ++i) { |
79 if (libbrlapi_loader_.Load(kSupportedVersions[i])) | 79 if (libbrlapi_loader_.Load(kSupportedVersions[i])) |
80 return; | 80 return; |
81 } | 81 } |
82 LOG(WARNING) << "Couldn't load libbrlapi: " << strerror(errno); | 82 LOG(WARNING) << "Couldn't load libbrlapi: " << strerror(errno); |
83 } | 83 } |
84 | 84 |
85 scoped_ptr<DisplayState> BrailleControllerImpl::GetDisplayState() { | 85 std::unique_ptr<DisplayState> BrailleControllerImpl::GetDisplayState() { |
86 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 86 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
87 StartConnecting(); | 87 StartConnecting(); |
88 scoped_ptr<DisplayState> display_state(new DisplayState); | 88 std::unique_ptr<DisplayState> display_state(new DisplayState); |
89 if (connection_.get() && connection_->Connected()) { | 89 if (connection_.get() && connection_->Connected()) { |
90 size_t size; | 90 size_t size; |
91 if (!connection_->GetDisplaySize(&size)) { | 91 if (!connection_->GetDisplaySize(&size)) { |
92 Disconnect(); | 92 Disconnect(); |
93 } else if (size > 0) { // size == 0 means no display present. | 93 } else if (size > 0) { // size == 0 means no display present. |
94 display_state->available = true; | 94 display_state->available = true; |
95 display_state->text_cell_count.reset(new int(size)); | 95 display_state->text_cell_count.reset(new int(size)); |
96 } | 96 } |
97 } | 97 } |
98 return display_state; | 98 return display_state; |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 &BrailleControllerImpl::TryToConnect, | 254 &BrailleControllerImpl::TryToConnect, |
255 base::Unretained(this)), | 255 base::Unretained(this)), |
256 delay); | 256 delay); |
257 } | 257 } |
258 | 258 |
259 void BrailleControllerImpl::Disconnect() { | 259 void BrailleControllerImpl::Disconnect() { |
260 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 260 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
261 if (!connection_ || !connection_->Connected()) | 261 if (!connection_ || !connection_->Connected()) |
262 return; | 262 return; |
263 connection_->Disconnect(); | 263 connection_->Disconnect(); |
264 DispatchOnDisplayStateChanged(scoped_ptr<DisplayState>(new DisplayState())); | 264 DispatchOnDisplayStateChanged( |
| 265 std::unique_ptr<DisplayState>(new DisplayState())); |
265 } | 266 } |
266 | 267 |
267 scoped_ptr<BrlapiConnection> BrailleControllerImpl::CreateBrlapiConnection() { | 268 std::unique_ptr<BrlapiConnection> |
| 269 BrailleControllerImpl::CreateBrlapiConnection() { |
268 DCHECK(libbrlapi_loader_.loaded()); | 270 DCHECK(libbrlapi_loader_.loaded()); |
269 return BrlapiConnection::Create(&libbrlapi_loader_); | 271 return BrlapiConnection::Create(&libbrlapi_loader_); |
270 } | 272 } |
271 | 273 |
272 void BrailleControllerImpl::DispatchKeys() { | 274 void BrailleControllerImpl::DispatchKeys() { |
273 DCHECK(connection_.get()); | 275 DCHECK(connection_.get()); |
274 brlapi_keyCode_t code; | 276 brlapi_keyCode_t code; |
275 while (true) { | 277 while (true) { |
276 int result = connection_->ReadKey(&code); | 278 int result = connection_->ReadKey(&code); |
277 if (result < 0) { // Error. | 279 if (result < 0) { // Error. |
278 brlapi_error_t* err = connection_->BrlapiError(); | 280 brlapi_error_t* err = connection_->BrlapiError(); |
279 if (err->brlerrno == BRLAPI_ERROR_LIBCERR && err->libcerrno == EINTR) | 281 if (err->brlerrno == BRLAPI_ERROR_LIBCERR && err->libcerrno == EINTR) |
280 continue; | 282 continue; |
281 // Disconnect on other errors. | 283 // Disconnect on other errors. |
282 VLOG(1) << "BrlAPI error: " << connection_->BrlapiStrError(); | 284 VLOG(1) << "BrlAPI error: " << connection_->BrlapiStrError(); |
283 Disconnect(); | 285 Disconnect(); |
284 return; | 286 return; |
285 } else if (result == 0) { // No more data. | 287 } else if (result == 0) { // No more data. |
286 return; | 288 return; |
287 } | 289 } |
288 scoped_ptr<KeyEvent> event = BrlapiKeyCodeToEvent(code); | 290 std::unique_ptr<KeyEvent> event = BrlapiKeyCodeToEvent(code); |
289 if (event) | 291 if (event) |
290 DispatchKeyEvent(std::move(event)); | 292 DispatchKeyEvent(std::move(event)); |
291 } | 293 } |
292 } | 294 } |
293 | 295 |
294 void BrailleControllerImpl::DispatchKeyEvent(scoped_ptr<KeyEvent> event) { | 296 void BrailleControllerImpl::DispatchKeyEvent(std::unique_ptr<KeyEvent> event) { |
295 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 297 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
296 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 298 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
297 base::Bind( | 299 base::Bind( |
298 &BrailleControllerImpl::DispatchKeyEvent, | 300 &BrailleControllerImpl::DispatchKeyEvent, |
299 base::Unretained(this), | 301 base::Unretained(this), |
300 base::Passed(&event))); | 302 base::Passed(&event))); |
301 return; | 303 return; |
302 } | 304 } |
303 VLOG(1) << "Dispatching key event: " << *event->ToValue(); | 305 VLOG(1) << "Dispatching key event: " << *event->ToValue(); |
304 FOR_EACH_OBSERVER(BrailleObserver, observers_, OnBrailleKeyEvent(*event)); | 306 FOR_EACH_OBSERVER(BrailleObserver, observers_, OnBrailleKeyEvent(*event)); |
305 } | 307 } |
306 | 308 |
307 void BrailleControllerImpl::DispatchOnDisplayStateChanged( | 309 void BrailleControllerImpl::DispatchOnDisplayStateChanged( |
308 scoped_ptr<DisplayState> new_state) { | 310 std::unique_ptr<DisplayState> new_state) { |
309 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 311 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
310 if (!BrowserThread::PostTask( | 312 if (!BrowserThread::PostTask( |
311 BrowserThread::UI, FROM_HERE, | 313 BrowserThread::UI, FROM_HERE, |
312 base::Bind(&BrailleControllerImpl::DispatchOnDisplayStateChanged, | 314 base::Bind(&BrailleControllerImpl::DispatchOnDisplayStateChanged, |
313 base::Unretained(this), | 315 base::Unretained(this), |
314 base::Passed(&new_state)))) { | 316 base::Passed(&new_state)))) { |
315 NOTREACHED(); | 317 NOTREACHED(); |
316 } | 318 } |
317 return; | 319 return; |
318 } | 320 } |
319 FOR_EACH_OBSERVER(BrailleObserver, observers_, | 321 FOR_EACH_OBSERVER(BrailleObserver, observers_, |
320 OnBrailleDisplayStateChanged(*new_state)); | 322 OnBrailleDisplayStateChanged(*new_state)); |
321 } | 323 } |
322 | 324 |
323 } // namespace braille_display_private | 325 } // namespace braille_display_private |
324 } // namespace api | 326 } // namespace api |
325 } // namespace extensions | 327 } // namespace extensions |
OLD | NEW |