blob: ab0225f0cb1645da97d1c3063c7b85895bed27e5 [file] [log] [blame]
Arman Ugurayfe65fb72015-07-24 19:14:42 -07001//
2// Copyright (C) 2015 Google, Inc.
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
19#include <memory>
20
21#include <base/macros.h>
22#include <base/message_loop/message_loop.h>
23
24namespace ipc {
25class IPCManager;
26} // namespace ipc
27
28namespace bluetooth {
29
30class CoreStack;
31class Settings;
32
33// The Daemon class is a singleton that represents the root of the ownership
34// hierarchy. The single instance sets up and owns the main event loop, the IPC
35// handlers, global Settings, and the core Bluetooth stack.
36class Daemon {
37 public:
38 // Initializes the daemon. This must be called to at the start of the
39 // application to set up the global daemon instance and everything it manages.
40 // Returns false in case of a failure.
41 static bool Initialize();
42
43 // Cleans up all the resources associated with the global Daemon object.
44 static void ShutDown();
45
46 // Returns the singleton Daemon instance. All classes can interact with the
47 // Daemon, obtain its resources etc using this getter.
48 static Daemon* Get();
49
50 // The global Settings object. All classes have direct access to this through
51 // the Daemon object.
52 Settings* settings() const { return settings_.get(); }
53
54 // The main event loop. This should be used for any events and delayed tasks
55 // that should be executed on the daemon's main thread.
56 base::MessageLoop* message_loop() const { return message_loop_.get(); }
57
58 // Starts the daemon's main loop.
59 void StartMainLoop();
60
61 private:
62 Daemon();
63 ~Daemon();
64
65 // Private instance helper for Initialize().
66 bool Init();
67
68 bool initialized_;
69 std::unique_ptr<base::MessageLoop> message_loop_;
70 std::unique_ptr<Settings> settings_;
71 std::unique_ptr<CoreStack> core_stack_;
72 std::unique_ptr<ipc::IPCManager> ipc_manager_;
73
74 DISALLOW_COPY_AND_ASSIGN(Daemon);
75};
76
77} // namespace bluetooth