OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/policy_change_watcher.h" |
| 6 |
| 7 #include "base/json/json_writer.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/prefs/pref_registry_simple.h" |
| 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/values.h" |
| 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/policy/policy_namespace.h" |
| 16 #include "chrome/browser/prefs/pref_registry_syncable.h" |
| 17 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/common/pref_names.h" |
| 20 #include "crypto/sha2.h" |
| 21 |
| 22 namespace policy { |
| 23 |
| 24 PolicyServiceChangeWatcher::PolicyServiceChangeWatcher( |
| 25 PolicyService* policy_service, |
| 26 const std::string& current_hash) |
| 27 : domains_pending_initialization_(0), |
| 28 current_hash_(current_hash), |
| 29 policy_service_(policy_service) { |
| 30 } |
| 31 |
| 32 PolicyServiceChangeWatcher::~PolicyServiceChangeWatcher() { |
| 33 for (size_t i = 0; i < POLICY_DOMAIN_SIZE; ++i) |
| 34 policy_service_->RemoveObserver(static_cast<PolicyDomain>(i), this); |
| 35 } |
| 36 |
| 37 void PolicyServiceChangeWatcher::OnPolicyUpdated(const PolicyNamespace& ns, |
| 38 const PolicyMap& previous, |
| 39 const PolicyMap& current) { |
| 40 if (is_initialization_complete()) |
| 41 CheckIfPolicyChanged(policy_service_->GetAllPolicies()); |
| 42 } |
| 43 |
| 44 void PolicyServiceChangeWatcher::OnPolicyServiceInitialized( |
| 45 PolicyDomain domain) { |
| 46 --domains_pending_initialization_; |
| 47 if (is_initialization_complete()) |
| 48 CheckIfPolicyChanged(policy_service_->GetAllPolicies()); |
| 49 } |
| 50 |
| 51 void PolicyServiceChangeWatcher::Init() { |
| 52 for (size_t i = 0; i < POLICY_DOMAIN_SIZE; ++i) { |
| 53 PolicyDomain domain = static_cast<PolicyDomain>(i); |
| 54 policy_service_->AddObserver(domain, this); |
| 55 if (!policy_service_->IsInitializationComplete(domain)) |
| 56 ++domains_pending_initialization_; |
| 57 } |
| 58 if (is_initialization_complete()) |
| 59 CheckIfPolicyChanged(policy_service_->GetAllPolicies()); |
| 60 } |
| 61 |
| 62 void PolicyServiceChangeWatcher::CheckIfPolicyChanged( |
| 63 const PolicyBundle& bundle) { |
| 64 scoped_ptr<base::DictionaryValue> policy(new base::DictionaryValue); |
| 65 for (PolicyBundle::const_iterator bundle_it = bundle.begin(); |
| 66 bundle_it != bundle.end(); ++bundle_it) { |
| 67 if (bundle_it->second->empty()) |
| 68 continue; |
| 69 |
| 70 base::DictionaryValue* map = new base::DictionaryValue; |
| 71 for (PolicyMap::const_iterator map_it = bundle_it->second->begin(); |
| 72 map_it != bundle_it->second->end(); ++map_it) { |
| 73 base::DictionaryValue* entry = new base::DictionaryValue; |
| 74 entry->SetInteger("level", map_it->second.level); |
| 75 entry->SetInteger("scope", map_it->second.scope); |
| 76 entry->Set("value", map_it->second.value->DeepCopy()); |
| 77 map->Set(map_it->first, entry); |
| 78 } |
| 79 const PolicyNamespace& ns = bundle_it->first; |
| 80 policy->Set(base::IntToString(ns.domain) + ":" + ns.component_id, map); |
| 81 } |
| 82 |
| 83 std::string json; |
| 84 base::JSONWriter::Write(policy.get(), &json); |
| 85 char hash[crypto::kSHA256Length]; |
| 86 crypto::SHA256HashString(json, &hash, sizeof(hash)); |
| 87 const std::string new_hash = base::HexEncode(&hash, sizeof(hash)); |
| 88 |
| 89 if (new_hash != current_hash_) { |
| 90 current_hash_ = new_hash; |
| 91 OnPolicyChanged(); |
| 92 } |
| 93 } |
| 94 |
| 95 DevicePolicyChangeWatcher::Observer::~Observer() { |
| 96 } |
| 97 |
| 98 DevicePolicyChangeWatcher::DevicePolicyChangeWatcher() |
| 99 : PolicyServiceChangeWatcher(g_browser_process->policy_service(), "") { |
| 100 Init(); |
| 101 LOG(ERROR) << "*** DevicePolicyChangeWatcher UP"; |
| 102 } |
| 103 |
| 104 DevicePolicyChangeWatcher::~DevicePolicyChangeWatcher() { |
| 105 LOG(ERROR) << "*** DevicePolicyChangeWatcher DOWN"; |
| 106 } |
| 107 |
| 108 void DevicePolicyChangeWatcher::AddObserver(Observer* observer) { |
| 109 observers_.AddObserver(observer); |
| 110 if (is_initialization_complete()) |
| 111 observer->OnDevicePolicyStatusChanged(ListUsersWithChangedDevicePolicy()); |
| 112 } |
| 113 |
| 114 void DevicePolicyChangeWatcher::RemoveObserver(Observer* observer) { |
| 115 observers_.RemoveObserver(observer); |
| 116 } |
| 117 |
| 118 // static |
| 119 void DevicePolicyChangeWatcher::RegisterPrefs(PrefRegistrySimple* registry) { |
| 120 registry->RegisterDictionaryPref(prefs::kDevicePolicyHashes); |
| 121 } |
| 122 |
| 123 void DevicePolicyChangeWatcher::AcknowledgePolicyChange( |
| 124 const std::string& username) { |
| 125 if (!is_initialization_complete()) |
| 126 return; |
| 127 |
| 128 DictionaryPrefUpdate pref_update(g_browser_process->local_state(), |
| 129 prefs::kDevicePolicyHashes); |
| 130 pref_update->SetWithoutPathExpansion( |
| 131 username, |
| 132 base::Value::CreateStringValue(current_hash())); |
| 133 |
| 134 NotifyDevicePolicyStatusChanged(); |
| 135 } |
| 136 |
| 137 std::vector<std::string> |
| 138 DevicePolicyChangeWatcher::ListUsersWithChangedDevicePolicy() const { |
| 139 DCHECK(is_initialization_complete()); |
| 140 std::vector<std::string> users_with_changed_device_policy; |
| 141 for (base::DictionaryValue::Iterator it( |
| 142 *g_browser_process->local_state()->GetDictionary( |
| 143 prefs::kDevicePolicyHashes)); |
| 144 !it.IsAtEnd(); it.Advance()) { |
| 145 std::string previous_hash; |
| 146 if (!it.value().GetAsString(&previous_hash) || |
| 147 previous_hash != current_hash()) { |
| 148 users_with_changed_device_policy.push_back(it.key()); |
| 149 } |
| 150 } |
| 151 return users_with_changed_device_policy; |
| 152 } |
| 153 |
| 154 void DevicePolicyChangeWatcher::NotifyDevicePolicyStatusChanged() { |
| 155 std::vector<std::string> users_with_changed_device_policy = |
| 156 ListUsersWithChangedDevicePolicy(); |
| 157 FOR_EACH_OBSERVER( |
| 158 Observer, |
| 159 observers_, |
| 160 OnDevicePolicyStatusChanged(users_with_changed_device_policy)); |
| 161 } |
| 162 |
| 163 void DevicePolicyChangeWatcher::OnPolicyChanged() { |
| 164 NotifyDevicePolicyStatusChanged(); |
| 165 } |
| 166 |
| 167 UserPolicyChangeWatcher::UserPolicyChangeWatcher(Profile* profile) |
| 168 : PolicyServiceChangeWatcher( |
| 169 profile->GetPolicyService(), |
| 170 profile->GetPrefs()->GetString(prefs::kUserPolicyHash)) { |
| 171 Init(); |
| 172 } |
| 173 |
| 174 UserPolicyChangeWatcher::~UserPolicyChangeWatcher() { |
| 175 } |
| 176 |
| 177 // static |
| 178 void UserPolicyChangeWatcher::RegisterUserPrefs( |
| 179 PrefRegistrySyncable* registry) { |
| 180 registry->RegisterStringPref(prefs::kUserPolicyHash, |
| 181 std::string(), |
| 182 PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 183 } |
| 184 |
| 185 void UserPolicyChangeWatcher::OnPolicyChanged() { |
| 186 LOG(ERROR) << "New user policy: " << current_hash(); |
| 187 } |
| 188 |
| 189 |
| 190 } // namespace policy |
OLD | NEW |