1 |
efrain |
1 |
/*
|
|
|
2 |
* Copyright 2019 Google
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
#import "FirebaseRemoteConfig/Sources/FIRRemoteConfigComponent.h"
|
|
|
18 |
|
|
|
19 |
#import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
|
|
|
20 |
#import "FirebaseRemoteConfig/Sources/Private/FIRRemoteConfig_Private.h"
|
|
|
21 |
#import "FirebaseRemoteConfig/Sources/RCNConfigContent.h"
|
|
|
22 |
#import "FirebaseRemoteConfig/Sources/RCNConfigDBManager.h"
|
|
|
23 |
#import "Interop/Analytics/Public/FIRAnalyticsInterop.h"
|
|
|
24 |
|
|
|
25 |
@implementation FIRRemoteConfigComponent
|
|
|
26 |
|
|
|
27 |
/// Default method for retrieving a Remote Config instance, or creating one if it doesn't exist.
|
|
|
28 |
- (FIRRemoteConfig *)remoteConfigForNamespace:(NSString *)remoteConfigNamespace {
|
|
|
29 |
if (!remoteConfigNamespace) {
|
|
|
30 |
// TODO: Throw an error? Return nil? What do we want to do?
|
|
|
31 |
return nil;
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
// Validate the required information is available.
|
|
|
35 |
FIROptions *options = self.app.options;
|
|
|
36 |
NSString *errorPropertyName;
|
|
|
37 |
if (options.googleAppID.length == 0) {
|
|
|
38 |
errorPropertyName = @"googleAppID";
|
|
|
39 |
} else if (options.GCMSenderID.length == 0) {
|
|
|
40 |
errorPropertyName = @"GCMSenderID";
|
|
|
41 |
} else if (options.projectID.length == 0) {
|
|
|
42 |
errorPropertyName = @"projectID";
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
if (errorPropertyName) {
|
|
|
46 |
NSString *const kFirebaseConfigErrorDomain = @"com.firebase.config";
|
|
|
47 |
[NSException
|
|
|
48 |
raise:kFirebaseConfigErrorDomain
|
|
|
49 |
format:@"%@",
|
|
|
50 |
[NSString
|
|
|
51 |
stringWithFormat:
|
|
|
52 |
@"Firebase Remote Config is missing the required %@ property from the "
|
|
|
53 |
@"configured FirebaseApp and will not be able to function properly. Please "
|
|
|
54 |
@"fix this issue to ensure that Firebase is correctly configured.",
|
|
|
55 |
errorPropertyName]];
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
FIRRemoteConfig *instance = self.instances[remoteConfigNamespace];
|
|
|
59 |
if (!instance) {
|
|
|
60 |
FIRApp *app = self.app;
|
|
|
61 |
id<FIRAnalyticsInterop> analytics =
|
|
|
62 |
app.isDefaultApp ? FIR_COMPONENT(FIRAnalyticsInterop, app.container) : nil;
|
|
|
63 |
instance = [[FIRRemoteConfig alloc] initWithAppName:app.name
|
|
|
64 |
FIROptions:app.options
|
|
|
65 |
namespace:remoteConfigNamespace
|
|
|
66 |
DBManager:[RCNConfigDBManager sharedInstance]
|
|
|
67 |
configContent:[RCNConfigContent sharedInstance]
|
|
|
68 |
analytics:analytics];
|
|
|
69 |
self.instances[remoteConfigNamespace] = instance;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
return instance;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/// Default initializer.
|
|
|
76 |
- (instancetype)initWithApp:(FIRApp *)app {
|
|
|
77 |
self = [super init];
|
|
|
78 |
if (self) {
|
|
|
79 |
_app = app;
|
|
|
80 |
_instances = [[NSMutableDictionary alloc] initWithCapacity:1];
|
|
|
81 |
}
|
|
|
82 |
return self;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
#pragma mark - Lifecycle
|
|
|
86 |
|
|
|
87 |
+ (void)load {
|
|
|
88 |
// Register as an internal library to be part of the initialization process. The name comes from
|
|
|
89 |
// go/firebase-sdk-platform-info.
|
|
|
90 |
[FIRApp registerInternalLibrary:self withName:@"fire-rc"];
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
#pragma mark - Interoperability
|
|
|
94 |
|
|
|
95 |
+ (NSArray<FIRComponent *> *)componentsToRegister {
|
|
|
96 |
FIRDependency *analyticsDep = [FIRDependency dependencyWithProtocol:@protocol(FIRAnalyticsInterop)
|
|
|
97 |
isRequired:NO];
|
|
|
98 |
FIRComponent *rcProvider = [FIRComponent
|
|
|
99 |
componentWithProtocol:@protocol(FIRRemoteConfigProvider)
|
|
|
100 |
instantiationTiming:FIRInstantiationTimingAlwaysEager
|
|
|
101 |
dependencies:@[ analyticsDep ]
|
|
|
102 |
creationBlock:^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
|
|
|
103 |
// Cache the component so instances of Remote Config are cached.
|
|
|
104 |
*isCacheable = YES;
|
|
|
105 |
return [[FIRRemoteConfigComponent alloc] initWithApp:container.app];
|
|
|
106 |
}];
|
|
|
107 |
return @[ rcProvider ];
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
@end
|