1 // Copyright 2021 gRPC authors.
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 <grpc/support/port_platform.h>
16 
17 #ifndef GRPC_NO_BINDER
18 
19 #include <grpcpp/security/binder_security_policy.h>
20 #include <grpcpp/security/server_credentials.h>
21 
22 #include "src/core/ext/transport/binder/server/binder_server.h"
23 #include "src/core/ext/transport/binder/wire_format/binder_android.h"
24 
25 namespace grpc {
26 namespace experimental {
27 
28 namespace {
29 
30 class BinderServerCredentialsImpl final : public ServerCredentials {
31  public:
BinderServerCredentialsImpl(std::shared_ptr<grpc::experimental::binder::SecurityPolicy> security_policy)32   explicit BinderServerCredentialsImpl(
33       std::shared_ptr<grpc::experimental::binder::SecurityPolicy>
34           security_policy)
35       : security_policy_(security_policy) {}
36 #ifdef GPR_SUPPORT_BINDER_TRANSPORT
AddPortToServer(const std::string & addr,grpc_server * server)37   int AddPortToServer(const std::string& addr, grpc_server* server) override {
38     return grpc_core::AddBinderPort(
39         std::string(addr), server,
40         [](grpc_binder::TransactionReceiver::OnTransactCb transact_cb) {
41           return std::make_unique<grpc_binder::TransactionReceiverAndroid>(
42               nullptr, std::move(transact_cb));
43         },
44         security_policy_);
45   }
46 #else
AddPortToServer(const std::string &,grpc_server *)47   int AddPortToServer(const std::string& /*addr*/,
48                       grpc_server* /*server*/) override {
49     return 0;
50   }
51 #endif  // GPR_SUPPORT_BINDER_TRANSPORT
52 
SetAuthMetadataProcessor(const std::shared_ptr<AuthMetadataProcessor> &)53   void SetAuthMetadataProcessor(
54       const std::shared_ptr<AuthMetadataProcessor>& /*processor*/) override {
55     grpc_core::Crash("unreachable");
56   }
57 
58  private:
IsInsecure() const59   bool IsInsecure() const override { return true; }
60 
61   std::shared_ptr<grpc::experimental::binder::SecurityPolicy> security_policy_;
62 };
63 
64 }  // namespace
65 
BinderServerCredentials(std::shared_ptr<grpc::experimental::binder::SecurityPolicy> security_policy)66 std::shared_ptr<ServerCredentials> BinderServerCredentials(
67     std::shared_ptr<grpc::experimental::binder::SecurityPolicy>
68         security_policy) {
69   GPR_ASSERT(security_policy != nullptr);
70   return std::shared_ptr<ServerCredentials>(
71       new BinderServerCredentialsImpl(security_policy));
72 }
73 
74 }  // namespace experimental
75 }  // namespace grpc
76 #endif
77