<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/dom_ui/search_engine_manager_handler.h"

#include "app/l10n_util.h"
#include "base/callback.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/search_engines/keyword_editor_controller.h"
#include "chrome/browser/search_engines/template_url_table_model.h"
#include "chrome/common/url_constants.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"

SearchEngineManagerHandler::SearchEngineManagerHandler() {
}

SearchEngineManagerHandler::~SearchEngineManagerHandler() {
  if (controller_.get() && controller_->table_model())
    controller_->table_model()->SetObserver(NULL);
}

void SearchEngineManagerHandler::Initialize() {
  controller_.reset(new KeywordEditorController(dom_ui_->GetProfile()));
  controller_->table_model()->SetObserver(this);
}

void SearchEngineManagerHandler::GetLocalizedValues(
    DictionaryValue* localized_strings) {
  DCHECK(localized_strings);

  localized_strings->SetString("searchEngineManagerPage",
      l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_WINDOW_TITLE));
  localized_strings->SetString("searchEngineTableNameHeader",
      l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN));
  localized_strings->SetString("searchEngineTableKeywordHeader",
      l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN));
  localized_strings->SetString("addSearchEngineButton",
      l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_NEW_BUTTON));
  localized_strings->SetString("removeSearchEngineButton",
      l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_REMOVE_BUTTON));
  localized_strings->SetString("editSearchEngineButton",
      l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_EDIT_BUTTON));
  localized_strings->SetString("makeDefaultSearchEngineButton",
      l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_MAKE_DEFAULT_BUTTON));
}

void SearchEngineManagerHandler::RegisterMessages() {
  dom_ui_->RegisterMessageCallback(
      "managerSetDefaultSearchEngine",
      NewCallback(this, &SearchEngineManagerHandler::SetDefaultSearchEngine));
  dom_ui_->RegisterMessageCallback(
      "removeSearchEngine",
      NewCallback(this, &SearchEngineManagerHandler::RemoveSearchEngine));
}

void SearchEngineManagerHandler::OnModelChanged() {
  ListValue engine_list;

  // Find the default engine.
  const TemplateURL* default_engine =
      controller_->url_model()->GetDefaultSearchProvider();
  int default_index = controller_->table_model()->IndexOfTemplateURL(
      default_engine);

  // Add the first group (default search engine options).
  engine_list.Append(CreateDictionaryForHeading(0));
  int last_default_engine_index =
      controller_->table_model()->last_search_engine_index();
  for (int i = 0; i < last_default_engine_index; ++i) {
    engine_list.Append(CreateDictionaryForEngine(i, i == default_index));
  }

  // Add the second group (other search templates).
  engine_list.Append(CreateDictionaryForHeading(1));
  if (last_default_engine_index < 0)
    last_default_engine_index = 0;
  int engine_count = controller_->table_model()->RowCount();
  for (int i = last_default_engine_index; i < engine_count; ++i) {
    engine_list.Append(CreateDictionaryForEngine(i, i == default_index));
  }
<commit_msg>DOMUI prefs: Fix a race condition in pushing search engine information to DOMUI<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/dom_ui/search_engine_manager_handler.h"

#include "app/l10n_util.h"
#include "base/callback.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/search_engines/keyword_editor_controller.h"
#include "chrome/browser/search_engines/template_url_table_model.h"
#include "chrome/common/url_constants.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"

SearchEngineManagerHandler::SearchEngineManagerHandler() {
}

SearchEngineManagerHandler::~SearchEngineManagerHandler() {
  if (controller_.get() && controller_->table_model())
    controller_->table_model()->SetObserver(NULL);
}

void SearchEngineManagerHandler::Initialize() {
  controller_.reset(new KeywordEditorController(dom_ui_->GetProfile()));
  if (controller_.get()) {
    controller_->table_model()->SetObserver(this);
    OnModelChanged();
  }
}

void SearchEngineManagerHandler::GetLocalizedValues(
    DictionaryValue* localized_strings) {
  DCHECK(localized_strings);

  localized_strings->SetString("searchEngineManagerPage",
      l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_WINDOW_TITLE));
  localized_strings->SetString("searchEngineTableNameHeader",
      l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN));
  localized_strings->SetString("searchEngineTableKeywordHeader",
      l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN));
  localized_strings->SetString("addSearchEngineButton",
      l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_NEW_BUTTON));
  localized_strings->SetString("removeSearchEngineButton",
      l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_REMOVE_BUTTON));
  localized_strings->SetString("editSearchEngineButton",
      l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_EDIT_BUTTON));
  localized_strings->SetString("makeDefaultSearchEngineButton",
      l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_MAKE_DEFAULT_BUTTON));
}

void SearchEngineManagerHandler::RegisterMessages() {
  dom_ui_->RegisterMessageCallback(
      "managerSetDefaultSearchEngine",
      NewCallback(this, &SearchEngineManagerHandler::SetDefaultSearchEngine));
  dom_ui_->RegisterMessageCallback(
      "removeSearchEngine",
      NewCallback(this, &SearchEngineManagerHandler::RemoveSearchEngine));
}

void SearchEngineManagerHandler::OnModelChanged() {
  if (!controller_->loaded())
    return;

  ListValue engine_list;

  // Find the default engine.
  const TemplateURL* default_engine =
      controller_->url_model()->GetDefaultSearchProvider();
  int default_index = controller_->table_model()->IndexOfTemplateURL(
      default_engine);

  // Add the first group (default search engine options).
  engine_list.Append(CreateDictionaryForHeading(0));
  int last_default_engine_index =
      controller_->table_model()->last_search_engine_index();
  for (int i = 0; i < last_default_engine_index; ++i) {
    engine_list.Append(CreateDictionaryForEngine(i, i == default_index));
  }

  // Add the second group (other search templates).
  engine_list.Append(CreateDictionaryForHeading(1));
  if (last_default_engine_index < 0)
    last_default_engine_index = 0;
  int engine_count = controller_->table_model()->RowCount();
  for (int i = last_default_engine_index; i < engine_count; ++i) {
    engine_list.Append(CreateDictionaryForEngine(i, i == default_index));
  }

{'fraction_non_alphanumeric': 0.10766073871409029, 'fraction_numerical': 0.0106703146374829, 'mean_word_length': 6.912337662337662, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 50, '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': '3222685', 'n_tokens_mistral': 2242, 'n_tokens_neox': 2216, 'n_words': 332}