Fix build break
[chromium-blink-merge.git] / chrome / browser / speech / tts_extension_loader_chromeos.cc
blob2195a72d51b78f7c197eccee65111770c56711fa
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 {
24 public:
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();
34 private:
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.
47 return true;
50 virtual ProfileKeyedService* BuildServiceInstanceFor(Profile* profile) const
51 OVERRIDE {
52 return new TtsExtensionLoaderChromeOs(profile);
56 TtsExtensionLoaderChromeOs*
57 TtsExtensionLoaderChromeOs::GetInstance(Profile* profile) {
58 return TtsExtensionLoaderChromeOsFactory::GetInstance()
59 ->GetForProfile(profile);
62 TtsExtensionLoaderChromeOs::TtsExtensionLoaderChromeOs(
63 Profile* profile)
64 : profile_(profile) {
65 tts_state_ = IsTtsLoadedInThisProfile() ? TTS_LOADED : TTS_NOT_LOADED;
67 extensions::ExtensionSystem* system =
68 extensions::ExtensionSystem::Get(profile_);
69 DCHECK(system);
70 extensions::EventRouter* event_router = system->event_router();
71 DCHECK(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)
78 return false;
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);
85 base::FilePath path =
86 base::FilePath(extension_misc::kSpeechSynthesisExtensionPath);
87 extension_service->component_loader()->Add(IDR_SPEECH_SYNTHESIS_MANIFEST,
88 path);
89 return true;
92 bool TtsExtensionLoaderChromeOs::IsTtsLoadedInThisProfile() {
93 extensions::ExtensionSystem* system =
94 extensions::ExtensionSystem::Get(profile_);
95 DCHECK(system);
96 extensions::EventRouter* event_router = system->event_router();
97 DCHECK(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)) {
104 return true;
107 return false;
110 void TtsExtensionLoaderChromeOs::OnListenerAdded(
111 const extensions::EventListenerInfo& details) {
112 if (details.extension_id != extension_misc::kSpeechSynthesisExtensionId)
113 return;
115 if (!IsTtsLoadedInThisProfile())
116 return;
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();