OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/search/hotword_audio_history_handler.h" | 5 #include "chrome/browser/search/hotword_audio_history_handler.h" |
6 | 6 |
7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "chrome/browser/extensions/api/hotword_private/hotword_private_api.h" | 8 #include "chrome/browser/extensions/api/hotword_private/hotword_private_api.h" |
9 #include "chrome/browser/history/web_history_service.h" | 9 #include "chrome/browser/history/web_history_service.h" |
10 #include "chrome/browser/history/web_history_service_factory.h" | 10 #include "chrome/browser/history/web_history_service_factory.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
13 | 13 |
14 using extensions::BrowserContextKeyedAPIFactory; | 14 using extensions::BrowserContextKeyedAPIFactory; |
15 using extensions::HotwordPrivateEventService; | 15 using extensions::HotwordPrivateEventService; |
16 | 16 |
| 17 // Max number of hours between audio history checks. |
| 18 static const int kHoursUntilNextAudioHistoryCheck = 24; |
| 19 |
17 HotwordAudioHistoryHandler::HotwordAudioHistoryHandler( | 20 HotwordAudioHistoryHandler::HotwordAudioHistoryHandler( |
18 content::BrowserContext* context) | 21 content::BrowserContext* context, |
19 : profile_(Profile::FromBrowserContext(context)), | 22 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
| 23 : task_runner_(task_runner), |
| 24 profile_(Profile::FromBrowserContext(context)), |
20 weak_ptr_factory_(this) { | 25 weak_ptr_factory_(this) { |
21 } | 26 } |
22 | 27 |
23 HotwordAudioHistoryHandler::~HotwordAudioHistoryHandler() { | 28 HotwordAudioHistoryHandler::~HotwordAudioHistoryHandler() { |
24 } | 29 } |
25 | 30 |
26 history::WebHistoryService* HotwordAudioHistoryHandler::GetWebHistory() { | 31 history::WebHistoryService* HotwordAudioHistoryHandler::GetWebHistory() { |
27 return WebHistoryServiceFactory::GetForProfile(profile_); | 32 return WebHistoryServiceFactory::GetForProfile(profile_); |
28 } | 33 } |
29 | 34 |
| 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)); |
| 45 } |
| 46 |
| 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); |
| 52 } |
| 53 } |
| 54 |
30 void HotwordAudioHistoryHandler::GetAudioHistoryEnabled( | 55 void HotwordAudioHistoryHandler::GetAudioHistoryEnabled( |
31 const HotwordAudioHistoryCallback& callback) { | 56 const HotwordAudioHistoryCallback& callback) { |
32 history::WebHistoryService* web_history = GetWebHistory(); | 57 history::WebHistoryService* web_history = GetWebHistory(); |
33 if (web_history) { | 58 if (web_history) { |
34 web_history->GetAudioHistoryEnabled( | 59 web_history->GetAudioHistoryEnabled( |
35 base::Bind(&HotwordAudioHistoryHandler::GetAudioHistoryComplete, | 60 base::Bind(&HotwordAudioHistoryHandler::GetAudioHistoryComplete, |
36 weak_ptr_factory_.GetWeakPtr(), | 61 weak_ptr_factory_.GetWeakPtr(), |
37 callback)); | 62 callback)); |
38 } else { | 63 } else { |
39 // If web_history is null, the user is not signed in so the opt-in | 64 // If web_history is null, the user is not signed in. Set the opt-in value |
40 // should be seen as false. Run the callback with false for success | 65 // to the last known value and run the callback with false for success. |
41 // and false for the enabled value. | |
42 PrefService* prefs = profile_->GetPrefs(); | 66 PrefService* prefs = profile_->GetPrefs(); |
43 prefs->SetBoolean(prefs::kHotwordAudioLoggingEnabled, false); | 67 callback.Run(false, prefs->GetBoolean(prefs::kHotwordAudioLoggingEnabled)); |
44 callback.Run(false, false); | |
45 } | 68 } |
46 } | 69 } |
47 | 70 |
48 void HotwordAudioHistoryHandler::SetAudioHistoryEnabled( | 71 void HotwordAudioHistoryHandler::SetAudioHistoryEnabled( |
49 const bool enabled, | 72 const bool enabled, |
50 const HotwordAudioHistoryCallback& callback) { | 73 const HotwordAudioHistoryCallback& callback) { |
51 history::WebHistoryService* web_history = GetWebHistory(); | 74 history::WebHistoryService* web_history = GetWebHistory(); |
52 if (web_history) { | 75 if (web_history) { |
53 web_history->SetAudioHistoryEnabled( | 76 web_history->SetAudioHistoryEnabled( |
54 enabled, | 77 enabled, |
55 base::Bind(&HotwordAudioHistoryHandler::SetAudioHistoryComplete, | 78 base::Bind(&HotwordAudioHistoryHandler::SetAudioHistoryComplete, |
56 weak_ptr_factory_.GetWeakPtr(), | 79 weak_ptr_factory_.GetWeakPtr(), |
57 enabled, | 80 enabled, |
58 callback)); | 81 callback)); |
59 } else { | 82 } else { |
60 // If web_history is null, run the callback with false for success | 83 // If web_history is null, run the callback with false for success |
61 // and false for the enabled value. | 84 // and return the last known value for the opt-in pref. |
62 callback.Run(false, false); | 85 PrefService* prefs = profile_->GetPrefs(); |
| 86 callback.Run(false, prefs->GetBoolean(prefs::kHotwordAudioLoggingEnabled)); |
63 } | 87 } |
64 } | 88 } |
65 | 89 |
66 void HotwordAudioHistoryHandler::GetAudioHistoryComplete( | 90 void HotwordAudioHistoryHandler::GetAudioHistoryComplete( |
67 const HotwordAudioHistoryCallback& callback, | 91 const HotwordAudioHistoryCallback& callback, |
68 bool success, bool new_enabled_value) { | 92 bool success, bool new_enabled_value) { |
| 93 // Initialize value to the last known value of the audio history pref. |
69 PrefService* prefs = profile_->GetPrefs(); | 94 PrefService* prefs = profile_->GetPrefs(); |
70 // Set preference to false if the call was not successful to err on the safe | 95 bool value = prefs->GetBoolean(prefs::kHotwordAudioLoggingEnabled); |
71 // side. | 96 // If the call was successful, use the new value for updates. |
72 bool new_value = success && new_enabled_value; | 97 if (success) { |
73 prefs->SetBoolean(prefs::kHotwordAudioLoggingEnabled, new_value); | 98 value = new_enabled_value; |
74 | 99 prefs->SetBoolean(prefs::kHotwordAudioLoggingEnabled, value); |
75 callback.Run(success, new_value); | 100 // If the setting is now turned off, always on should also be turned off. |
| 101 if (!value) |
| 102 prefs->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, false); |
| 103 } |
| 104 callback.Run(success, value); |
76 } | 105 } |
77 | 106 |
78 void HotwordAudioHistoryHandler::SetAudioHistoryComplete( | 107 void HotwordAudioHistoryHandler::SetAudioHistoryComplete( |
79 bool new_enabled_value, | 108 bool new_enabled_value, |
80 const HotwordAudioHistoryCallback& callback, | 109 const HotwordAudioHistoryCallback& callback, |
81 bool success, bool callback_enabled_value) { | 110 bool success, bool callback_enabled_value) { |
82 if (success) { | 111 UpdateLocalPreference(success, new_enabled_value); |
83 PrefService* prefs = profile_->GetPrefs(); | |
84 prefs->SetBoolean(prefs::kHotwordAudioLoggingEnabled, new_enabled_value); | |
85 } | |
86 | |
87 callback.Run(success, new_enabled_value); | 112 callback.Run(success, new_enabled_value); |
88 } | 113 } |
OLD | NEW |