1 //
2 //  Copyright (C) 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 #endif  // !defined(OS_GENERIC)
22 #include "service/ipc/ipc_handler_linux.h"
23 
24 namespace ipc {
25 
IPCManager(bluetooth::Adapter * adapter)26 IPCManager::IPCManager(bluetooth::Adapter* adapter) : adapter_(adapter) {
27   CHECK(adapter_);
28 }
29 
~IPCManager()30 IPCManager::~IPCManager() {
31   // Don't rely on the handlers getting destroyed since another thread might be
32   // holding a reference to them. Instead, explicitly stop them here.
33   if (BinderStarted()) binder_handler_->Stop();
34   if (LinuxStarted()) linux_handler_->Stop();
35 }
36 
Start(Type type,Delegate * delegate)37 bool IPCManager::Start(Type type, Delegate* delegate) {
38   switch (type) {
39     case TYPE_LINUX:
40       if (LinuxStarted()) {
41         LOG(ERROR) << "IPCManagerLinux already started.";
42         return false;
43       }
44 
45       linux_handler_ = new IPCHandlerLinux(adapter_, delegate);
46       if (!linux_handler_->Run()) {
47         linux_handler_ = nullptr;
48         return false;
49       }
50       return true;
51 #if !defined(OS_GENERIC)
52     case TYPE_BINDER:
53       if (BinderStarted()) {
54         LOG(ERROR) << "IPCManagerBinder already started.";
55         return false;
56       }
57 
58       binder_handler_ = new IPCHandlerBinder(adapter_, delegate);
59       if (!binder_handler_->Run()) {
60         binder_handler_ = nullptr;
61         return false;
62       }
63       return true;
64 #endif  // !defined(OS_GENERIC)
65     default:
66       LOG(ERROR) << "Unsupported IPC type given: " << type;
67   }
68 
69   return false;
70 }
71 
BinderStarted() const72 bool IPCManager::BinderStarted() const { return binder_handler_.get(); }
73 
LinuxStarted() const74 bool IPCManager::LinuxStarted() const { return linux_handler_.get(); }
75 
76 }  // namespace ipc
77