OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/speech/extension_api/tts_engine_extension_observer.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/memory/singleton.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_system_factory.h" |
| 11 #include "chrome/browser/profiles/incognito_helpers.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/speech/extension_api/tts_engine_extension_api.h" |
| 14 #include "chrome/browser/speech/tts_controller.h" |
| 15 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 16 #include "components/keyed_service/content/browser_context_keyed_service_factory
.h" |
| 17 #include "components/keyed_service/core/keyed_service.h" |
| 18 #include "extensions/browser/event_router.h" |
| 19 #include "extensions/browser/extension_system.h" |
| 20 #include "grit/browser_resources.h" |
| 21 |
| 22 // Factory to load one instance of TtsExtensionLoaderChromeOs per profile. |
| 23 class TtsEngineExtensionObserverFactory |
| 24 : public BrowserContextKeyedServiceFactory { |
| 25 public: |
| 26 static TtsEngineExtensionObserver* GetForProfile(Profile* profile) { |
| 27 return static_cast<TtsEngineExtensionObserver*>( |
| 28 GetInstance()->GetServiceForBrowserContext(profile, true)); |
| 29 } |
| 30 |
| 31 static TtsEngineExtensionObserverFactory* GetInstance() { |
| 32 return Singleton<TtsEngineExtensionObserverFactory>::get(); |
| 33 } |
| 34 |
| 35 private: |
| 36 friend struct DefaultSingletonTraits<TtsEngineExtensionObserverFactory>; |
| 37 |
| 38 TtsEngineExtensionObserverFactory() |
| 39 : BrowserContextKeyedServiceFactory( |
| 40 "TtsEngineExtensionObserver", |
| 41 BrowserContextDependencyManager::GetInstance()) { |
| 42 DependsOn(extensions::ExtensionSystemFactory::GetInstance()); |
| 43 } |
| 44 |
| 45 virtual ~TtsEngineExtensionObserverFactory() {} |
| 46 |
| 47 virtual content::BrowserContext* GetBrowserContextToUse( |
| 48 content::BrowserContext* context) const OVERRIDE { |
| 49 // If given an incognito profile (including the Chrome OS login |
| 50 // profile), share the service with the original profile. |
| 51 return chrome::GetBrowserContextRedirectedInIncognito(context); |
| 52 } |
| 53 |
| 54 virtual KeyedService* BuildServiceInstanceFor( |
| 55 content::BrowserContext* profile) const OVERRIDE { |
| 56 return new TtsEngineExtensionObserver(static_cast<Profile*>(profile)); |
| 57 } |
| 58 }; |
| 59 |
| 60 TtsEngineExtensionObserver* TtsEngineExtensionObserver::GetInstance( |
| 61 Profile* profile) { |
| 62 return TtsEngineExtensionObserverFactory::GetInstance()->GetForProfile( |
| 63 profile); |
| 64 } |
| 65 |
| 66 TtsEngineExtensionObserver::TtsEngineExtensionObserver(Profile* profile) |
| 67 : extension_registry_observer_(this), |
| 68 profile_(profile), |
| 69 saw_tts_engine_added_(false) { |
| 70 extension_registry_observer_.Add( |
| 71 extensions::ExtensionRegistry::Get(profile_)); |
| 72 |
| 73 extensions::ExtensionSystem* system = |
| 74 extensions::ExtensionSystem::Get(profile_); |
| 75 DCHECK(system); |
| 76 extensions::EventRouter* event_router = system->event_router(); |
| 77 DCHECK(event_router); |
| 78 event_router->RegisterObserver(this, tts_engine_events::kOnSpeak); |
| 79 event_router->RegisterObserver(this, tts_engine_events::kOnStop); |
| 80 } |
| 81 |
| 82 TtsEngineExtensionObserver::~TtsEngineExtensionObserver() { |
| 83 } |
| 84 |
| 85 bool TtsEngineExtensionObserver::SawExtensionLoad( |
| 86 const std::string& extension_id, |
| 87 bool update) { |
| 88 bool previously_loaded = |
| 89 engine_extension_ids_.find(extension_id) != engine_extension_ids_.end(); |
| 90 |
| 91 if (update) |
| 92 engine_extension_ids_.insert(extension_id); |
| 93 |
| 94 return previously_loaded; |
| 95 } |
| 96 |
| 97 void TtsEngineExtensionObserver::Shutdown() { |
| 98 extensions::EventRouter::Get(profile_)->UnregisterObserver(this); |
| 99 } |
| 100 |
| 101 bool TtsEngineExtensionObserver::IsLoadedTtsEngine( |
| 102 const std::string& extension_id) { |
| 103 extensions::ExtensionSystem* system = |
| 104 extensions::ExtensionSystem::Get(profile_); |
| 105 DCHECK(system); |
| 106 extensions::EventRouter* event_router = system->event_router(); |
| 107 DCHECK(event_router); |
| 108 if (event_router->ExtensionHasEventListener(extension_id, |
| 109 tts_engine_events::kOnSpeak) && |
| 110 event_router->ExtensionHasEventListener(extension_id, |
| 111 tts_engine_events::kOnStop)) { |
| 112 return true; |
| 113 } |
| 114 |
| 115 return false; |
| 116 } |
| 117 |
| 118 void TtsEngineExtensionObserver::OnListenerAdded( |
| 119 const extensions::EventListenerInfo& details) { |
| 120 if (!IsLoadedTtsEngine(details.extension_id)) |
| 121 return; |
| 122 |
| 123 if (!saw_tts_engine_added_) { |
| 124 saw_tts_engine_added_ = true; |
| 125 TtsController::GetInstance()->RetrySpeakingQueuedUtterances(); |
| 126 } |
| 127 |
| 128 TtsController::GetInstance()->VoicesChanged(); |
| 129 engine_extension_ids_.insert(details.extension_id); |
| 130 } |
| 131 |
| 132 void TtsEngineExtensionObserver::OnExtensionUnloaded( |
| 133 content::BrowserContext* browser_context, |
| 134 const extensions::Extension* extension, |
| 135 extensions::UnloadedExtensionInfo::Reason reason) { |
| 136 if (engine_extension_ids_.find(extension->id()) != |
| 137 engine_extension_ids_.end()) { |
| 138 engine_extension_ids_.erase(extension->id()); |
| 139 TtsController::GetInstance()->VoicesChanged(); |
| 140 } |
| 141 } |
OLD | NEW |