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