Home | History | Annotate | Download | only in spellcheck
      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/extensions/api/spellcheck/spellcheck_api.h"
      6 
      7 #include "base/lazy_instance.h"
      8 #include "chrome/browser/spellchecker/spellcheck_factory.h"
      9 #include "chrome/browser/spellchecker/spellcheck_service.h"
     10 #include "chrome/common/extensions/api/spellcheck/spellcheck_handler.h"
     11 #include "extensions/browser/extension_registry.h"
     12 #include "extensions/common/manifest_constants.h"
     13 
     14 namespace extensions {
     15 
     16 namespace errors = manifest_errors;
     17 
     18 namespace {
     19 
     20 SpellcheckDictionaryInfo* GetSpellcheckDictionaryInfo(
     21     const Extension* extension) {
     22   SpellcheckDictionaryInfo *spellcheck_info =
     23       static_cast<SpellcheckDictionaryInfo*>(
     24           extension->GetManifestData(manifest_keys::kSpellcheck));
     25   return spellcheck_info;
     26 }
     27 
     28 SpellcheckService::DictionaryFormat GetDictionaryFormat(
     29     const std::string& format) {
     30   if (format == "hunspell") {
     31     return SpellcheckService::DICT_HUNSPELL;
     32   } else if (format == "text") {
     33     return SpellcheckService::DICT_TEXT;
     34   } else {
     35     return SpellcheckService::DICT_UNKNOWN;
     36   }
     37 }
     38 
     39 }  // namespace
     40 
     41 SpellcheckAPI::SpellcheckAPI(content::BrowserContext* context)
     42     : extension_registry_observer_(this) {
     43   extension_registry_observer_.Add(ExtensionRegistry::Get(context));
     44 }
     45 
     46 SpellcheckAPI::~SpellcheckAPI() {
     47 }
     48 
     49 static base::LazyInstance<BrowserContextKeyedAPIFactory<SpellcheckAPI> >
     50     g_factory = LAZY_INSTANCE_INITIALIZER;
     51 
     52 // static
     53 BrowserContextKeyedAPIFactory<SpellcheckAPI>*
     54 SpellcheckAPI::GetFactoryInstance() {
     55   return g_factory.Pointer();
     56 }
     57 
     58 void SpellcheckAPI::OnExtensionLoaded(content::BrowserContext* browser_context,
     59                                       const Extension* extension) {
     60   SpellcheckDictionaryInfo* spellcheck_info =
     61       GetSpellcheckDictionaryInfo(extension);
     62   if (spellcheck_info) {
     63     // TODO(rlp): Handle load failure. =
     64     SpellcheckService* spellcheck =
     65         SpellcheckServiceFactory::GetForContext(browser_context);
     66     spellcheck->LoadExternalDictionary(
     67         spellcheck_info->language,
     68         spellcheck_info->locale,
     69         spellcheck_info->path,
     70         GetDictionaryFormat(spellcheck_info->format));
     71   }
     72 }
     73 void SpellcheckAPI::OnExtensionUnloaded(
     74     content::BrowserContext* browser_context,
     75     const Extension* extension,
     76     UnloadedExtensionInfo::Reason reason) {
     77   SpellcheckDictionaryInfo* spellcheck_info =
     78       GetSpellcheckDictionaryInfo(extension);
     79   if (spellcheck_info) {
     80     // TODO(rlp): Handle unload failure.
     81     SpellcheckService* spellcheck =
     82         SpellcheckServiceFactory::GetForContext(browser_context);
     83     spellcheck->UnloadExternalDictionary(spellcheck_info->path);
     84   }
     85 }
     86 
     87 template <>
     88 void
     89 BrowserContextKeyedAPIFactory<SpellcheckAPI>::DeclareFactoryDependencies() {
     90   DependsOn(SpellcheckServiceFactory::GetInstance());
     91 }
     92 
     93 }  // namespace extensions
     94