OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/policy/core/common/policy_loader_mac.h" | 5 #include "components/policy/core/common/policy_loader_mac.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/mac/foundation_util.h" | 11 #include "base/mac/foundation_util.h" |
12 #include "base/mac/scoped_cftyperef.h" | 12 #include "base/mac/scoped_cftyperef.h" |
13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
14 #include "base/platform_file.h" | 14 #include "base/platform_file.h" |
15 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
16 #include "base/strings/sys_string_conversions.h" | 16 #include "base/strings/sys_string_conversions.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "components/policy/core/common/external_data_fetcher.h" | 18 #include "components/policy/core/common/external_data_fetcher.h" |
| 19 #include "components/policy/core/common/mac_util.h" |
19 #include "components/policy/core/common/policy_bundle.h" | 20 #include "components/policy/core/common/policy_bundle.h" |
20 #include "components/policy/core/common/policy_load_status.h" | 21 #include "components/policy/core/common/policy_load_status.h" |
21 #include "components/policy/core/common/policy_map.h" | 22 #include "components/policy/core/common/policy_map.h" |
22 #include "components/policy/core/common/preferences_mac.h" | 23 #include "components/policy/core/common/preferences_mac.h" |
23 #include "components/policy/core/common/schema.h" | 24 #include "components/policy/core/common/schema.h" |
24 #include "components/policy/core/common/schema_map.h" | 25 #include "components/policy/core/common/schema_map.h" |
25 | 26 |
26 using base::mac::CFCast; | |
27 using base::ScopedCFTypeRef; | 27 using base::ScopedCFTypeRef; |
28 | 28 |
29 namespace policy { | 29 namespace policy { |
30 | 30 |
31 namespace { | |
32 | |
33 // Callback function for CFDictionaryApplyFunction. |key| and |value| are an | |
34 // entry of the CFDictionary that should be converted into an equivalent entry | |
35 // in the DictionaryValue in |context|. | |
36 void DictionaryEntryToValue(const void* key, const void* value, void* context) { | |
37 if (CFStringRef cf_key = CFCast<CFStringRef>(key)) { | |
38 base::Value* converted = | |
39 PolicyLoaderMac::CreateValueFromProperty( | |
40 static_cast<CFPropertyListRef>(value)); | |
41 if (converted) { | |
42 const std::string string = base::SysCFStringRefToUTF8(cf_key); | |
43 static_cast<base::DictionaryValue *>(context)->Set(string, converted); | |
44 } | |
45 } | |
46 } | |
47 | |
48 // Callback function for CFArrayApplyFunction. |value| is an entry of the | |
49 // CFArray that should be converted into an equivalent entry in the ListValue | |
50 // in |context|. | |
51 void ArrayEntryToValue(const void* value, void* context) { | |
52 base::Value* converted = | |
53 PolicyLoaderMac::CreateValueFromProperty( | |
54 static_cast<CFPropertyListRef>(value)); | |
55 if (converted) | |
56 static_cast<base::ListValue *>(context)->Append(converted); | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 PolicyLoaderMac::PolicyLoaderMac( | 31 PolicyLoaderMac::PolicyLoaderMac( |
62 scoped_refptr<base::SequencedTaskRunner> task_runner, | 32 scoped_refptr<base::SequencedTaskRunner> task_runner, |
63 const base::FilePath& managed_policy_path, | 33 const base::FilePath& managed_policy_path, |
64 MacPreferences* preferences) | 34 MacPreferences* preferences) |
65 : AsyncPolicyLoader(task_runner), | 35 : AsyncPolicyLoader(task_runner), |
66 preferences_(preferences), | 36 preferences_(preferences), |
67 managed_policy_path_(managed_policy_path) {} | 37 managed_policy_path_(managed_policy_path) {} |
68 | 38 |
69 PolicyLoaderMac::~PolicyLoaderMac() {} | 39 PolicyLoaderMac::~PolicyLoaderMac() {} |
70 | 40 |
(...skipping 24 matching lines...) Expand all Loading... |
95 base::ScopedCFTypeRef<CFPropertyListRef> value( | 65 base::ScopedCFTypeRef<CFPropertyListRef> value( |
96 preferences_->CopyAppValue(name, kCFPreferencesCurrentApplication)); | 66 preferences_->CopyAppValue(name, kCFPreferencesCurrentApplication)); |
97 if (!value.get()) | 67 if (!value.get()) |
98 continue; | 68 continue; |
99 policy_present = true; | 69 policy_present = true; |
100 bool forced = | 70 bool forced = |
101 preferences_->AppValueIsForced(name, kCFPreferencesCurrentApplication); | 71 preferences_->AppValueIsForced(name, kCFPreferencesCurrentApplication); |
102 PolicyLevel level = forced ? POLICY_LEVEL_MANDATORY : | 72 PolicyLevel level = forced ? POLICY_LEVEL_MANDATORY : |
103 POLICY_LEVEL_RECOMMENDED; | 73 POLICY_LEVEL_RECOMMENDED; |
104 // TODO(joaodasilva): figure the policy scope. | 74 // TODO(joaodasilva): figure the policy scope. |
105 base::Value* policy = CreateValueFromProperty(value); | 75 scoped_ptr<base::Value> policy = PropertyToValue(value); |
106 if (policy) | 76 if (policy) { |
107 chrome_policy.Set(it.key(), level, POLICY_SCOPE_USER, policy, NULL); | 77 chrome_policy.Set( |
108 else | 78 it.key(), level, POLICY_SCOPE_USER, policy.release(), NULL); |
| 79 } else { |
109 status.Add(POLICY_LOAD_STATUS_PARSE_ERROR); | 80 status.Add(POLICY_LOAD_STATUS_PARSE_ERROR); |
| 81 } |
110 } | 82 } |
111 | 83 |
112 if (!policy_present) | 84 if (!policy_present) |
113 status.Add(POLICY_LOAD_STATUS_NO_POLICY); | 85 status.Add(POLICY_LOAD_STATUS_NO_POLICY); |
114 | 86 |
115 // Load policy for the registered components. | 87 // Load policy for the registered components. |
116 LoadPolicyForDomain(POLICY_DOMAIN_EXTENSIONS, "extensions", bundle.get()); | 88 LoadPolicyForDomain(POLICY_DOMAIN_EXTENSIONS, "extensions", bundle.get()); |
117 | 89 |
118 return bundle.Pass(); | 90 return bundle.Pass(); |
119 } | 91 } |
120 | 92 |
121 base::Time PolicyLoaderMac::LastModificationTime() { | 93 base::Time PolicyLoaderMac::LastModificationTime() { |
122 base::File::Info file_info; | 94 base::File::Info file_info; |
123 if (!base::GetFileInfo(managed_policy_path_, &file_info) || | 95 if (!base::GetFileInfo(managed_policy_path_, &file_info) || |
124 file_info.is_directory) { | 96 file_info.is_directory) { |
125 return base::Time(); | 97 return base::Time(); |
126 } | 98 } |
127 | 99 |
128 return file_info.last_modified; | 100 return file_info.last_modified; |
129 } | 101 } |
130 | 102 |
131 // static | |
132 base::Value* PolicyLoaderMac::CreateValueFromProperty( | |
133 CFPropertyListRef property) { | |
134 if (CFCast<CFNullRef>(property)) | |
135 return base::Value::CreateNullValue(); | |
136 | |
137 if (CFBooleanRef boolean = CFCast<CFBooleanRef>(property)) | |
138 return base::Value::CreateBooleanValue(CFBooleanGetValue(boolean)); | |
139 | |
140 if (CFNumberRef number = CFCast<CFNumberRef>(property)) { | |
141 // CFNumberGetValue() converts values implicitly when the conversion is not | |
142 // lossy. Check the type before trying to convert. | |
143 if (CFNumberIsFloatType(number)) { | |
144 double double_value; | |
145 if (CFNumberGetValue(number, kCFNumberDoubleType, &double_value)) | |
146 return base::Value::CreateDoubleValue(double_value); | |
147 } else { | |
148 int int_value; | |
149 if (CFNumberGetValue(number, kCFNumberIntType, &int_value)) | |
150 return base::Value::CreateIntegerValue(int_value); | |
151 } | |
152 } | |
153 | |
154 if (CFStringRef string = CFCast<CFStringRef>(property)) | |
155 return base::Value::CreateStringValue(base::SysCFStringRefToUTF8(string)); | |
156 | |
157 if (CFDictionaryRef dict = CFCast<CFDictionaryRef>(property)) { | |
158 base::DictionaryValue* dict_value = new base::DictionaryValue(); | |
159 CFDictionaryApplyFunction(dict, DictionaryEntryToValue, dict_value); | |
160 return dict_value; | |
161 } | |
162 | |
163 if (CFArrayRef array = CFCast<CFArrayRef>(property)) { | |
164 base::ListValue* list_value = new base::ListValue(); | |
165 CFArrayApplyFunction(array, | |
166 CFRangeMake(0, CFArrayGetCount(array)), | |
167 ArrayEntryToValue, | |
168 list_value); | |
169 return list_value; | |
170 } | |
171 | |
172 return NULL; | |
173 } | |
174 | |
175 void PolicyLoaderMac::LoadPolicyForDomain( | 103 void PolicyLoaderMac::LoadPolicyForDomain( |
176 PolicyDomain domain, | 104 PolicyDomain domain, |
177 const std::string& domain_name, | 105 const std::string& domain_name, |
178 PolicyBundle* bundle) { | 106 PolicyBundle* bundle) { |
179 std::string id_prefix(base::mac::BaseBundleID()); | 107 std::string id_prefix(base::mac::BaseBundleID()); |
180 id_prefix.append(".").append(domain_name).append("."); | 108 id_prefix.append(".").append(domain_name).append("."); |
181 | 109 |
182 const ComponentMap* components = schema_map()->GetComponents(domain); | 110 const ComponentMap* components = schema_map()->GetComponents(domain); |
183 if (!components) | 111 if (!components) |
184 return; | 112 return; |
(...skipping 26 matching lines...) Expand all Loading... |
211 base::ScopedCFTypeRef<CFStringRef> pref_name( | 139 base::ScopedCFTypeRef<CFStringRef> pref_name( |
212 base::SysUTF8ToCFStringRef(it.key())); | 140 base::SysUTF8ToCFStringRef(it.key())); |
213 base::ScopedCFTypeRef<CFPropertyListRef> value( | 141 base::ScopedCFTypeRef<CFPropertyListRef> value( |
214 preferences_->CopyAppValue(pref_name, bundle_id)); | 142 preferences_->CopyAppValue(pref_name, bundle_id)); |
215 if (!value.get()) | 143 if (!value.get()) |
216 continue; | 144 continue; |
217 bool forced = | 145 bool forced = |
218 preferences_->AppValueIsForced(pref_name, bundle_id); | 146 preferences_->AppValueIsForced(pref_name, bundle_id); |
219 PolicyLevel level = forced ? POLICY_LEVEL_MANDATORY : | 147 PolicyLevel level = forced ? POLICY_LEVEL_MANDATORY : |
220 POLICY_LEVEL_RECOMMENDED; | 148 POLICY_LEVEL_RECOMMENDED; |
221 scoped_ptr<base::Value> policy_value(CreateValueFromProperty(value)); | 149 scoped_ptr<base::Value> policy_value = PropertyToValue(value); |
222 if (policy_value) { | 150 if (policy_value) { |
223 policy->Set(it.key(), level, POLICY_SCOPE_USER, | 151 policy->Set(it.key(), level, POLICY_SCOPE_USER, |
224 policy_value.release(), NULL); | 152 policy_value.release(), NULL); |
225 } | 153 } |
226 } | 154 } |
227 } | 155 } |
228 | 156 |
229 void PolicyLoaderMac::OnFileUpdated(const base::FilePath& path, bool error) { | 157 void PolicyLoaderMac::OnFileUpdated(const base::FilePath& path, bool error) { |
230 if (!error) | 158 if (!error) |
231 Reload(false); | 159 Reload(false); |
232 } | 160 } |
233 | 161 |
234 } // namespace policy | 162 } // namespace policy |
OLD | NEW |