Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 1 | // |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google, Inc. |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at: |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | #pragma once |
| 18 | |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 19 | #include <base/macros.h> |
| 20 | #include <base/message_loop/message_loop.h> |
| 21 | |
| 22 | namespace ipc { |
| 23 | class IPCManager; |
| 24 | } // namespace ipc |
| 25 | |
| 26 | namespace bluetooth { |
| 27 | |
| 28 | class CoreStack; |
| 29 | class Settings; |
| 30 | |
| 31 | // The Daemon class is a singleton that represents the root of the ownership |
| 32 | // hierarchy. The single instance sets up and owns the main event loop, the IPC |
| 33 | // handlers, global Settings, and the core Bluetooth stack. |
| 34 | class Daemon { |
| 35 | public: |
| 36 | // Initializes the daemon. This must be called to at the start of the |
| 37 | // application to set up the global daemon instance and everything it manages. |
| 38 | // Returns false in case of a failure. |
| 39 | static bool Initialize(); |
| 40 | |
| 41 | // Cleans up all the resources associated with the global Daemon object. |
| 42 | static void ShutDown(); |
| 43 | |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 44 | // Assigns the global Daemon instance for testing. Should only be called from |
| 45 | // test code. |
| 46 | static void InitializeForTesting(Daemon* test_daemon); |
| 47 | |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 48 | // Returns the singleton Daemon instance. All classes can interact with the |
| 49 | // Daemon, obtain its resources etc using this getter. |
| 50 | static Daemon* Get(); |
| 51 | |
| 52 | // The global Settings object. All classes have direct access to this through |
| 53 | // the Daemon object. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 54 | virtual Settings* GetSettings() const = 0; |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 55 | |
| 56 | // The main event loop. This should be used for any events and delayed tasks |
| 57 | // that should be executed on the daemon's main thread. |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 58 | virtual base::MessageLoop* GetMessageLoop() const = 0; |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 59 | |
| 60 | // Starts the daemon's main loop. |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 61 | virtual void StartMainLoop() = 0; |
| 62 | |
| 63 | protected: |
| 64 | Daemon() = default; |
| 65 | virtual ~Daemon() = default; |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 66 | |
| 67 | private: |
Arman Uguray | f8881fe | 2015-08-04 12:56:56 -0700 | [diff] [blame] | 68 | // Internal instance helper called by Initialize(). |
| 69 | virtual bool Init() = 0; |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 70 | |
| 71 | DISALLOW_COPY_AND_ASSIGN(Daemon); |
| 72 | }; |
| 73 | |
| 74 | } // namespace bluetooth |