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 <algorithm> | 7 #include <algorithm> |
8 #include <cerrno> | 8 #include <cerrno> |
9 #include <cstring> | 9 #include <cstring> |
10 #include <vector> | 10 #include <vector> |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 if (function.is_null()) { | 127 if (function.is_null()) { |
128 create_brlapi_connection_function_ = base::Bind( | 128 create_brlapi_connection_function_ = base::Bind( |
129 &BrailleControllerImpl::CreateBrlapiConnection, | 129 &BrailleControllerImpl::CreateBrlapiConnection, |
130 base::Unretained(this)); | 130 base::Unretained(this)); |
131 } else { | 131 } else { |
132 create_brlapi_connection_function_ = function; | 132 create_brlapi_connection_function_ = function; |
133 } | 133 } |
134 } | 134 } |
135 | 135 |
136 void BrailleControllerImpl::PokeSocketDirForTesting() { | 136 void BrailleControllerImpl::PokeSocketDirForTesting() { |
137 OnSocketDirChanged(base::FilePath(), false /*error*/); | 137 OnSocketDirChangedOnIOThread(); |
138 } | 138 } |
139 | 139 |
140 void BrailleControllerImpl::StartConnecting() { | 140 void BrailleControllerImpl::StartConnecting() { |
141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
142 if (started_connecting_) | 142 if (started_connecting_) |
143 return; | 143 return; |
144 started_connecting_ = true; | 144 started_connecting_ = true; |
145 TryLoadLibBrlApi(); | 145 TryLoadLibBrlApi(); |
146 if (!libbrlapi_loader_.loaded()) { | 146 if (!libbrlapi_loader_.loaded()) { |
147 return; | 147 return; |
148 } | 148 } |
149 base::FilePath brlapi_dir(BRLAPI_SOCKETPATH); | 149 // One could argue that there is a race condition between starting to |
150 if (!file_path_watcher_.Watch( | 150 // watch the socket asynchonously and trying to connect. While this is true, |
151 brlapi_dir, false, base::Bind( | 151 // we are actually retrying to connect for a while, so this doesn't matter |
152 &BrailleControllerImpl::OnSocketDirChanged, | 152 // in practice. |
153 base::Unretained(this)))) { | 153 BrowserThread::PostTask( |
154 LOG(WARNING) << "Couldn't watch brlapi directory " << BRLAPI_SOCKETPATH; | 154 BrowserThread::FILE, FROM_HERE, base::Bind( |
155 return; | 155 &BrailleControllerImpl::StartWatchingSocketDirOnFileThread, |
156 } | 156 base::Unretained(this))); |
157 ResetRetryConnectHorizon(); | 157 ResetRetryConnectHorizon(); |
158 TryToConnect(); | 158 TryToConnect(); |
159 } | 159 } |
160 | 160 |
161 void BrailleControllerImpl::OnSocketDirChanged(const base::FilePath& path, | 161 void BrailleControllerImpl::StartWatchingSocketDirOnFileThread() { |
162 bool error) { | 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 163 base::FilePath brlapi_dir(BRLAPI_SOCKETPATH); |
164 DCHECK(libbrlapi_loader_.loaded()); | 164 if (!file_path_watcher_.Watch( |
| 165 brlapi_dir, false, base::Bind( |
| 166 &BrailleControllerImpl::OnSocketDirChangedOnFileThread, |
| 167 base::Unretained(this)))) { |
| 168 LOG(WARNING) << "Couldn't watch brlapi directory " << BRLAPI_SOCKETPATH; |
| 169 } |
| 170 } |
| 171 |
| 172 void BrailleControllerImpl::OnSocketDirChangedOnFileThread( |
| 173 const base::FilePath& path, bool error) { |
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
165 if (error) { | 175 if (error) { |
166 LOG(ERROR) << "Error watching brlapi directory: " << path.value(); | 176 LOG(ERROR) << "Error watching brlapi directory: " << path.value(); |
167 return; | 177 return; |
168 } | 178 } |
| 179 BrowserThread::PostTask( |
| 180 BrowserThread::IO, FROM_HERE, base::Bind( |
| 181 &BrailleControllerImpl::OnSocketDirChangedOnIOThread, |
| 182 base::Unretained(this))); |
| 183 } |
| 184 |
| 185 void BrailleControllerImpl::OnSocketDirChangedOnIOThread() { |
| 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
169 LOG(INFO) << "BrlAPI directory changed"; | 187 LOG(INFO) << "BrlAPI directory changed"; |
170 // Every directory change resets the max retry time to the appropriate delay | 188 // Every directory change resets the max retry time to the appropriate delay |
171 // into the future. | 189 // into the future. |
172 ResetRetryConnectHorizon(); | 190 ResetRetryConnectHorizon(); |
173 // Try after an initial delay to give the driver a chance to connect. | 191 // Try after an initial delay to give the driver a chance to connect. |
174 ScheduleTryToConnect(); | 192 ScheduleTryToConnect(); |
175 } | 193 } |
176 | 194 |
177 void BrailleControllerImpl::TryToConnect() { | 195 void BrailleControllerImpl::TryToConnect() { |
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 base::Passed(&new_state))); | 341 base::Passed(&new_state))); |
324 return; | 342 return; |
325 } | 343 } |
326 FOR_EACH_OBSERVER(BrailleObserver, observers_, | 344 FOR_EACH_OBSERVER(BrailleObserver, observers_, |
327 OnDisplayStateChanged(*new_state)); | 345 OnDisplayStateChanged(*new_state)); |
328 } | 346 } |
329 | 347 |
330 } // namespace braille_display_private | 348 } // namespace braille_display_private |
331 } // namespace api | 349 } // namespace api |
332 } // namespace extensions | 350 } // namespace extensions |
OLD | NEW |