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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BRAILLE_DISPLAY_PRIVATE_BRAILLE_CONTROLLER_BRLAPI_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_BRAILLE_DISPLAY_PRIVATE_BRAILLE_CONTROLLER_BRLAPI_H_ 7 8 #include "base/files/file_path.h" 9 #include "base/files/file_path_watcher.h" 10 #include "base/gtest_prod_util.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/singleton.h" 13 #include "base/observer_list.h" 14 #include "chrome/browser/extensions/api/braille_display_private/braille_controller.h" 15 #include "chrome/browser/extensions/api/braille_display_private/brlapi_connection.h" 16 #include "library_loaders/libbrlapi.h" 17 18 namespace extensions { 19 namespace api { 20 namespace braille_display_private { 21 22 // BrailleController implementation based on libbrlapi from brltty. 23 class BrailleControllerImpl : public BrailleController { 24 public: 25 static BrailleControllerImpl* GetInstance(); 26 virtual scoped_ptr<DisplayState> GetDisplayState() OVERRIDE; 27 virtual void WriteDots(const std::string& cells) OVERRIDE; 28 virtual void AddObserver(BrailleObserver* observer) OVERRIDE; 29 virtual void RemoveObserver(BrailleObserver* observer) OVERRIDE; 30 31 private: 32 // For the unit tests. 33 friend class BrailleDisplayPrivateApiTest; 34 friend class MockBrlapiConnection; 35 36 BrailleControllerImpl(); 37 virtual ~BrailleControllerImpl(); 38 void TryLoadLibBrlApi(); 39 40 typedef base::Callback<scoped_ptr<BrlapiConnection>()> 41 CreateBrlapiConnectionFunction; 42 43 // For dependency injection in tests. Sets the function used to create 44 // brlapi connections. 45 void SetCreateBrlapiConnectionForTesting( 46 const CreateBrlapiConnectionFunction& callback); 47 48 // Makes the controller try to reconnect (if disconnected) as if the brlapi 49 // socket directory had changed. 50 void PokeSocketDirForTesting(); 51 52 // Tries to connect and starts watching for new brlapi servers. 53 // No-op if already called. 54 void StartConnecting(); 55 void StartWatchingSocketDirOnFileThread(); 56 void OnSocketDirChangedOnFileThread(const base::FilePath& path, bool error); 57 void OnSocketDirChangedOnIOThread(); 58 void TryToConnect(); 59 void ResetRetryConnectHorizon(); 60 void ScheduleTryToConnect(); 61 void Disconnect(); 62 scoped_ptr<BrlapiConnection> CreateBrlapiConnection(); 63 void DispatchKeys(); 64 void DispatchKeyEvent(scoped_ptr<KeyEvent> event); 65 void DispatchOnDisplayStateChanged(scoped_ptr<DisplayState> new_state); 66 67 CreateBrlapiConnectionFunction create_brlapi_connection_function_; 68 69 // Manipulated on the IO thread. 70 LibBrlapiLoader libbrlapi_loader_; 71 scoped_ptr<BrlapiConnection> connection_; 72 bool started_connecting_; 73 bool connect_scheduled_; 74 base::Time retry_connect_horizon_; 75 76 // Manipulated on the UI thread. 77 ObserverList<BrailleObserver> observers_; 78 79 // Manipulated on the FILE thread. 80 base::FilePathWatcher file_path_watcher_; 81 82 friend struct DefaultSingletonTraits<BrailleControllerImpl>; 83 84 DISALLOW_COPY_AND_ASSIGN(BrailleControllerImpl); 85 }; 86 87 } // namespace braille_display_private 88 } // namespace api 89 } // namespace extensions 90 91 #endif // CHROME_BROWSER_EXTENSIONS_API_BRAILLE_DISPLAY_PRIVATE_BRAILLE_CONTROLLER_BRLAPI_H_ 92