1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/spellchecker/spellcheck_factory.h"
6
7#include "base/prefs/pref_service.h"
8#include "chrome/browser/profiles/incognito_helpers.h"
9#include "chrome/browser/spellchecker/spellcheck_service.h"
10#include "chrome/common/pref_names.h"
11#include "chrome/grit/locale_settings.h"
12#include "components/keyed_service/content/browser_context_dependency_manager.h"
13#include "components/pref_registry/pref_registry_syncable.h"
14#include "components/user_prefs/user_prefs.h"
15#include "content/public/browser/render_process_host.h"
16
17// static
18SpellcheckService* SpellcheckServiceFactory::GetForContext(
19    content::BrowserContext* context) {
20  return static_cast<SpellcheckService*>(
21      GetInstance()->GetServiceForBrowserContext(context, true));
22}
23
24// static
25SpellcheckService* SpellcheckServiceFactory::GetForRenderProcessId(
26    int render_process_id) {
27  content::RenderProcessHost* host =
28      content::RenderProcessHost::FromID(render_process_id);
29  if (!host)
30    return NULL;
31  content::BrowserContext* context = host->GetBrowserContext();
32  if (!context)
33    return NULL;
34  return GetForContext(context);
35}
36
37// static
38SpellcheckServiceFactory* SpellcheckServiceFactory::GetInstance() {
39  return Singleton<SpellcheckServiceFactory>::get();
40}
41
42SpellcheckServiceFactory::SpellcheckServiceFactory()
43    : BrowserContextKeyedServiceFactory(
44        "SpellcheckService",
45        BrowserContextDependencyManager::GetInstance()) {
46  // TODO(erg): Uncomment these as they are initialized.
47  // DependsOn(RequestContextFactory::GetInstance());
48}
49
50SpellcheckServiceFactory::~SpellcheckServiceFactory() {}
51
52KeyedService* SpellcheckServiceFactory::BuildServiceInstanceFor(
53    content::BrowserContext* context) const {
54  // Many variables are initialized from the |context| in the SpellcheckService.
55  SpellcheckService* spellcheck = new SpellcheckService(context);
56
57  PrefService* prefs = user_prefs::UserPrefs::Get(context);
58  DCHECK(prefs);
59
60  // Instantiates Metrics object for spellchecking for use.
61  spellcheck->StartRecordingMetrics(
62      prefs->GetBoolean(prefs::kEnableContinuousSpellcheck));
63
64  return spellcheck;
65}
66
67void SpellcheckServiceFactory::RegisterProfilePrefs(
68    user_prefs::PrefRegistrySyncable* user_prefs) {
69  // TODO(estade): IDS_SPELLCHECK_DICTIONARY should be an ASCII string.
70  user_prefs->RegisterLocalizedStringPref(
71      prefs::kSpellCheckDictionary,
72      IDS_SPELLCHECK_DICTIONARY,
73      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
74  user_prefs->RegisterBooleanPref(
75      prefs::kSpellCheckUseSpellingService,
76      false,
77      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
78  user_prefs->RegisterBooleanPref(
79      prefs::kEnableContinuousSpellcheck,
80      true,
81      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
82  user_prefs->RegisterBooleanPref(
83      prefs::kEnableAutoSpellCorrect,
84      false,
85      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
86}
87
88content::BrowserContext* SpellcheckServiceFactory::GetBrowserContextToUse(
89    content::BrowserContext* context) const {
90  return chrome::GetBrowserContextRedirectedInIncognito(context);
91}
92
93bool SpellcheckServiceFactory::ServiceIsNULLWhileTesting() const {
94  return true;
95}
96