Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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_reader.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "chrome/browser/browser_process.h" | |
| 12 #include "chrome/browser/policy/browser_policy_connector.h" | |
| 13 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
| 14 #include "chrome/browser/policy/configuration_policy_store_interface.h" | |
| 15 | |
| 16 namespace policy { | |
| 17 | |
| 18 // This class functions as a container for policy status information used by the | |
| 19 // ConfigurationPolicyReader class. It obtains policy values from a | |
| 20 // ConfigurationPolicyProvider and maps them to their status information. | |
| 21 class ConfigurationPolicyStatusKeeper | |
| 22 : public ConfigurationPolicyStoreInterface { | |
| 23 public: | |
| 24 ConfigurationPolicyStatusKeeper(ConfigurationPolicyProvider* provider, | |
| 25 PolicyStatusInfo::PolicyLevel policy_level); | |
| 26 virtual ~ConfigurationPolicyStatusKeeper(); | |
| 27 | |
| 28 // ConfigurationPolicyStoreInterface methods. | |
| 29 virtual void Apply(ConfigurationPolicyType policy, base::Value* value); | |
| 30 | |
| 31 // Returns a pointer to a DictionaryValue containing the status information | |
| 32 // of the policy |policy|. The caller acquires ownership of the returned | |
| 33 // value. Returns NULL if no such policy is stored in this keeper. | |
| 34 DictionaryValue* GetPolicyStatus(ConfigurationPolicyType policy) const; | |
| 35 | |
| 36 private: | |
| 37 typedef std::map<ConfigurationPolicyType, PolicyStatusInfo*> PolicyStatusMap; | |
| 38 typedef std::map<ConfigurationPolicyType, string16> PolicyNameMap; | |
| 39 typedef ConfigurationPolicyProvider::PolicyDefinitionList | |
| 40 PolicyDefinitionList; | |
| 41 | |
| 42 // Mapping from ConfigurationPolicyType to PolicyStatusInfo. | |
| 43 PolicyStatusMap policy_map_; | |
| 44 | |
| 45 // The level of the policies stored in this keeper (mandatory or | |
| 46 // recommended). | |
| 47 PolicyStatusInfo::PolicyLevel policy_level_; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyStatusKeeper); | |
| 50 }; | |
| 51 | |
| 52 // ConfigurationPolicyStatusKeeper | |
| 53 ConfigurationPolicyStatusKeeper::ConfigurationPolicyStatusKeeper( | |
| 54 ConfigurationPolicyProvider* provider, | |
| 55 PolicyStatusInfo::PolicyLevel policy_level) : policy_level_(policy_level) { | |
| 56 | |
| 57 if (!provider->Provide(this)) | |
| 58 LOG(WARNING) << "Failed to get policy from provider."; | |
| 59 } | |
| 60 | |
| 61 ConfigurationPolicyStatusKeeper::~ConfigurationPolicyStatusKeeper() { | |
| 62 PolicyStatusMap::iterator entry = policy_map_.begin(); | |
| 63 for (; entry != policy_map_.end(); ++entry) | |
| 64 delete entry->second; | |
|
Mattias Nissler (ping if slow)
2011/08/29 11:24:40
Please use STLDeleteContainerPointers from base/st
| |
| 65 policy_map_.clear(); | |
| 66 } | |
| 67 | |
| 68 void ConfigurationPolicyStatusKeeper::Apply( | |
| 69 ConfigurationPolicyType policy, base::Value* value) { | |
| 70 string16 name = PolicyStatus::GetPolicyName(policy); | |
| 71 | |
| 72 if (name == string16()) { | |
| 73 NOTREACHED(); | |
| 74 } | |
| 75 | |
| 76 // TODO(simo) actually determine whether the policy is a user or a device one | |
| 77 // and whether the policy could be enforced or not once this information | |
| 78 // is available. | |
| 79 PolicyStatusInfo* info = new PolicyStatusInfo(name, | |
| 80 PolicyStatusInfo::USER, | |
| 81 policy_level_, | |
| 82 value, | |
| 83 PolicyStatusInfo::ENFORCED, | |
| 84 string16()); | |
| 85 policy_map_[policy] = info; | |
| 86 } | |
| 87 | |
| 88 DictionaryValue* ConfigurationPolicyStatusKeeper::GetPolicyStatus( | |
| 89 ConfigurationPolicyType policy) const { | |
| 90 PolicyStatusMap::const_iterator entry = policy_map_.find(policy); | |
| 91 return entry != policy_map_.end() ? | |
| 92 (entry->second)->GetDictionaryValue() : NULL; | |
| 93 } | |
| 94 | |
| 95 // ConfigurationPolicyReader | |
| 96 ConfigurationPolicyReader::~ConfigurationPolicyReader() { | |
| 97 } | |
| 98 | |
| 99 void ConfigurationPolicyReader::OnUpdatePolicy() { | |
| 100 Refresh(); | |
| 101 } | |
| 102 | |
| 103 void ConfigurationPolicyReader::OnProviderGoingAway() { | |
| 104 provider_ = NULL; | |
| 105 } | |
| 106 | |
| 107 // static | |
| 108 ConfigurationPolicyReader* | |
| 109 ConfigurationPolicyReader::CreateManagedPlatformPolicyReader( | |
| 110 PolicyStatusInfo::PolicyLevel policy_level) { | |
| 111 BrowserPolicyConnector* connector = | |
| 112 g_browser_process->browser_policy_connector(); | |
| 113 return new ConfigurationPolicyReader( | |
| 114 connector->GetManagedPlatformProvider(), policy_level); | |
|
Mattias Nissler (ping if slow)
2011/08/29 11:24:40
I think I said this before, but I cannot seem to r
| |
| 115 } | |
| 116 | |
| 117 // static | |
| 118 ConfigurationPolicyReader* | |
| 119 ConfigurationPolicyReader::CreateManagedCloudPolicyReader( | |
| 120 PolicyStatusInfo::PolicyLevel policy_level) { | |
| 121 BrowserPolicyConnector* connector = | |
| 122 g_browser_process->browser_policy_connector(); | |
| 123 return new ConfigurationPolicyReader( | |
| 124 connector->GetManagedCloudProvider(), policy_level); | |
| 125 } | |
| 126 | |
| 127 // static | |
| 128 ConfigurationPolicyReader* | |
| 129 ConfigurationPolicyReader::CreateRecommendedPlatformPolicyReader( | |
| 130 PolicyStatusInfo::PolicyLevel policy_level) { | |
| 131 BrowserPolicyConnector* connector = | |
| 132 g_browser_process->browser_policy_connector(); | |
| 133 return new ConfigurationPolicyReader( | |
| 134 connector->GetRecommendedPlatformProvider(), policy_level); | |
| 135 } | |
| 136 | |
| 137 // static | |
| 138 ConfigurationPolicyReader* | |
| 139 ConfigurationPolicyReader::CreateRecommendedCloudPolicyReader( | |
| 140 PolicyStatusInfo::PolicyLevel policy_level) { | |
| 141 BrowserPolicyConnector* connector = | |
| 142 g_browser_process->browser_policy_connector(); | |
| 143 return new ConfigurationPolicyReader( | |
| 144 connector->GetRecommendedCloudProvider(), policy_level); | |
| 145 } | |
| 146 | |
| 147 DictionaryValue* ConfigurationPolicyReader::GetPolicyStatus( | |
| 148 ConfigurationPolicyType policy) const { | |
| 149 return policy_keeper_->GetPolicyStatus(policy); | |
| 150 } | |
| 151 | |
| 152 ConfigurationPolicyReader::ConfigurationPolicyReader( | |
| 153 ConfigurationPolicyProvider* provider, | |
| 154 PolicyStatusInfo::PolicyLevel policy_level) | |
| 155 : provider_(provider), | |
| 156 policy_level_(policy_level) { | |
| 157 if (provider_) { | |
| 158 // Read initial policy. | |
| 159 policy_keeper_.reset( | |
| 160 new ConfigurationPolicyStatusKeeper(provider, policy_level)); | |
| 161 registrar_.Init(provider_, this); | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 void ConfigurationPolicyReader::Refresh() { | |
| 166 if (!provider_) | |
| 167 return; | |
| 168 | |
| 169 // Read policy state into a new keeper and swap out old keeper. | |
| 170 scoped_ptr<ConfigurationPolicyStatusKeeper> new_keeper( | |
| 171 new ConfigurationPolicyStatusKeeper(provider_, policy_level_)); | |
| 172 policy_keeper_.reset(new_keeper.release()); | |
| 173 | |
| 174 // TODO(simo) create an observer class which PolicyStatus can implement so | |
| 175 // that it can be notified of changes. | |
| 176 } | |
| 177 | |
| 178 // PolicyStatus | |
| 179 PolicyStatus::PolicyStatus(ConfigurationPolicyReader* managed_platform, | |
| 180 ConfigurationPolicyReader* managed_cloud, | |
| 181 ConfigurationPolicyReader* recommended_platform, | |
| 182 ConfigurationPolicyReader* recommended_cloud) | |
| 183 : managed_platform_(managed_platform), | |
| 184 managed_cloud_(managed_cloud), | |
|
Mattias Nissler (ping if slow)
2011/08/29 11:24:40
indentation (line up with managed_platform_ in lin
| |
| 185 recommended_platform_(recommended_platform), | |
| 186 recommended_cloud_(recommended_cloud) { | |
| 187 } | |
| 188 | |
| 189 PolicyStatus::~PolicyStatus() { | |
| 190 } | |
| 191 | |
| 192 ListValue* PolicyStatus::GetPolicyStatusList(bool* any_policies_sent) const { | |
| 193 ListValue* result = new ListValue(); | |
| 194 std::vector<PolicyStatusInfo*> unsent_policies; | |
| 195 | |
| 196 *any_policies_sent = false; | |
| 197 const PolicyDefinitionList* supported_policies = | |
| 198 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(); | |
| 199 const PolicyDefinitionList::Entry* policy = supported_policies->begin; | |
| 200 for ( ; policy != supported_policies->end; ++policy) { | |
| 201 if (!AddPolicyFromReaders(policy->policy_type, result)) { | |
| 202 PolicyStatusInfo* info = | |
| 203 new PolicyStatusInfo(ASCIIToUTF16(policy->name), | |
| 204 PolicyStatusInfo::SOURCE_TYPE_UNDEFINED, | |
| 205 PolicyStatusInfo::LEVEL_UNDEFINED, | |
| 206 Value::CreateNullValue(), | |
| 207 PolicyStatusInfo::STATUS_UNDEFINED, | |
| 208 string16()); | |
| 209 unsent_policies.push_back(info); | |
| 210 } else { | |
| 211 *any_policies_sent = true; | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 // Add policies that weren't actually sent from providers to list. | |
| 216 std::vector<PolicyStatusInfo*>::const_iterator info = unsent_policies.begin(); | |
| 217 for ( ; info != unsent_policies.end(); ++info) | |
| 218 result->Append((*info)->GetDictionaryValue()); | |
| 219 | |
| 220 return result; | |
| 221 } | |
| 222 | |
| 223 // static | |
| 224 string16 PolicyStatus::GetPolicyName(ConfigurationPolicyType policy_type) { | |
| 225 static std::map<ConfigurationPolicyType, string16> name_map; | |
| 226 static const ConfigurationPolicyProvider::PolicyDefinitionList* | |
| 227 supported_policies; | |
| 228 | |
| 229 if (!supported_policies) { | |
| 230 supported_policies = | |
| 231 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(); | |
| 232 | |
| 233 // Create mapping from ConfigurationPolicyTypes to actual policy names. | |
| 234 const ConfigurationPolicyProvider::PolicyDefinitionList::Entry* entry = | |
| 235 supported_policies->begin; | |
| 236 for ( ; entry != supported_policies->end; ++entry) | |
| 237 name_map[entry->policy_type] = ASCIIToUTF16(entry->name); | |
| 238 } | |
| 239 | |
| 240 std::map<ConfigurationPolicyType, string16>::const_iterator entry = | |
| 241 name_map.find(policy_type); | |
| 242 | |
| 243 if (entry == name_map.end()) | |
| 244 return string16(); | |
| 245 | |
| 246 return entry->second; | |
| 247 } | |
| 248 | |
| 249 bool PolicyStatus::AddPolicyFromReaders( | |
| 250 ConfigurationPolicyType policy, ListValue* list) const { | |
| 251 DictionaryValue* mp_policy = | |
| 252 managed_platform_->GetPolicyStatus(policy); | |
| 253 DictionaryValue* mc_policy = | |
| 254 managed_cloud_->GetPolicyStatus(policy); | |
| 255 DictionaryValue* rp_policy = | |
| 256 recommended_platform_->GetPolicyStatus(policy); | |
| 257 DictionaryValue* rc_policy = | |
| 258 recommended_cloud_->GetPolicyStatus(policy); | |
| 259 | |
| 260 bool added_policy = mp_policy || mc_policy || rp_policy || rc_policy; | |
| 261 | |
| 262 if (mp_policy) | |
| 263 list->Append(mp_policy); | |
| 264 if (mc_policy) | |
| 265 list->Append(mc_policy); | |
| 266 if (rp_policy) | |
| 267 list->Append(rp_policy); | |
| 268 if (rc_policy) | |
| 269 list->Append(rc_policy); | |
| 270 | |
| 271 return added_policy; | |
| 272 } | |
| 273 | |
| 274 } // namespace policy | |
| OLD | NEW |