Home | History | Annotate | Download | only in policy
      1 // Copyright (c) 2011 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 "chrome/browser/policy/managed_prefs_banner_base.h"
      6 
      7 #include "chrome/browser/browser_process.h"
      8 #include "chrome/browser/prefs/pref_service.h"
      9 #include "chrome/browser/prefs/pref_set_observer.h"
     10 #include "chrome/common/pref_names.h"
     11 #include "content/common/notification_details.h"
     12 #include "content/common/notification_type.h"
     13 
     14 #if defined(OS_CHROMEOS)
     15 #include "chrome/browser/chromeos/cros_settings_names.h"
     16 #endif
     17 
     18 namespace policy {
     19 
     20 ManagedPrefsBannerBase::ManagedPrefsBannerBase(PrefService* user_prefs,
     21                                                OptionsPage page) {
     22   Init(g_browser_process->local_state(), user_prefs, page);
     23 }
     24 
     25 ManagedPrefsBannerBase::ManagedPrefsBannerBase(PrefService* local_state,
     26                                                PrefService* user_prefs,
     27                                                OptionsPage page) {
     28   Init(local_state, user_prefs, page);
     29 }
     30 
     31 ManagedPrefsBannerBase::~ManagedPrefsBannerBase() {}
     32 
     33 void ManagedPrefsBannerBase::AddLocalStatePref(const char* pref) {
     34   local_state_set_->AddPref(pref);
     35 }
     36 
     37 void ManagedPrefsBannerBase::RemoveLocalStatePref(const char* pref) {
     38   local_state_set_->RemovePref(pref);
     39 }
     40 
     41 void ManagedPrefsBannerBase::AddUserPref(const char* pref) {
     42   user_pref_set_->AddPref(pref);
     43 }
     44 
     45 void ManagedPrefsBannerBase::RemoveUserPref(const char* pref) {
     46   user_pref_set_->RemovePref(pref);
     47 }
     48 
     49 bool ManagedPrefsBannerBase::DetermineVisibility() const {
     50   return local_state_set_->IsManaged() || user_pref_set_->IsManaged();
     51 }
     52 
     53 void ManagedPrefsBannerBase::Init(PrefService* local_state,
     54                                   PrefService* user_prefs,
     55                                   OptionsPage page) {
     56   local_state_set_.reset(new PrefSetObserver(local_state, this));
     57   user_pref_set_.reset(new PrefSetObserver(user_prefs, this));
     58 
     59   switch (page) {
     60     case OPTIONS_PAGE_GENERAL:
     61       AddUserPref(prefs::kHomePage);
     62       AddUserPref(prefs::kHomePageIsNewTabPage);
     63       AddUserPref(prefs::kShowHomeButton);
     64       AddUserPref(prefs::kRestoreOnStartup);
     65       AddUserPref(prefs::kURLsToRestoreOnStartup);
     66       AddUserPref(prefs::kDefaultSearchProviderEnabled);
     67       AddUserPref(prefs::kDefaultSearchProviderName);
     68       AddUserPref(prefs::kDefaultSearchProviderKeyword);
     69       AddUserPref(prefs::kDefaultSearchProviderInstantURL);
     70       AddUserPref(prefs::kDefaultSearchProviderSearchURL);
     71       AddUserPref(prefs::kDefaultSearchProviderSuggestURL);
     72       AddUserPref(prefs::kDefaultSearchProviderIconURL);
     73       AddUserPref(prefs::kDefaultSearchProviderEncodings);
     74       AddUserPref(prefs::kInstantEnabled);
     75       AddLocalStatePref(prefs::kDefaultBrowserSettingEnabled);
     76       break;
     77     case OPTIONS_PAGE_CONTENT:
     78       AddUserPref(prefs::kSyncManaged);
     79       AddUserPref(prefs::kAutofillEnabled);
     80       AddUserPref(prefs::kPasswordManagerEnabled);
     81 #if defined(OS_CHROMEOS)
     82       AddUserPref(prefs::kEnableScreenLock);
     83 #endif
     84       break;
     85     case OPTIONS_PAGE_ADVANCED:
     86       AddUserPref(prefs::kAlternateErrorPagesEnabled);
     87       AddUserPref(prefs::kSearchSuggestEnabled);
     88       AddUserPref(prefs::kNetworkPredictionEnabled);
     89       AddUserPref(prefs::kDisableSpdy);
     90       AddUserPref(prefs::kSafeBrowsingEnabled);
     91 #if defined(GOOGLE_CHROME_BUILD)
     92       AddLocalStatePref(prefs::kMetricsReportingEnabled);
     93 #endif
     94       AddUserPref(prefs::kProxy);
     95       AddUserPref(prefs::kCloudPrintProxyEnabled);
     96       AddUserPref(prefs::kDownloadDefaultDirectory);
     97       AddUserPref(prefs::kEnableTranslate);
     98       break;
     99 #if defined(OS_CHROMEOS)
    100     case OPTIONS_PAGE_ACCOUNTS:
    101       AddLocalStatePref(chromeos::kAccountsPrefAllowGuest);
    102       AddLocalStatePref(chromeos::kAccountsPrefAllowNewUser);
    103       AddLocalStatePref(chromeos::kAccountsPrefShowUserNamesOnSignIn);
    104       AddLocalStatePref(chromeos::kAccountsPrefUsers);
    105       AddLocalStatePref(chromeos::kSystemTimezone);
    106       break;
    107 #endif
    108     default:
    109       NOTREACHED();
    110   }
    111 }
    112 
    113 void ManagedPrefsBannerBase::Observe(NotificationType type,
    114                                      const NotificationSource& source,
    115                                      const NotificationDetails& details) {
    116   if (NotificationType::PREF_CHANGED == type) {
    117     std::string* pref = Details<std::string>(details).ptr();
    118     if (pref && (local_state_set_->IsObserved(*pref) ||
    119                  user_pref_set_->IsObserved(*pref)))
    120       OnUpdateVisibility();
    121   }
    122 }
    123 
    124 }  // namespace policy
    125