summaryrefslogtreecommitdiffstats
path: root/chrome/browser/managed_prefs_banner_base.cc
blob: 5251d7fe4e3b95540f4d956060414afbefce285e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/managed_prefs_banner_base.h"

#include "chrome/browser/pref_service.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_source.h"
#include "chrome/common/notification_type.h"

ManagedPrefsBannerBase::ManagedPrefsBannerBase(PrefService* prefs,
                                               const wchar_t** relevant_prefs,
                                               size_t count)
    : prefs_(prefs),
      relevant_prefs_(relevant_prefs, relevant_prefs + count) {
  for (PrefSet::const_iterator pref(relevant_prefs_.begin());
       pref != relevant_prefs_.end(); ++pref)
    prefs_->AddPrefObserver(pref->c_str(), this);
}

ManagedPrefsBannerBase::~ManagedPrefsBannerBase() {
  for (PrefSet::const_iterator pref(relevant_prefs_.begin());
       pref != relevant_prefs_.end(); ++pref)
    prefs_->RemovePrefObserver(pref->c_str(), this);
}

void ManagedPrefsBannerBase::Observe(NotificationType type,
                                     const NotificationSource& source,
                                     const NotificationDetails& details) {
  if (NotificationType::PREF_CHANGED == type) {
    std::wstring* pref = Details<std::wstring>(details).ptr();
    if (pref && relevant_prefs_.count(*pref))
      OnUpdateVisibility();
  }
}

bool ManagedPrefsBannerBase::DetermineVisibility() const {
  for (PrefSet::const_iterator pref_name(relevant_prefs_.begin());
       pref_name != relevant_prefs_.end(); ++pref_name) {
    const PrefService::Preference* pref =
        prefs_->FindPreference(pref_name->c_str());
    if (pref && pref->IsManaged())
      return true;
  }
  return false;
}