Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 "FirebaseMessaging/Sources/Token/FIRMessagingKeychain.h"
18
 
19
#import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
20
 
21
NSString *const kFIRMessagingKeychainErrorDomain = @"com.google.iid";
22
 
23
@interface FIRMessagingKeychain () {
24
  dispatch_queue_t _keychainOperationQueue;
25
}
26
 
27
@end
28
 
29
@implementation FIRMessagingKeychain
30
 
31
+ (instancetype)sharedInstance {
32
  static FIRMessagingKeychain *sharedInstance;
33
  static dispatch_once_t onceToken;
34
  dispatch_once(&onceToken, ^{
35
    sharedInstance = [[FIRMessagingKeychain alloc] init];
36
  });
37
  return sharedInstance;
38
}
39
 
40
- (instancetype)init {
41
  self = [super init];
42
  if (self) {
43
    _keychainOperationQueue =
44
        dispatch_queue_create("com.google.FirebaseInstanceID.Keychain", DISPATCH_QUEUE_SERIAL);
45
  }
46
  return self;
47
}
48
 
49
- (CFTypeRef)itemWithQuery:(NSDictionary *)keychainQuery {
50
  __block SecKeyRef keyRef = NULL;
51
  dispatch_sync(_keychainOperationQueue, ^{
52
    OSStatus status =
53
        SecItemCopyMatching((__bridge CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyRef);
54
 
55
    if (status != noErr) {
56
      if (keyRef) {
57
        CFRelease(keyRef);
58
      }
59
      FIRMessagingLoggerDebug(kFIRMessagingKeychainReadItemError,
60
                              @"Info is not found in Keychain. OSStatus: %d. Keychain query: %@",
61
                              (int)status, keychainQuery);
62
    }
63
  });
64
  return keyRef;
65
}
66
 
67
- (void)removeItemWithQuery:(NSDictionary *)keychainQuery
68
                    handler:(void (^)(NSError *error))handler {
69
  dispatch_async(_keychainOperationQueue, ^{
70
    OSStatus status = SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
71
    if (status != noErr) {
72
      FIRMessagingLoggerDebug(
73
          kFIRMessagingKeychainDeleteItemError,
74
          @"Couldn't delete item from Keychain OSStatus: %d with the keychain query %@",
75
          (int)status, keychainQuery);
76
    }
77
 
78
    if (handler) {
79
      NSError *error = nil;
80
      // When item is not found, it should NOT be considered as an error. The operation should
81
      // continue.
82
      if (status != noErr && status != errSecItemNotFound) {
83
        error = [NSError errorWithDomain:kFIRMessagingKeychainErrorDomain code:status userInfo:nil];
84
      }
85
      dispatch_async(dispatch_get_main_queue(), ^{
86
        handler(error);
87
      });
88
    }
89
  });
90
}
91
 
92
- (void)addItemWithQuery:(NSDictionary *)keychainQuery handler:(void (^)(NSError *))handler {
93
  dispatch_async(_keychainOperationQueue, ^{
94
    OSStatus status = SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL);
95
 
96
    if (handler) {
97
      NSError *error = nil;
98
      if (status != noErr) {
99
        FIRMessagingLoggerWarn(kFIRMessagingKeychainAddItemError,
100
                               @"Couldn't add item to Keychain OSStatus: %d", (int)status);
101
        error = [NSError errorWithDomain:kFIRMessagingKeychainErrorDomain code:status userInfo:nil];
102
      }
103
      dispatch_async(dispatch_get_main_queue(), ^{
104
        handler(error);
105
      });
106
    }
107
  });
108
}
109
 
110
@end