| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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/extension_speech_input_manager.h" | |
| 6 | |
| 7 #include "chrome/browser/profiles/profile_dependency_manager.h" | |
| 8 #include "chrome/browser/profiles/profile_keyed_service.h" | |
| 9 #include "chrome/browser/profiles/profile_keyed_service_factory.h" | |
| 10 #include "chrome/common/chrome_notification_types.h" | |
| 11 #include "chrome/common/extensions/extension.h" | |
| 12 #include "content/common/notification_service.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // Wrap an ExtensionSpeechInputManager using scoped_refptr to avoid | |
| 17 // assertion failures on destruction because of not using release(). | |
| 18 class ExtensionSpeechInputManagerWrapper : public ProfileKeyedService { | |
| 19 public: | |
| 20 explicit ExtensionSpeechInputManagerWrapper( | |
| 21 ExtensionSpeechInputManager* manager) | |
| 22 : manager_(manager) {} | |
| 23 | |
| 24 virtual ~ExtensionSpeechInputManagerWrapper() {} | |
| 25 | |
| 26 ExtensionSpeechInputManager* manager() const { return manager_.get(); } | |
| 27 | |
| 28 private: | |
| 29 // Methods from ProfileKeyedService. | |
| 30 virtual void Shutdown() OVERRIDE { | |
| 31 manager()->ShutdownOnUIThread(); | |
| 32 } | |
| 33 | |
| 34 scoped_refptr<ExtensionSpeechInputManager> manager_; | |
| 35 }; | |
| 36 | |
| 37 } | |
| 38 | |
| 39 // Factory for ExtensionSpeechInputManagers as profile keyed services. | |
| 40 class ExtensionSpeechInputManager::Factory : public ProfileKeyedServiceFactory { | |
| 41 public: | |
| 42 static void Initialize(); | |
| 43 static Factory* GetInstance(); | |
| 44 | |
| 45 ExtensionSpeechInputManagerWrapper* GetForProfile(Profile* profile); | |
| 46 | |
| 47 private: | |
| 48 friend struct DefaultSingletonTraits<Factory>; | |
| 49 | |
| 50 Factory(); | |
| 51 virtual ~Factory(); | |
| 52 | |
| 53 // ProfileKeyedServiceFactory methods: | |
| 54 virtual ProfileKeyedService* BuildServiceInstanceFor( | |
| 55 Profile* profile) const; | |
| 56 virtual bool ServiceRedirectedInIncognito() { return false; } | |
| 57 virtual bool ServiceIsNULLWhileTesting() { return true; } | |
| 58 virtual bool ServiceIsCreatedWithProfile() { return true; } | |
| 59 | |
| 60 DISALLOW_COPY_AND_ASSIGN(Factory); | |
| 61 }; | |
| 62 | |
| 63 void ExtensionSpeechInputManager::Factory::Initialize() { | |
| 64 GetInstance(); | |
| 65 } | |
| 66 | |
| 67 ExtensionSpeechInputManager::Factory* | |
| 68 ExtensionSpeechInputManager::Factory::GetInstance() { | |
| 69 return Singleton<ExtensionSpeechInputManager::Factory>::get(); | |
| 70 } | |
| 71 | |
| 72 ExtensionSpeechInputManagerWrapper* | |
| 73 ExtensionSpeechInputManager::Factory::GetForProfile( | |
| 74 Profile* profile) { | |
| 75 return static_cast<ExtensionSpeechInputManagerWrapper*>( | |
| 76 GetServiceForProfile(profile, true)); | |
| 77 } | |
| 78 | |
| 79 ExtensionSpeechInputManager::Factory::Factory() | |
| 80 : ProfileKeyedServiceFactory(ProfileDependencyManager::GetInstance()) { | |
| 81 } | |
| 82 | |
| 83 ExtensionSpeechInputManager::Factory::~Factory() { | |
| 84 } | |
| 85 | |
| 86 ProfileKeyedService* | |
| 87 ExtensionSpeechInputManager::Factory::BuildServiceInstanceFor( | |
| 88 Profile* profile) const { | |
| 89 scoped_refptr<ExtensionSpeechInputManager> manager( | |
| 90 new ExtensionSpeechInputManager(profile)); | |
| 91 return new ExtensionSpeechInputManagerWrapper(manager); | |
| 92 } | |
| 93 | |
| 94 ExtensionSpeechInputManager::ExtensionSpeechInputManager(Profile* profile) | |
| 95 : profile_(profile), | |
| 96 extension_(NULL) { | |
| 97 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | |
| 98 Source<Profile>(profile_)); | |
| 99 } | |
| 100 | |
| 101 ExtensionSpeechInputManager::~ExtensionSpeechInputManager() { | |
| 102 } | |
| 103 | |
| 104 ExtensionSpeechInputManager* ExtensionSpeechInputManager::GetForProfile( | |
| 105 Profile* profile) { | |
| 106 ExtensionSpeechInputManagerWrapper *wrapper = | |
| 107 Factory::GetInstance()->GetForProfile(profile); | |
| 108 if (!wrapper) | |
| 109 return NULL; | |
| 110 return wrapper->manager(); | |
| 111 } | |
| 112 | |
| 113 void ExtensionSpeechInputManager::InitializeFactory() { | |
| 114 Factory::Initialize(); | |
| 115 } | |
| 116 | |
| 117 void ExtensionSpeechInputManager::Observe(int type, | |
| 118 const NotificationSource& source, | |
| 119 const NotificationDetails& details) { | |
| 120 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) | |
| 121 ExtensionUnloaded(Details<UnloadedExtensionInfo>(details)->extension->id()); | |
| 122 else | |
| 123 NOTREACHED(); | |
| 124 } | |
| 125 | |
| 126 void ExtensionSpeechInputManager::ShutdownOnUIThread() { | |
| 127 // TODO(leandrogracia): Force stop to speech recognition if active. | |
| 128 registrar_.RemoveAll(); | |
| 129 profile_ = NULL; | |
| 130 } | |
| 131 | |
| 132 void ExtensionSpeechInputManager::ExtensionUnloaded(const std::string& id) { | |
| 133 // TODO(leandrogracia): Force stop to speech recognition if the extension | |
| 134 // is currently using the API. | |
| 135 } | |
| 136 | |
| 137 void ExtensionSpeechInputManager::Start(const Extension *extension) { | |
| 138 // TODO(leandrogracia): Start speech recognition. | |
| 139 } | |
| 140 | |
| 141 void ExtensionSpeechInputManager::Stop(const Extension *extension) { | |
| 142 // TODO(leandrogracia): Stop speech recognition. | |
| 143 } | |
| 144 | |
| OLD | NEW |