Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / spellchecker / spellcheck_factory.cc
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/browser_process.h"
9 #include "chrome/browser/profiles/incognito_helpers.h"
10 #include "chrome/browser/spellchecker/spellcheck_service.h"
11 #include "chrome/common/pref_names.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 #include "grit/locale_settings.h"
17
18 // static
19 SpellcheckService* SpellcheckServiceFactory::GetForContext(
20     content::BrowserContext* context) {
21   return static_cast<SpellcheckService*>(
22       GetInstance()->GetServiceForBrowserContext(context, true));
23 }
24
25 // static
26 SpellcheckService* SpellcheckServiceFactory::GetForRenderProcessId(
27     int render_process_id) {
28   content::RenderProcessHost* host =
29       content::RenderProcessHost::FromID(render_process_id);
30   if (!host)
31     return NULL;
32   content::BrowserContext* context = host->GetBrowserContext();
33   if (!context)
34     return NULL;
35   return GetForContext(context);
36 }
37
38 // static
39 SpellcheckServiceFactory* SpellcheckServiceFactory::GetInstance() {
40   return Singleton<SpellcheckServiceFactory>::get();
41 }
42
43 SpellcheckServiceFactory::SpellcheckServiceFactory()
44     : BrowserContextKeyedServiceFactory(
45         "SpellcheckService",
46         BrowserContextDependencyManager::GetInstance()) {
47   // TODO(erg): Uncomment these as they are initialized.
48   // DependsOn(RequestContextFactory::GetInstance());
49 }
50
51 SpellcheckServiceFactory::~SpellcheckServiceFactory() {}
52
53 KeyedService* SpellcheckServiceFactory::BuildServiceInstanceFor(
54     content::BrowserContext* context) const {
55   // Many variables are initialized from the |context| in the SpellcheckService.
56   SpellcheckService* spellcheck = new SpellcheckService(context);
57
58   PrefService* prefs = user_prefs::UserPrefs::Get(context);
59   DCHECK(prefs);
60
61   // Instantiates Metrics object for spellchecking for use.
62   spellcheck->StartRecordingMetrics(
63       prefs->GetBoolean(prefs::kEnableContinuousSpellcheck));
64
65   return spellcheck;
66 }
67
68 void SpellcheckServiceFactory::RegisterProfilePrefs(
69     user_prefs::PrefRegistrySyncable* user_prefs) {
70   // TODO(estade): IDS_SPELLCHECK_DICTIONARY should be an ASCII string.
71   user_prefs->RegisterLocalizedStringPref(
72       prefs::kSpellCheckDictionary,
73       IDS_SPELLCHECK_DICTIONARY,
74       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
75   user_prefs->RegisterBooleanPref(
76       prefs::kSpellCheckUseSpellingService,
77       false,
78       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
79   user_prefs->RegisterBooleanPref(
80       prefs::kEnableContinuousSpellcheck,
81       true,
82       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
83   user_prefs->RegisterBooleanPref(
84       prefs::kEnableAutoSpellCorrect,
85       false,
86       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
87 }
88
89 content::BrowserContext* SpellcheckServiceFactory::GetBrowserContextToUse(
90     content::BrowserContext* context) const {
91   return chrome::GetBrowserContextRedirectedInIncognito(context);
92 }
93
94 bool SpellcheckServiceFactory::ServiceIsNULLWhileTesting() const {
95   return true;
96 }