Revert 285173 "Removed InProcessBrowserTest::CleanUpOnMainThread()"
[chromium-blink-merge.git] / chrome / browser / extensions / api / hotword_private / hotword_private_api.cc
blob42bbe1808d3ca56f0c11b54b67d1d261996876dd
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.
5 #include "chrome/browser/extensions/api/hotword_private/hotword_private_api.h"
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"
16 namespace extensions {
18 namespace OnEnabledChanged =
19 api::hotword_private::OnEnabledChanged;
21 static base::LazyInstance<
22 BrowserContextKeyedAPIFactory<HotwordPrivateEventService> > g_factory =
23 LAZY_INSTANCE_INITIALIZER;
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)));
35 HotwordPrivateEventService::~HotwordPrivateEventService() {
38 void HotwordPrivateEventService::Shutdown() {
41 // static
42 BrowserContextKeyedAPIFactory<HotwordPrivateEventService>*
43 HotwordPrivateEventService::GetFactoryInstance() {
44 return g_factory.Pointer();
47 // static
48 const char* HotwordPrivateEventService::service_name() {
49 return "HotwordPrivateEventService";
52 void HotwordPrivateEventService::OnEnabledChanged(
53 const std::string& pref_name) {
54 DCHECK_EQ(pref_name, std::string(prefs::kHotwordSearchEnabled));
55 SignalEvent(OnEnabledChanged::kEventName);
58 void HotwordPrivateEventService::OnHotwordSessionRequested() {
59 SignalEvent(api::hotword_private::OnHotwordSessionRequested::kEventName);
62 void HotwordPrivateEventService::OnHotwordSessionStopped() {
63 SignalEvent(api::hotword_private::OnHotwordSessionStopped::kEventName);
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());
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());
80 PrefService* prefs = GetProfile()->GetPrefs();
81 prefs->SetBoolean(prefs::kHotwordSearchEnabled, params->state);
82 return true;
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());
90 PrefService* prefs = GetProfile()->GetPrefs();
91 prefs->SetBoolean(prefs::kHotwordAudioLoggingEnabled, params->state);
92 return true;
95 bool HotwordPrivateGetStatusFunction::RunSync() {
96 api::hotword_private::StatusDetails result;
98 HotwordService* hotword_service =
99 HotwordServiceFactory::GetForProfile(GetProfile());
100 if (!hotword_service)
101 result.available = false;
102 else
103 result.available = hotword_service->IsServiceAvailable();
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();
112 SetResult(result.ToValue().release());
113 return true;
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());
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;
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;
136 } // namespace extensions