ipc_handler_binder.cc revision e6d1202990e0ede65db4470b4eaa20fc4b1f791e
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/binder/ipc_handler_binder.h" 18 19#include <base/bind.h> 20#include <base/logging.h> 21#include <base/message_loop/message_loop.h> 22#include <binder/IPCThreadState.h> 23#include <binder/IServiceManager.h> 24#include <binder/ProcessState.h> 25 26#include "service/ipc/binder/bluetooth_binder_server.h" 27 28using android::defaultServiceManager; 29using android::sp; 30using android::status_t; 31using android::String8; 32using android::String16; 33 34namespace ipc { 35 36std::string kServiceName = "bluetooth-service"; 37 38IPCHandlerBinder::IPCHandlerBinder( 39 bluetooth::Adapter* adapter, 40 IPCManager::Delegate* delegate) 41 : IPCHandler(adapter, delegate) { 42} 43 44IPCHandlerBinder::~IPCHandlerBinder() { 45} 46 47bool IPCHandlerBinder::Run() { 48 CHECK(adapter()); 49 50 // Register the IBluetooth service with the Android ServiceManager. 51 android::sp<binder::BluetoothBinderServer> bt_server = 52 new binder::BluetoothBinderServer(adapter()); 53 status_t status = defaultServiceManager()->addService( 54 String16(String8(kServiceName.c_str())), 55 bt_server); 56 if (status != android::NO_ERROR) { 57 LOG(ERROR) << "Failed to register Bluetooth service with ServiceManager"; 58 return false; 59 } 60 61 // Notify the delegate. We do this in the message loop to avoid reentrancy. 62 if (delegate()) { 63 base::MessageLoop::current()->task_runner()->PostTask( 64 FROM_HERE, 65 base::Bind(&IPCHandlerBinder::NotifyStarted, this)); 66 } 67 68 android::ProcessState::self()->startThreadPool(); 69 70 return true; 71} 72 73void IPCHandlerBinder::Stop() { 74 // TODO(armansito): There are several methods in android::IPCThreadState that 75 // are related to shutting down the threadpool, however I haven't been able to 76 // make them shut things down cleanly. Figure out the right way to stop the 77 // Binder IPC here. 78} 79 80void IPCHandlerBinder::NotifyStarted() { 81 if (delegate()) 82 delegate()->OnIPCHandlerStarted(IPCManager::TYPE_BINDER); 83} 84 85} // namespace ipc 86