OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/spellcheck/spellcheck_api.h" | 5 #include "chrome/browser/extensions/api/spellcheck/spellcheck_api.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "chrome/browser/chrome_notification_types.h" | |
9 #include "chrome/browser/profiles/profile.h" | |
10 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 8 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
11 #include "chrome/browser/spellchecker/spellcheck_service.h" | 9 #include "chrome/browser/spellchecker/spellcheck_service.h" |
12 #include "chrome/common/extensions/api/spellcheck/spellcheck_handler.h" | 10 #include "chrome/common/extensions/api/spellcheck/spellcheck_handler.h" |
13 #include "content/public/browser/notification_details.h" | 11 #include "extensions/browser/extension_registry.h" |
14 #include "content/public/browser/notification_source.h" | |
15 #include "extensions/common/manifest_constants.h" | 12 #include "extensions/common/manifest_constants.h" |
16 | 13 |
17 namespace extensions { | 14 namespace extensions { |
18 | 15 |
19 namespace errors = manifest_errors; | 16 namespace errors = manifest_errors; |
20 | 17 |
21 namespace { | 18 namespace { |
22 | 19 |
23 SpellcheckDictionaryInfo* GetSpellcheckDictionaryInfo( | 20 SpellcheckDictionaryInfo* GetSpellcheckDictionaryInfo( |
24 const Extension* extension) { | 21 const Extension* extension) { |
25 SpellcheckDictionaryInfo *spellcheck_info = | 22 SpellcheckDictionaryInfo *spellcheck_info = |
26 static_cast<SpellcheckDictionaryInfo*>( | 23 static_cast<SpellcheckDictionaryInfo*>( |
27 extension->GetManifestData(manifest_keys::kSpellcheck)); | 24 extension->GetManifestData(manifest_keys::kSpellcheck)); |
28 return spellcheck_info; | 25 return spellcheck_info; |
29 } | 26 } |
30 | 27 |
31 SpellcheckService::DictionaryFormat GetDictionaryFormat(std::string format) { | 28 SpellcheckService::DictionaryFormat GetDictionaryFormat(std::string format) { |
32 if (format == "hunspell") { | 29 if (format == "hunspell") { |
33 return SpellcheckService::DICT_HUNSPELL; | 30 return SpellcheckService::DICT_HUNSPELL; |
34 } else if (format == "text") { | 31 } else if (format == "text") { |
35 return SpellcheckService::DICT_TEXT; | 32 return SpellcheckService::DICT_TEXT; |
36 } else { | 33 } else { |
37 return SpellcheckService::DICT_UNKNOWN; | 34 return SpellcheckService::DICT_UNKNOWN; |
38 } | 35 } |
39 } | 36 } |
40 | 37 |
41 } // namespace | 38 } // namespace |
42 | 39 |
43 SpellcheckAPI::SpellcheckAPI(content::BrowserContext* context) { | 40 SpellcheckAPI::SpellcheckAPI(content::BrowserContext* context) |
44 Profile* profile = Profile::FromBrowserContext(context); | 41 : extension_registry_observer_(this) { |
45 registrar_.Add(this, | 42 extension_registry_observer_.Add(ExtensionRegistry::Get(context)); |
46 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | |
47 content::Source<Profile>(profile)); | |
48 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
49 content::Source<Profile>(profile)); | |
50 } | 43 } |
51 | 44 |
52 SpellcheckAPI::~SpellcheckAPI() { | 45 SpellcheckAPI::~SpellcheckAPI() { |
53 } | 46 } |
54 | 47 |
55 static base::LazyInstance<BrowserContextKeyedAPIFactory<SpellcheckAPI> > | 48 static base::LazyInstance<BrowserContextKeyedAPIFactory<SpellcheckAPI> > |
56 g_factory = LAZY_INSTANCE_INITIALIZER; | 49 g_factory = LAZY_INSTANCE_INITIALIZER; |
57 | 50 |
58 // static | 51 // static |
59 BrowserContextKeyedAPIFactory<SpellcheckAPI>* | 52 BrowserContextKeyedAPIFactory<SpellcheckAPI>* |
60 SpellcheckAPI::GetFactoryInstance() { | 53 SpellcheckAPI::GetFactoryInstance() { |
61 return g_factory.Pointer(); | 54 return g_factory.Pointer(); |
62 } | 55 } |
63 | 56 |
64 void SpellcheckAPI::Observe(int type, | 57 void SpellcheckAPI::OnExtensionLoaded(content::BrowserContext* browser_context, |
65 const content::NotificationSource& source, | 58 const Extension* extension) { |
66 const content::NotificationDetails& details) { | 59 SpellcheckDictionaryInfo* spellcheck_info = |
67 Profile* profile = content::Source<Profile>(source).ptr(); | 60 GetSpellcheckDictionaryInfo(extension); |
68 SpellcheckService* spellcheck = NULL; | 61 if (spellcheck_info) { |
69 switch (type) { | 62 // TODO(rlp): Handle load failure. = |
70 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: { | 63 SpellcheckService* spellcheck = |
71 const Extension* extension = content::Details<Extension>(details).ptr(); | 64 SpellcheckServiceFactory::GetForContext(browser_context); |
72 SpellcheckDictionaryInfo* spellcheck_info = | 65 spellcheck->LoadExternalDictionary( |
73 GetSpellcheckDictionaryInfo(extension); | 66 spellcheck_info->language, |
74 if (spellcheck_info) { | 67 spellcheck_info->locale, |
75 // TODO(rlp): Handle load failure. = | 68 spellcheck_info->path, |
76 spellcheck = SpellcheckServiceFactory::GetForContext(profile); | 69 GetDictionaryFormat(spellcheck_info->format)); |
77 spellcheck->LoadExternalDictionary( | 70 } |
78 spellcheck_info->language, | 71 } |
79 spellcheck_info->locale, | 72 void SpellcheckAPI::OnExtensionUnloaded( |
80 spellcheck_info->path, | 73 content::BrowserContext* browser_context, |
81 GetDictionaryFormat(spellcheck_info->format)); | 74 const Extension* extension, |
82 } | 75 UnloadedExtensionInfo::Reason reason) { |
83 break; | 76 SpellcheckDictionaryInfo* spellcheck_info = |
84 } | 77 GetSpellcheckDictionaryInfo(extension); |
85 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | 78 if (spellcheck_info) { |
86 const Extension* extension = | 79 // TODO(rlp): Handle unload failure. |
87 content::Details<UnloadedExtensionInfo>(details)->extension; | 80 SpellcheckService* spellcheck = |
88 SpellcheckDictionaryInfo* spellcheck_info = | 81 SpellcheckServiceFactory::GetForContext(browser_context); |
89 GetSpellcheckDictionaryInfo(extension); | 82 spellcheck->UnloadExternalDictionary(spellcheck_info->path); |
90 if (spellcheck_info) { | |
91 // TODO(rlp): Handle unload failure. | |
92 spellcheck = SpellcheckServiceFactory::GetForContext(profile); | |
93 spellcheck->UnloadExternalDictionary(spellcheck_info->path); | |
94 } | |
95 break; | |
96 } | |
97 default: | |
98 NOTREACHED(); | |
99 } | 83 } |
100 } | 84 } |
101 | 85 |
102 template <> | 86 template <> |
103 void | 87 void |
104 BrowserContextKeyedAPIFactory<SpellcheckAPI>::DeclareFactoryDependencies() { | 88 BrowserContextKeyedAPIFactory<SpellcheckAPI>::DeclareFactoryDependencies() { |
105 DependsOn(SpellcheckServiceFactory::GetInstance()); | 89 DependsOn(SpellcheckServiceFactory::GetInstance()); |
106 } | 90 } |
107 | 91 |
108 } // namespace extensions | 92 } // namespace extensions |
OLD | NEW |