• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/extensions/api/hotword_private/hotword_private_api.h"
6 
7 #include "base/lazy_instance.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/search/hotword_client.h"
11 #include "chrome/browser/search/hotword_service.h"
12 #include "chrome/browser/search/hotword_service_factory.h"
13 #include "chrome/common/pref_names.h"
14 #include "extensions/browser/event_router.h"
15 
16 namespace extensions {
17 
18 namespace OnEnabledChanged =
19     api::hotword_private::OnEnabledChanged;
20 
21 static base::LazyInstance<
22     BrowserContextKeyedAPIFactory<HotwordPrivateEventService> > g_factory =
23     LAZY_INSTANCE_INITIALIZER;
24 
HotwordPrivateEventService(content::BrowserContext * context)25 HotwordPrivateEventService::HotwordPrivateEventService(
26     content::BrowserContext* context)
27     : profile_(Profile::FromBrowserContext(context)) {
28   pref_change_registrar_.Init(profile_->GetPrefs());
29   pref_change_registrar_.Add(
30       prefs::kHotwordSearchEnabled,
31       base::Bind(&HotwordPrivateEventService::OnEnabledChanged,
32                  base::Unretained(this)));
33 }
34 
~HotwordPrivateEventService()35 HotwordPrivateEventService::~HotwordPrivateEventService() {
36 }
37 
Shutdown()38 void HotwordPrivateEventService::Shutdown() {
39 }
40 
41 // static
42 BrowserContextKeyedAPIFactory<HotwordPrivateEventService>*
GetFactoryInstance()43 HotwordPrivateEventService::GetFactoryInstance() {
44   return g_factory.Pointer();
45 }
46 
47 // static
service_name()48 const char* HotwordPrivateEventService::service_name() {
49   return "HotwordPrivateEventService";
50 }
51 
OnEnabledChanged(const std::string & pref_name)52 void HotwordPrivateEventService::OnEnabledChanged(
53     const std::string& pref_name) {
54   DCHECK_EQ(pref_name, std::string(prefs::kHotwordSearchEnabled));
55   SignalEvent(OnEnabledChanged::kEventName);
56 }
57 
OnHotwordSessionRequested()58 void HotwordPrivateEventService::OnHotwordSessionRequested() {
59   SignalEvent(api::hotword_private::OnHotwordSessionRequested::kEventName);
60 }
61 
OnHotwordSessionStopped()62 void HotwordPrivateEventService::OnHotwordSessionStopped() {
63   SignalEvent(api::hotword_private::OnHotwordSessionStopped::kEventName);
64 }
65 
SignalEvent(const std::string & event_name)66 void HotwordPrivateEventService::SignalEvent(const std::string& event_name) {
67   EventRouter* router = EventRouter::Get(profile_);
68   if (!router || !router->HasEventListener(event_name))
69     return;
70   scoped_ptr<base::ListValue> args(new base::ListValue());
71   scoped_ptr<Event> event(new Event(event_name, args.Pass()));
72   router->BroadcastEvent(event.Pass());
73 }
74 
RunSync()75 bool HotwordPrivateSetEnabledFunction::RunSync() {
76   scoped_ptr<api::hotword_private::SetEnabled::Params> params(
77       api::hotword_private::SetEnabled::Params::Create(*args_));
78   EXTENSION_FUNCTION_VALIDATE(params.get());
79 
80   PrefService* prefs = GetProfile()->GetPrefs();
81   prefs->SetBoolean(prefs::kHotwordSearchEnabled, params->state);
82   return true;
83 }
84 
RunSync()85 bool HotwordPrivateSetAudioLoggingEnabledFunction::RunSync() {
86   scoped_ptr<api::hotword_private::SetEnabled::Params> params(
87       api::hotword_private::SetEnabled::Params::Create(*args_));
88   EXTENSION_FUNCTION_VALIDATE(params.get());
89 
90   PrefService* prefs = GetProfile()->GetPrefs();
91   prefs->SetBoolean(prefs::kHotwordAudioLoggingEnabled, params->state);
92   return true;
93 }
94 
RunSync()95 bool HotwordPrivateGetStatusFunction::RunSync() {
96   api::hotword_private::StatusDetails result;
97 
98   HotwordService* hotword_service =
99       HotwordServiceFactory::GetForProfile(GetProfile());
100   if (!hotword_service)
101     result.available = false;
102   else
103     result.available = hotword_service->IsServiceAvailable();
104 
105   PrefService* prefs = GetProfile()->GetPrefs();
106   result.enabled_set = prefs->HasPrefPath(prefs::kHotwordSearchEnabled);
107   result.enabled = prefs->GetBoolean(prefs::kHotwordSearchEnabled);
108   result.audio_logging_enabled = false;
109   if (hotword_service)
110     result.audio_logging_enabled = hotword_service->IsOptedIntoAudioLogging();
111 
112   SetResult(result.ToValue().release());
113   return true;
114 }
115 
RunSync()116 bool HotwordPrivateSetHotwordSessionStateFunction::RunSync() {
117   scoped_ptr<api::hotword_private::SetHotwordSessionState::Params> params(
118       api::hotword_private::SetHotwordSessionState::Params::Create(*args_));
119   EXTENSION_FUNCTION_VALIDATE(params.get());
120 
121   HotwordService* hotword_service =
122       HotwordServiceFactory::GetForProfile(GetProfile());
123   if (hotword_service && hotword_service->client())
124     hotword_service->client()->OnHotwordStateChanged(params->started);
125   return true;
126 }
127 
RunSync()128 bool HotwordPrivateNotifyHotwordRecognitionFunction::RunSync() {
129   HotwordService* hotword_service =
130       HotwordServiceFactory::GetForProfile(GetProfile());
131   if (hotword_service && hotword_service->client())
132     hotword_service->client()->OnHotwordRecognized();
133   return true;
134 }
135 
136 }  // namespace extensions
137