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/speech/speech_input_extension_manager.h" | 5 #include "chrome/browser/speech/speech_input_extension_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/lazy_instance.h" |
9 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
10 #include "base/values.h" | 11 #include "base/values.h" |
11 #include "chrome/browser/extensions/event_router.h" | 12 #include "chrome/browser/extensions/event_router.h" |
| 13 #include "chrome/browser/extensions/extension_function_registry.h" |
12 #include "chrome/browser/extensions/extension_host.h" | 14 #include "chrome/browser/extensions/extension_host.h" |
13 #include "chrome/browser/extensions/extension_process_manager.h" | 15 #include "chrome/browser/extensions/extension_process_manager.h" |
14 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
15 #include "chrome/browser/extensions/extension_system.h" | 17 #include "chrome/browser/extensions/extension_system.h" |
16 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/profiles/profile_dependency_manager.h" | 19 #include "chrome/browser/profiles/profile_dependency_manager.h" |
18 #include "chrome/browser/profiles/profile_keyed_service.h" | 20 #include "chrome/browser/profiles/profile_keyed_service.h" |
19 #include "chrome/browser/profiles/profile_keyed_service_factory.h" | 21 #include "chrome/browser/profiles/profile_keyed_service_factory.h" |
| 22 #include "chrome/browser/speech/speech_input_extension_api.h" |
20 #include "chrome/common/chrome_notification_types.h" | 23 #include "chrome/common/chrome_notification_types.h" |
21 #include "chrome/common/extensions/extension.h" | 24 #include "chrome/common/extensions/extension.h" |
22 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
23 #include "content/public/browser/notification_registrar.h" | 26 #include "content/public/browser/notification_registrar.h" |
24 #include "content/public/browser/notification_service.h" | 27 #include "content/public/browser/notification_service.h" |
25 #include "content/public/browser/render_process_host.h" | 28 #include "content/public/browser/render_process_host.h" |
26 #include "content/public/browser/speech_recognition_manager.h" | 29 #include "content/public/browser/speech_recognition_manager.h" |
27 #include "content/public/browser/speech_recognition_session_config.h" | 30 #include "content/public/browser/speech_recognition_session_config.h" |
28 #include "content/public/browser/speech_recognition_session_context.h" | 31 #include "content/public/browser/speech_recognition_session_context.h" |
29 #include "content/public/common/speech_recognition_error.h" | 32 #include "content/public/common/speech_recognition_error.h" |
(...skipping 21 matching lines...) Expand all Loading... |
51 | 54 |
52 const char kUtteranceKey[] = "utterance"; | 55 const char kUtteranceKey[] = "utterance"; |
53 const char kConfidenceKey[] = "confidence"; | 56 const char kConfidenceKey[] = "confidence"; |
54 const char kHypothesesKey[] = "hypotheses"; | 57 const char kHypothesesKey[] = "hypotheses"; |
55 | 58 |
56 const char kOnErrorEvent[] = "experimental.speechInput.onError"; | 59 const char kOnErrorEvent[] = "experimental.speechInput.onError"; |
57 const char kOnResultEvent[] = "experimental.speechInput.onResult"; | 60 const char kOnResultEvent[] = "experimental.speechInput.onResult"; |
58 const char kOnSoundStartEvent[] = "experimental.speechInput.onSoundStart"; | 61 const char kOnSoundStartEvent[] = "experimental.speechInput.onSoundStart"; |
59 const char kOnSoundEndEvent[] = "experimental.speechInput.onSoundEnd"; | 62 const char kOnSoundEndEvent[] = "experimental.speechInput.onSoundEnd"; |
60 | 63 |
61 // Wrap an SpeechInputExtensionManager using scoped_refptr to avoid | |
62 // assertion failures on destruction because of not using release(). | |
63 class SpeechInputExtensionManagerWrapper : public ProfileKeyedService { | |
64 public: | |
65 explicit SpeechInputExtensionManagerWrapper( | |
66 SpeechInputExtensionManager* manager) | |
67 : manager_(manager) {} | |
68 | |
69 virtual ~SpeechInputExtensionManagerWrapper() {} | |
70 | |
71 SpeechInputExtensionManager* manager() const { return manager_.get(); } | |
72 | |
73 private: | |
74 // Methods from ProfileKeyedService. | |
75 virtual void Shutdown() OVERRIDE { | |
76 manager()->ShutdownOnUIThread(); | |
77 } | |
78 | |
79 scoped_refptr<SpeechInputExtensionManager> manager_; | |
80 }; | |
81 } | |
82 | |
83 // Factory for SpeechInputExtensionManagers as profile keyed services. | |
84 class SpeechInputExtensionManager::Factory : public ProfileKeyedServiceFactory { | |
85 public: | |
86 static void Initialize(); | |
87 static Factory* GetInstance(); | |
88 | |
89 SpeechInputExtensionManagerWrapper* GetForProfile(Profile* profile); | |
90 | |
91 private: | |
92 friend struct DefaultSingletonTraits<Factory>; | |
93 | |
94 Factory(); | |
95 virtual ~Factory(); | |
96 | |
97 // ProfileKeyedServiceFactory methods: | |
98 virtual ProfileKeyedService* BuildServiceInstanceFor( | |
99 Profile* profile) const OVERRIDE; | |
100 virtual bool ServiceRedirectedInIncognito() const OVERRIDE { return false; } | |
101 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { return true; } | |
102 virtual bool ServiceIsCreatedWithProfile() const OVERRIDE { return true; } | |
103 | |
104 DISALLOW_COPY_AND_ASSIGN(Factory); | |
105 }; | |
106 | |
107 void SpeechInputExtensionManager::Factory::Initialize() { | |
108 GetInstance(); | |
109 } | |
110 | |
111 SpeechInputExtensionManager::Factory* | |
112 SpeechInputExtensionManager::Factory::GetInstance() { | |
113 return Singleton<SpeechInputExtensionManager::Factory>::get(); | |
114 } | |
115 | |
116 SpeechInputExtensionManagerWrapper* | |
117 SpeechInputExtensionManager::Factory::GetForProfile( | |
118 Profile* profile) { | |
119 return static_cast<SpeechInputExtensionManagerWrapper*>( | |
120 GetServiceForProfile(profile, true)); | |
121 } | |
122 | |
123 SpeechInputExtensionManager::Factory::Factory() | |
124 : ProfileKeyedServiceFactory("SpeechInputExtensionManager", | |
125 ProfileDependencyManager::GetInstance()) { | |
126 } | |
127 | |
128 SpeechInputExtensionManager::Factory::~Factory() { | |
129 } | |
130 | |
131 ProfileKeyedService* | |
132 SpeechInputExtensionManager::Factory::BuildServiceInstanceFor( | |
133 Profile* profile) const { | |
134 scoped_refptr<SpeechInputExtensionManager> manager( | |
135 new SpeechInputExtensionManager(profile)); | |
136 return new SpeechInputExtensionManagerWrapper(manager); | |
137 } | 64 } |
138 | 65 |
139 SpeechInputExtensionInterface::SpeechInputExtensionInterface() { | 66 SpeechInputExtensionInterface::SpeechInputExtensionInterface() { |
140 } | 67 } |
141 | 68 |
142 SpeechInputExtensionInterface::~SpeechInputExtensionInterface() { | 69 SpeechInputExtensionInterface::~SpeechInputExtensionInterface() { |
143 } | 70 } |
144 | 71 |
145 SpeechInputExtensionManager::SpeechInputExtensionManager(Profile* profile) | 72 SpeechInputExtensionManager::SpeechInputExtensionManager(Profile* profile) |
146 : profile_(profile), | 73 : profile_(profile), |
147 state_(kIdle), | 74 state_(kIdle), |
148 registrar_(new content::NotificationRegistrar), | 75 registrar_(new content::NotificationRegistrar), |
149 speech_interface_(NULL), | 76 speech_interface_(NULL), |
150 is_recognition_in_progress_(false), | 77 is_recognition_in_progress_(false), |
151 speech_recognition_session_id_( | 78 speech_recognition_session_id_( |
152 SpeechRecognitionManager::kSessionIDInvalid) { | 79 SpeechRecognitionManager::kSessionIDInvalid) { |
153 registrar_->Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 80 registrar_->Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
154 content::Source<Profile>(profile_)); | 81 content::Source<Profile>(profile_)); |
155 } | 82 } |
156 | 83 |
157 SpeechInputExtensionManager::~SpeechInputExtensionManager() { | 84 SpeechInputExtensionManager::~SpeechInputExtensionManager() { |
158 } | 85 } |
159 | 86 |
160 SpeechInputExtensionManager* SpeechInputExtensionManager::GetForProfile( | 87 SpeechInputExtensionManager* SpeechInputExtensionManager::GetForProfile( |
161 Profile* profile) { | 88 Profile* profile) { |
162 SpeechInputExtensionManagerWrapper* wrapper = | 89 extensions::SpeechInputAPI* speech_input_api = |
163 Factory::GetInstance()->GetForProfile(profile); | 90 extensions::ProfileKeyedAPIFactory<extensions::SpeechInputAPI>:: |
164 if (!wrapper) | 91 GetForProfile(profile); |
| 92 if (!speech_input_api) |
165 return NULL; | 93 return NULL; |
166 return wrapper->manager(); | 94 return speech_input_api->manager(); |
167 } | |
168 | |
169 void SpeechInputExtensionManager::InitializeFactory() { | |
170 Factory::Initialize(); | |
171 } | 95 } |
172 | 96 |
173 void SpeechInputExtensionManager::Observe(int type, | 97 void SpeechInputExtensionManager::Observe(int type, |
174 const content::NotificationSource& source, | 98 const content::NotificationSource& source, |
175 const content::NotificationDetails& details) { | 99 const content::NotificationDetails& details) { |
176 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { | 100 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { |
177 ExtensionUnloaded( | 101 ExtensionUnloaded( |
178 content::Details<extensions::UnloadedExtensionInfo>(details)-> | 102 content::Details<extensions::UnloadedExtensionInfo>(details)-> |
179 extension->id()); | 103 extension->id()); |
180 } else { | 104 } else { |
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
758 content::NotificationService::current()->Notify( | 682 content::NotificationService::current()->Notify( |
759 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED, | 683 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED, |
760 // Guarded by the state_ == kShutdown check. | 684 // Guarded by the state_ == kShutdown check. |
761 content::Source<Profile>(profile_), | 685 content::Source<Profile>(profile_), |
762 content::Details<std::string>(&extension_id)); | 686 content::Details<std::string>(&extension_id)); |
763 } | 687 } |
764 | 688 |
765 void SpeechInputExtensionManager::OnAudioLevelsChange(int session_id, | 689 void SpeechInputExtensionManager::OnAudioLevelsChange(int session_id, |
766 float volume, | 690 float volume, |
767 float noise_volume) {} | 691 float noise_volume) {} |
| 692 |
| 693 namespace extensions { |
| 694 |
| 695 SpeechInputAPI::SpeechInputAPI(Profile* profile) |
| 696 : manager_(new SpeechInputExtensionManager(profile)) { |
| 697 ExtensionFunctionRegistry* registry = |
| 698 ExtensionFunctionRegistry::GetInstance(); |
| 699 registry->RegisterFunction<StartSpeechInputFunction>(); |
| 700 registry->RegisterFunction<StopSpeechInputFunction>(); |
| 701 registry->RegisterFunction<IsRecordingSpeechInputFunction>(); |
| 702 } |
| 703 |
| 704 SpeechInputAPI::~SpeechInputAPI() { |
| 705 } |
| 706 |
| 707 void SpeechInputAPI::Shutdown() { |
| 708 manager_->ShutdownOnUIThread(); |
| 709 } |
| 710 |
| 711 static base::LazyInstance<ProfileKeyedAPIFactory<SpeechInputAPI> > |
| 712 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 713 |
| 714 // static |
| 715 ProfileKeyedAPIFactory<SpeechInputAPI>* SpeechInputAPI::GetFactoryInstance() { |
| 716 return &g_factory.Get(); |
| 717 } |
| 718 |
| 719 } // namespace extensions |
OLD | NEW |