| 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/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 11 #include "base/files/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/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "components/policy/core/common/external_data_fetcher.h" | 17 #include "components/policy/core/common/external_data_fetcher.h" |
| 17 #include "components/policy/core/common/mac_util.h" | 18 #include "components/policy/core/common/mac_util.h" |
| 18 #include "components/policy/core/common/policy_bundle.h" | 19 #include "components/policy/core/common/policy_bundle.h" |
| 19 #include "components/policy/core/common/policy_load_status.h" | 20 #include "components/policy/core/common/policy_load_status.h" |
| 20 #include "components/policy/core/common/policy_map.h" | 21 #include "components/policy/core/common/policy_map.h" |
| 21 #include "components/policy/core/common/preferences_mac.h" | 22 #include "components/policy/core/common/preferences_mac.h" |
| 22 #include "components/policy/core/common/schema.h" | 23 #include "components/policy/core/common/schema.h" |
| 23 #include "components/policy/core/common/schema_map.h" | 24 #include "components/policy/core/common/schema_map.h" |
| 24 | 25 |
| 25 using base::ScopedCFTypeRef; | 26 using base::ScopedCFTypeRef; |
| 26 | 27 |
| 27 namespace policy { | 28 namespace policy { |
| 28 | 29 |
| 29 PolicyLoaderMac::PolicyLoaderMac( | 30 PolicyLoaderMac::PolicyLoaderMac( |
| 30 scoped_refptr<base::SequencedTaskRunner> task_runner, | 31 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 31 const base::FilePath& managed_policy_path, | 32 const base::FilePath& managed_policy_path, |
| 32 MacPreferences* preferences) | 33 MacPreferences* preferences) |
| 33 : AsyncPolicyLoader(task_runner), | 34 : AsyncPolicyLoader(task_runner), |
| 34 preferences_(preferences), | 35 preferences_(preferences), |
| 35 managed_policy_path_(managed_policy_path) {} | 36 managed_policy_path_(managed_policy_path), |
| 37 application_id_(kCFPreferencesCurrentApplication) { |
| 38 } |
| 36 | 39 |
| 37 PolicyLoaderMac::~PolicyLoaderMac() {} | 40 PolicyLoaderMac::PolicyLoaderMac( |
| 41 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 42 const base::FilePath& managed_policy_path, |
| 43 MacPreferences* preferences, |
| 44 CFStringRef application_id) |
| 45 : AsyncPolicyLoader(task_runner), |
| 46 preferences_(preferences), |
| 47 managed_policy_path_(managed_policy_path), |
| 48 application_id_(application_id) { |
| 49 } |
| 50 |
| 51 PolicyLoaderMac::~PolicyLoaderMac() { |
| 52 } |
| 38 | 53 |
| 39 void PolicyLoaderMac::InitOnBackgroundThread() { | 54 void PolicyLoaderMac::InitOnBackgroundThread() { |
| 40 if (!managed_policy_path_.empty()) { | 55 if (!managed_policy_path_.empty()) { |
| 41 watcher_.Watch( | 56 watcher_.Watch( |
| 42 managed_policy_path_, false, | 57 managed_policy_path_, false, |
| 43 base::Bind(&PolicyLoaderMac::OnFileUpdated, base::Unretained(this))); | 58 base::Bind(&PolicyLoaderMac::OnFileUpdated, base::Unretained(this))); |
| 44 } | 59 } |
| 45 } | 60 } |
| 46 | 61 |
| 47 scoped_ptr<PolicyBundle> PolicyLoaderMac::Load() { | 62 scoped_ptr<PolicyBundle> PolicyLoaderMac::Load() { |
| 48 preferences_->AppSynchronize(kCFPreferencesCurrentApplication); | 63 preferences_->AppSynchronize(application_id_); |
| 49 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); | 64 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); |
| 50 | 65 |
| 51 // Load Chrome's policy. | 66 // Load Chrome's policy. |
| 52 PolicyMap& chrome_policy = | 67 PolicyMap& chrome_policy = |
| 53 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); | 68 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); |
| 54 | 69 |
| 55 PolicyLoadStatusSample status; | 70 PolicyLoadStatusSample status; |
| 56 bool policy_present = false; | 71 bool policy_present = false; |
| 57 const Schema* schema = | 72 const Schema* schema = |
| 58 schema_map()->GetSchema(PolicyNamespace(POLICY_DOMAIN_CHROME, "")); | 73 schema_map()->GetSchema(PolicyNamespace(POLICY_DOMAIN_CHROME, "")); |
| 59 for (Schema::Iterator it = schema->GetPropertiesIterator(); | 74 for (Schema::Iterator it = schema->GetPropertiesIterator(); !it.IsAtEnd(); |
| 60 !it.IsAtEnd(); it.Advance()) { | 75 it.Advance()) { |
| 61 base::ScopedCFTypeRef<CFStringRef> name( | 76 base::ScopedCFTypeRef<CFStringRef> name( |
| 62 base::SysUTF8ToCFStringRef(it.key())); | 77 base::SysUTF8ToCFStringRef(it.key())); |
| 63 base::ScopedCFTypeRef<CFPropertyListRef> value( | 78 base::ScopedCFTypeRef<CFPropertyListRef> value( |
| 64 preferences_->CopyAppValue(name, kCFPreferencesCurrentApplication)); | 79 preferences_->CopyAppValue(name, application_id_)); |
| 65 if (!value.get()) | 80 if (!value.get()) |
| 66 continue; | 81 continue; |
| 67 policy_present = true; | 82 policy_present = true; |
| 68 bool forced = | 83 bool forced = preferences_->AppValueIsForced(name, application_id_); |
| 69 preferences_->AppValueIsForced(name, kCFPreferencesCurrentApplication); | 84 PolicyLevel level = |
| 70 PolicyLevel level = forced ? POLICY_LEVEL_MANDATORY : | 85 forced ? POLICY_LEVEL_MANDATORY : POLICY_LEVEL_RECOMMENDED; |
| 71 POLICY_LEVEL_RECOMMENDED; | |
| 72 // TODO(joaodasilva): figure the policy scope. | 86 // TODO(joaodasilva): figure the policy scope. |
| 73 scoped_ptr<base::Value> policy = PropertyToValue(value); | 87 scoped_ptr<base::Value> policy = PropertyToValue(value); |
| 74 if (policy) { | 88 if (policy) { |
| 75 chrome_policy.Set( | 89 chrome_policy.Set(it.key(), level, POLICY_SCOPE_USER, policy.release(), |
| 76 it.key(), level, POLICY_SCOPE_USER, policy.release(), NULL); | 90 NULL); |
| 77 } else { | 91 } else { |
| 78 status.Add(POLICY_LOAD_STATUS_PARSE_ERROR); | 92 status.Add(POLICY_LOAD_STATUS_PARSE_ERROR); |
| 79 } | 93 } |
| 80 } | 94 } |
| 81 | 95 |
| 82 if (!policy_present) | 96 if (!policy_present) |
| 83 status.Add(POLICY_LOAD_STATUS_NO_POLICY); | 97 status.Add(POLICY_LOAD_STATUS_NO_POLICY); |
| 84 | 98 |
| 85 // Load policy for the registered components. | 99 // Load policy for the registered components. |
| 86 LoadPolicyForDomain(POLICY_DOMAIN_EXTENSIONS, "extensions", bundle.get()); | 100 LoadPolicyForDomain(POLICY_DOMAIN_EXTENSIONS, "extensions", bundle.get()); |
| 87 | 101 |
| 88 return bundle.Pass(); | 102 return bundle.Pass(); |
| 89 } | 103 } |
| 90 | 104 |
| 91 base::Time PolicyLoaderMac::LastModificationTime() { | 105 base::Time PolicyLoaderMac::LastModificationTime() { |
| 92 base::File::Info file_info; | 106 base::File::Info file_info; |
| 93 if (!base::GetFileInfo(managed_policy_path_, &file_info) || | 107 if (!base::GetFileInfo(managed_policy_path_, &file_info) || |
| 94 file_info.is_directory) { | 108 file_info.is_directory) { |
| 95 return base::Time(); | 109 return base::Time(); |
| 96 } | 110 } |
| 97 | 111 |
| 98 return file_info.last_modified; | 112 return file_info.last_modified; |
| 99 } | 113 } |
| 100 | 114 |
| 101 void PolicyLoaderMac::LoadPolicyForDomain( | 115 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 102 PolicyDomain domain, | 116 |
| 103 const std::string& domain_name, | 117 base::FilePath PolicyLoaderMac::GetManagedPolicyPath(CFStringRef bundle_id) { |
| 104 PolicyBundle* bundle) { | 118 // This constructs the path to the plist file in which Mac OS X stores the |
| 119 // managed preference for the application. This is undocumented and therefore |
| 120 // fragile, but if it doesn't work out, AsyncPolicyLoader has a task that |
| 121 // polls periodically in order to reload managed preferences later even if we |
| 122 // missed the change. |
| 123 |
| 124 base::FilePath path; |
| 125 if (!base::mac::GetLocalDirectory(NSLibraryDirectory, &path)) |
| 126 return base::FilePath(); |
| 127 path = path.Append(FILE_PATH_LITERAL("Managed Preferences")); |
| 128 char* login = getlogin(); |
| 129 if (!login) |
| 130 return base::FilePath(); |
| 131 path = path.AppendASCII(login); |
| 132 return path.Append(base::SysCFStringRefToUTF8(bundle_id) + ".plist"); |
| 133 } |
| 134 |
| 135 #endif |
| 136 |
| 137 void PolicyLoaderMac::LoadPolicyForDomain(PolicyDomain domain, |
| 138 const std::string& domain_name, |
| 139 PolicyBundle* bundle) { |
| 105 std::string id_prefix(base::mac::BaseBundleID()); | 140 std::string id_prefix(base::mac::BaseBundleID()); |
| 106 id_prefix.append(".").append(domain_name).append("."); | 141 id_prefix.append(".").append(domain_name).append("."); |
| 107 | 142 |
| 108 const ComponentMap* components = schema_map()->GetComponents(domain); | 143 const ComponentMap* components = schema_map()->GetComponents(domain); |
| 109 if (!components) | 144 if (!components) |
| 110 return; | 145 return; |
| 111 | 146 |
| 112 for (ComponentMap::const_iterator it = components->begin(); | 147 for (ComponentMap::const_iterator it = components->begin(); |
| 113 it != components->end(); ++it) { | 148 it != components->end(); ++it) { |
| 114 PolicyMap policy; | 149 PolicyMap policy; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 125 // TODO(joaodasilva): Extensions may be registered in a ComponentMap | 160 // TODO(joaodasilva): Extensions may be registered in a ComponentMap |
| 126 // without a schema, to allow a graceful update of the Legacy Browser Support | 161 // without a schema, to allow a graceful update of the Legacy Browser Support |
| 127 // extension on Windows. Remove this check once that support is removed. | 162 // extension on Windows. Remove this check once that support is removed. |
| 128 if (!schema.valid()) | 163 if (!schema.valid()) |
| 129 return; | 164 return; |
| 130 | 165 |
| 131 base::ScopedCFTypeRef<CFStringRef> bundle_id( | 166 base::ScopedCFTypeRef<CFStringRef> bundle_id( |
| 132 base::SysUTF8ToCFStringRef(bundle_id_string)); | 167 base::SysUTF8ToCFStringRef(bundle_id_string)); |
| 133 preferences_->AppSynchronize(bundle_id); | 168 preferences_->AppSynchronize(bundle_id); |
| 134 | 169 |
| 135 for (Schema::Iterator it = schema.GetPropertiesIterator(); | 170 for (Schema::Iterator it = schema.GetPropertiesIterator(); !it.IsAtEnd(); |
| 136 !it.IsAtEnd(); it.Advance()) { | 171 it.Advance()) { |
| 137 base::ScopedCFTypeRef<CFStringRef> pref_name( | 172 base::ScopedCFTypeRef<CFStringRef> pref_name( |
| 138 base::SysUTF8ToCFStringRef(it.key())); | 173 base::SysUTF8ToCFStringRef(it.key())); |
| 139 base::ScopedCFTypeRef<CFPropertyListRef> value( | 174 base::ScopedCFTypeRef<CFPropertyListRef> value( |
| 140 preferences_->CopyAppValue(pref_name, bundle_id)); | 175 preferences_->CopyAppValue(pref_name, bundle_id)); |
| 141 if (!value.get()) | 176 if (!value.get()) |
| 142 continue; | 177 continue; |
| 143 bool forced = | 178 bool forced = preferences_->AppValueIsForced(pref_name, bundle_id); |
| 144 preferences_->AppValueIsForced(pref_name, bundle_id); | 179 PolicyLevel level = |
| 145 PolicyLevel level = forced ? POLICY_LEVEL_MANDATORY : | 180 forced ? POLICY_LEVEL_MANDATORY : POLICY_LEVEL_RECOMMENDED; |
| 146 POLICY_LEVEL_RECOMMENDED; | |
| 147 scoped_ptr<base::Value> policy_value = PropertyToValue(value); | 181 scoped_ptr<base::Value> policy_value = PropertyToValue(value); |
| 148 if (policy_value) { | 182 if (policy_value) { |
| 149 policy->Set(it.key(), level, POLICY_SCOPE_USER, | 183 policy->Set(it.key(), level, POLICY_SCOPE_USER, policy_value.release(), |
| 150 policy_value.release(), NULL); | 184 NULL); |
| 151 } | 185 } |
| 152 } | 186 } |
| 153 } | 187 } |
| 154 | 188 |
| 155 void PolicyLoaderMac::OnFileUpdated(const base::FilePath& path, bool error) { | 189 void PolicyLoaderMac::OnFileUpdated(const base::FilePath& path, bool error) { |
| 156 if (!error) | 190 if (!error) |
| 157 Reload(false); | 191 Reload(false); |
| 158 } | 192 } |
| 159 | 193 |
| 160 } // namespace policy | 194 } // namespace policy |
| OLD | NEW |