| 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/brlapi_connectio
n.h" | 5 #include "chrome/browser/extensions/api/braille_display_private/brlapi_connectio
n.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 | 8 |
| 9 #include "base/files/file_descriptor_watcher_posix.h" |
| 9 #include "base/macros.h" | 10 #include "base/macros.h" |
| 10 #include "base/memory/free_deleter.h" | 11 #include "base/memory/free_deleter.h" |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 | 14 |
| 15 namespace extensions { | 15 namespace extensions { |
| 16 using base::MessageLoopForIO; | |
| 17 namespace api { | 16 namespace api { |
| 18 namespace braille_display_private { | 17 namespace braille_display_private { |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 // Default virtual terminal. This can be overriden by setting the | 20 // Default virtual terminal. This can be overriden by setting the |
| 22 // WINDOWPATH environment variable. This is only used when not running | 21 // WINDOWPATH environment variable. This is only used when not running |
| 23 // under Crhome OS (that is in aura for a Linux desktop). | 22 // under Crhome OS (that is in aura for a Linux desktop). |
| 24 // TODO(plundblad): Find a way to detect the controlling terminal of the | 23 // TODO(plundblad): Find a way to detect the controlling terminal of the |
| 25 // X server. | 24 // X server. |
| 26 static const int kDefaultTtyLinux = 7; | 25 static const int kDefaultTtyLinux = 7; |
| 27 #if defined(OS_CHROMEOS) | 26 #if defined(OS_CHROMEOS) |
| 28 // The GUI is always running on vt1 in Chrome OS. | 27 // The GUI is always running on vt1 in Chrome OS. |
| 29 static const int kDefaultTtyChromeOS = 1; | 28 static const int kDefaultTtyChromeOS = 1; |
| 30 #endif | 29 #endif |
| 31 } // namespace | 30 } // namespace |
| 32 | 31 |
| 33 class BrlapiConnectionImpl : public BrlapiConnection, | 32 class BrlapiConnectionImpl : public BrlapiConnection { |
| 34 MessageLoopForIO::Watcher { | |
| 35 public: | 33 public: |
| 36 explicit BrlapiConnectionImpl(LibBrlapiLoader* loader) : | 34 explicit BrlapiConnectionImpl(LibBrlapiLoader* loader) : |
| 37 libbrlapi_loader_(loader) {} | 35 libbrlapi_loader_(loader) {} |
| 38 | 36 |
| 39 ~BrlapiConnectionImpl() override { Disconnect(); } | 37 ~BrlapiConnectionImpl() override { Disconnect(); } |
| 40 | 38 |
| 41 ConnectResult Connect(const OnDataReadyCallback& on_data_ready) override; | 39 ConnectResult Connect(const OnDataReadyCallback& on_data_ready) override; |
| 42 void Disconnect() override; | 40 void Disconnect() override; |
| 43 bool Connected() override { return handle_ != nullptr; } | 41 bool Connected() override { return handle_ != nullptr; } |
| 44 brlapi_error_t* BrlapiError() override; | 42 brlapi_error_t* BrlapiError() override; |
| 45 std::string BrlapiStrError() override; | 43 std::string BrlapiStrError() override; |
| 46 bool GetDisplaySize(size_t* size) override; | 44 bool GetDisplaySize(size_t* size) override; |
| 47 bool WriteDots(const unsigned char* cells) override; | 45 bool WriteDots(const unsigned char* cells) override; |
| 48 int ReadKey(brlapi_keyCode_t* keyCode) override; | 46 int ReadKey(brlapi_keyCode_t* keyCode) override; |
| 49 | 47 |
| 50 // MessageLoopForIO::Watcher | |
| 51 void OnFileCanReadWithoutBlocking(int fd) override { on_data_ready_.Run(); } | |
| 52 | |
| 53 void OnFileCanWriteWithoutBlocking(int fd) override {} | |
| 54 | |
| 55 private: | 48 private: |
| 56 bool CheckConnected(); | 49 bool CheckConnected(); |
| 57 ConnectResult ConnectResultForError(); | 50 ConnectResult ConnectResultForError(); |
| 58 | 51 |
| 59 LibBrlapiLoader* libbrlapi_loader_; | 52 LibBrlapiLoader* libbrlapi_loader_; |
| 60 std::unique_ptr<brlapi_handle_t, base::FreeDeleter> handle_; | 53 std::unique_ptr<brlapi_handle_t, base::FreeDeleter> handle_; |
| 61 MessageLoopForIO::FileDescriptorWatcher fd_controller_; | 54 std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_controller_; |
| 62 OnDataReadyCallback on_data_ready_; | |
| 63 | 55 |
| 64 DISALLOW_COPY_AND_ASSIGN(BrlapiConnectionImpl); | 56 DISALLOW_COPY_AND_ASSIGN(BrlapiConnectionImpl); |
| 65 }; | 57 }; |
| 66 | 58 |
| 67 BrlapiConnection::BrlapiConnection() { | 59 BrlapiConnection::BrlapiConnection() { |
| 68 } | 60 } |
| 69 | 61 |
| 70 BrlapiConnection::~BrlapiConnection() { | 62 BrlapiConnection::~BrlapiConnection() { |
| 71 } | 63 } |
| 72 | 64 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 BRLAPI_KEY_TYPE_CMD | BRLAPI_KEY_CMD_PASSDOTS, | 117 BRLAPI_KEY_TYPE_CMD | BRLAPI_KEY_CMD_PASSDOTS, |
| 126 }; | 118 }; |
| 127 if (libbrlapi_loader_->brlapi__acceptKeys( | 119 if (libbrlapi_loader_->brlapi__acceptKeys( |
| 128 handle_.get(), brlapi_rangeType_command, extraKeys, | 120 handle_.get(), brlapi_rangeType_command, extraKeys, |
| 129 arraysize(extraKeys)) < 0) { | 121 arraysize(extraKeys)) < 0) { |
| 130 LOG(ERROR) << "Couldn't acceptKeys: " << BrlapiStrError(); | 122 LOG(ERROR) << "Couldn't acceptKeys: " << BrlapiStrError(); |
| 131 Disconnect(); | 123 Disconnect(); |
| 132 return CONNECT_ERROR_RETRY; | 124 return CONNECT_ERROR_RETRY; |
| 133 } | 125 } |
| 134 | 126 |
| 135 if (!MessageLoopForIO::current()->WatchFileDescriptor( | 127 fd_controller_ = |
| 136 fd, true, MessageLoopForIO::WATCH_READ, &fd_controller_, this)) { | 128 base::FileDescriptorWatcher::WatchReadable(fd, on_data_ready); |
| 137 LOG(ERROR) << "Couldn't watch file descriptor " << fd; | |
| 138 Disconnect(); | |
| 139 return CONNECT_ERROR_RETRY; | |
| 140 } | |
| 141 | |
| 142 on_data_ready_ = on_data_ready; | |
| 143 | 129 |
| 144 return CONNECT_SUCCESS; | 130 return CONNECT_SUCCESS; |
| 145 } | 131 } |
| 146 | 132 |
| 147 void BrlapiConnectionImpl::Disconnect() { | 133 void BrlapiConnectionImpl::Disconnect() { |
| 148 if (!handle_) { | 134 if (!handle_) { |
| 149 return; | 135 return; |
| 150 } | 136 } |
| 151 fd_controller_.StopWatchingFileDescriptor(); | 137 fd_controller_.reset(); |
| 152 libbrlapi_loader_->brlapi__closeConnection( | 138 libbrlapi_loader_->brlapi__closeConnection( |
| 153 handle_.get()); | 139 handle_.get()); |
| 154 handle_.reset(); | 140 handle_.reset(); |
| 155 } | 141 } |
| 156 | 142 |
| 157 brlapi_error_t* BrlapiConnectionImpl::BrlapiError() { | 143 brlapi_error_t* BrlapiConnectionImpl::BrlapiError() { |
| 158 return libbrlapi_loader_->brlapi_error_location(); | 144 return libbrlapi_loader_->brlapi_error_location(); |
| 159 } | 145 } |
| 160 | 146 |
| 161 std::string BrlapiConnectionImpl::BrlapiStrError() { | 147 std::string BrlapiConnectionImpl::BrlapiStrError() { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 if (error->brlerrno == BRLAPI_ERROR_LIBCERR | 196 if (error->brlerrno == BRLAPI_ERROR_LIBCERR |
| 211 && error->libcerrno == ENOENT) { | 197 && error->libcerrno == ENOENT) { |
| 212 return CONNECT_ERROR_NO_RETRY; | 198 return CONNECT_ERROR_NO_RETRY; |
| 213 } | 199 } |
| 214 return CONNECT_ERROR_RETRY; | 200 return CONNECT_ERROR_RETRY; |
| 215 } | 201 } |
| 216 | 202 |
| 217 } // namespace braille_display_private | 203 } // namespace braille_display_private |
| 218 } // namespace api | 204 } // namespace api |
| 219 } // namespace extensions | 205 } // namespace extensions |
| OLD | NEW |