daemon.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // Copyright 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. #pragma once
  17. #include <base/macros.h>
  18. #include <base/message_loop/message_loop.h>
  19. namespace ipc {
  20. class IPCManager;
  21. } // namespace ipc
  22. namespace bluetooth {
  23. class CoreStack;
  24. class Settings;
  25. // The Daemon class is a singleton that represents the root of the ownership
  26. // hierarchy. The single instance sets up and owns the main event loop, the IPC
  27. // handlers, global Settings, and the core Bluetooth stack.
  28. class Daemon {
  29. public:
  30. // Initializes the daemon. This must be called to at the start of the
  31. // application to set up the global daemon instance and everything it manages.
  32. // Returns false in case of a failure.
  33. static bool Initialize();
  34. // Cleans up all the resources associated with the global Daemon object.
  35. static void ShutDown();
  36. // Assigns the global Daemon instance for testing. Should only be called from
  37. // test code.
  38. static void InitializeForTesting(Daemon* test_daemon);
  39. // Returns the singleton Daemon instance. All classes can interact with the
  40. // Daemon, obtain its resources etc using this getter.
  41. static Daemon* Get();
  42. // The global Settings object. All classes have direct access to this through
  43. // the Daemon object.
  44. virtual Settings* GetSettings() const = 0;
  45. // The main event loop. This should be used for any events and delayed tasks
  46. // that should be executed on the daemon's main thread.
  47. virtual base::MessageLoop* GetMessageLoop() const = 0;
  48. // Starts the daemon's main loop.
  49. virtual void StartMainLoop() = 0;
  50. protected:
  51. Daemon() = default;
  52. virtual ~Daemon() = default;
  53. private:
  54. // Internal instance helper called by Initialize().
  55. virtual bool Init() = 0;
  56. DISALLOW_COPY_AND_ASSIGN(Daemon);
  57. };
  58. } // namespace bluetooth