Thomas Tangl | 7969bbd | 2018-08-02 15:25:22 | [diff] [blame] | 1 | // Copyright 2018 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. |
| 4 | |
| 5 | #include "components/unified_consent/unified_consent_service_client.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | |
| 9 | namespace unified_consent { |
| 10 | |
| 11 | UnifiedConsentServiceClient::UnifiedConsentServiceClient() {} |
| 12 | UnifiedConsentServiceClient::~UnifiedConsentServiceClient() {} |
| 13 | |
Thomas Tangl | bb76eac | 2018-11-28 09:45:02 | [diff] [blame] | 14 | bool UnifiedConsentServiceClient::IsServiceSupported(Service service) { |
| 15 | return GetServiceState(service) != ServiceState::kNotSupported; |
| 16 | } |
| 17 | |
Thomas Tangl | 7969bbd | 2018-08-02 15:25:22 | [diff] [blame] | 18 | void UnifiedConsentServiceClient::AddObserver(Observer* observer) { |
| 19 | observer_list_.AddObserver(observer); |
| 20 | } |
| 21 | |
| 22 | void UnifiedConsentServiceClient::RemoveObserver(Observer* observer) { |
| 23 | observer_list_.RemoveObserver(observer); |
| 24 | } |
| 25 | |
| 26 | void UnifiedConsentServiceClient::ObserveServicePrefChange( |
| 27 | Service service, |
| 28 | const std::string& pref_name, |
| 29 | PrefService* pref_service) { |
| 30 | service_prefs_[pref_name] = service; |
| 31 | |
| 32 | // First access to the pref registrar of |pref_service| in the map |
| 33 | // automatically creates an entry for it. |
| 34 | PrefChangeRegistrar* pref_change_registrar = |
| 35 | &(pref_change_registrars_[pref_service]); |
| 36 | if (!pref_change_registrar->prefs()) |
| 37 | pref_change_registrar->Init(pref_service); |
| 38 | pref_change_registrar->Add( |
| 39 | pref_name, |
| 40 | base::BindRepeating(&UnifiedConsentServiceClient::OnPrefChanged, |
| 41 | base::Unretained(this))); |
| 42 | } |
| 43 | |
| 44 | void UnifiedConsentServiceClient::FireOnServiceStateChanged(Service service) { |
| 45 | for (auto& observer : observer_list_) |
| 46 | observer.OnServiceStateChanged(service); |
| 47 | } |
| 48 | |
| 49 | void UnifiedConsentServiceClient::OnPrefChanged(const std::string& pref_name) { |
| 50 | DCHECK(service_prefs_.count(pref_name)); |
| 51 | FireOnServiceStateChanged(service_prefs_[pref_name]); |
| 52 | } |
| 53 | |
| 54 | } // namespace unified_consent |