OLD | NEW |
| (Empty) |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
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 #import "remoting/client/ios/host_preferences_persistence.h" | |
13 | |
14 namespace { | |
15 static NSString* const kHostPreferencesDataKeyHostDictionary = | |
16 @"kHostPreferencesDataKeyHostDictionary"; | |
17 static NSString* const kHostPreferencesHostIdKey = @"HostId"; | |
18 static NSString* const kHostPreferencesPairIdKey = @"PairId"; | |
19 static NSString* const kHostPreferencesPairSecretKey = @"PairSecret"; | |
20 } // namespace | |
21 | |
22 @interface HostPreferences () | |
23 | |
24 // Load the known hosts from the Keychain. | |
25 // If no data exists, return an empty dictionary | |
26 + (NSMutableDictionary*)loadHosts; | |
27 | |
28 @end | |
29 | |
30 @implementation HostPreferences | |
31 | |
32 @synthesize hostId = _hostId; | |
33 @synthesize pairId = _pairId; | |
34 @synthesize pairSecret = _pairSecret; | |
35 | |
36 #pragma mark - Public | |
37 | |
38 - (void)saveToKeychain { | |
39 NSMutableDictionary* hosts = [HostPreferences loadHosts]; | |
40 [hosts setObject:self forKey:_hostId]; | |
41 | |
42 NSData* writeData = [NSKeyedArchiver archivedDataWithRootObject:hosts]; | |
43 | |
44 NSError* keychainError = | |
45 remoting::ios::WriteHostPreferencesToKeychain(writeData); | |
46 | |
47 LOG_IF(ERROR, !keychainError) << "Could not write to keychain."; | |
48 } | |
49 | |
50 + (HostPreferences*)hostForId:(NSString*)hostId { | |
51 NSMutableDictionary* hosts = [HostPreferences loadHosts]; | |
52 HostPreferences* host = hosts[hostId]; | |
53 if (!host) { | |
54 host = [[HostPreferences alloc] init]; | |
55 host.hostId = hostId; | |
56 host.pairId = @""; | |
57 host.pairSecret = @""; | |
58 } | |
59 return host; | |
60 } | |
61 | |
62 #pragma mark - Private | |
63 | |
64 + (NSMutableDictionary*)loadHosts { | |
65 NSData* readData = remoting::ios::ReadHostPreferencesFromKeychain(); | |
66 if (readData) { | |
67 return [NSKeyedUnarchiver unarchiveObjectWithData:readData]; | |
68 } else { | |
69 return [[NSMutableDictionary alloc] init]; | |
70 } | |
71 } | |
72 | |
73 #pragma mark - NSCoding | |
74 | |
75 - (instancetype)initWithCoder:(NSCoder*)coder { | |
76 self = [super init]; | |
77 if (self) { | |
78 [self setHostId:[coder decodeObjectForKey:kHostPreferencesHostIdKey]]; | |
79 [self setPairId:[coder decodeObjectForKey:kHostPreferencesPairIdKey]]; | |
80 [self | |
81 setPairSecret:[coder decodeObjectForKey:kHostPreferencesPairSecretKey]]; | |
82 } | |
83 return self; | |
84 } | |
85 | |
86 - (void)encodeWithCoder:(NSCoder*)coder { | |
87 [coder encodeObject:_hostId forKey:kHostPreferencesHostIdKey]; | |
88 [coder encodeObject:_pairId forKey:kHostPreferencesPairIdKey]; | |
89 [coder encodeObject:_pairSecret forKey:kHostPreferencesPairSecretKey]; | |
90 } | |
91 | |
92 @end | |
OLD | NEW |