| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/policy/configuration_policy_provider_mac.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/file_path.h" | |
| 10 #include "base/file_util.h" | |
| 11 #include "base/mac/foundation_util.h" | |
| 12 #include "base/mac/scoped_cftyperef.h" | |
| 13 #include "base/path_service.h" | |
| 14 #include "base/platform_file.h" | |
| 15 #include "base/sys_string_conversions.h" | |
| 16 #include "base/values.h" | |
| 17 #include "chrome/browser/policy/policy_bundle.h" | |
| 18 #include "chrome/browser/policy/policy_map.h" | |
| 19 #include "chrome/browser/preferences_mac.h" | |
| 20 #include "chrome/common/chrome_paths.h" | |
| 21 #include "policy/policy_constants.h" | |
| 22 | |
| 23 using base::mac::CFCast; | |
| 24 using base::mac::ScopedCFTypeRef; | |
| 25 | |
| 26 namespace policy { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 FilePath GetManagedPolicyPath() { | |
| 31 // This constructs the path to the plist file in which Mac OS X stores the | |
| 32 // managed preference for the application. This is undocumented and therefore | |
| 33 // fragile, but if it doesn't work out, FileBasedPolicyLoader has a task that | |
| 34 // polls periodically in order to reload managed preferences later even if we | |
| 35 // missed the change. | |
| 36 FilePath path; | |
| 37 if (!PathService::Get(chrome::DIR_MANAGED_PREFS, &path)) | |
| 38 return FilePath(); | |
| 39 | |
| 40 CFBundleRef bundle(CFBundleGetMainBundle()); | |
| 41 if (!bundle) | |
| 42 return FilePath(); | |
| 43 | |
| 44 CFStringRef bundle_id = CFBundleGetIdentifier(bundle); | |
| 45 if (!bundle_id) | |
| 46 return FilePath(); | |
| 47 | |
| 48 return path.Append(base::SysCFStringRefToUTF8(bundle_id) + ".plist"); | |
| 49 } | |
| 50 | |
| 51 // Callback function for CFDictionaryApplyFunction. |key| and |value| are an | |
| 52 // entry of the CFDictionary that should be converted into an equivalent entry | |
| 53 // in the DictionaryValue in |context|. | |
| 54 void DictionaryEntryToValue(const void* key, const void* value, void* context) { | |
| 55 if (CFStringRef cf_key = CFCast<CFStringRef>(key)) { | |
| 56 base::Value* converted = | |
| 57 MacPreferencesPolicyProviderDelegate::CreateValueFromProperty( | |
| 58 static_cast<CFPropertyListRef>(value)); | |
| 59 if (converted) { | |
| 60 const std::string string = base::SysCFStringRefToUTF8(cf_key); | |
| 61 static_cast<base::DictionaryValue *>(context)->Set(string, converted); | |
| 62 } | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 // Callback function for CFArrayApplyFunction. |value| is an entry of the | |
| 67 // CFArray that should be converted into an equivalent entry in the ListValue | |
| 68 // in |context|. | |
| 69 void ArrayEntryToValue(const void* value, void* context) { | |
| 70 base::Value* converted = | |
| 71 MacPreferencesPolicyProviderDelegate::CreateValueFromProperty( | |
| 72 static_cast<CFPropertyListRef>(value)); | |
| 73 if (converted) | |
| 74 static_cast<base::ListValue *>(context)->Append(converted); | |
| 75 } | |
| 76 | |
| 77 } // namespace | |
| 78 | |
| 79 MacPreferencesPolicyProviderDelegate::MacPreferencesPolicyProviderDelegate( | |
| 80 MacPreferences* preferences, | |
| 81 const PolicyDefinitionList* policy_list) | |
| 82 : FileBasedPolicyProvider::ProviderDelegate(GetManagedPolicyPath()), | |
| 83 policy_list_(policy_list), | |
| 84 preferences_(preferences) {} | |
| 85 | |
| 86 MacPreferencesPolicyProviderDelegate::~MacPreferencesPolicyProviderDelegate() {} | |
| 87 | |
| 88 scoped_ptr<PolicyBundle> MacPreferencesPolicyProviderDelegate::Load() { | |
| 89 preferences_->AppSynchronize(kCFPreferencesCurrentApplication); | |
| 90 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); | |
| 91 PolicyMap& chrome_policy = bundle->Get(POLICY_DOMAIN_CHROME, std::string()); | |
| 92 | |
| 93 const PolicyDefinitionList::Entry* current; | |
| 94 for (current = policy_list_->begin; current != policy_list_->end; ++current) { | |
| 95 base::mac::ScopedCFTypeRef<CFStringRef> name( | |
| 96 base::SysUTF8ToCFStringRef(current->name)); | |
| 97 base::mac::ScopedCFTypeRef<CFPropertyListRef> value( | |
| 98 preferences_->CopyAppValue(name, kCFPreferencesCurrentApplication)); | |
| 99 if (!value.get()) | |
| 100 continue; | |
| 101 bool forced = | |
| 102 preferences_->AppValueIsForced(name, kCFPreferencesCurrentApplication); | |
| 103 PolicyLevel level = forced ? POLICY_LEVEL_MANDATORY : | |
| 104 POLICY_LEVEL_RECOMMENDED; | |
| 105 // TODO(joaodasilva): figure the policy scope. | |
| 106 base::Value* policy = CreateValueFromProperty(value); | |
| 107 if (policy) | |
| 108 chrome_policy.Set(current->name, level, POLICY_SCOPE_USER, policy); | |
| 109 } | |
| 110 | |
| 111 return bundle.Pass(); | |
| 112 } | |
| 113 | |
| 114 base::Time MacPreferencesPolicyProviderDelegate::GetLastModification() { | |
| 115 base::PlatformFileInfo file_info; | |
| 116 if (!file_util::GetFileInfo(config_file_path(), &file_info) || | |
| 117 file_info.is_directory) { | |
| 118 return base::Time(); | |
| 119 } | |
| 120 | |
| 121 return file_info.last_modified; | |
| 122 } | |
| 123 | |
| 124 // static | |
| 125 base::Value* MacPreferencesPolicyProviderDelegate::CreateValueFromProperty( | |
| 126 CFPropertyListRef property) { | |
| 127 if (CFCast<CFNullRef>(property)) | |
| 128 return base::Value::CreateNullValue(); | |
| 129 | |
| 130 if (CFBooleanRef boolean = CFCast<CFBooleanRef>(property)) | |
| 131 return base::Value::CreateBooleanValue(CFBooleanGetValue(boolean)); | |
| 132 | |
| 133 if (CFNumberRef number = CFCast<CFNumberRef>(property)) { | |
| 134 // CFNumberGetValue() converts values implicitly when the conversion is not | |
| 135 // lossy. Check the type before trying to convert. | |
| 136 if (CFNumberIsFloatType(number)) { | |
| 137 double double_value; | |
| 138 if (CFNumberGetValue(number, kCFNumberDoubleType, &double_value)) | |
| 139 return base::Value::CreateDoubleValue(double_value); | |
| 140 } else { | |
| 141 int int_value; | |
| 142 if (CFNumberGetValue(number, kCFNumberIntType, &int_value)) | |
| 143 return base::Value::CreateIntegerValue(int_value); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 if (CFStringRef string = CFCast<CFStringRef>(property)) | |
| 148 return base::Value::CreateStringValue(base::SysCFStringRefToUTF8(string)); | |
| 149 | |
| 150 if (CFDictionaryRef dict = CFCast<CFDictionaryRef>(property)) { | |
| 151 base::DictionaryValue* dict_value = new base::DictionaryValue(); | |
| 152 CFDictionaryApplyFunction(dict, DictionaryEntryToValue, dict_value); | |
| 153 return dict_value; | |
| 154 } | |
| 155 | |
| 156 if (CFArrayRef array = CFCast<CFArrayRef>(property)) { | |
| 157 base::ListValue* list_value = new base::ListValue(); | |
| 158 CFArrayApplyFunction(array, | |
| 159 CFRangeMake(0, CFArrayGetCount(array)), | |
| 160 ArrayEntryToValue, | |
| 161 list_value); | |
| 162 return list_value; | |
| 163 } | |
| 164 | |
| 165 return NULL; | |
| 166 } | |
| 167 | |
| 168 ConfigurationPolicyProviderMac::ConfigurationPolicyProviderMac( | |
| 169 const PolicyDefinitionList* policy_list) | |
| 170 : FileBasedPolicyProvider( | |
| 171 policy_list, | |
| 172 new MacPreferencesPolicyProviderDelegate(new MacPreferences(), | |
| 173 policy_list)) {} | |
| 174 | |
| 175 ConfigurationPolicyProviderMac::ConfigurationPolicyProviderMac( | |
| 176 const PolicyDefinitionList* policy_list, | |
| 177 MacPreferences* preferences) | |
| 178 : FileBasedPolicyProvider( | |
| 179 policy_list, | |
| 180 new MacPreferencesPolicyProviderDelegate(preferences, | |
| 181 policy_list)) {} | |
| 182 | |
| 183 } // namespace policy | |
| OLD | NEW |