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