Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// Copyright 2019 Google
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
#import "Crashlytics/Crashlytics/Settings/FIRCLSSettingsManager.h"
16
 
17
#import "Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionToken.h"
18
#import "Crashlytics/Crashlytics/Helpers/FIRCLSDefines.h"
19
#import "Crashlytics/Crashlytics/Helpers/FIRCLSLogger.h"
20
#import "Crashlytics/Crashlytics/Models/FIRCLSFileManager.h"
21
#import "Crashlytics/Crashlytics/Models/FIRCLSInstallIdentifierModel.h"
22
#import "Crashlytics/Crashlytics/Models/FIRCLSSettings.h"
23
#import "Crashlytics/Crashlytics/Settings/Models/FIRCLSApplicationIdentifierModel.h"
24
#import "Crashlytics/Crashlytics/Settings/Operations/FIRCLSDownloadAndSaveSettingsOperation.h"
25
#import "Crashlytics/Shared/FIRCLSConstants.h"
26
#import "Crashlytics/Shared/FIRCLSNetworking/FIRCLSFABNetworkClient.h"
27
#import "Crashlytics/Shared/FIRCLSNetworking/FIRCLSURLBuilder.h"
28
 
29
@interface FIRCLSSettingsManager () <FIRCLSDownloadAndSaveSettingsOperationDelegate>
30
 
31
@property(nonatomic, strong) FIRCLSApplicationIdentifierModel *appIDModel;
32
@property(nonatomic, strong) FIRCLSInstallIdentifierModel *installIDModel;
33
 
34
@property(nonatomic, strong) FIRCLSSettings *settings;
35
 
36
@property(nonatomic, strong) FIRCLSFileManager *fileManager;
37
 
38
@property(nonatomic) NSDictionary *configuration;
39
@property(nonatomic) NSDictionary *defaultConfiguration;
40
@property(nonatomic, copy) NSString *googleAppID;
41
@property(nonatomic, copy) NSDictionary *kitVersionsByKitBundleIdentifier;
42
@property(nonatomic, readonly) FIRCLSFABNetworkClient *networkClient;
43
 
44
@end
45
 
46
@implementation FIRCLSSettingsManager
47
 
48
- (instancetype)initWithAppIDModel:(FIRCLSApplicationIdentifierModel *)appIDModel
49
                    installIDModel:(FIRCLSInstallIdentifierModel *)installIDModel
50
                          settings:(FIRCLSSettings *)settings
51
                       fileManager:(FIRCLSFileManager *)fileManager
52
                       googleAppID:(NSString *)googleAppID {
53
  self = [super init];
54
  if (!self) {
55
    return nil;
56
  }
57
 
58
  _appIDModel = appIDModel;
59
  _installIDModel = installIDModel;
60
  _settings = settings;
61
  _fileManager = fileManager;
62
  _googleAppID = googleAppID;
63
 
64
  _networkClient = [[FIRCLSFABNetworkClient alloc] initWithQueue:nil];
65
 
66
  return self;
67
}
68
 
69
- (void)beginSettingsWithGoogleAppId:(NSString *)googleAppID
70
                               token:(FIRCLSDataCollectionToken *)token {
71
  NSParameterAssert(googleAppID);
72
 
73
  self.googleAppID = googleAppID;
74
 
75
  // This map helps us determine what versions of the SDK
76
  // are out there. We're keeping the Fabric value in there for
77
  // backwards compatibility
78
  // TODO(b/141747635)
79
  self.kitVersionsByKitBundleIdentifier = @{
80
    FIRCLSApplicationGetSDKBundleID() : FIRCLSSDKVersion(),
81
  };
82
 
83
  [self beginSettingsDownload:token];
84
}
85
 
86
#pragma mark Helper methods
87
 
88
/**
89
 * Makes a settings download request. If the request fails, the error is handled silently (with a
90
 * log statement).
91
 */
92
- (void)beginSettingsDownload:(FIRCLSDataCollectionToken *)token {
93
  FIRCLSDownloadAndSaveSettingsOperation *operation = nil;
94
  operation = [[FIRCLSDownloadAndSaveSettingsOperation alloc]
95
        initWithGoogleAppID:self.googleAppID
96
                   delegate:self
97
                settingsURL:self.settingsURL
98
      settingsDirectoryPath:self.fileManager.settingsDirectoryPath
99
           settingsFilePath:self.fileManager.settingsFilePath
100
             installIDModel:self.installIDModel
101
              networkClient:self.networkClient
102
                      token:token];
103
 
104
  [operation startWithToken:token];
105
}
106
 
107
- (void)finishNetworkingSession {
108
  [self.networkClient invalidateAndCancel];
109
}
110
 
111
#pragma mark FIRCLSDownloadAndSaveSettingsOperationDelegate methods
112
 
113
- (void)operation:(FIRCLSDownloadAndSaveSettingsOperation *)operation
114
    didDownloadAndSaveSettingsWithError:(nullable NSError *)error {
115
  if (error) {
116
    NSString *message = @"Failed to download settings.";
117
    if (error.userInfo && [error.userInfo objectForKey:@"status_code"] &&
118
        [[error.userInfo objectForKey:@"status_code"]
119
            isEqualToNumber:[NSNumber numberWithInt:404]]) {
120
      NSString *debugHint = @"If this is your first time launching the app, make sure you have "
121
                            @"enabled Crashlytics in the Firebase Console.";
122
      message = [NSString stringWithFormat:@"%@ %@", message, debugHint];
123
    }
124
    FIRCLSErrorLog(@"%@ %@", message, error);
125
    [self finishNetworkingSession];
126
    return;
127
  }
128
 
129
  FIRCLSDebugLog(@"Settings downloaded successfully");
130
 
131
  NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
132
  [self.settings cacheSettingsWithGoogleAppID:self.googleAppID currentTimestamp:currentTimestamp];
133
 
134
  // we're all set!
135
  [self finishNetworkingSession];
136
}
137
 
138
- (NSURL *)settingsURL {
139
  // GET
140
  // /spi/v2/platforms/:platform/apps/:identifier/settings?build_version=1234&display_version=abc&instance=xyz&source=1
141
  FIRCLSURLBuilder *url = [FIRCLSURLBuilder URLWithBase:FIRCLSSettingsEndpoint];
142
 
143
  [url appendComponent:@"/spi/v2/platforms/"];
144
  [url escapeAndAppendComponent:self.appIDModel.platform];
145
  [url appendComponent:@"/gmp/"];
146
  [url escapeAndAppendComponent:self.googleAppID];
147
  [url appendComponent:@"/settings"];
148
 
149
  [url appendValue:self.appIDModel.buildVersion forQueryParam:@"build_version"];
150
  [url appendValue:self.appIDModel.displayVersion forQueryParam:@"display_version"];
151
  [url appendValue:self.appIDModel.buildInstanceID forQueryParam:@"instance"];
152
  [url appendValue:@(self.appIDModel.installSource) forQueryParam:@"source"];
153
  // TODO: find the right param name for KitVersions and add them here
154
  return url.URL;
155
}
156
 
157
@end