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