Home | History | Annotate | Download | only in spellchecker
      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/browser_context_keyed_service/browser_context_dependency_manager.h"
     13 #include "components/user_prefs/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 BrowserContextKeyedService* 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::kSpellCheckConfirmDialogShown,
     77       false,
     78       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     79   user_prefs->RegisterBooleanPref(
     80       prefs::kSpellCheckUseSpellingService,
     81       false,
     82       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     83   user_prefs->RegisterBooleanPref(
     84       prefs::kEnableContinuousSpellcheck,
     85       true,
     86       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
     87   user_prefs->RegisterBooleanPref(
     88       prefs::kEnableAutoSpellCorrect,
     89       false,
     90       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
     91 }
     92 
     93 content::BrowserContext* SpellcheckServiceFactory::GetBrowserContextToUse(
     94     content::BrowserContext* context) const {
     95   return chrome::GetBrowserContextRedirectedInIncognito(context);
     96 }
     97 
     98 bool SpellcheckServiceFactory::ServiceIsNULLWhileTesting() const {
     99   return true;
    100 }
    101