Rename vector icon header files.
[chromium-blink-merge.git] / chrome / browser / search / hotword_audio_history_handler.cc
blob7c84b156e9ac00094b1ca6ded31b6c7a33af073c
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/search/hotword_audio_history_handler.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/extensions/api/hotword_private/hotword_private_api.h"
9 #include "chrome/browser/history/web_history_service_factory.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/pref_names.h"
12 #include "components/history/core/browser/web_history_service.h"
14 using extensions::BrowserContextKeyedAPIFactory;
15 using extensions::HotwordPrivateEventService;
17 // Max number of hours between audio history checks.
18 static const int kHoursUntilNextAudioHistoryCheck = 24;
20 HotwordAudioHistoryHandler::HotwordAudioHistoryHandler(
21 content::BrowserContext* context,
22 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
23 : task_runner_(task_runner),
24 profile_(Profile::FromBrowserContext(context)),
25 weak_ptr_factory_(this) {
28 HotwordAudioHistoryHandler::~HotwordAudioHistoryHandler() {
31 history::WebHistoryService* HotwordAudioHistoryHandler::GetWebHistory() {
32 return WebHistoryServiceFactory::GetForProfile(profile_);
35 void HotwordAudioHistoryHandler::UpdateAudioHistoryState() {
36 GetAudioHistoryEnabled(
37 base::Bind(&HotwordAudioHistoryHandler::UpdateLocalPreference,
38 weak_ptr_factory_.GetWeakPtr()));
39 // Set the function to update in a day.
40 task_runner_->PostDelayedTask(
41 FROM_HERE,
42 base::Bind(&HotwordAudioHistoryHandler::UpdateAudioHistoryState,
43 weak_ptr_factory_.GetWeakPtr()),
44 base::TimeDelta::FromHours(kHoursUntilNextAudioHistoryCheck));
47 void HotwordAudioHistoryHandler::UpdateLocalPreference(
48 bool success, bool new_enabled_value) {
49 if (success) {
50 PrefService* prefs = profile_->GetPrefs();
51 prefs->SetBoolean(prefs::kHotwordAudioLoggingEnabled, new_enabled_value);
55 void HotwordAudioHistoryHandler::GetAudioHistoryEnabled(
56 const HotwordAudioHistoryCallback& callback) {
57 history::WebHistoryService* web_history = GetWebHistory();
58 if (web_history) {
59 web_history->GetAudioHistoryEnabled(
60 base::Bind(&HotwordAudioHistoryHandler::GetAudioHistoryComplete,
61 weak_ptr_factory_.GetWeakPtr(),
62 callback));
63 } else {
64 // If web_history is null, the user is not signed in. Set the opt-in value
65 // to the last known value and run the callback with false for success.
66 PrefService* prefs = profile_->GetPrefs();
67 callback.Run(false, prefs->GetBoolean(prefs::kHotwordAudioLoggingEnabled));
71 void HotwordAudioHistoryHandler::SetAudioHistoryEnabled(
72 const bool enabled,
73 const HotwordAudioHistoryCallback& callback) {
74 history::WebHistoryService* web_history = GetWebHistory();
75 if (web_history) {
76 web_history->SetAudioHistoryEnabled(
77 enabled,
78 base::Bind(&HotwordAudioHistoryHandler::SetAudioHistoryComplete,
79 weak_ptr_factory_.GetWeakPtr(),
80 enabled,
81 callback));
82 } else {
83 // If web_history is null, run the callback with false for success
84 // and return the last known value for the opt-in pref.
85 PrefService* prefs = profile_->GetPrefs();
86 callback.Run(false, prefs->GetBoolean(prefs::kHotwordAudioLoggingEnabled));
90 void HotwordAudioHistoryHandler::GetAudioHistoryComplete(
91 const HotwordAudioHistoryCallback& callback,
92 bool success, bool new_enabled_value) {
93 // Initialize value to the last known value of the audio history pref.
94 PrefService* prefs = profile_->GetPrefs();
95 bool value = prefs->GetBoolean(prefs::kHotwordAudioLoggingEnabled);
96 // If the call was successful, use the new value for updates.
97 if (success) {
98 value = new_enabled_value;
99 prefs->SetBoolean(prefs::kHotwordAudioLoggingEnabled, value);
100 // If the setting is now turned off, always on should also be turned off,
101 // and the speaker model should be deleted.
102 if (!value) {
103 if (prefs->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled)) {
104 prefs->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, false);
105 HotwordPrivateEventService* event_service =
106 BrowserContextKeyedAPIFactory<HotwordPrivateEventService>::Get(
107 profile_);
108 if (event_service)
109 event_service->OnDeleteSpeakerModel();
113 callback.Run(success, value);
116 void HotwordAudioHistoryHandler::SetAudioHistoryComplete(
117 bool new_enabled_value,
118 const HotwordAudioHistoryCallback& callback,
119 bool success, bool callback_enabled_value) {
120 UpdateLocalPreference(success, new_enabled_value);
121 callback.Run(success, new_enabled_value);