| 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/history/web_history_service.h" |
| 9 #include "chrome/browser/history/web_history_service_factory.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
| 10 | 12 |
| 11 HotwordAudioHistoryHandler::HotwordAudioHistoryHandler( | 13 HotwordAudioHistoryHandler::HotwordAudioHistoryHandler( |
| 12 content::BrowserContext* context) | 14 content::BrowserContext* context) |
| 13 : profile_(Profile::FromBrowserContext(context)) { | 15 : profile_(Profile::FromBrowserContext(context)), |
| 14 pref_change_registrar_.Init(profile_->GetPrefs()); | 16 weak_factory_(this) { |
| 15 pref_change_registrar_.Add( | |
| 16 prefs::kHotwordAudioHistoryEnabled, | |
| 17 base::Bind(&HotwordAudioHistoryHandler::OnAudioHistoryEnabledChanged, | |
| 18 base::Unretained(this))); | |
| 19 | 17 |
| 20 // Poll for current value on init which should happen on first use by | 18 // Poll for current value on init which should happen on first use by |
| 21 // way of the hotword service init. | 19 // way of the hotword service init. |
| 22 GetAudioHistoryEnabled(); | 20 GetAudioHistoryEnabled(); |
| 23 } | 21 } |
| 24 | 22 |
| 25 HotwordAudioHistoryHandler::~HotwordAudioHistoryHandler() { | 23 HotwordAudioHistoryHandler::~HotwordAudioHistoryHandler() { |
| 26 } | 24 } |
| 27 | 25 |
| 28 bool HotwordAudioHistoryHandler::GetAudioHistoryEnabled() { | 26 void HotwordAudioHistoryHandler::GetAudioHistoryEnabled() { |
| 29 // TODO(rlp): fill in. | 27 history::WebHistoryService* web_history = |
| 30 return false; | 28 WebHistoryServiceFactory::GetForProfile(profile_); |
| 29 if (web_history) { |
| 30 web_history->GetAudioHistoryEnabled( |
| 31 base::Bind(&HotwordAudioHistoryHandler::AudioHistoryComplete, |
| 32 weak_factory_.GetWeakPtr())); |
| 33 } else { |
| 34 // If web_history is null, the user is not signed in. |
| 35 PrefService* prefs = profile_->GetPrefs(); |
| 36 prefs->SetBoolean(prefs::kHotwordAudioHistoryEnabled, false); |
| 37 } |
| 31 } | 38 } |
| 32 | 39 |
| 33 void HotwordAudioHistoryHandler::SetAudioHistoryEnabled(const bool enabled) { | 40 void HotwordAudioHistoryHandler::SetAudioHistoryEnabled(const bool enabled) { |
| 34 // TODO(rlp): fill in. | 41 history::WebHistoryService* web_history = |
| 42 WebHistoryServiceFactory::GetForProfile(profile_); |
| 43 if (web_history) { |
| 44 web_history->SetAudioHistoryEnabled( |
| 45 enabled, |
| 46 base::Bind(&HotwordAudioHistoryHandler::AudioHistoryComplete, |
| 47 weak_factory_.GetWeakPtr())); |
| 48 } |
| 35 } | 49 } |
| 36 | 50 |
| 37 void HotwordAudioHistoryHandler::OnAudioHistoryEnabledChanged( | 51 void HotwordAudioHistoryHandler::AudioHistoryComplete( |
| 38 const std::string& pref_name) { | 52 bool success, bool new_enabled_value) { |
| 39 DCHECK(pref_name == std::string(prefs::kHotwordAudioHistoryEnabled)); | |
| 40 | |
| 41 PrefService* prefs = profile_->GetPrefs(); | 53 PrefService* prefs = profile_->GetPrefs(); |
| 42 SetAudioHistoryEnabled(prefs->GetBoolean(prefs::kHotwordAudioHistoryEnabled)); | 54 // Set preference to false if the call was not successful to err on the safe |
| 55 // side. |
| 56 prefs->SetBoolean(prefs::kHotwordAudioHistoryEnabled, |
| 57 success && new_enabled_value); |
| 43 } | 58 } |
| OLD | NEW |