// Copyright (c) 2012 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/spellchecker/spellcheck_factory.h"

#ifndef TOOLKIT_QT
#include "chrome/browser/profiles/incognito_helpers.h"
#endif
#include "chrome/browser/spellchecker/spellcheck_service.h"
#ifndef TOOLKIT_QT
#include "chrome/grit/locale_settings.h"
#endif
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/spellcheck/browser/pref_names.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/render_process_host.h"
#include "services/service_manager/public/cpp/identity.h"
#include "ui/base/l10n/l10n_util.h"

// static
SpellcheckService* SpellcheckServiceFactory::GetForContext(
    content::BrowserContext* context) {
  return static_cast<SpellcheckService*>(
      GetInstance()->GetServiceForBrowserContext(context, true));
}

// static
SpellcheckService* SpellcheckServiceFactory::GetForRenderer(
    const service_manager::Identity& renderer_identity) {
  content::BrowserContext* context =
      content::BrowserContext::GetBrowserContextForServiceUserId(
          renderer_identity.user_id());
  if (!context)
    return nullptr;
  return GetForContext(context);
}

// static
SpellcheckServiceFactory* SpellcheckServiceFactory::GetInstance() {
  return base::Singleton<SpellcheckServiceFactory>::get();
}

SpellcheckServiceFactory::SpellcheckServiceFactory()
    : BrowserContextKeyedServiceFactory(
        "SpellcheckService",
        BrowserContextDependencyManager::GetInstance()) {
  // TODO(erg): Uncomment these as they are initialized.
  // DependsOn(RequestContextFactory::GetInstance());
}

SpellcheckServiceFactory::~SpellcheckServiceFactory() {}

KeyedService* SpellcheckServiceFactory::BuildServiceInstanceFor(
    content::BrowserContext* context) const {
  // Many variables are initialized from the |context| in the SpellcheckService.
  SpellcheckService* spellcheck = new SpellcheckService(context);

  PrefService* prefs = user_prefs::UserPrefs::Get(context);
  DCHECK(prefs);

  // Instantiates Metrics object for spellchecking for use.
  spellcheck->StartRecordingMetrics(
      prefs->GetBoolean(spellcheck::prefs::kSpellCheckEnable));

  return spellcheck;
}

void SpellcheckServiceFactory::RegisterProfilePrefs(
    user_prefs::PrefRegistrySyncable* user_prefs) {
  user_prefs->RegisterListPref(spellcheck::prefs::kSpellCheckDictionaries,
                               std::make_unique<base::ListValue>());
  user_prefs->RegisterListPref(spellcheck::prefs::kSpellCheckForcedDictionaries,
                               std::make_unique<base::ListValue>());
  // Continue registering kSpellCheckDictionary for preference migration.
  // TODO(estade): remove: crbug.com/751275
#ifndef TOOLKIT_QT
  user_prefs->RegisterStringPref(
      spellcheck::prefs::kSpellCheckDictionary,
      l10n_util::GetStringUTF8(IDS_SPELLCHECK_DICTIONARY));
#endif
  user_prefs->RegisterBooleanPref(
      spellcheck::prefs::kSpellCheckUseSpellingService, false);
#if defined(OS_ANDROID)
  uint32_t flags = PrefRegistry::NO_REGISTRATION_FLAGS;
#else
  uint32_t flags = user_prefs::PrefRegistrySyncable::SYNCABLE_PREF;
#endif
  user_prefs->RegisterBooleanPref(spellcheck::prefs::kSpellCheckEnable, true,
                                  flags);
}

content::BrowserContext* SpellcheckServiceFactory::GetBrowserContextToUse(
    content::BrowserContext* context) const {
#ifndef TOOLKIT_QT
  return chrome::GetBrowserContextRedirectedInIncognito(context);
#else
  return context;
#endif
}

bool SpellcheckServiceFactory::ServiceIsNULLWhileTesting() const {
  return true;
}
