FIRCLSContextManager.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // Copyright 2022 Google LLC
  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. #import "Crashlytics/Crashlytics/Controllers/FIRCLSContextManager.h"
  16. #import "Crashlytics/Crashlytics/Components/FIRCLSContext.h"
  17. @interface FIRCLSContextManager ()
  18. @property(nonatomic, assign) BOOL hasInitializedContext;
  19. @property(nonatomic, strong) FIRCLSInternalReport *report;
  20. @property(nonatomic, strong) FIRCLSSettings *settings;
  21. @property(nonatomic, strong) FIRCLSFileManager *fileManager;
  22. @end
  23. @implementation FIRCLSContextManager
  24. - (instancetype)init {
  25. self = [super init];
  26. if (!self) {
  27. return self;
  28. }
  29. _appQualitySessionId = @"";
  30. return self;
  31. }
  32. - (BOOL)setupContextWithReport:(FIRCLSInternalReport *)report
  33. settings:(FIRCLSSettings *)settings
  34. fileManager:(FIRCLSFileManager *)fileManager {
  35. _report = report;
  36. _settings = settings;
  37. _fileManager = fileManager;
  38. _hasInitializedContext = true;
  39. FIRCLSContextInitData *initDataObj = self.buildInitData;
  40. return FIRCLSContextInitialize(initDataObj, self.fileManager);
  41. }
  42. - (void)setAppQualitySessionId:(NSString *)appQualitySessionId {
  43. _appQualitySessionId = appQualitySessionId;
  44. // This may be called before the context is originally initialized. In that case
  45. // skip the write because it will be written as soon as the context is initialized.
  46. // On future Session ID updates, this will be true and the context metadata will be
  47. // rewritten.
  48. if (!self.hasInitializedContext) {
  49. return;
  50. }
  51. FIRCLSContextInitData *initDataObj = self.buildInitData;
  52. if (!FIRCLSContextRecordMetadata(self.report.path, initDataObj)) {
  53. FIRCLSErrorLog(@"Failed to write context file while updating App Quality Session ID");
  54. }
  55. }
  56. - (FIRCLSContextInitData *)buildInitData {
  57. return FIRCLSContextBuildInitData(self.report, self.settings, self.fileManager,
  58. self.appQualitySessionId);
  59. }
  60. @end