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.
5 #include "chrome/browser/speech/tts_extension_loader_chromeos.h"
7 #include "base/logging.h"
8 #include "base/memory/singleton.h"
9 #include "chrome/browser/extensions/component_loader.h"
10 #include "chrome/browser/extensions/event_router.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_system.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/profiles/profile_dependency_manager.h"
15 #include "chrome/browser/profiles/profile_keyed_service.h"
16 #include "chrome/browser/profiles/profile_keyed_service_factory.h"
17 #include "chrome/browser/speech/extension_api/tts_engine_extension_api.h"
18 #include "chrome/browser/speech/tts_controller.h"
19 #include "chrome/common/extensions/extension_constants.h"
20 #include "grit/browser_resources.h"
22 // Factory to load one instance of TtsExtensionLoaderChromeOs per profile.
23 class TtsExtensionLoaderChromeOsFactory
: public ProfileKeyedServiceFactory
{
25 static TtsExtensionLoaderChromeOs
* GetForProfile(Profile
* profile
) {
26 return static_cast<TtsExtensionLoaderChromeOs
*>(
27 GetInstance()->GetServiceForProfile(profile
, true));
30 static TtsExtensionLoaderChromeOsFactory
* GetInstance() {
31 return Singleton
<TtsExtensionLoaderChromeOsFactory
>::get();
35 friend struct DefaultSingletonTraits
<TtsExtensionLoaderChromeOsFactory
>;
37 TtsExtensionLoaderChromeOsFactory() : ProfileKeyedServiceFactory(
38 "TtsExtensionLoaderChromeOs",
39 ProfileDependencyManager::GetInstance())
42 virtual ~TtsExtensionLoaderChromeOsFactory() {}
44 virtual bool ServiceRedirectedInIncognito() const OVERRIDE
{
45 // If given an incognito profile (including the Chrome OS login
46 // profile), share the service with the original profile.
50 virtual ProfileKeyedService
* BuildServiceInstanceFor(Profile
* profile
) const
52 return new TtsExtensionLoaderChromeOs(profile
);
56 TtsExtensionLoaderChromeOs
*
57 TtsExtensionLoaderChromeOs::GetInstance(Profile
* profile
) {
58 return TtsExtensionLoaderChromeOsFactory::GetInstance()
59 ->GetForProfile(profile
);
62 TtsExtensionLoaderChromeOs::TtsExtensionLoaderChromeOs(
65 tts_state_
= IsTtsLoadedInThisProfile() ? TTS_LOADED
: TTS_NOT_LOADED
;
67 extensions::ExtensionSystem
* system
=
68 extensions::ExtensionSystem::Get(profile_
);
70 extensions::EventRouter
* event_router
= system
->event_router();
72 event_router
->RegisterObserver(this, tts_engine_events::kOnSpeak
);
73 event_router
->RegisterObserver(this, tts_engine_events::kOnStop
);
76 bool TtsExtensionLoaderChromeOs::LoadTtsExtension() {
77 if (tts_state_
== TTS_LOADED
|| tts_state_
== TTS_LOADING
)
80 // Load the component extension into this profile.
81 LOG(INFO
) << "Loading TTS component extension.";
82 tts_state_
= TTS_LOADING
;
83 ExtensionService
* extension_service
= profile_
->GetExtensionService();
84 DCHECK(extension_service
);
86 base::FilePath(extension_misc::kSpeechSynthesisExtensionPath
);
87 extension_service
->component_loader()->Add(IDR_SPEECH_SYNTHESIS_MANIFEST
,
92 bool TtsExtensionLoaderChromeOs::IsTtsLoadedInThisProfile() {
93 extensions::ExtensionSystem
* system
=
94 extensions::ExtensionSystem::Get(profile_
);
96 extensions::EventRouter
* event_router
= system
->event_router();
98 if (event_router
->ExtensionHasEventListener(
99 extension_misc::kSpeechSynthesisExtensionId
,
100 tts_engine_events::kOnSpeak
) &&
101 event_router
->ExtensionHasEventListener(
102 extension_misc::kSpeechSynthesisExtensionId
,
103 tts_engine_events::kOnStop
)) {
110 void TtsExtensionLoaderChromeOs::OnListenerAdded(
111 const extensions::EventListenerInfo
& details
) {
112 if (details
.extension_id
!= extension_misc::kSpeechSynthesisExtensionId
)
115 if (!IsTtsLoadedInThisProfile())
118 if (tts_state_
== TTS_LOADING
) {
119 LOG(INFO
) << "TTS component extension loaded, retrying queued utterances.";
120 tts_state_
= TTS_LOADED
;
121 TtsController::GetInstance()->RetrySpeakingQueuedUtterances();