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 | #include "service/ipc/ipc_manager.h" |
| 18 | |
Jakub Pawlowski | 470adc8 | 2016-02-22 12:57:31 -0800 | [diff] [blame] | 19 | #if !defined(OS_GENERIC) |
Arman Uguray | b2286f3 | 2015-08-05 21:19:02 -0700 | [diff] [blame] | 20 | #include "service/ipc/binder/ipc_handler_binder.h" |
Jakub Pawlowski | 79c2ff9 | 2016-10-31 12:56:12 -0700 | [diff] [blame] | 21 | #else |
| 22 | #include "service/ipc/dbus/ipc_handler_dbus.h" |
Jakub Pawlowski | 470adc8 | 2016-02-22 12:57:31 -0800 | [diff] [blame] | 23 | #endif // !defined(OS_GENERIC) |
Scott James Remnant | e415c05 | 2015-09-23 10:52:07 -0700 | [diff] [blame] | 24 | #include "service/ipc/ipc_handler_linux.h" |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ipc { |
| 27 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 28 | IPCManager::IPCManager(bluetooth::Adapter* adapter) : adapter_(adapter) { |
Arman Uguray | d6a4b0c | 2015-08-13 17:01:07 -0700 | [diff] [blame] | 29 | CHECK(adapter_); |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | IPCManager::~IPCManager() { |
Arman Uguray | e0d08c9 | 2015-08-04 18:59:27 -0700 | [diff] [blame] | 33 | // Don't rely on the handlers getting destroyed since another thread might be |
| 34 | // holding a reference to them. Instead, explicitly stop them here. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 35 | if (BinderStarted()) binder_handler_->Stop(); |
| 36 | if (LinuxStarted()) linux_handler_->Stop(); |
Jakub Pawlowski | 79c2ff9 | 2016-10-31 12:56:12 -0700 | [diff] [blame] | 37 | if (DBusStarted()) dbus_handler_->Stop(); |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Arman Uguray | e0d08c9 | 2015-08-04 18:59:27 -0700 | [diff] [blame] | 40 | bool IPCManager::Start(Type type, Delegate* delegate) { |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 41 | switch (type) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 42 | case TYPE_LINUX: |
| 43 | if (LinuxStarted()) { |
| 44 | LOG(ERROR) << "IPCManagerLinux already started."; |
| 45 | return false; |
| 46 | } |
Arman Uguray | b2286f3 | 2015-08-05 21:19:02 -0700 | [diff] [blame] | 47 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 48 | linux_handler_ = new IPCHandlerLinux(adapter_, delegate); |
| 49 | if (!linux_handler_->Run()) { |
| 50 | linux_handler_ = nullptr; |
| 51 | return false; |
| 52 | } |
| 53 | return true; |
Jakub Pawlowski | 79c2ff9 | 2016-10-31 12:56:12 -0700 | [diff] [blame] | 54 | |
Jakub Pawlowski | 470adc8 | 2016-02-22 12:57:31 -0800 | [diff] [blame] | 55 | #if !defined(OS_GENERIC) |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 56 | case TYPE_BINDER: |
| 57 | if (BinderStarted()) { |
| 58 | LOG(ERROR) << "IPCManagerBinder already started."; |
| 59 | return false; |
| 60 | } |
Arman Uguray | b2286f3 | 2015-08-05 21:19:02 -0700 | [diff] [blame] | 61 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 62 | binder_handler_ = new IPCHandlerBinder(adapter_, delegate); |
| 63 | if (!binder_handler_->Run()) { |
| 64 | binder_handler_ = nullptr; |
| 65 | return false; |
| 66 | } |
| 67 | return true; |
Jakub Pawlowski | 79c2ff9 | 2016-10-31 12:56:12 -0700 | [diff] [blame] | 68 | #else |
| 69 | case TYPE_DBUS: |
| 70 | if (DBusStarted()) { |
| 71 | LOG(ERROR) << "IPCManagerDBus already started."; |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | dbus_handler_ = new IPCHandlerDBus(adapter_, delegate); |
| 76 | if (!dbus_handler_->Run()) { |
| 77 | dbus_handler_ = nullptr; |
| 78 | return false; |
| 79 | } |
| 80 | return true; |
Jakub Pawlowski | 470adc8 | 2016-02-22 12:57:31 -0800 | [diff] [blame] | 81 | #endif // !defined(OS_GENERIC) |
Jakub Pawlowski | 79c2ff9 | 2016-10-31 12:56:12 -0700 | [diff] [blame] | 82 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 83 | default: |
| 84 | LOG(ERROR) << "Unsupported IPC type given: " << type; |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 90 | bool IPCManager::BinderStarted() const { return binder_handler_.get(); } |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 91 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 92 | bool IPCManager::LinuxStarted() const { return linux_handler_.get(); } |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 93 | |
Jakub Pawlowski | 79c2ff9 | 2016-10-31 12:56:12 -0700 | [diff] [blame] | 94 | bool IPCManager::DBusStarted() const { return dbus_handler_.get(); } |
| 95 | |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 96 | } // namespace ipc |