blob: 87d37710ebd2b0699bb14fdd73e45ff3e581a68a [file] [log] [blame]
Thomas Tangl7969bbd2018-08-02 15:25:221// 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
9namespace unified_consent {
10
11UnifiedConsentServiceClient::UnifiedConsentServiceClient() {}
12UnifiedConsentServiceClient::~UnifiedConsentServiceClient() {}
13
Thomas Tanglbb76eac2018-11-28 09:45:0214bool UnifiedConsentServiceClient::IsServiceSupported(Service service) {
15 return GetServiceState(service) != ServiceState::kNotSupported;
16}
17
Thomas Tangl7969bbd2018-08-02 15:25:2218void UnifiedConsentServiceClient::AddObserver(Observer* observer) {
19 observer_list_.AddObserver(observer);
20}
21
22void UnifiedConsentServiceClient::RemoveObserver(Observer* observer) {
23 observer_list_.RemoveObserver(observer);
24}
25
26void 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
44void UnifiedConsentServiceClient::FireOnServiceStateChanged(Service service) {
45 for (auto& observer : observer_list_)
46 observer.OnServiceStateChanged(service);
47}
48
49void 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