• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "libwebserv/binder_server.h"
16 
17 #include <utils/String16.h>
18 
19 #include "libwebserv/binder_constants.h"
20 
21 #include <string>
22 #include <vector>
23 
24 using android::String16;
25 using android::sp;
26 using android::IBinder;
27 using std::string;
28 using std::vector;
29 
30 namespace libwebserv {
31 
BinderServer(brillo::MessageLoop * message_loop,const base::Closure & on_server_online,const base::Closure & on_server_offline,android::BinderWrapper * binder_wrapper)32 BinderServer::BinderServer(brillo::MessageLoop* message_loop,
33                            const base::Closure& on_server_online,
34                            const base::Closure& on_server_offline,
35                            android::BinderWrapper* binder_wrapper)
36     : message_loop_{message_loop},
37       on_server_online_{on_server_online},
38       on_server_offline_{on_server_offline},
39       binder_wrapper_{binder_wrapper} {
40   message_loop_->PostTask(FROM_HERE,
41                           base::Bind(&BinderServer::TryConnecting,
42                                      weak_ptr_factory_.GetWeakPtr()));
43 }
44 
TryConnecting()45 void BinderServer::TryConnecting() {
46   ClearLocalState();
47   android::sp<android::IBinder> binder = binder_wrapper_->GetService(
48       libwebserv::kWebserverBinderServiceName);
49   if (!binder.get()) {
50     LOG(INFO) << "Webservd has not registered with service manager.";
51   } else if (!BuildLocalState(binder)) {
52     ClearLocalState();
53   } else {
54     // Got a binder, built up appropriate local state, our job is done.
55     return;
56   }
57   message_loop_->PostDelayedTask(FROM_HERE,
58                                  base::Bind(&BinderServer::TryConnecting,
59                                             weak_ptr_factory_.GetWeakPtr()),
60                                  base::TimeDelta::FromSeconds(1));
61 }
62 
63 
ClearLocalState()64 void BinderServer::ClearLocalState() {
65   // Remove all references to remote protocol handlers from our local wrappers.
66 
67   // TODO(wiley) Define this method
68   //for (auto& local_handler: local_protocol_handlers_) {
69   //  local_handler.second->ResetRemoteProtocolHandlers();
70   //}
71 
72   remote_server_.clear();
73 }
74 
BuildLocalState(android::sp<android::IBinder> server)75 bool BinderServer::BuildLocalState(android::sp<android::IBinder> server) {
76   remote_server_ = android::interface_cast<RemoteServer>(server);
77   vector<sp<IBinder>> remote_raw_binders;
78   if (!remote_server_->GetProtocolHandlers(&remote_raw_binders).isOk()) {
79     // Possibly the server died, this is not necessarily an error.
80     LOG(INFO) << "Webservd failed to tell us about protocol handlers.";
81     return false;
82   }
83 
84   // Tell the local wrappers about the remote handlers that exist now.
85   for (auto& raw_binder: remote_raw_binders) {
86     sp<RemoteProtocolHandler> remote_handler =
87         android::interface_cast<RemoteProtocolHandler>(raw_binder);
88     String16 name;
89     if (!remote_handler->GetName(&name).isOk()) {
90       LOG(INFO) << "Remote handler could not report its name.";
91       return false;
92     }
93     // TODO(wiley) Look for a BinderPHGroup by that name in local_handlers_
94     //             Create a new BinderPHGroup if necessary
95     //             Add it to the map under |name|
96     //             Update |it| appropriately
97     //             Tell |it| about |remote_handler|
98   }
99   return true;
100 }
101 
102 }  // namespace libwebserv
103