summaryrefslogtreecommitdiffstats
path: root/chrome/browser/managed_prefs_banner_base.cc
blob: 225db8d7b16f0632512f4b773e25b44a56824c11 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 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"
#include "chrome/common/pref_names.h"

ManagedPrefsBannerBase::ManagedPrefsBannerBase(PrefService* prefs,
                                               OptionsPage page)
    : prefs_(prefs) {
  switch (page) {
    case OPTIONS_PAGE_GENERAL:
      AddPref(prefs::kHomePage);
      AddPref(prefs::kHomePageIsNewTabPage);
      break;
    case OPTIONS_PAGE_CONTENT:
      AddPref(prefs::kSyncManaged);
      AddPref(prefs::kPasswordManagerEnabled);
      break;
    case OPTIONS_PAGE_ADVANCED:
      AddPref(prefs::kAlternateErrorPagesEnabled);
      AddPref(prefs::kSearchSuggestEnabled);
      AddPref(prefs::kDnsPrefetchingEnabled);
      AddPref(prefs::kSafeBrowsingEnabled);
#if defined(GOOGLE_CHROME_BUILD)
      AddPref(prefs::kMetricsReportingEnabled);
#endif
      break;
    default:
      NOTREACHED();
  }
}

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();
  }
}

void ManagedPrefsBannerBase::AddPref(const wchar_t* pref) {
  if (!relevant_prefs_.count(pref)) {
    if (prefs_->FindPreference(pref)) {
      prefs_->AddPrefObserver(pref, this);
      relevant_prefs_.insert(pref);
    }
  }
}

void ManagedPrefsBannerBase::RemovePref(const wchar_t* pref) {
  PrefSet::iterator iter = relevant_prefs_.find(pref);
  if (iter != relevant_prefs_.end()) {
    prefs_->RemovePrefObserver(pref, this);
    relevant_prefs_.erase(iter);
  }
}

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;
}