Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: chrome/browser/supervised_user/supervised_user_settings_service.cc

Issue 1563833002: [Part-2]Add Statistics for SupervisedUserSettingService during merging and syncing data. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/supervised_user/supervised_user_settings_service.h" 5 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 PersistentPrefStore* store = new JsonPrefStore( 71 PersistentPrefStore* store = new JsonPrefStore(
72 path, sequenced_task_runner, scoped_ptr<PrefFilter>()); 72 path, sequenced_task_runner, scoped_ptr<PrefFilter>());
73 Init(store); 73 Init(store);
74 if (load_synchronously) { 74 if (load_synchronously) {
75 store_->ReadPrefs(); 75 store_->ReadPrefs();
76 // TODO(bauerb): Temporary CHECK while investigating 76 // TODO(bauerb): Temporary CHECK while investigating
77 // https://crbug.com/425785. Remove (or change to DCHECK) once the bug 77 // https://crbug.com/425785. Remove (or change to DCHECK) once the bug
78 // is fixed. 78 // is fixed.
79 CHECK(store_->IsInitializationComplete()); 79 CHECK(store_->IsInitializationComplete());
80 } else { 80 } else {
81 store_->ReadPrefsAsync(NULL); 81 store_->ReadPrefsAsync(nullptr);
82 } 82 }
83 } 83 }
84 84
85 void SupervisedUserSettingsService::Init( 85 void SupervisedUserSettingsService::Init(
86 scoped_refptr<PersistentPrefStore> store) { 86 scoped_refptr<PersistentPrefStore> store) {
87 DCHECK(!store_.get()); 87 DCHECK(!store_.get());
88 store_ = store; 88 store_ = store;
89 store_->AddObserver(this); 89 store_->AddObserver(this);
90 } 90 }
91 91
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 const std::string& prefix, 127 const std::string& prefix,
128 const std::string& key) { 128 const std::string& key) {
129 return prefix + kSplitSettingKeySeparator + key; 129 return prefix + kSplitSettingKeySeparator + key;
130 } 130 }
131 131
132 void SupervisedUserSettingsService::UploadItem(const std::string& key, 132 void SupervisedUserSettingsService::UploadItem(const std::string& key,
133 scoped_ptr<base::Value> value) { 133 scoped_ptr<base::Value> value) {
134 DCHECK(!SettingShouldApplyToPrefs(key)); 134 DCHECK(!SettingShouldApplyToPrefs(key));
135 135
136 std::string key_suffix = key; 136 std::string key_suffix = key;
137 base::DictionaryValue* dict = NULL; 137 base::DictionaryValue* dict = nullptr;
138 if (sync_processor_) { 138 if (sync_processor_) {
139 content::RecordAction(UserMetricsAction("ManagedUsers_UploadItem_Syncing")); 139 content::RecordAction(UserMetricsAction("ManagedUsers_UploadItem_Syncing"));
140 dict = GetDictionaryAndSplitKey(&key_suffix); 140 dict = GetDictionaryAndSplitKey(&key_suffix);
141 DCHECK(GetQueuedItems()->empty()); 141 DCHECK(GetQueuedItems()->empty());
142 SyncChangeList change_list; 142 SyncChangeList change_list;
143 SyncData data = CreateSyncDataForSetting(key, *value); 143 SyncData data = CreateSyncDataForSetting(key, *value);
144 SyncChange::SyncChangeType change_type = 144 SyncChange::SyncChangeType change_type =
145 dict->HasKey(key_suffix) ? SyncChange::ACTION_UPDATE 145 dict->HasKey(key_suffix) ? SyncChange::ACTION_UPDATE
146 : SyncChange::ACTION_ADD; 146 : SyncChange::ACTION_ADD;
147 change_list.push_back(SyncChange(FROM_HERE, change_type, data)); 147 change_list.push_back(SyncChange(FROM_HERE, change_type, data));
148 SyncError error = 148 SyncError error =
149 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list); 149 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list);
150 DCHECK(!error.IsSet()) << error.ToString(); 150 DCHECK(!error.IsSet()) << error.ToString();
151 } else { 151 } else {
152 // Queue the item up to be uploaded when we start syncing 152 // Queue the item up to be uploaded when we start syncing
153 // (in MergeDataAndStartSyncing()). 153 // (in MergeDataAndStartSyncing()).
154 content::RecordAction(UserMetricsAction("ManagedUsers_UploadItem_Queued")); 154 content::RecordAction(UserMetricsAction("ManagedUsers_UploadItem_Queued"));
155 dict = GetQueuedItems(); 155 dict = GetQueuedItems();
156 } 156 }
157 dict->SetWithoutPathExpansion(key_suffix, value.release()); 157 dict->SetWithoutPathExpansion(key_suffix, value.release());
158 } 158 }
159 159
160 void SupervisedUserSettingsService::SetLocalSetting( 160 void SupervisedUserSettingsService::SetLocalSetting(
161 const std::string& key, 161 const std::string& key,
162 scoped_ptr<base::Value> value) { 162 scoped_ptr<base::Value> value) {
163 if (value) 163 if (value)
164 local_settings_->SetWithoutPathExpansion(key, value.release()); 164 local_settings_->SetWithoutPathExpansion(key, value.release());
165 else 165 else
166 local_settings_->RemoveWithoutPathExpansion(key, NULL); 166 local_settings_->RemoveWithoutPathExpansion(key, nullptr);
167 167
168 InformSubscribers(); 168 InformSubscribers();
169 } 169 }
170 170
171 // static 171 // static
172 SyncData SupervisedUserSettingsService::CreateSyncDataForSetting( 172 SyncData SupervisedUserSettingsService::CreateSyncDataForSetting(
173 const std::string& name, 173 const std::string& name,
174 const base::Value& value) { 174 const base::Value& value) {
175 std::string json_value; 175 std::string json_value;
176 base::JSONWriter::Write(value, &json_value); 176 base::JSONWriter::Write(value, &json_value);
177 ::sync_pb::EntitySpecifics specifics; 177 ::sync_pb::EntitySpecifics specifics;
178 specifics.mutable_managed_user_setting()->set_name(name); 178 specifics.mutable_managed_user_setting()->set_name(name);
179 specifics.mutable_managed_user_setting()->set_value(json_value); 179 specifics.mutable_managed_user_setting()->set_value(json_value);
180 return SyncData::CreateLocalData(name, name, specifics); 180 return SyncData::CreateLocalData(name, name, specifics);
181 } 181 }
182 182
183 void SupervisedUserSettingsService::Shutdown() { 183 void SupervisedUserSettingsService::Shutdown() {
184 store_->RemoveObserver(this); 184 store_->RemoveObserver(this);
185 } 185 }
186 186
187 SyncMergeResult SupervisedUserSettingsService::MergeDataAndStartSyncing( 187 SyncMergeResult SupervisedUserSettingsService::MergeDataAndStartSyncing(
188 ModelType type, 188 ModelType type,
189 const SyncDataList& initial_sync_data, 189 const SyncDataList& initial_sync_data,
190 scoped_ptr<SyncChangeProcessor> sync_processor, 190 scoped_ptr<SyncChangeProcessor> sync_processor,
191 scoped_ptr<SyncErrorFactory> error_handler) { 191 scoped_ptr<SyncErrorFactory> error_handler) {
192 DCHECK_EQ(SUPERVISED_USER_SETTINGS, type); 192 DCHECK_EQ(SUPERVISED_USER_SETTINGS, type);
193 sync_processor_ = std::move(sync_processor); 193 sync_processor_ = std::move(sync_processor);
194 error_handler_ = std::move(error_handler); 194 error_handler_ = std::move(error_handler);
195 195
196 std::set<std::string> seen_keys;
197 int num_before_association = 0;
198 // Getting number of atomic setting items.
199 num_before_association = GetAtomicSettings()->size();
200 for (base::DictionaryValue::Iterator it(*GetAtomicSettings()); !it.IsAtEnd();
201 it.Advance()) {
202 seen_keys.insert(it.key());
203 }
204 // Getting number of split setting items.
205 for (base::DictionaryValue::Iterator it(*GetSplitSettings()); !it.IsAtEnd();
206 it.Advance()) {
207 const base::DictionaryValue* dict = nullptr;
208 it.value().GetAsDictionary(&dict);
209 num_before_association += dict->size();
210 for (base::DictionaryValue::Iterator jt(*dict); !jt.IsAtEnd();
211 jt.Advance()) {
212 seen_keys.insert(MakeSplitSettingKey(it.key(), jt.key()));
213 }
214 }
215
216 int num_deleted = num_before_association;
217 // Getting number of queued items.
218 base::DictionaryValue* queued_items = GetQueuedItems();
219 if (queued_items)
Bernhard Bauer 2016/02/03 11:38:13 I think this will never be null, right?
Deepak 2016/02/29 07:14:10 Done.
220 num_before_association += queued_items->size();
221
196 // Clear all atomic and split settings, then recreate them from Sync data. 222 // Clear all atomic and split settings, then recreate them from Sync data.
197 Clear(); 223 Clear();
224 int num_added = 0;
225 int num_modified = 0;
226 std::set<std::string> added_sync_keys;
198 for (const SyncData& sync_data : initial_sync_data) { 227 for (const SyncData& sync_data : initial_sync_data) {
199 DCHECK_EQ(SUPERVISED_USER_SETTINGS, sync_data.GetDataType()); 228 DCHECK_EQ(SUPERVISED_USER_SETTINGS, sync_data.GetDataType());
200 const ::sync_pb::ManagedUserSettingSpecifics& supervised_user_setting = 229 const ::sync_pb::ManagedUserSettingSpecifics& supervised_user_setting =
201 sync_data.GetSpecifics().managed_user_setting(); 230 sync_data.GetSpecifics().managed_user_setting();
202 scoped_ptr<base::Value> value = 231 scoped_ptr<base::Value> value =
203 JSONReader::Read(supervised_user_setting.value()); 232 JSONReader::Read(supervised_user_setting.value());
204 std::string name_suffix = supervised_user_setting.name(); 233 std::string name_suffix = supervised_user_setting.name();
234 std::string name_key = name_suffix;
205 base::DictionaryValue* dict = GetDictionaryAndSplitKey(&name_suffix); 235 base::DictionaryValue* dict = GetDictionaryAndSplitKey(&name_suffix);
206 dict->SetWithoutPathExpansion(name_suffix, value.release()); 236 dict->SetWithoutPathExpansion(name_suffix, value.release());
237 if (seen_keys.find(name_key) == seen_keys.end()) {
238 added_sync_keys.insert(name_key);
239 num_added++;
240 } else {
241 num_modified++;
242 }
207 } 243 }
244
245 num_deleted -= num_modified;
246
208 store_->ReportValueChanged(kAtomicSettings, 247 store_->ReportValueChanged(kAtomicSettings,
209 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 248 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
210 store_->ReportValueChanged(kSplitSettings, 249 store_->ReportValueChanged(kSplitSettings,
211 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 250 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
212 InformSubscribers(); 251 InformSubscribers();
213 252
214 // Upload all the queued up items (either with an ADD or an UPDATE action, 253 // Upload all the queued up items (either with an ADD or an UPDATE action,
215 // depending on whether they already exist) and move them to split settings. 254 // depending on whether they already exist) and move them to split settings.
216 SyncChangeList change_list; 255 SyncChangeList change_list;
217 base::DictionaryValue* queued_items = GetQueuedItems();
218 for (base::DictionaryValue::Iterator it(*queued_items); !it.IsAtEnd(); 256 for (base::DictionaryValue::Iterator it(*queued_items); !it.IsAtEnd();
219 it.Advance()) { 257 it.Advance()) {
220 std::string key_suffix = it.key(); 258 std::string key_suffix = it.key();
259 std::string name_key = key_suffix;
221 base::DictionaryValue* dict = GetDictionaryAndSplitKey(&key_suffix); 260 base::DictionaryValue* dict = GetDictionaryAndSplitKey(&key_suffix);
222 SyncData data = CreateSyncDataForSetting(it.key(), it.value()); 261 SyncData data = CreateSyncDataForSetting(it.key(), it.value());
223 SyncChange::SyncChangeType change_type = 262 SyncChange::SyncChangeType change_type =
224 dict->HasKey(key_suffix) ? SyncChange::ACTION_UPDATE 263 dict->HasKey(key_suffix) ? SyncChange::ACTION_UPDATE
225 : SyncChange::ACTION_ADD; 264 : SyncChange::ACTION_ADD;
226 change_list.push_back(SyncChange(FROM_HERE, change_type, data)); 265 change_list.push_back(SyncChange(FROM_HERE, change_type, data));
227 dict->SetWithoutPathExpansion(key_suffix, it.value().DeepCopy()); 266 dict->SetWithoutPathExpansion(key_suffix, it.value().DeepCopy());
267 if (added_sync_keys.find(name_key) != added_sync_keys.end()) {
268 num_added--;
269 }
228 } 270 }
229 queued_items->Clear(); 271 queued_items->Clear();
230 272
231 SyncMergeResult result(SUPERVISED_USER_SETTINGS); 273 SyncMergeResult result(SUPERVISED_USER_SETTINGS);
232 // Process all the accumulated changes from the queued items. 274 // Process all the accumulated changes from the queued items.
233 if (change_list.size() > 0) { 275 if (change_list.size() > 0) {
234 store_->ReportValueChanged(kQueuedItems, 276 store_->ReportValueChanged(kQueuedItems,
235 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 277 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
236 result.set_error( 278 result.set_error(
237 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list)); 279 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list));
238 } 280 }
239 281
240 // TODO(bauerb): Statistics? 282 // Calculating number of items after association.
283 int num_after_association = 0;
284 // Getting number of atomic setting items.
285 num_after_association = GetAtomicSettings()->size();
286 // Getting number of split setting items.
287 for (base::DictionaryValue::Iterator it(*GetSplitSettings()); !it.IsAtEnd();
288 it.Advance()) {
289 const base::DictionaryValue* dict = nullptr;
290 it.value().GetAsDictionary(&dict);
291 num_after_association += dict->size();
292 }
293 // Getting number of queued items.
294 queued_items = GetQueuedItems();
295 if (queued_items)
296 num_after_association += queued_items->size();
297
298 result.set_num_items_added(num_added);
299 result.set_num_items_modified(num_modified);
300 result.set_num_items_deleted(num_deleted);
301 result.set_num_items_before_association(num_before_association);
302 result.set_num_items_after_association(num_after_association);
241 return result; 303 return result;
242 } 304 }
243 305
244 void SupervisedUserSettingsService::StopSyncing(ModelType type) { 306 void SupervisedUserSettingsService::StopSyncing(ModelType type) {
245 DCHECK_EQ(syncer::SUPERVISED_USER_SETTINGS, type); 307 DCHECK_EQ(syncer::SUPERVISED_USER_SETTINGS, type);
246 sync_processor_.reset(); 308 sync_processor_.reset();
247 error_handler_.reset(); 309 error_handler_.reset();
248 } 310 }
249 311
250 SyncDataList SupervisedUserSettingsService::GetAllSyncData( 312 SyncDataList SupervisedUserSettingsService::GetAllSyncData(
251 ModelType type) const { 313 ModelType type) const {
252 DCHECK_EQ(syncer::SUPERVISED_USER_SETTINGS, type); 314 DCHECK_EQ(syncer::SUPERVISED_USER_SETTINGS, type);
253 SyncDataList data; 315 SyncDataList data;
254 for (base::DictionaryValue::Iterator it(*GetAtomicSettings()); !it.IsAtEnd(); 316 for (base::DictionaryValue::Iterator it(*GetAtomicSettings()); !it.IsAtEnd();
255 it.Advance()) { 317 it.Advance()) {
256 data.push_back(CreateSyncDataForSetting(it.key(), it.value())); 318 data.push_back(CreateSyncDataForSetting(it.key(), it.value()));
257 } 319 }
258 for (base::DictionaryValue::Iterator it(*GetSplitSettings()); !it.IsAtEnd(); 320 for (base::DictionaryValue::Iterator it(*GetSplitSettings()); !it.IsAtEnd();
259 it.Advance()) { 321 it.Advance()) {
260 const base::DictionaryValue* dict = NULL; 322 const base::DictionaryValue* dict = nullptr;
261 it.value().GetAsDictionary(&dict); 323 it.value().GetAsDictionary(&dict);
262 for (base::DictionaryValue::Iterator jt(*dict); 324 for (base::DictionaryValue::Iterator jt(*dict);
263 !jt.IsAtEnd(); jt.Advance()) { 325 !jt.IsAtEnd(); jt.Advance()) {
264 data.push_back(CreateSyncDataForSetting( 326 data.push_back(CreateSyncDataForSetting(
265 MakeSplitSettingKey(it.key(), jt.key()), jt.value())); 327 MakeSplitSettingKey(it.key(), jt.key()), jt.value()));
266 } 328 }
267 } 329 }
268 DCHECK_EQ(0u, GetQueuedItems()->size()); 330 DCHECK_EQ(0u, GetQueuedItems()->size());
269 return data; 331 return data;
270 } 332 }
(...skipping 20 matching lines...) Expand all
291 } else { 353 } else {
292 DLOG_IF(WARNING, change_type == SyncChange::ACTION_UPDATE) 354 DLOG_IF(WARNING, change_type == SyncChange::ACTION_UPDATE)
293 << "Value for key " << key << " doesn't exist yet"; 355 << "Value for key " << key << " doesn't exist yet";
294 } 356 }
295 dict->SetWithoutPathExpansion(key, value.release()); 357 dict->SetWithoutPathExpansion(key, value.release());
296 break; 358 break;
297 } 359 }
298 case SyncChange::ACTION_DELETE: { 360 case SyncChange::ACTION_DELETE: {
299 DLOG_IF(WARNING, !dict->HasKey(key)) << "Trying to delete nonexistent " 361 DLOG_IF(WARNING, !dict->HasKey(key)) << "Trying to delete nonexistent "
300 << "key " << key; 362 << "key " << key;
301 dict->RemoveWithoutPathExpansion(key, NULL); 363 dict->RemoveWithoutPathExpansion(key, nullptr);
302 break; 364 break;
303 } 365 }
304 case SyncChange::ACTION_INVALID: { 366 case SyncChange::ACTION_INVALID: {
305 NOTREACHED(); 367 NOTREACHED();
306 break; 368 break;
307 } 369 }
308 } 370 }
309 } 371 }
310 store_->ReportValueChanged(kAtomicSettings, 372 store_->ReportValueChanged(kAtomicSettings,
311 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 373 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
(...skipping 18 matching lines...) Expand all
330 } 392 }
331 393
332 // TODO(bauerb): Temporary CHECK while investigating https://crbug.com/425785. 394 // TODO(bauerb): Temporary CHECK while investigating https://crbug.com/425785.
333 // Remove (or change back to DCHECK) once the bug is fixed. 395 // Remove (or change back to DCHECK) once the bug is fixed.
334 CHECK(IsReady()); 396 CHECK(IsReady());
335 InformSubscribers(); 397 InformSubscribers();
336 } 398 }
337 399
338 base::DictionaryValue* SupervisedUserSettingsService::GetOrCreateDictionary( 400 base::DictionaryValue* SupervisedUserSettingsService::GetOrCreateDictionary(
339 const std::string& key) const { 401 const std::string& key) const {
340 base::Value* value = NULL; 402 base::Value* value = nullptr;
341 base::DictionaryValue* dict = NULL; 403 base::DictionaryValue* dict = nullptr;
342 if (store_->GetMutableValue(key, &value)) { 404 if (store_->GetMutableValue(key, &value)) {
343 bool success = value->GetAsDictionary(&dict); 405 bool success = value->GetAsDictionary(&dict);
344 DCHECK(success); 406 DCHECK(success);
345 } else { 407 } else {
346 dict = new base::DictionaryValue; 408 dict = new base::DictionaryValue;
347 store_->SetValue(key, make_scoped_ptr(dict), 409 store_->SetValue(key, make_scoped_ptr(dict),
348 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 410 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
349 } 411 }
350 412
351 return dict; 413 return dict;
(...skipping 13 matching lines...) Expand all
365 } 427 }
366 428
367 base::DictionaryValue* SupervisedUserSettingsService::GetDictionaryAndSplitKey( 429 base::DictionaryValue* SupervisedUserSettingsService::GetDictionaryAndSplitKey(
368 std::string* key) const { 430 std::string* key) const {
369 size_t pos = key->find_first_of(kSplitSettingKeySeparator); 431 size_t pos = key->find_first_of(kSplitSettingKeySeparator);
370 if (pos == std::string::npos) 432 if (pos == std::string::npos)
371 return GetAtomicSettings(); 433 return GetAtomicSettings();
372 434
373 base::DictionaryValue* split_settings = GetSplitSettings(); 435 base::DictionaryValue* split_settings = GetSplitSettings();
374 std::string prefix = key->substr(0, pos); 436 std::string prefix = key->substr(0, pos);
375 base::DictionaryValue* dict = NULL; 437 base::DictionaryValue* dict = nullptr;
376 if (!split_settings->GetDictionary(prefix, &dict)) { 438 if (!split_settings->GetDictionary(prefix, &dict)) {
377 dict = new base::DictionaryValue; 439 dict = new base::DictionaryValue;
378 DCHECK(!split_settings->HasKey(prefix)); 440 DCHECK(!split_settings->HasKey(prefix));
379 split_settings->Set(prefix, dict); 441 split_settings->Set(prefix, dict);
380 } 442 }
381 key->erase(0, pos + 1); 443 key->erase(0, pos + 1);
382 return dict; 444 return dict;
383 } 445 }
384 446
385 scoped_ptr<base::DictionaryValue> SupervisedUserSettingsService::GetSettings() { 447 scoped_ptr<base::DictionaryValue> SupervisedUserSettingsService::GetSettings() {
(...skipping 24 matching lines...) Expand all
410 return settings; 472 return settings;
411 } 473 }
412 474
413 void SupervisedUserSettingsService::InformSubscribers() { 475 void SupervisedUserSettingsService::InformSubscribers() {
414 if (!IsReady()) 476 if (!IsReady())
415 return; 477 return;
416 478
417 scoped_ptr<base::DictionaryValue> settings = GetSettings(); 479 scoped_ptr<base::DictionaryValue> settings = GetSettings();
418 callback_list_.Notify(settings.get()); 480 callback_list_.Notify(settings.get());
419 } 481 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698