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.
5 #include "chrome/browser/extensions/api/spellcheck/spellcheck_api.h"
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"
14 namespace extensions
{
16 namespace errors
= manifest_errors
;
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
;
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
;
35 return SpellcheckService::DICT_UNKNOWN
;
41 SpellcheckAPI::SpellcheckAPI(content::BrowserContext
* context
)
42 : extension_registry_observer_(this) {
43 extension_registry_observer_
.Add(ExtensionRegistry::Get(context
));
46 SpellcheckAPI::~SpellcheckAPI() {
49 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<SpellcheckAPI
> >
50 g_factory
= LAZY_INSTANCE_INITIALIZER
;
53 BrowserContextKeyedAPIFactory
<SpellcheckAPI
>*
54 SpellcheckAPI::GetFactoryInstance() {
55 return g_factory
.Pointer();
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
));
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
);
89 BrowserContextKeyedAPIFactory
<SpellcheckAPI
>::DeclareFactoryDependencies() {
90 DependsOn(SpellcheckServiceFactory::GetInstance());
93 } // namespace extensions