1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/supervised_user/supervised_user_whitelist_service.h"
#include <string>
#include "base/files/file_path.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/values.h"
#include "chrome/browser/component_updater/supervised_user_whitelist_installer.h"
#include "chrome/browser/supervised_user/supervised_user_site_list.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "sync/api/sync_change.h"
#include "sync/api/sync_data.h"
#include "sync/api/sync_error.h"
#include "sync/api/sync_error_factory.h"
#include "sync/api/sync_merge_result.h"
#include "sync/protocol/sync.pb.h"
const char kName[] = "name";
SupervisedUserWhitelistService::SupervisedUserWhitelistService(
PrefService* prefs,
scoped_ptr<component_updater::SupervisedUserWhitelistInstaller> installer)
: prefs_(prefs),
installer_(installer.Pass()),
weak_ptr_factory_(this) {
DCHECK(prefs);
DCHECK(installer_);
}
SupervisedUserWhitelistService::~SupervisedUserWhitelistService() {
}
// static
void SupervisedUserWhitelistService::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterDictionaryPref(
prefs::kSupervisedUserWhitelists,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}
void SupervisedUserWhitelistService::Init() {
const base::DictionaryValue* whitelists =
prefs_->GetDictionary(prefs::kSupervisedUserWhitelists);
for (base::DictionaryValue::Iterator it(*whitelists); !it.IsAtEnd();
it.Advance()) {
const base::DictionaryValue* dict = nullptr;
it.value().GetAsDictionary(&dict);
std::string name;
bool result = dict->GetString(kName, &name);
DCHECK(result);
bool new_installation = false;
RegisterWhitelist(it.key(), name, new_installation);
}
}
void SupervisedUserWhitelistService::AddSiteListsChangedCallback(
const SiteListsChangedCallback& callback) {
site_lists_changed_callbacks_.push_back(callback);
NotifyWhitelistsChanged();
}
void SupervisedUserWhitelistService::LoadWhitelistForTesting(
const std::string& id,
const base::FilePath& path) {
bool result = registered_whitelists_.insert(id).second;
DCHECK(result);
OnWhitelistReady(id, path);
}
void SupervisedUserWhitelistService::UnloadWhitelist(const std::string& id) {
bool result = registered_whitelists_.erase(id) > 0u;
DCHECK(result);
loaded_whitelists_.erase(id);
NotifyWhitelistsChanged();
}
// static
syncer::SyncData SupervisedUserWhitelistService::CreateWhitelistSyncData(
const std::string& id,
const std::string& name) {
sync_pb::EntitySpecifics specifics;
sync_pb::ManagedUserWhitelistSpecifics* whitelist =
specifics.mutable_managed_user_whitelist();
whitelist->set_id(id);
whitelist->set_name(name);
return syncer::SyncData::CreateLocalData(id, name, specifics);
}
syncer::SyncMergeResult
SupervisedUserWhitelistService::MergeDataAndStartSyncing(
syncer::ModelType type,
const syncer::SyncDataList& initial_sync_data,
scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
scoped_ptr<syncer::SyncErrorFactory> error_handler) {
DCHECK_EQ(syncer::SUPERVISED_USER_WHITELISTS, type);
syncer::SyncChangeList change_list;
syncer::SyncMergeResult result(syncer::SUPERVISED_USER_WHITELISTS);
DictionaryPrefUpdate update(prefs_, prefs::kSupervisedUserWhitelists);
base::DictionaryValue* pref_dict = update.Get();
result.set_num_items_before_association(pref_dict->size());
std::set<std::string> seen_ids;
int num_items_added = 0;
int num_items_modified = 0;
for (const syncer::SyncData& sync_data : initial_sync_data) {
DCHECK_EQ(syncer::SUPERVISED_USER_WHITELISTS, sync_data.GetDataType());
const sync_pb::ManagedUserWhitelistSpecifics& whitelist =
sync_data.GetSpecifics().managed_user_whitelist();
std::string id = whitelist.id();
std::string name = whitelist.name();
seen_ids.insert(id);
base::DictionaryValue* dict = nullptr;
if (pref_dict->GetDictionary(id, &dict)) {
std::string old_name;
bool result = dict->GetString(kName, &old_name);
DCHECK(result);
if (name != old_name) {
SetWhitelistProperties(dict, whitelist);
num_items_modified++;
}
} else {
num_items_added++;
AddNewWhitelist(pref_dict, whitelist);
}
}
std::set<std::string> ids_to_remove;
for (base::DictionaryValue::Iterator it(*pref_dict); !it.IsAtEnd();
it.Advance()) {
if (seen_ids.find(it.key()) == seen_ids.end())
ids_to_remove.insert(it.key());
}
for (const std::string& id : ids_to_remove)
RemoveWhitelist(pref_dict, id);
// Notify if whitelists have been uninstalled. We will notify about newly
// added whitelists later, when they are actually available
// (in OnWhitelistLoaded).
if (!ids_to_remove.empty())
NotifyWhitelistsChanged();
result.set_num_items_added(num_items_added);
result.set_num_items_modified(num_items_modified);
result.set_num_items_deleted(ids_to_remove.size());
result.set_num_items_after_association(pref_dict->size());
return result;
}
void SupervisedUserWhitelistService::StopSyncing(syncer::ModelType type) {
DCHECK_EQ(syncer::SUPERVISED_USER_WHITELISTS, type);
}
syncer::SyncDataList SupervisedUserWhitelistService::GetAllSyncData(
syncer::ModelType type) const {
syncer::SyncDataList sync_data;
const base::DictionaryValue* whitelists =
prefs_->GetDictionary(prefs::kSupervisedUserWhitelists);
for (base::DictionaryValue::Iterator it(*whitelists); !it.IsAtEnd();
it.Advance()) {
const std::string& id = it.key();
const base::DictionaryValue* dict = nullptr;
it.value().GetAsDictionary(&dict);
std::string name;
bool result = dict->GetString(kName, &name);
DCHECK(result);
sync_pb::EntitySpecifics specifics;
sync_pb::ManagedUserWhitelistSpecifics* whitelist =
specifics.mutable_managed_user_whitelist();
whitelist->set_id(id);
whitelist->set_name(name);
sync_data.push_back(syncer::SyncData::CreateLocalData(id, name, specifics));
}
return sync_data;
}
syncer::SyncError SupervisedUserWhitelistService::ProcessSyncChanges(
const tracked_objects::Location& from_here,
const syncer::SyncChangeList& change_list) {
bool whitelists_removed = false;
syncer::SyncError error;
DictionaryPrefUpdate update(prefs_, prefs::kSupervisedUserWhitelists);
base::DictionaryValue* pref_dict = update.Get();
for (const syncer::SyncChange& sync_change : change_list) {
syncer::SyncData data = sync_change.sync_data();
DCHECK_EQ(syncer::SUPERVISED_USER_WHITELISTS, data.GetDataType());
const sync_pb::ManagedUserWhitelistSpecifics& whitelist =
data.GetSpecifics().managed_user_whitelist();
std::string id = whitelist.id();
switch (sync_change.change_type()) {
case syncer::SyncChange::ACTION_ADD: {
DCHECK(!pref_dict->HasKey(id)) << id;
AddNewWhitelist(pref_dict, whitelist);
break;
}
case syncer::SyncChange::ACTION_UPDATE: {
base::DictionaryValue* dict = nullptr;
pref_dict->GetDictionaryWithoutPathExpansion(id, &dict);
SetWhitelistProperties(dict, whitelist);
break;
}
case syncer::SyncChange::ACTION_DELETE: {
DCHECK(pref_dict->HasKey(id)) << id;
RemoveWhitelist(pref_dict, id);
whitelists_removed = true;
break;
}
case syncer::SyncChange::ACTION_INVALID: {
NOTREACHED();
break;
}
}
}
if (whitelists_removed)
NotifyWhitelistsChanged();
return error;
}
void SupervisedUserWhitelistService::AddNewWhitelist(
base::DictionaryValue* pref_dict,
const sync_pb::ManagedUserWhitelistSpecifics& whitelist) {
bool new_installation = true;
RegisterWhitelist(whitelist.id(), whitelist.name(), new_installation);
scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
SetWhitelistProperties(dict.get(), whitelist);
pref_dict->SetWithoutPathExpansion(whitelist.id(), dict.release());
}
void SupervisedUserWhitelistService::SetWhitelistProperties(
base::DictionaryValue* dict,
const sync_pb::ManagedUserWhitelistSpecifics& whitelist) {
dict->SetString(kName, whitelist.name());
}
void SupervisedUserWhitelistService::RemoveWhitelist(
base::DictionaryValue* pref_dict,
const std::string& id) {
pref_dict->RemoveWithoutPathExpansion(id, NULL);
installer_->UnregisterWhitelist(id);
UnloadWhitelist(id);
}
void SupervisedUserWhitelistService::RegisterWhitelist(const std::string& id,
const std::string& name,
bool new_installation) {
bool result = registered_whitelists_.insert(id).second;
DCHECK(result);
installer_->RegisterWhitelist(
id, name, new_installation,
base::Bind(&SupervisedUserWhitelistService::OnWhitelistReady,
weak_ptr_factory_.GetWeakPtr(), id));
}
void SupervisedUserWhitelistService::NotifyWhitelistsChanged() {
std::vector<scoped_refptr<SupervisedUserSiteList> > whitelists;
for (const auto& whitelist : loaded_whitelists_)
whitelists.push_back(whitelist.second);
for (const auto& callback : site_lists_changed_callbacks_)
callback.Run(whitelists);
}
void SupervisedUserWhitelistService::OnWhitelistReady(
const std::string& id,
const base::FilePath& whitelist_path) {
// If the whitelist has been unregistered in the mean time, ignore it.
if (registered_whitelists_.count(id) == 0u)
return;
SupervisedUserSiteList::Load(
whitelist_path,
base::Bind(&SupervisedUserWhitelistService::OnWhitelistLoaded,
weak_ptr_factory_.GetWeakPtr(), id, base::TimeTicks::Now()));
}
void SupervisedUserWhitelistService::OnWhitelistLoaded(
const std::string& id,
base::TimeTicks start_time,
const scoped_refptr<SupervisedUserSiteList>& whitelist) {
if (!whitelist) {
LOG(WARNING) << "Couldn't load whitelist " << id;
return;
}
UMA_HISTOGRAM_TIMES("ManagedUsers.Whitelist.TotalLoadDuration",
base::TimeTicks::Now() - start_time);
// If the whitelist has been unregistered in the mean time, ignore it.
if (registered_whitelists_.count(id) == 0u)
return;
loaded_whitelists_[id] = whitelist;
NotifyWhitelistsChanged();
}
|