| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/speech/tts_extension_loader_chromeos.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/singleton.h" | |
| 9 #include "chrome/browser/extensions/component_loader.h" | |
| 10 #include "chrome/browser/extensions/extension_service.h" | |
| 11 #include "chrome/browser/extensions/extension_system.h" | |
| 12 #include "chrome/browser/extensions/extension_system_factory.h" | |
| 13 #include "chrome/browser/profiles/incognito_helpers.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 15 #include "chrome/browser/speech/extension_api/tts_engine_extension_api.h" | |
| 16 #include "chrome/browser/speech/tts_controller.h" | |
| 17 #include "chrome/common/extensions/extension_constants.h" | |
| 18 #include "components/browser_context_keyed_service/browser_context_dependency_ma
nager.h" | |
| 19 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | |
| 20 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" | |
| 21 #include "extensions/browser/event_router.h" | |
| 22 #include "grit/browser_resources.h" | |
| 23 | |
| 24 // Factory to load one instance of TtsExtensionLoaderChromeOs per profile. | |
| 25 class TtsExtensionLoaderChromeOsFactory | |
| 26 : public BrowserContextKeyedServiceFactory { | |
| 27 public: | |
| 28 static TtsExtensionLoaderChromeOs* GetForProfile(Profile* profile) { | |
| 29 return static_cast<TtsExtensionLoaderChromeOs*>( | |
| 30 GetInstance()->GetServiceForBrowserContext(profile, true)); | |
| 31 } | |
| 32 | |
| 33 static TtsExtensionLoaderChromeOsFactory* GetInstance() { | |
| 34 return Singleton<TtsExtensionLoaderChromeOsFactory>::get(); | |
| 35 } | |
| 36 | |
| 37 private: | |
| 38 friend struct DefaultSingletonTraits<TtsExtensionLoaderChromeOsFactory>; | |
| 39 | |
| 40 TtsExtensionLoaderChromeOsFactory() : BrowserContextKeyedServiceFactory( | |
| 41 "TtsExtensionLoaderChromeOs", | |
| 42 BrowserContextDependencyManager::GetInstance()) { | |
| 43 DependsOn(extensions::ExtensionSystemFactory::GetInstance()); | |
| 44 } | |
| 45 | |
| 46 virtual ~TtsExtensionLoaderChromeOsFactory() {} | |
| 47 | |
| 48 virtual content::BrowserContext* GetBrowserContextToUse( | |
| 49 content::BrowserContext* context) const OVERRIDE{ | |
| 50 // If given an incognito profile (including the Chrome OS login | |
| 51 // profile), share the service with the original profile. | |
| 52 return chrome::GetBrowserContextRedirectedInIncognito(context); | |
| 53 } | |
| 54 | |
| 55 virtual BrowserContextKeyedService* BuildServiceInstanceFor( | |
| 56 content::BrowserContext* profile) const OVERRIDE { | |
| 57 return new TtsExtensionLoaderChromeOs(static_cast<Profile*>(profile)); | |
| 58 } | |
| 59 }; | |
| 60 | |
| 61 TtsExtensionLoaderChromeOs* | |
| 62 TtsExtensionLoaderChromeOs::GetInstance(Profile* profile) { | |
| 63 return TtsExtensionLoaderChromeOsFactory::GetInstance() | |
| 64 ->GetForProfile(profile); | |
| 65 } | |
| 66 | |
| 67 TtsExtensionLoaderChromeOs::TtsExtensionLoaderChromeOs( | |
| 68 Profile* profile) | |
| 69 : profile_(profile) { | |
| 70 tts_state_ = IsTtsLoadedInThisProfile() ? TTS_LOADED : TTS_NOT_LOADED; | |
| 71 | |
| 72 extensions::ExtensionSystem* system = | |
| 73 extensions::ExtensionSystem::Get(profile_); | |
| 74 DCHECK(system); | |
| 75 extensions::EventRouter* event_router = system->event_router(); | |
| 76 DCHECK(event_router); | |
| 77 event_router->RegisterObserver(this, tts_engine_events::kOnSpeak); | |
| 78 event_router->RegisterObserver(this, tts_engine_events::kOnStop); | |
| 79 } | |
| 80 | |
| 81 bool TtsExtensionLoaderChromeOs::LoadTtsExtension() { | |
| 82 if (tts_state_ == TTS_LOADED || tts_state_ == TTS_LOADING) | |
| 83 return false; | |
| 84 | |
| 85 // Load the component extension into this profile. | |
| 86 LOG(INFO) << "Loading TTS component extension."; | |
| 87 tts_state_ = TTS_LOADING; | |
| 88 ExtensionService* extension_service = profile_->GetExtensionService(); | |
| 89 DCHECK(extension_service); | |
| 90 base::FilePath path = | |
| 91 base::FilePath(extension_misc::kSpeechSynthesisExtensionPath); | |
| 92 extension_service->component_loader()->Add(IDR_SPEECH_SYNTHESIS_MANIFEST, | |
| 93 path); | |
| 94 return true; | |
| 95 } | |
| 96 | |
| 97 void TtsExtensionLoaderChromeOs::Shutdown() { | |
| 98 extensions::ExtensionSystem::Get(profile_)-> | |
| 99 event_router()->UnregisterObserver(this); | |
| 100 } | |
| 101 | |
| 102 bool TtsExtensionLoaderChromeOs::IsTtsLoadedInThisProfile() { | |
| 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( | |
| 109 extension_misc::kSpeechSynthesisExtensionId, | |
| 110 tts_engine_events::kOnSpeak) && | |
| 111 event_router->ExtensionHasEventListener( | |
| 112 extension_misc::kSpeechSynthesisExtensionId, | |
| 113 tts_engine_events::kOnStop)) { | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 117 return false; | |
| 118 } | |
| 119 | |
| 120 void TtsExtensionLoaderChromeOs::OnListenerAdded( | |
| 121 const extensions::EventListenerInfo& details) { | |
| 122 if (details.extension_id != extension_misc::kSpeechSynthesisExtensionId) | |
| 123 return; | |
| 124 | |
| 125 if (!IsTtsLoadedInThisProfile()) | |
| 126 return; | |
| 127 | |
| 128 if (tts_state_ == TTS_LOADING) { | |
| 129 LOG(INFO) << "TTS component extension loaded, retrying queued utterances."; | |
| 130 tts_state_ = TTS_LOADED; | |
| 131 TtsController::GetInstance()->RetrySpeakingQueuedUtterances(); | |
| 132 } | |
| 133 } | |
| OLD | NEW |