• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "service/ipc/ipc_manager.h"
18 
19 #if !defined(OS_GENERIC)
20 #include "service/ipc/binder/ipc_handler_binder.h"
21 #else
22 #include "service/ipc/dbus/ipc_handler_dbus.h"
23 #endif  // !defined(OS_GENERIC)
24 #include "service/ipc/ipc_handler_linux.h"
25 
26 namespace ipc {
27 
IPCManager(bluetooth::Adapter * adapter)28 IPCManager::IPCManager(bluetooth::Adapter* adapter) : adapter_(adapter) {
29   CHECK(adapter_);
30 }
31 
~IPCManager()32 IPCManager::~IPCManager() {
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.
35   if (BinderStarted()) binder_handler_->Stop();
36   if (LinuxStarted()) linux_handler_->Stop();
37   if (DBusStarted()) dbus_handler_->Stop();
38 }
39 
Start(Type type,Delegate * delegate)40 bool IPCManager::Start(Type type, Delegate* delegate) {
41   switch (type) {
42     case TYPE_LINUX:
43       if (LinuxStarted()) {
44         LOG(ERROR) << "IPCManagerLinux already started.";
45         return false;
46       }
47 
48       linux_handler_ = new IPCHandlerLinux(adapter_, delegate);
49       if (!linux_handler_->Run()) {
50         linux_handler_ = nullptr;
51         return false;
52       }
53       return true;
54 
55 #if !defined(OS_GENERIC)
56     case TYPE_BINDER:
57       if (BinderStarted()) {
58         LOG(ERROR) << "IPCManagerBinder already started.";
59         return false;
60       }
61 
62       binder_handler_ = new IPCHandlerBinder(adapter_, delegate);
63       if (!binder_handler_->Run()) {
64         binder_handler_ = nullptr;
65         return false;
66       }
67       return true;
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;
81 #endif  // !defined(OS_GENERIC)
82 
83     default:
84       LOG(ERROR) << "Unsupported IPC type given: " << type;
85   }
86 
87   return false;
88 }
89 
BinderStarted() const90 bool IPCManager::BinderStarted() const { return binder_handler_.get(); }
91 
LinuxStarted() const92 bool IPCManager::LinuxStarted() const { return linux_handler_.get(); }
93 
DBusStarted() const94 bool IPCManager::DBusStarted() const { return dbus_handler_.get(); }
95 
96 }  // namespace ipc
97