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.
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_
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"
18 namespace extensions
{
20 namespace braille_display_private
{
22 // BrailleController implementation based on libbrlapi from brltty.
23 class BrailleControllerImpl
: public BrailleController
{
25 static BrailleControllerImpl
* GetInstance();
26 scoped_ptr
<DisplayState
> GetDisplayState() override
;
27 void WriteDots(const std::vector
<char>& cells
) override
;
28 void AddObserver(BrailleObserver
* observer
) override
;
29 void RemoveObserver(BrailleObserver
* observer
) override
;
32 // For the unit tests.
33 friend class BrailleDisplayPrivateApiTest
;
34 friend class MockBrlapiConnection
;
36 BrailleControllerImpl();
37 ~BrailleControllerImpl() override
;
38 void TryLoadLibBrlApi();
40 typedef base::Callback
<scoped_ptr
<BrlapiConnection
>()>
41 CreateBrlapiConnectionFunction
;
43 // For dependency injection in tests. Sets the function used to create
44 // brlapi connections.
45 void SetCreateBrlapiConnectionForTesting(
46 const CreateBrlapiConnectionFunction
& callback
);
48 // Makes the controller try to reconnect (if disconnected) as if the brlapi
49 // socket directory had changed.
50 void PokeSocketDirForTesting();
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();
59 void ResetRetryConnectHorizon();
60 void ScheduleTryToConnect();
62 scoped_ptr
<BrlapiConnection
> CreateBrlapiConnection();
64 void DispatchKeyEvent(scoped_ptr
<KeyEvent
> event
);
65 void DispatchOnDisplayStateChanged(scoped_ptr
<DisplayState
> new_state
);
67 CreateBrlapiConnectionFunction create_brlapi_connection_function_
;
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_
;
76 // Manipulated on the UI thread.
77 base::ObserverList
<BrailleObserver
> observers_
;
79 // Manipulated on the FILE thread.
80 base::FilePathWatcher file_path_watcher_
;
82 friend struct DefaultSingletonTraits
<BrailleControllerImpl
>;
84 DISALLOW_COPY_AND_ASSIGN(BrailleControllerImpl
);
87 } // namespace braille_display_private
89 } // namespace extensions
91 #endif // CHROME_BROWSER_EXTENSIONS_API_BRAILLE_DISPLAY_PRIVATE_BRAILLE_CONTROLLER_BRLAPI_H_