|
67 | 67 | /**
|
68 | 68 | * Finds gRPCCertificates.bundle inside the given parent, if it exists.
|
69 | 69 | *
|
70 |
| - * This function exists mostly to handle differences in platforms. |
71 |
| - * On iOS, resources are nested directly within the top-level of the parent |
72 |
| - * bundle, but on macOS this will actually be in Contents/Resources. |
| 70 | + * This function exists mostly to handle differences in platforms. On iOS, |
| 71 | + * resources are nested directly within the top-level of the parent bundle, but |
| 72 | + * on macOS this will actually be in Contents/Resources. |
73 | 73 | *
|
74 | 74 | * @param parent A framework or app bundle to check.
|
75 | 75 | * @return The nested gRPCCertificates.bundle if found, otherwise nil.
|
76 | 76 | */
|
77 | 77 | NSBundle* _Nullable FindCertBundleInParent(NSBundle* _Nullable parent) {
|
78 | 78 | if (!parent) return nil;
|
79 | 79 |
|
80 |
| - NSString* path = [parent pathForResource:@"gRPCCertificates" |
81 |
| - ofType:@"bundle"]; |
82 |
| - if (!path) return nil; |
| 80 | + static NSString* const bundle_names[] = { |
| 81 | + // gRPC-C++ 0.0.9 changed the name of the resource bundle to allow it to |
| 82 | + // coexist with Objective-C gRPC. |
| 83 | + @"gRPCCertificates-Cpp", |
83 | 84 |
|
84 |
| - return [[NSBundle alloc] initWithPath:path]; |
| 85 | + // Older gRPC-C++ or Objective-C gRPC use this name. |
| 86 | + @"gRPCCertificates", |
| 87 | + }; |
| 88 | + |
| 89 | + for (NSString* bundle_name : bundle_names) { |
| 90 | + NSString* path = [parent pathForResource:bundle_name ofType:@"bundle"]; |
| 91 | + if (path) { |
| 92 | + return [[NSBundle alloc] initWithPath:path]; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return nil; |
| 97 | +} |
| 98 | + |
| 99 | +using BundleLoader = NSBundle* _Nullable (*)(void); |
| 100 | + |
| 101 | +// CocoaPods: try to load from the gRPC-C++ Framework. |
| 102 | +NSBundle* _Nullable FindGrpcCppFrameworkBundle() { |
| 103 | + return [NSBundle bundleWithIdentifier:@"org.cocoapods.grpcpp"]; |
| 104 | +} |
| 105 | + |
| 106 | +// CocoaPods: also allow loading from the Objective-C gRPC Framework. |
| 107 | +NSBundle* _Nullable FindGrpcObjcFrameworkBundle() { |
| 108 | + return [NSBundle bundleWithIdentifier:@"org.cocoapods.GRPCClient"]; |
85 | 109 | }
|
86 | 110 |
|
| 111 | +// Carthage: try to load from the FirebaseFirestore.framework |
87 | 112 | NSBundle* _Nullable FindFirestoreFrameworkBundle() {
|
88 | 113 | // Load FIRFirestore reflectively to avoid a circular reference at build time.
|
89 | 114 | Class firestore_class = objc_getClass("FIRFirestore");
|
|
92 | 117 | return [NSBundle bundleForClass:firestore_class];
|
93 | 118 | }
|
94 | 119 |
|
| 120 | +// Carthage and manual projects: users manually adding resources to the project |
| 121 | +// may add the certificate to the main application bundle. Note that |
| 122 | +// `mainBundle` is nil for unit tests of library projects. |
| 123 | +NSBundle* _Nullable FindMainBundle() { |
| 124 | + return [NSBundle mainBundle]; |
| 125 | +} |
| 126 | + |
95 | 127 | /**
|
96 | 128 | * Finds the path to the roots.pem certificates file, wherever it may be.
|
97 | 129 | *
|
98 |
| - * Carthage users will find roots.pem inside gRPCCertificates.bundle in |
99 |
| - * the main bundle. |
| 130 | + * Carthage users will find roots.pem inside gRPCCertificates.bundle in the |
| 131 | + * main bundle. |
100 | 132 | *
|
101 |
| - * There have been enough variations and workarounds posted on this that |
102 |
| - * this also accepts the roots.pem file outside gRPCCertificates.bundle. |
| 133 | + * There have been enough variations and workarounds posted on this that this |
| 134 | + * also accepts the roots.pem file outside gRPCCertificates.bundle. |
103 | 135 | */
|
104 | 136 | NSString* FindPathToCertificatesFile() {
|
105 |
| - // Certificates file might be present in either the gRPC-C++ framework or (for |
106 |
| - // some projects) in the main bundle. |
107 |
| - NSBundle* bundles[] = { |
108 |
| - // CocoaPods: try to load from the gRPC-C++ Framework. |
109 |
| - [NSBundle bundleWithIdentifier:@"org.cocoapods.grpcpp"], |
110 |
| - |
111 |
| - // Carthage: try to load from the FirebaseFirestore.framework |
112 |
| - FindFirestoreFrameworkBundle(), |
113 |
| - |
114 |
| - // Carthage and manual projects: users manually adding resources to the |
115 |
| - // project may add the certificate to the main application bundle. Note |
116 |
| - // that `mainBundle` is nil for unit tests of library projects. |
117 |
| - [NSBundle mainBundle], |
| 137 | + // Certificates file might be present in gRPC frameworks, Firestore, or the |
| 138 | + // main bundle. |
| 139 | + static BundleLoader loaders[] = { |
| 140 | + FindGrpcCppFrameworkBundle, |
| 141 | + FindGrpcObjcFrameworkBundle, |
| 142 | + FindFirestoreFrameworkBundle, |
| 143 | + FindMainBundle, |
118 | 144 | };
|
119 | 145 |
|
120 | 146 | NSString* path = nil;
|
121 | 147 |
|
122 |
| - for (NSBundle* parent : bundles) { |
| 148 | + for (BundleLoader loader : loaders) { |
| 149 | + NSBundle* parent = loader(); |
123 | 150 | if (!parent) continue;
|
124 | 151 |
|
125 | 152 | NSBundle* certs_bundle = FindCertBundleInParent(parent);
|
|
0 commit comments