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 #import <Foundation/Foundation.h> | |
6 #import <OpenDirectory/OpenDirectory.h> | |
7 | |
8 #include "base/files/file_path.h" | |
9 #include "base/files/file_util.h" | |
10 #include "base/mac/foundation_util.h" | |
11 #include "base/strings/sys_string_conversions.h" | |
12 #include "base/version.h" | |
13 #include "components/update_client/updater_state.h" | |
14 | |
15 namespace update_client { | |
16 | |
17 namespace { | |
18 | |
19 const base::FilePath::CharType kKeystonePlist[] = FILE_PATH_LITERAL( | |
20 "Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/" | |
21 "Contents/Info.plist"); | |
22 | |
23 // Gets a value from the updater settings. Returns a retained object. | |
24 // T should be a toll-free Foundation framework type. See Apple's | |
25 // documentation for toll-free bridging. | |
26 template<class T> | |
27 T* CopyUpdaterSettingsValue(NSString* value_name) { | |
Robert Sesek
2017/05/05 21:53:56
Can this return a base::scoped_nsobject<T> to make
Boris Vidolov
2017/05/06 00:31:15
Done.
| |
28 CFStringRef app_id = CFSTR("com.google.Keystone.Agent"); | |
29 CFPropertyListRef plist = | |
30 CFPreferencesCopyAppValue(base::mac::NSToCFCast(value_name), app_id); | |
31 if (!plist) return nil; | |
32 return base::mac::ObjCCastStrict<T>(static_cast<id>(plist)); | |
33 } | |
34 | |
35 base::Time GetUpdaterSettingsTime(NSString* value_name) { | |
36 NSDate* date = CopyUpdaterSettingsValue<NSDate>(value_name); | |
37 base::Time result = | |
38 base::Time::FromCFAbsoluteTime([date timeIntervalSinceReferenceDate]); | |
39 [date release]; | |
40 | |
41 return result; | |
42 } | |
43 | |
44 base::Version GetVersionFromPlist(const base::FilePath& info_plist) { | |
45 @autoreleasepool { | |
Robert Sesek
2017/05/05 21:53:56
Use base::mac::ScopedNSAutoreleasePool from base/m
Boris Vidolov
2017/05/06 00:31:15
Done.
| |
46 NSData* data = | |
47 [NSData dataWithContentsOfFile: | |
48 base::mac::FilePathToNSString(info_plist)]; | |
49 if ([data length] == 0) { | |
50 return base::Version(); | |
51 } | |
52 NSDictionary* all_keys = base::mac::ObjCCastStrict<NSDictionary>( | |
53 [NSPropertyListSerialization propertyListWithData:data | |
54 options:NSPropertyListImmutable | |
55 format:nil | |
56 error:nil]); | |
57 if (all_keys == nil) { | |
58 return base::Version(); | |
59 } | |
60 CFStringRef version = | |
61 base::mac::GetValueFromDictionary<CFStringRef>( | |
62 base::mac::NSToCFCast(all_keys), | |
63 kCFBundleVersionKey); | |
64 if (version == NULL) { | |
65 return base::Version(); | |
66 } | |
67 return base::Version(base::SysCFStringRefToUTF8(version)); | |
68 } | |
69 } | |
70 | |
71 } // namespace | |
72 | |
73 std::string UpdaterState::GetUpdaterName() { | |
74 return std::string("Keystone"); | |
75 } | |
76 | |
77 base::Version UpdaterState::GetUpdaterVersion(bool /*is_machine*/) { | |
78 // System Keystone trumps user one, so check this one first | |
79 base::FilePath local_library; | |
80 bool success = base::mac::GetLocalDirectory(NSLibraryDirectory, | |
81 &local_library); | |
82 DCHECK(success); | |
83 base::FilePath system_bundle_plist = local_library.Append(kKeystonePlist); | |
84 base::Version systemKeystone = GetVersionFromPlist(system_bundle_plist); | |
Robert Sesek
2017/05/05 21:53:55
naming: system_keystone
Boris Vidolov
2017/05/06 00:31:15
Done.
| |
85 if (systemKeystone.IsValid()) { | |
86 return systemKeystone; | |
87 } | |
88 | |
89 base::FilePath user_bundle_plist = | |
90 base::mac::GetUserLibraryPath().Append(kKeystonePlist); | |
91 return GetVersionFromPlist(user_bundle_plist); | |
92 } | |
93 | |
94 base::Time UpdaterState::GetUpdaterLastStartedAU(bool /*is_machine*/) { | |
95 return GetUpdaterSettingsTime(@"lastCheckStartDate"); | |
96 } | |
97 | |
98 base::Time UpdaterState::GetUpdaterLastChecked(bool /*is_machine*/) { | |
99 return GetUpdaterSettingsTime(@"lastServerCheckDate"); | |
100 } | |
101 | |
102 bool UpdaterState::IsAutoupdateCheckEnabled() { | |
103 // Auto-update check period override (in seconds). | |
104 // Applies only to older versions of Keystone. | |
105 NSNumber* timeInterval = CopyUpdaterSettingsValue<NSNumber>(@"checkInterval"); | |
106 if (timeInterval == nil) return true; | |
107 int value = [timeInterval intValue]; | |
108 [timeInterval release]; | |
109 | |
110 return 0 < value && value < (24 * 60 * 60); | |
111 } | |
112 | |
113 int UpdaterState::GetUpdatePolicy() { | |
114 return -1; // Keystone does not support update policies. | |
115 } | |
116 | |
117 bool UpdaterState::IsJoinedToDomain() { | |
118 @autoreleasepool { | |
Robert Sesek
2017/05/05 21:53:56
ScopedNSAutoreleasePool
Boris Vidolov
2017/05/06 00:31:15
Done.
| |
119 ODSession* session = [ODSession defaultSession]; | |
120 if (session == nil) { | |
121 DLOG(WARNING) << "ODSession defult session is nil."; | |
122 return false; | |
123 } | |
124 | |
125 NSError* error = nil; | |
126 ODNode* node = [ODNode nodeWithSession:session | |
127 type:kODNodeTypeAuthentication | |
128 error:&error]; | |
129 if (node == nil) { | |
130 DLOG(WARNING) << "ODSession cannot obtain the authentication node: " | |
131 << base::mac::NSToCFCast(error); | |
132 return false; | |
133 } | |
134 | |
135 ODQuery* query = [ODQuery queryWithNode:node | |
136 forRecordTypes:kODRecordTypeUsers | |
137 attribute:kODAttributeTypeRecordName | |
138 matchType:kODMatchEqualTo | |
139 queryValues:NSUserName() | |
140 returnAttributes:kODAttributeTypeAllAttributes | |
141 maximumResults:0 | |
142 error:&error]; | |
143 if (query == nil) { | |
144 DLOG(WARNING) << "ODSession cannot create user query: " | |
145 << base::mac::NSToCFCast(error); | |
146 return false; | |
147 } | |
148 | |
149 NSArray* results = [query resultsAllowingPartial:NO error:&error]; | |
150 if (!results) { | |
151 DLOG(WARNING) << "ODSession cannot obtain current user node: " | |
152 << base::mac::NSToCFCast(error); | |
153 return false; | |
154 } | |
155 if (results.count != 1) { | |
156 DLOG(WARNING) << @"ODSession unexpected number of nodes: " | |
157 << results.count; | |
158 } | |
159 for (id element in results) { | |
160 ODRecord* record = base::mac::ObjCCastStrict<ODRecord>(element); | |
161 NSArray* attributes = | |
162 [record valuesForAttribute:kODAttributeTypeMetaRecordName | |
163 error:NULL]; | |
164 for (id attribute in attributes) { | |
165 NSString *attributeValue = | |
Robert Sesek
2017/05/05 21:53:55
nit: * goes next to NSString
Robert Sesek
2017/05/05 21:53:56
naming: attribute_value
Boris Vidolov
2017/05/06 00:31:15
Done.
Boris Vidolov
2017/05/06 00:31:15
Done.
| |
166 base::mac::ObjCCastStrict<NSString>(attribute); | |
167 // Example: "uid=johnsmith,ou=People,dc=chromium,dc=org | |
168 NSRange dc = [attributeValue rangeOfString:@"(^|,)\\s*dc=" | |
169 options:NSRegularExpressionSearch]; | |
170 if (dc.length > 0) { | |
171 return true; | |
172 } | |
173 } | |
174 } | |
175 } | |
176 return false; | |
177 } | |
178 | |
179 } // namespace update_client | |
OLD | NEW |