• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/chrome_notification_types.h"
9 #include "chrome/browser/spellchecker/spellcheck_factory.h"
10 #include "chrome/browser/spellchecker/spellcheck_service.h"
11 #include "chrome/common/extensions/api/spellcheck/spellcheck_handler.h"
12 #include "content/public/browser/notification_details.h"
13 #include "content/public/browser/notification_source.h"
14 #include "extensions/common/manifest_constants.h"
15 
16 namespace extensions {
17 
18 namespace errors = manifest_errors;
19 
20 namespace {
21 
GetSpellcheckDictionaryInfo(const Extension * extension)22 SpellcheckDictionaryInfo* GetSpellcheckDictionaryInfo(
23     const Extension* extension) {
24   SpellcheckDictionaryInfo *spellcheck_info =
25       static_cast<SpellcheckDictionaryInfo*>(
26           extension->GetManifestData(manifest_keys::kSpellcheck));
27   return spellcheck_info;
28 }
29 
GetDictionaryFormat(std::string format)30 SpellcheckService::DictionaryFormat GetDictionaryFormat(std::string format) {
31   if (format == "hunspell") {
32     return SpellcheckService::DICT_HUNSPELL;
33   } else if (format == "text") {
34     return SpellcheckService::DICT_TEXT;
35   } else {
36     return SpellcheckService::DICT_UNKNOWN;
37   }
38 }
39 
40 }  // namespace
41 
42 
SpellcheckAPI(Profile * profile)43 SpellcheckAPI::SpellcheckAPI(Profile* profile) {
44   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
45                  content::Source<Profile>(profile));
46   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
47                  content::Source<Profile>(profile));
48 }
49 
~SpellcheckAPI()50 SpellcheckAPI::~SpellcheckAPI() {
51 }
52 
53 static base::LazyInstance<ProfileKeyedAPIFactory<SpellcheckAPI> >
54 g_factory = LAZY_INSTANCE_INITIALIZER;
55 
56 // static
GetFactoryInstance()57 ProfileKeyedAPIFactory<SpellcheckAPI>* SpellcheckAPI::GetFactoryInstance() {
58   return &g_factory.Get();
59 }
60 
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)61 void SpellcheckAPI::Observe(int type,
62                             const content::NotificationSource& source,
63                             const content::NotificationDetails& details) {
64   Profile* profile = content::Source<Profile>(source).ptr();
65   SpellcheckService* spellcheck = NULL;
66   switch (type) {
67     case chrome::NOTIFICATION_EXTENSION_LOADED: {
68       const Extension* extension = content::Details<Extension>(details).ptr();
69       SpellcheckDictionaryInfo* spellcheck_info =
70           GetSpellcheckDictionaryInfo(extension);
71       if (spellcheck_info) {
72         // TODO(rlp): Handle load failure. =
73         spellcheck = SpellcheckServiceFactory::GetForContext(profile);
74         spellcheck->LoadExternalDictionary(
75             spellcheck_info->language,
76             spellcheck_info->locale,
77             spellcheck_info->path,
78             GetDictionaryFormat(spellcheck_info->format));
79       }
80       break;
81     }
82     case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
83       const Extension* extension =
84           content::Details<UnloadedExtensionInfo>(details)->extension;
85       SpellcheckDictionaryInfo* spellcheck_info =
86           GetSpellcheckDictionaryInfo(extension);
87       if (spellcheck_info) {
88         // TODO(rlp): Handle unload failure.
89         spellcheck = SpellcheckServiceFactory::GetForContext(profile);
90         spellcheck->UnloadExternalDictionary(spellcheck_info->path);
91       }
92       break;
93     }
94     default:
95       NOTREACHED();
96   }
97 }
98 
99 template <>
DeclareFactoryDependencies()100 void ProfileKeyedAPIFactory<SpellcheckAPI>::DeclareFactoryDependencies() {
101   DependsOn(SpellcheckServiceFactory::GetInstance());
102 }
103 
104 }  // namespace extensions
105