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 #ifndef GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_CLIENT_CONNECTION_ID_GENERATOR_H
16 #define GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_CLIENT_CONNECTION_ID_GENERATOR_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include <map>
21 
22 #include "absl/strings/string_view.h"
23 
24 #include "src/core/lib/gprpp/sync.h"
25 
26 namespace grpc_binder {
27 
28 // Generates somewhat human-readable unique identifiers from package name and
29 // class name. We will generate a Id that only contains unreserved URI
30 // characters (uppercase and lowercase letters, decimal digits, hyphen, period,
31 // underscore, and tilde).
32 class ConnectionIdGenerator {
33  public:
34   std::string Generate(absl::string_view uri);
35 
36  private:
37   // Our generated Id need to fit in unix socket path length limit. We use 100
38   // here to be safe.
39   const size_t kPathLengthLimit = 100;
40 
41   grpc_core::Mutex m_;
42   // Every generated identifier will followed by the value of this counter to
43   // make sure every generated id is unique.
44   int count_ ABSL_GUARDED_BY(m_);
45 };
46 
47 ConnectionIdGenerator* GetConnectionIdGenerator();
48 
49 }  // namespace grpc_binder
50 
51 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_CLIENT_CONNECTION_ID_GENERATOR_H
52