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
14namespace extensions {
15
16namespace errors = manifest_errors;
17
18namespace {
19
20SpellcheckDictionaryInfo* 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
28SpellcheckService::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
41SpellcheckAPI::SpellcheckAPI(content::BrowserContext* context)
42    : extension_registry_observer_(this) {
43  extension_registry_observer_.Add(ExtensionRegistry::Get(context));
44}
45
46SpellcheckAPI::~SpellcheckAPI() {
47}
48
49static base::LazyInstance<BrowserContextKeyedAPIFactory<SpellcheckAPI> >
50    g_factory = LAZY_INSTANCE_INITIALIZER;
51
52// static
53BrowserContextKeyedAPIFactory<SpellcheckAPI>*
54SpellcheckAPI::GetFactoryInstance() {
55  return g_factory.Pointer();
56}
57
58void 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}
73void 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
87template <>
88void
89BrowserContextKeyedAPIFactory<SpellcheckAPI>::DeclareFactoryDependencies() {
90  DependsOn(SpellcheckServiceFactory::GetInstance());
91}
92
93}  // namespace extensions
94