Chromium Code Reviews| 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 the call was successful, set the last_successful time stamp and | |
|
Anand Mistry (off Chromium)
2014/12/13 00:29:49
What last_successful time stamp?
rpetterson
2014/12/13 01:12:02
Removed. Left over comment from when I was using a
| |
| 50 // update the pref. | |
| 51 if (success) { | |
| 52 PrefService* prefs = profile_->GetPrefs(); | |
| 53 prefs->SetBoolean(prefs::kHotwordAudioLoggingEnabled, new_enabled_value); | |
| 54 } | |
| 55 } | |
| 56 | |
| 30 void HotwordAudioHistoryHandler::GetAudioHistoryEnabled( | 57 void HotwordAudioHistoryHandler::GetAudioHistoryEnabled( |
| 31 const HotwordAudioHistoryCallback& callback) { | 58 const HotwordAudioHistoryCallback& callback) { |
| 32 history::WebHistoryService* web_history = GetWebHistory(); | 59 history::WebHistoryService* web_history = GetWebHistory(); |
| 33 if (web_history) { | 60 if (web_history) { |
| 34 web_history->GetAudioHistoryEnabled( | 61 web_history->GetAudioHistoryEnabled( |
| 35 base::Bind(&HotwordAudioHistoryHandler::GetAudioHistoryComplete, | 62 base::Bind(&HotwordAudioHistoryHandler::GetAudioHistoryComplete, |
| 36 weak_ptr_factory_.GetWeakPtr(), | 63 weak_ptr_factory_.GetWeakPtr(), |
| 37 callback)); | 64 callback)); |
| 38 } else { | 65 } else { |
| 39 // If web_history is null, the user is not signed in so the opt-in | 66 // 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 | 67 // 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(); | 68 PrefService* prefs = profile_->GetPrefs(); |
| 43 prefs->SetBoolean(prefs::kHotwordAudioLoggingEnabled, false); | 69 callback.Run(false, prefs->GetBoolean(prefs::kHotwordAudioLoggingEnabled)); |
| 44 callback.Run(false, false); | |
| 45 } | 70 } |
| 46 } | 71 } |
| 47 | 72 |
| 48 void HotwordAudioHistoryHandler::SetAudioHistoryEnabled( | 73 void HotwordAudioHistoryHandler::SetAudioHistoryEnabled( |
| 49 const bool enabled, | 74 const bool enabled, |
| 50 const HotwordAudioHistoryCallback& callback) { | 75 const HotwordAudioHistoryCallback& callback) { |
| 51 history::WebHistoryService* web_history = GetWebHistory(); | 76 history::WebHistoryService* web_history = GetWebHistory(); |
| 52 if (web_history) { | 77 if (web_history) { |
| 53 web_history->SetAudioHistoryEnabled( | 78 web_history->SetAudioHistoryEnabled( |
| 54 enabled, | 79 enabled, |
| 55 base::Bind(&HotwordAudioHistoryHandler::SetAudioHistoryComplete, | 80 base::Bind(&HotwordAudioHistoryHandler::SetAudioHistoryComplete, |
| 56 weak_ptr_factory_.GetWeakPtr(), | 81 weak_ptr_factory_.GetWeakPtr(), |
| 57 enabled, | 82 enabled, |
| 58 callback)); | 83 callback)); |
| 59 } else { | 84 } else { |
| 60 // If web_history is null, run the callback with false for success | 85 // If web_history is null, run the callback with false for success |
| 61 // and false for the enabled value. | 86 // and return the last known value for the opt-in pref. |
| 62 callback.Run(false, false); | 87 PrefService* prefs = profile_->GetPrefs(); |
| 88 callback.Run(false, prefs->GetBoolean(prefs::kHotwordAudioLoggingEnabled)); | |
| 63 } | 89 } |
| 64 } | 90 } |
| 65 | 91 |
| 66 void HotwordAudioHistoryHandler::GetAudioHistoryComplete( | 92 void HotwordAudioHistoryHandler::GetAudioHistoryComplete( |
| 67 const HotwordAudioHistoryCallback& callback, | 93 const HotwordAudioHistoryCallback& callback, |
| 68 bool success, bool new_enabled_value) { | 94 bool success, bool new_enabled_value) { |
| 95 // Initialize value to the last known value of the audio history pref. | |
| 69 PrefService* prefs = profile_->GetPrefs(); | 96 PrefService* prefs = profile_->GetPrefs(); |
| 70 // Set preference to false if the call was not successful to err on the safe | 97 bool value = prefs->GetBoolean(prefs::kHotwordAudioLoggingEnabled); |
| 71 // side. | 98 // If the call was successful, use the new value for updates. |
| 72 bool new_value = success && new_enabled_value; | 99 if (success) { |
| 73 prefs->SetBoolean(prefs::kHotwordAudioLoggingEnabled, new_value); | 100 value = new_enabled_value; |
| 74 | 101 prefs->SetBoolean(prefs::kHotwordAudioLoggingEnabled, value); |
| 75 callback.Run(success, new_value); | 102 // If the setting is now turned off, always on shoudl also be turned off. |
|
Anand Mistry (off Chromium)
2014/12/13 00:29:49
s/shoudl/should
rpetterson
2014/12/13 01:12:02
Done.
| |
| 103 if (!value) | |
| 104 prefs->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, false); | |
| 105 } | |
| 106 callback.Run(success, value); | |
| 76 } | 107 } |
| 77 | 108 |
| 78 void HotwordAudioHistoryHandler::SetAudioHistoryComplete( | 109 void HotwordAudioHistoryHandler::SetAudioHistoryComplete( |
| 79 bool new_enabled_value, | 110 bool new_enabled_value, |
| 80 const HotwordAudioHistoryCallback& callback, | 111 const HotwordAudioHistoryCallback& callback, |
| 81 bool success, bool callback_enabled_value) { | 112 bool success, bool callback_enabled_value) { |
| 82 if (success) { | 113 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); | 114 callback.Run(success, new_enabled_value); |
| 88 } | 115 } |
| OLD | NEW |