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