Chromium Code Reviews| Index: chrome/browser/chromeos/policy/recommendation_restorer.cc |
| diff --git a/chrome/browser/chromeos/policy/recommendation_restorer.cc b/chrome/browser/chromeos/policy/recommendation_restorer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..882e819b794deb853f8c427e011520247740241b |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/policy/recommendation_restorer.cc |
| @@ -0,0 +1,175 @@ |
| +// Copyright (c) 2013 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/chromeos/policy/recommendation_restorer.h" |
| + |
| +#include "ash/shell.h" |
| +#include "ash/wm/user_activity_detector.h" |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/command_line.h" |
| +#include "base/location.h" |
| +#include "base/logging.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "base/stl_util.h" |
| +#include "base/time.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/chromeos/profiles/profile_helper.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chromeos/chromeos_switches.h" |
| +#include "content/public/browser/notification_details.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/notification_source.h" |
| + |
| +namespace policy { |
| + |
| +namespace { |
| + // The amount of idle time after which recommended values are restored. |
| + const int kRestoreDelayInMs = 60 * 1000; // 1 minute. |
| +} // namespace |
| + |
| +RecommendationRestorerInternal::RecommendationRestorerInternal( |
| + PrefService* prefs) { |
| + registrar_.Init(prefs); |
| + registrar_.Add(prefs::kLargeCursorEnabled, |
| + base::Bind(&RecommendationRestorerInternal::CheckPref, |
| + base::Unretained(this))); |
| + registrar_.Add(prefs::kSpokenFeedbackEnabled, |
| + base::Bind(&RecommendationRestorerInternal::CheckPref, |
| + base::Unretained(this))); |
| + registrar_.Add(prefs::kHighContrastEnabled, |
| + base::Bind(&RecommendationRestorerInternal::CheckPref, |
| + base::Unretained(this))); |
| + registrar_.Add(prefs::kScreenMagnifierEnabled, |
| + base::Bind(&RecommendationRestorerInternal::CheckPref, |
| + base::Unretained(this))); |
| + registrar_.Add(prefs::kScreenMagnifierType, |
| + base::Bind(&RecommendationRestorerInternal::CheckPref, |
| + base::Unretained(this))); |
| + |
| + RestoreAll(); |
| +} |
| + |
| +RecommendationRestorerInternal::~RecommendationRestorerInternal() { |
| + if (ash::Shell::HasInstance()) |
| + ash::Shell::GetInstance()->user_activity_detector()->RemoveObserver(this); |
| + |
| + STLDeleteValues(&recommended_values_); |
| +} |
| + |
| +void RecommendationRestorerInternal::OnUserActivity() { |
| + if (restore_timer_.IsRunning()) |
| + restore_timer_.Reset(); |
| +} |
| + |
| +void RecommendationRestorerInternal::CheckPref( |
| + const std::string& pref_name) { |
| + const PrefService::Preference* pref = |
| + registrar_.prefs()->FindPreference(pref_name.c_str()); |
| + if (!pref) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + const base::Value* recommended_value = pref->GetRecommendedValue(); |
| + if (!recommended_value) { |
| + std::map<std::string, base::Value*>::iterator recommended_value_entry = |
| + recommended_values_.find(pref->name()); |
| + if (recommended_value_entry != recommended_values_.end()) { |
| + delete recommended_value_entry->second; |
| + recommended_values_.erase(recommended_value_entry); |
| + } |
| + return; |
| + } |
| + |
| + base::Value*& recommended_value_entry = recommended_values_[pref->name()]; |
| + if (!base::Value::Equals(recommended_value, recommended_value_entry)) { |
|
Mattias Nissler (ping if slow)
2013/06/12 13:55:16
So this recommended_values_ thing is just here to
bartfab (slow)
2013/06/12 18:57:49
No, the |recommended_values_| are here so that I c
Mattias Nissler (ping if slow)
2013/06/13 18:11:46
I think a better implementation for this would be
|
| + delete recommended_value_entry; |
| + recommended_value_entry = recommended_value->DeepCopy(); |
| + PrefService* prefs = registrar_.prefs(); |
| + prefs->ClearPref(pref->name().c_str()); |
| + return; |
| + } |
| + |
| + if (!pref->HasUserSetting()) |
| + return; |
| + |
| + // Listen for user activity so that the timer can be reset while the user is |
| + // active, causing it to fire only when the user remains idle for |
| + // |kRestoreDelayInMs|. |
| + if (ash::Shell::HasInstance()) { |
| + ash::UserActivityDetector* user_activity_detector = |
| + ash::Shell::GetInstance()->user_activity_detector(); |
| + if (!user_activity_detector->HasObserver(this)) |
| + user_activity_detector->AddObserver(this); |
| + } |
| + |
| + // A single timer is sufficient as every pref changed initiated by the user |
| + // implies user activity, meaning that even if there was a separate timer per |
| + // pref, they would all be reset at this point, causing them to fire at |
| + // exactly the same time. |
| + restore_timer_.Start( |
| + FROM_HERE, |
| + base::TimeDelta::FromMilliseconds(kRestoreDelayInMs), |
| + base::Bind(&RecommendationRestorerInternal::RestoreAll, |
| + base::Unretained(this))); |
| +} |
| + |
| +void RecommendationRestorerInternal::RestoreAll() { |
| + STLDeleteValues(&recommended_values_); |
| + |
| + CheckPref(prefs::kLargeCursorEnabled); |
| + CheckPref(prefs::kSpokenFeedbackEnabled); |
| + CheckPref(prefs::kHighContrastEnabled); |
| + CheckPref(prefs::kScreenMagnifierEnabled); |
| + CheckPref(prefs::kScreenMagnifierType); |
| +} |
| + |
| +RecommendationRestorer::RecommendationRestorer(Profile* profile) { |
| + if (!chromeos::ProfileHelper::IsSigninProfile(profile)) |
| + return; |
| + |
| + // Recommended values are restored in the login profile while the login screen |
| + // is being shown. If this is a restart after a browser crash inside a user |
| + // session, do nothing. |
| + if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| + chromeos::switches::kLoginManager)) { |
|
Mattias Nissler (ping if slow)
2013/06/12 13:55:16
So IsIsgninProfile returns true in the restart cas
bartfab (slow)
2013/06/12 18:57:49
IsSigninProfile returns true when the BCKS is crea
Mattias Nissler (ping if slow)
2013/06/13 18:11:46
So you're saying the accessibility code picks up p
|
| + return; |
| + } |
| + |
| + // Create an object that will handle the actual restoring of recommended |
| + // values. |
| + recommendation_restorer_internal_.reset(new RecommendationRestorerInternal( |
| + profile->GetPrefs())); |
| + |
| + // Subscribe to login notifications so that the restoring can be stopped when |
| + // a user session begins. |
| + registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
| + content::NotificationService::AllSources()); |
| +} |
| + |
| +RecommendationRestorer::~RecommendationRestorer() { |
| +} |
| + |
| +void RecommendationRestorer::Shutdown() { |
| + recommendation_restorer_internal_.reset(); |
| +} |
| + |
| +void RecommendationRestorer::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) { |
| + // Stop restoring recommended values in the login profile when a user |
| + // session begins. |
| + recommendation_restorer_internal_.reset(); |
| + registrar_.RemoveAll(); |
| + } else { |
| + NOTREACHED(); |
| + } |
| +} |
| + |
| +} // namespace policy |