Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
|
Sergey Ulanov
2017/01/18 22:20:35
2017
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 6 #error "This file requires ARC support." | |
| 7 #endif | |
| 8 | |
| 9 #import "remoting/client/ios/host_preferences.h" | |
| 10 | |
| 11 #import "base/logging.h" | |
| 12 | |
| 13 namespace { | |
| 14 static NSString* const kHostPreferencesDataKeyHostDictionary = | |
| 15 @"kHostPreferencesDataKeyHostDictionary"; | |
| 16 static NSString* const kHostPreferencesHostIdKey = @"HostId"; | |
| 17 static NSString* const kHostPreferencesPairIdKey = @"PairId"; | |
| 18 static NSString* const kHostPreferencesPairSecretKey = @"PairSecret"; | |
| 19 } // namespace | |
| 20 | |
| 21 @interface HostPreferences () | |
| 22 | |
| 23 // Load the known hosts from the Keychain. | |
| 24 // If no data exists, return an empty dictionary | |
| 25 + (NSMutableDictionary*)loadHosts; | |
| 26 | |
| 27 @end | |
| 28 | |
| 29 @implementation HostPreferences | |
| 30 | |
| 31 @synthesize hostId = _hostId; | |
| 32 @synthesize pairId = _pairId; | |
| 33 @synthesize pairSecret = _pairSecret; | |
| 34 | |
| 35 #pragma mark - Public | |
| 36 | |
| 37 - (void)saveToKeychain { | |
| 38 NSMutableDictionary* hosts = [HostPreferences loadHosts]; | |
| 39 [hosts setObject:self forKey:_hostId]; | |
| 40 | |
| 41 NSData* writeData = [NSKeyedArchiver archivedDataWithRootObject:hosts]; | |
| 42 | |
| 43 NSError* keychainError = | |
| 44 remoting::ios::writeHostPreferencesToKeychain(writeData); | |
| 45 | |
| 46 DLOG_IF(ERROR, !keychainError) << "Could not write to keychain."; | |
| 47 } | |
| 48 | |
| 49 + (HostPreferences*)hostForId:(NSString*)hostId { | |
| 50 NSMutableDictionary* hosts = [HostPreferences loadHosts]; | |
| 51 HostPreferences* host = hosts[hostId]; | |
| 52 if (!host) { | |
| 53 host = [[HostPreferences alloc] init]; | |
| 54 host.hostId = hostId; | |
| 55 host.pairId = @""; | |
| 56 host.pairSecret = @""; | |
| 57 } | |
| 58 return host; | |
| 59 } | |
| 60 | |
| 61 #pragma mark - Private | |
| 62 | |
| 63 + (NSMutableDictionary*)loadHosts { | |
| 64 NSData* readData = remoting::ios::readHostPreferencesFromKeychain(); | |
| 65 if (readData) { | |
| 66 return [NSKeyedUnarchiver unarchiveObjectWithData:readData]; | |
| 67 } else { | |
| 68 return [[NSMutableDictionary alloc] init]; | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 #pragma mark - NSCoding | |
| 73 | |
| 74 - (instancetype)initWithCoder:(NSCoder*)coder { | |
| 75 self = [super init]; | |
| 76 if (self) { | |
| 77 [self setHostId:[coder decodeObjectForKey:kHostPreferencesHostIdKey]]; | |
| 78 [self setPairId:[coder decodeObjectForKey:kHostPreferencesPairIdKey]]; | |
| 79 [self | |
| 80 setPairSecret:[coder decodeObjectForKey:kHostPreferencesPairSecretKey]]; | |
| 81 } | |
| 82 return self; | |
| 83 } | |
| 84 | |
| 85 - (void)encodeWithCoder:(NSCoder*)coder { | |
| 86 [coder encodeObject:_hostId forKey:kHostPreferencesHostIdKey]; | |
| 87 [coder encodeObject:_pairId forKey:kHostPreferencesPairIdKey]; | |
| 88 [coder encodeObject:_pairSecret forKey:kHostPreferencesPairSecretKey]; | |
| 89 } | |
| 90 | |
| 91 @end | |
| OLD | NEW |