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