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/profiles/profile.h"
     11 #include "chrome/browser/spellchecker/spellcheck_service.h"
     12 #include "chrome/common/pref_names.h"
     13 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
     14 #include "components/user_prefs/pref_registry_syncable.h"
     15 #include "content/public/browser/render_process_host.h"
     16 #include "grit/locale_settings.h"
     17 
     18 // static
     19 SpellcheckService* SpellcheckServiceFactory::GetForProfile(Profile* profile) {
     20   return static_cast<SpellcheckService*>(
     21       GetInstance()->GetServiceForBrowserContext(profile, true));
     22 }
     23 
     24 // static
     25 SpellcheckService* 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   Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext());
     32   if (!profile)
     33     return NULL;
     34   return GetForProfile(profile);
     35 }
     36 
     37 // static
     38 SpellcheckService* SpellcheckServiceFactory::GetForProfileWithoutCreating(
     39     Profile* profile) {
     40   return static_cast<SpellcheckService*>(
     41       GetInstance()->GetServiceForBrowserContext(profile, false));
     42 }
     43 
     44 // static
     45 SpellcheckServiceFactory* SpellcheckServiceFactory::GetInstance() {
     46   return Singleton<SpellcheckServiceFactory>::get();
     47 }
     48 
     49 SpellcheckServiceFactory::SpellcheckServiceFactory()
     50     : BrowserContextKeyedServiceFactory(
     51         "SpellcheckService",
     52         BrowserContextDependencyManager::GetInstance()) {
     53   // TODO(erg): Uncomment these as they are initialized.
     54   // DependsOn(RequestContextFactory::GetInstance());
     55 }
     56 
     57 SpellcheckServiceFactory::~SpellcheckServiceFactory() {}
     58 
     59 BrowserContextKeyedService* SpellcheckServiceFactory::BuildServiceInstanceFor(
     60     content::BrowserContext* context) const {
     61   Profile* profile = static_cast<Profile*>(context);
     62 
     63   // Many variables are initialized from the profile in the SpellcheckService.
     64   DCHECK(profile);
     65   SpellcheckService* spellcheck = new SpellcheckService(profile);
     66 
     67   // Instantiates Metrics object for spellchecking for use.
     68   spellcheck->StartRecordingMetrics(
     69       profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck));
     70 
     71   return spellcheck;
     72 }
     73 
     74 void SpellcheckServiceFactory::RegisterProfilePrefs(
     75     user_prefs::PrefRegistrySyncable* user_prefs) {
     76   // TODO(estade): IDS_SPELLCHECK_DICTIONARY should be an ASCII string.
     77   user_prefs->RegisterLocalizedStringPref(
     78       prefs::kSpellCheckDictionary,
     79       IDS_SPELLCHECK_DICTIONARY,
     80       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     81   user_prefs->RegisterBooleanPref(
     82       prefs::kSpellCheckConfirmDialogShown,
     83       false,
     84       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     85   user_prefs->RegisterBooleanPref(
     86       prefs::kSpellCheckUseSpellingService,
     87       false,
     88       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     89   user_prefs->RegisterBooleanPref(
     90       prefs::kEnableContinuousSpellcheck,
     91       true,
     92       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
     93   user_prefs->RegisterBooleanPref(
     94       prefs::kEnableAutoSpellCorrect,
     95       false,
     96       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
     97 }
     98 
     99 content::BrowserContext* SpellcheckServiceFactory::GetBrowserContextToUse(
    100     content::BrowserContext* context) const {
    101   return chrome::GetBrowserContextRedirectedInIncognito(context);
    102 }
    103 
    104 bool SpellcheckServiceFactory::ServiceIsNULLWhileTesting() const {
    105   return true;
    106 }
    107