xref: /aosp_15_r20/external/grpc-grpc/src/objective-c/GRPCClient/GRPCCall+Interceptor.mm (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1/*
2 *
3 * Copyright 2019 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19#import "GRPCCall+Interceptor.h"
20#import "GRPCInterceptor.h"
21
22static id<GRPCInterceptorFactory> globalInterceptorFactory = nil;
23static NSLock *globalInterceptorLock = nil;
24static dispatch_once_t onceToken;
25
26@implementation GRPCCall2 (Interceptor)
27
28+ (void)registerGlobalInterceptor:(id<GRPCInterceptorFactory>)interceptorFactory {
29  if (interceptorFactory == nil) {
30    return;
31  }
32  dispatch_once(&onceToken, ^{
33    globalInterceptorLock = [[NSLock alloc] init];
34  });
35  [globalInterceptorLock lock];
36  if (globalInterceptorFactory != nil) {
37    [globalInterceptorLock unlock];
38    [NSException
39         raise:NSInternalInconsistencyException
40        format:@"Global interceptor is already registered. Only one global interceptor can be "
41               @"registered in a process."];
42    return;
43  }
44
45  globalInterceptorFactory = interceptorFactory;
46  [globalInterceptorLock unlock];
47}
48
49+ (id<GRPCInterceptorFactory>)globalInterceptorFactory {
50  dispatch_once(&onceToken, ^{
51    globalInterceptorLock = [[NSLock alloc] init];
52  });
53  id<GRPCInterceptorFactory> factory;
54  [globalInterceptorLock lock];
55  factory = globalInterceptorFactory;
56  [globalInterceptorLock unlock];
57
58  return factory;
59}
60
61@end
62