service: Refactor IPC and singletons

This CL makes the following major refactors to the system service code:

  1. A new global Daemon object is introduced, which manages the main event
  loop and all other subsystems of the Bluetooth daemon. This object is the only
  singleton class and initializes and owns everything else.

  2. Everything that was a singleton and/or was initialized directly in main.cpp
  is now a) no longer a singleton; b) now initialized and owned by the global
  Daemon instance.

  3. All of the Chromecast specific IPC code has been moved into the ipc/
  subdirectory. This directory is meant for everything that is IPC related,
  paving the way for enabling multiple IPC systems (domain-socket based, Binder
  based, etc) simultaneously in the future. Main changes to the Chromecast IPC
  code are:

      a. All files and classes have been renamed to reflect the
      UNIX-domain-socket-specific nature of the IPC mechanism.

      b. The code no longer hogs up the main thread while listening for
      connections. All of this logic has been moved to a dedicated thread with
      its own MessageLoopForIO, so that it can use the built-in mechanisms for
      polling on client sockets in the future.

Bug: 22532180
Change-Id: I42db06dba6cff3bc8f8101a1ea2b6787a69409fd
diff --git a/service/daemon.h b/service/daemon.h
new file mode 100644
index 0000000..ab0225f
--- /dev/null
+++ b/service/daemon.h
@@ -0,0 +1,77 @@
+//
+//  Copyright (C) 2015 Google, Inc.
+//
+//  Licensed under the Apache License, Version 2.0 (the "License");
+//  you may not use this file except in compliance with the License.
+//  You may obtain a copy of the License at:
+//
+//  http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+
+#pragma once
+
+#include <memory>
+
+#include <base/macros.h>
+#include <base/message_loop/message_loop.h>
+
+namespace ipc {
+class IPCManager;
+}  // namespace ipc
+
+namespace bluetooth {
+
+class CoreStack;
+class Settings;
+
+// The Daemon class is a singleton that represents the root of the ownership
+// hierarchy. The single instance sets up and owns the main event loop, the IPC
+// handlers, global Settings, and the core Bluetooth stack.
+class Daemon {
+ public:
+  // Initializes the daemon. This must be called to at the start of the
+  // application to set up the global daemon instance and everything it manages.
+  // Returns false in case of a failure.
+  static bool Initialize();
+
+  // Cleans up all the resources associated with the global Daemon object.
+  static void ShutDown();
+
+  // Returns the singleton Daemon instance. All classes can interact with the
+  // Daemon, obtain its resources etc using this getter.
+  static Daemon* Get();
+
+  // The global Settings object. All classes have direct access to this through
+  // the Daemon object.
+  Settings* settings() const { return settings_.get(); }
+
+  // The main event loop. This should be used for any events and delayed tasks
+  // that should be executed on the daemon's main thread.
+  base::MessageLoop* message_loop() const { return message_loop_.get(); }
+
+  // Starts the daemon's main loop.
+  void StartMainLoop();
+
+ private:
+  Daemon();
+  ~Daemon();
+
+  // Private instance helper for Initialize().
+  bool Init();
+
+  bool initialized_;
+  std::unique_ptr<base::MessageLoop> message_loop_;
+  std::unique_ptr<Settings> settings_;
+  std::unique_ptr<CoreStack> core_stack_;
+  std::unique_ptr<ipc::IPCManager> ipc_manager_;
+
+  DISALLOW_COPY_AND_ASSIGN(Daemon);
+};
+
+}  // namespace bluetooth