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 17 #pragma once 18 19 #include <memory> 20 21 #include <base/memory/ref_counted.h> 22 23 namespace bluetooth { 24 class Adapter; 25 } // namespace bluetooth 26 27 namespace ipc { 28 29 class IPCHandler; 30 31 // IPCManager is a class for initializing and running supported IPC mechanisms. 32 // It manages the life-time of different IPC flavors that are available on the 33 // system. There are two flavors: a Linux sequential packet domain socket based 34 // system and one based on the Binder-based android.bluetooth framework. 35 class IPCManager { 36 public: 37 // Possible IPC types. 38 enum Type { 39 TYPE_LINUX, // IPC based on a Linux sequential packet domain socket 40 TYPE_BINDER, // IPC based on the Binder 41 TYPE_DBUS // IPC based on the DBus 42 }; 43 44 // Interface for observing events from an IPC mechanism. These methods will be 45 // called on the thread that started the particular IPC type. 46 class Delegate { 47 public: 48 Delegate() = default; 49 Delegate(const Delegate&) = delete; 50 Delegate& operator=(const Delegate&) = delete; 51 52 virtual ~Delegate() = default; 53 54 // Called when an IPC mechanism has successfully started and is ready for 55 // client connections. 56 virtual void OnIPCHandlerStarted(Type type) = 0; 57 58 // Called when an IPC mechanism has stopped. This may happen due to an error 59 // in initialization or due to a regular shut down routine. 60 virtual void OnIPCHandlerStopped(Type type) = 0; 61 }; 62 63 explicit IPCManager(bluetooth::Adapter* adapter); 64 IPCManager(const IPCManager&) = delete; 65 IPCManager& operator=(const IPCManager&) = delete; 66 67 ~IPCManager(); 68 69 // Initialize the underlying IPC handler based on |type|, if that type has not 70 // yet been initialized and returns true on success. Returns false if that 71 // type has already been initialized or an error occurs. 72 // 73 // If TYPE_LINUX is given, the file path to use for the domain socket will be 74 // obtained from the global Settings object. Hence, the Settings object must 75 // have been initialized before calling this method. 76 // 77 // |delegate| must out-live the IPCManager and the underlying handler. Users 78 // can guarantee proper clean up by deallocating |delegate| when or after 79 // Delegate::OnIPCHandlerStopped is called. It is safe to destroy |delegate| 80 // after destroying the IPCManager instance, as the destructor will join and 81 // clean up all underlying threads. 82 bool Start(Type type, Delegate* delegate); 83 84 // Returns true if an IPC type has been initialized. 85 bool BinderStarted() const; 86 bool LinuxStarted() const; 87 bool DBusStarted() const; 88 89 private: 90 IPCManager() = default; 91 92 // Pointers to the different IPC handler classes. These are initialized and 93 // owned by us. 94 scoped_refptr<IPCHandler> binder_handler_; 95 scoped_refptr<IPCHandler> linux_handler_; 96 scoped_refptr<IPCHandler> dbus_handler_; 97 98 // The Bluetooth adapter instance. This is owned by Daemon so we keep a raw 99 // pointer to it. 100 bluetooth::Adapter* adapter_; 101 }; 102 103 } // namespace ipc 104