| OLD | NEW |
| 1 // Copyright (c) 2011 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_reader.h" | 5 #include "chrome/browser/policy/configuration_policy_reader.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 12 #include "base/string16.h" | 13 #include "base/string16.h" |
| 13 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/policy/browser_policy_connector.h" | 16 #include "chrome/browser/policy/browser_policy_connector.h" |
| 16 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 17 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 17 #include "chrome/browser/policy/policy_error_map.h" | 18 #include "chrome/browser/policy/policy_error_map.h" |
| 18 #include "chrome/browser/policy/policy_map.h" | 19 #include "chrome/browser/policy/policy_map.h" |
| 19 #include "policy/policy_constants.h" | 20 #include "policy/policy_constants.h" |
| 20 | 21 |
| 21 namespace policy { | 22 namespace policy { |
| 22 | 23 |
| 23 // This class functions as a container for policy status information used by the | 24 // This class functions as a container for policy status information used by the |
| 24 // ConfigurationPolicyReader class. It obtains policy values from a | 25 // ConfigurationPolicyReader class. It obtains policy values from a |
| 25 // ConfigurationPolicyProvider and maps them to their status information. | 26 // ConfigurationPolicyProvider and maps them to their status information. |
| 26 class ConfigurationPolicyStatusKeeper { | 27 class ConfigurationPolicyStatusKeeper { |
| 27 public: | 28 public: |
| 28 ConfigurationPolicyStatusKeeper(ConfigurationPolicyProvider* provider, | 29 explicit ConfigurationPolicyStatusKeeper( |
| 29 PolicyStatusInfo::PolicyLevel policy_level); | 30 ConfigurationPolicyProvider* provider); |
| 30 virtual ~ConfigurationPolicyStatusKeeper(); | 31 virtual ~ConfigurationPolicyStatusKeeper(); |
| 31 | 32 |
| 32 // Returns a pointer to a DictionaryValue containing the status information | 33 // Returns a pointer to a DictionaryValue containing the status information |
| 33 // of the policy |policy|. The caller acquires ownership of the returned | 34 // of the policy |policy|. The caller acquires ownership of the returned |
| 34 // value. Returns NULL if no such policy is stored in this keeper. | 35 // value. Returns NULL if no such policy is stored in this keeper. |
| 35 DictionaryValue* GetPolicyStatus(ConfigurationPolicyType policy) const; | 36 DictionaryValue* GetPolicyStatus(const char* policy) const; |
| 36 | 37 |
| 37 private: | 38 private: |
| 38 typedef std::map<ConfigurationPolicyType, PolicyStatusInfo*> PolicyStatusMap; | 39 typedef std::map<std::string, PolicyStatusInfo*> PolicyStatusMap; |
| 39 | 40 |
| 40 // Calls Provide() on the passed in |provider| to get policy values. | 41 // Calls Provide() on the passed in |provider| to get policy values. |
| 41 void GetPoliciesFromProvider(ConfigurationPolicyProvider* provider); | 42 void GetPoliciesFromProvider(ConfigurationPolicyProvider* provider); |
| 42 | 43 |
| 43 // Mapping from ConfigurationPolicyType to PolicyStatusInfo. | 44 // Mapping from policy name to PolicyStatusInfo. |
| 44 PolicyStatusMap policy_map_; | 45 PolicyStatusMap policy_map_; |
| 45 | 46 |
| 46 // The level of the policies stored in this keeper (mandatory or | |
| 47 // recommended). | |
| 48 PolicyStatusInfo::PolicyLevel policy_level_; | |
| 49 | |
| 50 DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyStatusKeeper); | 47 DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyStatusKeeper); |
| 51 }; | 48 }; |
| 52 | 49 |
| 53 // ConfigurationPolicyStatusKeeper | 50 // ConfigurationPolicyStatusKeeper |
| 54 ConfigurationPolicyStatusKeeper::ConfigurationPolicyStatusKeeper( | 51 ConfigurationPolicyStatusKeeper::ConfigurationPolicyStatusKeeper( |
| 55 ConfigurationPolicyProvider* provider, | 52 ConfigurationPolicyProvider* provider) { |
| 56 PolicyStatusInfo::PolicyLevel policy_level) : policy_level_(policy_level) { | |
| 57 GetPoliciesFromProvider(provider); | 53 GetPoliciesFromProvider(provider); |
| 58 } | 54 } |
| 59 | 55 |
| 60 ConfigurationPolicyStatusKeeper::~ConfigurationPolicyStatusKeeper() { | 56 ConfigurationPolicyStatusKeeper::~ConfigurationPolicyStatusKeeper() { |
| 61 STLDeleteContainerPairSecondPointers(policy_map_.begin(), policy_map_.end()); | 57 STLDeleteContainerPairSecondPointers(policy_map_.begin(), policy_map_.end()); |
| 62 policy_map_.clear(); | 58 policy_map_.clear(); |
| 63 } | 59 } |
| 64 | 60 |
| 65 DictionaryValue* ConfigurationPolicyStatusKeeper::GetPolicyStatus( | 61 DictionaryValue* ConfigurationPolicyStatusKeeper::GetPolicyStatus( |
| 66 ConfigurationPolicyType policy) const { | 62 const char* policy) const { |
| 67 PolicyStatusMap::const_iterator entry = policy_map_.find(policy); | 63 PolicyStatusMap::const_iterator entry = policy_map_.find(policy); |
| 68 return entry != policy_map_.end() ? | 64 return entry != policy_map_.end() ? |
| 69 (entry->second)->GetDictionaryValue() : NULL; | 65 (entry->second)->GetDictionaryValue() : NULL; |
| 70 } | 66 } |
| 71 | 67 |
| 72 void ConfigurationPolicyStatusKeeper::GetPoliciesFromProvider( | 68 void ConfigurationPolicyStatusKeeper::GetPoliciesFromProvider( |
| 73 ConfigurationPolicyProvider* provider) { | 69 ConfigurationPolicyProvider* provider) { |
| 74 PolicyMap policies; | 70 PolicyMap policies; |
| 75 if (!provider->Provide(&policies)) | 71 if (!provider->Provide(&policies)) |
| 76 DLOG(WARNING) << "Failed to get policy from provider."; | 72 DLOG(WARNING) << "Failed to get policy from provider."; |
| 77 | 73 |
| 78 PolicyErrorMap errors; | 74 PolicyErrorMap errors; |
| 79 const ConfigurationPolicyHandlerList* handler_list = | 75 const ConfigurationPolicyHandlerList* handler_list = |
| 80 g_browser_process->browser_policy_connector()->GetHandlerList(); | 76 g_browser_process->browser_policy_connector()->GetHandlerList(); |
| 81 handler_list->ApplyPolicySettings(policies, NULL, &errors); | 77 handler_list->ApplyPolicySettings(policies, NULL, &errors); |
| 82 handler_list->PrepareForDisplaying(&policies); | 78 handler_list->PrepareForDisplaying(&policies); |
| 83 | 79 |
| 84 PolicyMap::const_iterator policy = policies.begin(); | 80 PolicyMap::const_iterator policy = policies.begin(); |
| 85 for ( ; policy != policies.end(); ++policy) { | 81 for ( ; policy != policies.end(); ++policy) { |
| 86 string16 name = ASCIIToUTF16(GetPolicyName(policy->first)); | 82 string16 name = ASCIIToUTF16(policy->first); |
| 83 const PolicyMap::Entry& entry = policy->second; |
| 87 string16 error_message = errors.GetErrors(policy->first); | 84 string16 error_message = errors.GetErrors(policy->first); |
| 88 PolicyStatusInfo::PolicyStatus status = | 85 PolicyStatusInfo::PolicyStatus status = |
| 89 error_message.empty() ? PolicyStatusInfo::ENFORCED | 86 error_message.empty() ? PolicyStatusInfo::ENFORCED |
| 90 : PolicyStatusInfo::FAILED; | 87 : PolicyStatusInfo::FAILED; |
| 91 // TODO(joaodasilva): determine whether this is a user or device policy. | 88 policy_map_[policy->first] = new PolicyStatusInfo(name, |
| 92 // http://crbug.com/102114 | 89 entry.scope, |
| 93 PolicyStatusInfo* info = new PolicyStatusInfo(name, | 90 entry.level, |
| 94 PolicyStatusInfo::USER, | 91 entry.value->DeepCopy(), |
| 95 policy_level_, | 92 status, |
| 96 policy->second->DeepCopy(), | 93 error_message); |
| 97 status, | |
| 98 error_message); | |
| 99 policy_map_[policy->first] = info; | |
| 100 } | 94 } |
| 101 } | 95 } |
| 102 | 96 |
| 103 // ConfigurationPolicyReader | 97 // ConfigurationPolicyReader |
| 104 ConfigurationPolicyReader::ConfigurationPolicyReader( | 98 ConfigurationPolicyReader::ConfigurationPolicyReader( |
| 105 ConfigurationPolicyProvider* provider, | 99 ConfigurationPolicyProvider* provider) |
| 106 PolicyStatusInfo::PolicyLevel policy_level) | 100 : provider_(provider) { |
| 107 : provider_(provider), | |
| 108 policy_level_(policy_level) { | |
| 109 if (provider_) { | 101 if (provider_) { |
| 110 // Read initial policy. | 102 // Read initial policy. |
| 111 policy_keeper_.reset( | 103 policy_keeper_.reset(new ConfigurationPolicyStatusKeeper(provider_)); |
| 112 new ConfigurationPolicyStatusKeeper(provider_, policy_level)); | |
| 113 registrar_.Init(provider_, this); | 104 registrar_.Init(provider_, this); |
| 114 } | 105 } |
| 115 } | 106 } |
| 116 | 107 |
| 117 ConfigurationPolicyReader::~ConfigurationPolicyReader() { | 108 ConfigurationPolicyReader::~ConfigurationPolicyReader() { |
| 118 } | 109 } |
| 119 | 110 |
| 120 void ConfigurationPolicyReader::OnUpdatePolicy( | 111 void ConfigurationPolicyReader::OnUpdatePolicy( |
| 121 ConfigurationPolicyProvider* provider) { | 112 ConfigurationPolicyProvider* provider) { |
| 122 Refresh(); | 113 Refresh(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 133 | 124 |
| 134 void ConfigurationPolicyReader::RemoveObserver(Observer* observer) { | 125 void ConfigurationPolicyReader::RemoveObserver(Observer* observer) { |
| 135 observers_.RemoveObserver(observer); | 126 observers_.RemoveObserver(observer); |
| 136 } | 127 } |
| 137 | 128 |
| 138 // static | 129 // static |
| 139 ConfigurationPolicyReader* | 130 ConfigurationPolicyReader* |
| 140 ConfigurationPolicyReader::CreateManagedPlatformPolicyReader() { | 131 ConfigurationPolicyReader::CreateManagedPlatformPolicyReader() { |
| 141 BrowserPolicyConnector* connector = | 132 BrowserPolicyConnector* connector = |
| 142 g_browser_process->browser_policy_connector(); | 133 g_browser_process->browser_policy_connector(); |
| 143 return new ConfigurationPolicyReader( | 134 return new ConfigurationPolicyReader(connector->GetManagedPlatformProvider()); |
| 144 connector->GetManagedPlatformProvider(), PolicyStatusInfo::MANDATORY); | |
| 145 } | 135 } |
| 146 | 136 |
| 147 // static | 137 // static |
| 148 ConfigurationPolicyReader* | 138 ConfigurationPolicyReader* |
| 149 ConfigurationPolicyReader::CreateManagedCloudPolicyReader() { | 139 ConfigurationPolicyReader::CreateManagedCloudPolicyReader() { |
| 150 BrowserPolicyConnector* connector = | 140 BrowserPolicyConnector* connector = |
| 151 g_browser_process->browser_policy_connector(); | 141 g_browser_process->browser_policy_connector(); |
| 152 return new ConfigurationPolicyReader( | 142 return new ConfigurationPolicyReader(connector->GetManagedCloudProvider()); |
| 153 connector->GetManagedCloudProvider(), PolicyStatusInfo::MANDATORY); | |
| 154 } | 143 } |
| 155 | 144 |
| 156 // static | 145 // static |
| 157 ConfigurationPolicyReader* | 146 ConfigurationPolicyReader* |
| 158 ConfigurationPolicyReader::CreateRecommendedPlatformPolicyReader() { | 147 ConfigurationPolicyReader::CreateRecommendedPlatformPolicyReader() { |
| 159 BrowserPolicyConnector* connector = | 148 BrowserPolicyConnector* connector = |
| 160 g_browser_process->browser_policy_connector(); | 149 g_browser_process->browser_policy_connector(); |
| 161 return new ConfigurationPolicyReader( | 150 return new ConfigurationPolicyReader( |
| 162 connector->GetRecommendedPlatformProvider(), | 151 connector->GetRecommendedPlatformProvider()); |
| 163 PolicyStatusInfo::RECOMMENDED); | |
| 164 } | 152 } |
| 165 | 153 |
| 166 // static | 154 // static |
| 167 ConfigurationPolicyReader* | 155 ConfigurationPolicyReader* |
| 168 ConfigurationPolicyReader::CreateRecommendedCloudPolicyReader() { | 156 ConfigurationPolicyReader::CreateRecommendedCloudPolicyReader() { |
| 169 BrowserPolicyConnector* connector = | 157 BrowserPolicyConnector* connector = |
| 170 g_browser_process->browser_policy_connector(); | 158 g_browser_process->browser_policy_connector(); |
| 171 return new ConfigurationPolicyReader( | 159 return new ConfigurationPolicyReader( |
| 172 connector->GetRecommendedCloudProvider(), PolicyStatusInfo::RECOMMENDED); | 160 connector->GetRecommendedCloudProvider()); |
| 173 } | 161 } |
| 174 | 162 |
| 175 DictionaryValue* ConfigurationPolicyReader::GetPolicyStatus( | 163 DictionaryValue* ConfigurationPolicyReader::GetPolicyStatus( |
| 176 ConfigurationPolicyType policy) const { | 164 const char* policy) const { |
| 177 if (policy_keeper_.get()) | 165 if (policy_keeper_.get()) |
| 178 return policy_keeper_->GetPolicyStatus(policy); | 166 return policy_keeper_->GetPolicyStatus(policy); |
| 179 return NULL; | 167 return NULL; |
| 180 } | 168 } |
| 181 | 169 |
| 182 ConfigurationPolicyReader::ConfigurationPolicyReader() | 170 ConfigurationPolicyReader::ConfigurationPolicyReader() |
| 183 : provider_(NULL), | 171 : provider_(NULL), |
| 184 policy_level_(PolicyStatusInfo::LEVEL_UNDEFINED), | |
| 185 policy_keeper_(NULL) { | 172 policy_keeper_(NULL) { |
| 186 } | 173 } |
| 187 | 174 |
| 188 void ConfigurationPolicyReader::Refresh() { | 175 void ConfigurationPolicyReader::Refresh() { |
| 189 if (!provider_) | 176 if (!provider_) |
| 190 return; | 177 return; |
| 191 | 178 policy_keeper_.reset(new ConfigurationPolicyStatusKeeper(provider_)); |
| 192 policy_keeper_.reset( | |
| 193 new ConfigurationPolicyStatusKeeper(provider_, policy_level_)); | |
| 194 | |
| 195 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyValuesChanged()); | 179 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyValuesChanged()); |
| 196 } | 180 } |
| 197 | 181 |
| 198 // PolicyStatus | 182 // PolicyStatus |
| 199 PolicyStatus::PolicyStatus(ConfigurationPolicyReader* managed_platform, | 183 PolicyStatus::PolicyStatus(ConfigurationPolicyReader* managed_platform, |
| 200 ConfigurationPolicyReader* managed_cloud, | 184 ConfigurationPolicyReader* managed_cloud, |
| 201 ConfigurationPolicyReader* recommended_platform, | 185 ConfigurationPolicyReader* recommended_platform, |
| 202 ConfigurationPolicyReader* recommended_cloud) | 186 ConfigurationPolicyReader* recommended_cloud) |
| 203 : managed_platform_(managed_platform), | 187 : managed_platform_(managed_platform), |
| 204 managed_cloud_(managed_cloud), | 188 managed_cloud_(managed_cloud), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 225 | 209 |
| 226 ListValue* PolicyStatus::GetPolicyStatusList(bool* any_policies_set) const { | 210 ListValue* PolicyStatus::GetPolicyStatusList(bool* any_policies_set) const { |
| 227 ListValue* result = new ListValue(); | 211 ListValue* result = new ListValue(); |
| 228 std::vector<DictionaryValue*> unset_policies; | 212 std::vector<DictionaryValue*> unset_policies; |
| 229 | 213 |
| 230 *any_policies_set = false; | 214 *any_policies_set = false; |
| 231 const PolicyDefinitionList* supported_policies = | 215 const PolicyDefinitionList* supported_policies = |
| 232 GetChromePolicyDefinitionList(); | 216 GetChromePolicyDefinitionList(); |
| 233 const PolicyDefinitionList::Entry* policy = supported_policies->begin; | 217 const PolicyDefinitionList::Entry* policy = supported_policies->begin; |
| 234 for ( ; policy != supported_policies->end; ++policy) { | 218 for ( ; policy != supported_policies->end; ++policy) { |
| 235 if (!AddPolicyFromReaders(policy->policy_type, result)) { | 219 if (!AddPolicyFromReaders(policy->name, result)) { |
| 236 PolicyStatusInfo info(ASCIIToUTF16(policy->name), | 220 PolicyStatusInfo info(ASCIIToUTF16(policy->name), |
| 237 PolicyStatusInfo::SOURCE_TYPE_UNDEFINED, | 221 POLICY_SCOPE_USER, |
| 238 PolicyStatusInfo::LEVEL_UNDEFINED, | 222 POLICY_LEVEL_MANDATORY, |
| 239 Value::CreateNullValue(), | 223 Value::CreateNullValue(), |
| 240 PolicyStatusInfo::STATUS_UNDEFINED, | 224 PolicyStatusInfo::STATUS_UNDEFINED, |
| 241 string16()); | 225 string16()); |
| 242 unset_policies.push_back(info.GetDictionaryValue()); | 226 unset_policies.push_back(info.GetDictionaryValue()); |
| 243 } else { | 227 } else { |
| 244 *any_policies_set = true; | 228 *any_policies_set = true; |
| 245 } | 229 } |
| 246 } | 230 } |
| 247 | 231 |
| 248 // Add policies that weren't actually sent from providers to list. | 232 // Add policies that weren't actually sent from providers to list. |
| 249 std::vector<DictionaryValue*>::const_iterator info = unset_policies.begin(); | 233 std::vector<DictionaryValue*>::const_iterator info = unset_policies.begin(); |
| 250 for ( ; info != unset_policies.end(); ++info) | 234 for ( ; info != unset_policies.end(); ++info) |
| 251 result->Append(*info); | 235 result->Append(*info); |
| 252 | 236 |
| 253 return result; | 237 return result; |
| 254 } | 238 } |
| 255 | 239 |
| 256 bool PolicyStatus::AddPolicyFromReaders( | 240 bool PolicyStatus::AddPolicyFromReaders( |
| 257 ConfigurationPolicyType policy, ListValue* list) const { | 241 const char* policy, ListValue* list) const { |
| 258 DictionaryValue* mp_policy = | 242 DictionaryValue* mp_policy = |
| 259 managed_platform_->GetPolicyStatus(policy); | 243 managed_platform_->GetPolicyStatus(policy); |
| 260 DictionaryValue* mc_policy = | 244 DictionaryValue* mc_policy = |
| 261 managed_cloud_->GetPolicyStatus(policy); | 245 managed_cloud_->GetPolicyStatus(policy); |
| 262 DictionaryValue* rp_policy = | 246 DictionaryValue* rp_policy = |
| 263 recommended_platform_->GetPolicyStatus(policy); | 247 recommended_platform_->GetPolicyStatus(policy); |
| 264 DictionaryValue* rc_policy = | 248 DictionaryValue* rc_policy = |
| 265 recommended_cloud_->GetPolicyStatus(policy); | 249 recommended_cloud_->GetPolicyStatus(policy); |
| 266 | 250 |
| 267 bool added_policy = mp_policy || mc_policy || rp_policy || rc_policy; | 251 bool added_policy = mp_policy || mc_policy || rp_policy || rc_policy; |
| 268 | 252 |
| 269 if (mp_policy) | 253 if (mp_policy) |
| 270 list->Append(mp_policy); | 254 list->Append(mp_policy); |
| 271 if (mc_policy) | 255 if (mc_policy) |
| 272 list->Append(mc_policy); | 256 list->Append(mc_policy); |
| 273 if (rp_policy) | 257 if (rp_policy) |
| 274 list->Append(rp_policy); | 258 list->Append(rp_policy); |
| 275 if (rc_policy) | 259 if (rc_policy) |
| 276 list->Append(rc_policy); | 260 list->Append(rc_policy); |
| 277 | 261 |
| 278 return added_policy; | 262 return added_policy; |
| 279 } | 263 } |
| 280 | 264 |
| 281 } // namespace policy | 265 } // namespace policy |
| OLD | NEW |