<commit_before>// Copyright (c) 2010 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/sync/glue/preference_model_associator.h"

#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/values.h"
#include "chrome/browser/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/sync/engine/syncapi.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/protocol/preference_specifics.pb.h"
#include "chrome/common/json_value_serializer.h"
#include "chrome/common/pref_names.h"

namespace browser_sync {

PreferenceModelAssociator::PreferenceModelAssociator(
    ProfileSyncService* sync_service)
    : sync_service_(sync_service),
      preferences_node_id_(sync_api::kInvalidId),
      ALLOW_THIS_IN_INITIALIZER_LIST(persist_associations_(this)) {
  DCHECK(sync_service_);
  synced_preferences_.insert(prefs::kHomePageIsNewTabPage);
  synced_preferences_.insert(prefs::kHomePage);
  synced_preferences_.insert(prefs::kRestoreOnStartup);
  synced_preferences_.insert(prefs::kURLsToRestoreOnStartup);
  synced_preferences_.insert(prefs::kShowBookmarkBar);
}

bool PreferenceModelAssociator::AssociateModels() {

  // TODO(albertb): Attempt to load the model association from storage.
  PrefService* pref_service = sync_service_->profile()->GetPrefs();

  int64 root_id;
  if (!GetSyncIdForTaggedNode(kPreferencesTag, &root_id)) {
    sync_service_->OnUnrecoverableError();
    LOG(ERROR) << "Server did not create the top-level preferences node. We "
               << "might be running against an out-of-date server.";
    return false;
  }

  sync_api::WriteTransaction trans(
      sync_service()->backend()->GetUserShareHandle());
  sync_api::ReadNode root(&trans);
  if (!root.InitByIdLookup(root_id)) {
    sync_service_->OnUnrecoverableError();
    LOG(ERROR) << "Server did not create the top-level preferences node. We "
               << "might be running against an out-of-date server.";
    return false;
  }

  base::JSONReader reader;
  for (std::set<std::wstring>::iterator it = synced_preferences_.begin();
       it != synced_preferences_.end(); ++it) {
    std::string tag = WideToUTF8(*it);
<commit_msg>Add home button to synced prefs.<commit_after>// Copyright (c) 2010 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/sync/glue/preference_model_associator.h"

#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/values.h"
#include "chrome/browser/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/sync/engine/syncapi.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/protocol/preference_specifics.pb.h"
#include "chrome/common/json_value_serializer.h"
#include "chrome/common/pref_names.h"

namespace browser_sync {

PreferenceModelAssociator::PreferenceModelAssociator(
    ProfileSyncService* sync_service)
    : sync_service_(sync_service),
      preferences_node_id_(sync_api::kInvalidId),
      ALLOW_THIS_IN_INITIALIZER_LIST(persist_associations_(this)) {
  DCHECK(sync_service_);
  synced_preferences_.insert(prefs::kHomePageIsNewTabPage);
  synced_preferences_.insert(prefs::kHomePage);
  synced_preferences_.insert(prefs::kRestoreOnStartup);
  synced_preferences_.insert(prefs::kURLsToRestoreOnStartup);
  synced_preferences_.insert(prefs::kShowBookmarkBar);
  synced_preferences_.insert(prefs::kShowHomeButton);
}

bool PreferenceModelAssociator::AssociateModels() {

  // TODO(albertb): Attempt to load the model association from storage.
  PrefService* pref_service = sync_service_->profile()->GetPrefs();

  int64 root_id;
  if (!GetSyncIdForTaggedNode(kPreferencesTag, &root_id)) {
    sync_service_->OnUnrecoverableError();
    LOG(ERROR) << "Server did not create the top-level preferences node. We "
               << "might be running against an out-of-date server.";
    return false;
  }

  sync_api::WriteTransaction trans(
      sync_service()->backend()->GetUserShareHandle());
  sync_api::ReadNode root(&trans);
  if (!root.InitByIdLookup(root_id)) {
    sync_service_->OnUnrecoverableError();
    LOG(ERROR) << "Server did not create the top-level preferences node. We "
               << "might be running against an out-of-date server.";
    return false;
  }

  base::JSONReader reader;
  for (std::set<std::wstring>::iterator it = synced_preferences_.begin();
       it != synced_preferences_.end(); ++it) {
    std::string tag = WideToUTF8(*it);

{'fraction_non_alphanumeric': 0.1206126356094448, 'fraction_numerical': 0.0029780897681344395, 'mean_word_length': 5.162516382699869, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7585976', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1483, 'n_words': 292}